1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| extension CVPartlyRefreshable { func append(section: CVPartlyRefreshSection) { section.refresh = self section.items.forEach { $0.refresh = self } section.onRemove = { [weak self] stn in self?.sections.removeAll { $0 == stn } } sections.append(section) section.apply() } }
extension CVPartlyRefreshSection { func apply() { refresh?.operationQueue.addOperation { [weak self] in guard let self, let dataSource = self.refresh?.dataSource else { return } var snapshot = dataSource.snapshot() snapshot.appendSections([self]) snapshot.appendItems(self.items) dataSource.apply(snapshot, animatingDifferences: true) } }
func reload() { refresh?.operationQueue.addOperation { [weak self] in guard let self, let dataSource = self.refresh?.dataSource else { return } var snapshot = dataSource.snapshot() guard let sectionIdx = snapshot.indexOfSection(self) else { return } let section = snapshot.sectionIdentifiers[sectionIdx] snapshot.reloadItems(snapshot.itemIdentifiers(inSection: section)) dataSource.apply(snapshot) } }
func reload(items: [CVPartlyRefreshItem], completion: (() -> Void)? = nil) { refresh?.operationQueue.addOperation { [weak self] in guard let self, let dataSource = self.refresh?.dataSource else { return } self.items = items var snapshot = dataSource.snapshot() guard let sectionIdx = snapshot.indexOfSection(self), sectionIdx < snapshot.numberOfSections else { return } let section = snapshot.sectionIdentifiers[sectionIdx] snapshot.deleteItems(snapshot.itemIdentifiers(inSection: section)) snapshot.appendItems(items, toSection: section) dataSource.apply(snapshot, completion: completion) } }
func append( items: [CVPartlyRefreshItem], reloadItem: CVPartlyRefreshItem? = nil, removed: [CVPartlyRefreshItem] = [], completion: (() -> Void)? = nil ) { refresh?.operationQueue.addOperation { [weak self] in guard let self, let dataSource = self.refresh?.dataSource else { return } self.items.removeAll { removed.contains($0) } self.items.append(contentsOf: items) var snapshot = dataSource.snapshot() guard let sectionIdx = snapshot.indexOfSection(self), sectionIdx < snapshot.numberOfSections else { return } let section = snapshot.sectionIdentifiers[sectionIdx] if !removed.isEmpty { let deletedItems = removed .compactMap { snapshot.indexOfItem($0) } .map { snapshot.itemIdentifiers[$0] } snapshot.deleteItems(deletedItems) } snapshot.appendItems(items, toSection: section) if let reloadItem = reloadItem { snapshot.reloadItems([reloadItem]) } dataSource.apply(snapshot, completion: completion) } }
func insert( item: CVPartlyRefreshItem, after: CVPartlyRefreshItem, reload: CVPartlyRefreshItem? = nil, removed: CVPartlyRefreshItem? = nil, completion: (() -> Void)? = nil ) { refresh?.operationQueue.addOperation { [weak self] in guard let self, let dataSource = self.refresh?.dataSource else { return } guard let afterIndexPath = dataSource.indexPath(for: after) else { return } var snapshot = dataSource.snapshot() guard let sectionIdx = snapshot.indexOfSection(self), sectionIdx < snapshot.numberOfSections else { return } self.items.insert(item, at: afterIndexPath.row + 1) self.items.removeAll { $0 == removed } if let removed = removed, let removedIdx = snapshot.indexOfItem(removed) { snapshot.deleteItems([snapshot.itemIdentifiers[removedIdx]]) } snapshot.insertItems([item], afterItem: after) if let reload = reload, let idx = snapshot.indexOfItem(reload) { snapshot.reloadItems([snapshot.itemIdentifiers[idx]]) } dataSource.apply(snapshot, completion: completion) } }
func remove(items: [CVPartlyRefreshItem], completion: (() -> Void)? = nil) { refresh?.operationQueue.addOperation { [weak self] in guard let self, let dataSource = self.refresh?.dataSource else { return } self.items.removeAll(where: { items.contains($0) }) var snapshot = dataSource.snapshot() guard let sectionIdx = snapshot.indexOfSection(self), sectionIdx < snapshot.numberOfSections else { return } let allItems = snapshot.itemIdentifiers let deletedItems = items.compactMap { snapshot.indexOfItem($0) }.map { allItems[$0] } snapshot.deleteItems(deletedItems) dataSource.apply(snapshot, completion: completion) } }
func remove() { refresh?.operationQueue.addOperation { [weak self] in guard let self, let dataSource = self.refresh?.dataSource else { return } var snapshot = dataSource.snapshot() guard let sectionIdx = snapshot.indexOfSection(self), sectionIdx < snapshot.numberOfSections else { return } onRemove?(self) snapshot.deleteSections([snapshot.sectionIdentifiers[sectionIdx]]) dataSource.apply(snapshot) } } }
extension CVPartlyRefreshItem { func reload(animated: Bool = true, completion: (() -> Void)? = nil) { refresh?.operationQueue.addOperation { [weak self] in guard let self, let dataSource = self.refresh?.dataSource else { return } var snapshot = dataSource.snapshot() guard let itemIdx = snapshot.indexOfItem(self), itemIdx < snapshot.numberOfItems else { return } snapshot.reloadItems([snapshot.itemIdentifiers[itemIdx]]) dataSource.apply( snapshot, animatingDifferences: animated, completion: completion ) } }
func remove() { refresh?.operationQueue.addOperation { [weak self] in guard let self, let dataSource = self.refresh?.dataSource else { return } var snapshot = dataSource.snapshot() guard let itemIdx = snapshot.indexOfItem(self) else { return } onRemove?(self) snapshot.deleteItems([snapshot.itemIdentifiers[itemIdx]]) dataSource.apply(snapshot) } } }
|