Convert TimelineTableViewController to use DiffableTimelineLikeTableViewController

This commit is contained in:
Shadowfacts 2021-06-20 22:27:38 -04:00
parent 49572c1fec
commit 5b8e97287e
2 changed files with 104 additions and 71 deletions

View File

@ -58,7 +58,7 @@ class ListTimelineViewController: TimelineTableViewController {
dismiss(animated: true) dismiss(animated: true)
// todo: show loading indicator // todo: show loading indicator
reloadInitialItems() reloadInitial()
} }
} }

View File

@ -11,7 +11,7 @@ import Pachyderm
typealias TimelineEntry = (id: String, state: StatusState) typealias TimelineEntry = (id: String, state: StatusState)
class TimelineTableViewController: TimelineLikeTableViewController<TimelineEntry> { class TimelineTableViewController: DiffableTimelineLikeTableViewController<TimelineTableViewController.Section, TimelineTableViewController.Item> {
let timeline: Timeline let timeline: Timeline
weak var mastodonController: MastodonController! weak var mastodonController: MastodonController!
@ -44,10 +44,8 @@ class TimelineTableViewController: TimelineLikeTableViewController<TimelineEntry
// decrement reference counts of any statuses we still have // decrement reference counts of any statuses we still have
// if the app is currently being quit, this will not affect the persisted data because // if the app is currently being quit, this will not affect the persisted data because
// the persistent container would already have been saved in SceneDelegate.sceneDidEnterBackground(_:) // the persistent container would already have been saved in SceneDelegate.sceneDidEnterBackground(_:)
for section in sections { for case let .status(id: id, state: _) in dataSource.snapshot().itemIdentifiers {
for (id, _) in section { persistentContainer.status(for: id)?.decrementReferenceCount()
persistentContainer.status(for: id)?.decrementReferenceCount()
}
} }
} }
@ -57,98 +55,127 @@ class TimelineTableViewController: TimelineLikeTableViewController<TimelineEntry
tableView.register(UINib(nibName: "TimelineStatusTableViewCell", bundle: .main), forCellReuseIdentifier: "statusCell") tableView.register(UINib(nibName: "TimelineStatusTableViewCell", bundle: .main), forCellReuseIdentifier: "statusCell")
} }
// MARK: - DiffableTimelineLikeTableViewController
override class func refreshCommandTitle() -> String { override class func refreshCommandTitle() -> String {
return NSLocalizedString("Refresh Statuses", comment: "refresh status command discoverability title") return NSLocalizedString("Refresh Statuses", comment: "refresh status command discoverability title")
} }
override func willRemoveRows(at indexPaths: [IndexPath]) { override func cellProvider(_ tableView: UITableView, _ indexPath: IndexPath, _ item: Item) -> UITableViewCell? {
for indexPath in indexPaths { guard case let .status(id: id, state: state) = item,
let id = item(for: indexPath).id let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? TimelineStatusTableViewCell else {
mastodonController.persistentContainer.status(for: id)?.decrementReferenceCount() return nil
} }
cell.delegate = self
cell.updateUI(statusID: id, state: state)
return cell
} }
override func loadInitialItems(completion: @escaping ([TimelineEntry]) -> Void) { override func loadInitialItems(completion: @escaping (LoadResult) -> Void) {
guard let mastodonController = mastodonController else {
completion(.failure(.noClient))
return
}
let request = Client.getStatuses(timeline: timeline) let request = Client.getStatuses(timeline: timeline)
mastodonController?.run(request) { (response) in mastodonController.run(request) { response in
guard case let .success(statuses, pagination) = response else { switch response {
completion([]) case let .failure(error):
return completion(.failure(.client(error)))
}
case let .success(statuses, pagination):
self.newer = pagination?.newer self.newer = pagination?.newer
self.older = pagination?.older self.older = pagination?.older
self.mastodonController?.persistentContainer.addAll(statuses: statuses) { self.mastodonController.persistentContainer.addAll(statuses: statuses) {
completion(statuses.map { ($0.id, .unknown) }) var snapshot = Snapshot()
snapshot.appendSections([.statuses])
snapshot.appendItems(statuses.map { .status(id: $0.id, state: .unknown) })
completion(.success(snapshot))
}
} }
} }
} }
override func loadOlder(completion: @escaping ([TimelineEntry]) -> Void) { override func loadOlderItems(currentSnapshot: Snapshot, completion: @escaping (LoadResult) -> Void) {
guard let older = older else { guard let older = older else {
completion([]) completion(.failure(.noOlder))
return return
} }
let request = Client.getStatuses(timeline: timeline, range: older) let request = Client.getStatuses(timeline: timeline, range: older)
mastodonController.run(request) { (response) in mastodonController.run(request) { response in
guard case let .success(statuses, pagination) = response else { switch response {
completion([]) case let .failure(error):
return completion(.failure(.client(error)))
}
case let .success(statuses, pagination):
self.older = pagination?.older self.older = pagination?.older
self.mastodonController?.persistentContainer.addAll(statuses: statuses) { self.mastodonController.persistentContainer.addAll(statuses: statuses) {
completion(statuses.map { ($0.id, .unknown) }) var snapshot = currentSnapshot
snapshot.appendItems(statuses.map { .status(id: $0.id, state: .unknown) }, toSection: .statuses)
completion(.success(snapshot))
}
} }
} }
} }
override func loadNewer(completion: @escaping ([TimelineEntry]) -> Void) { override func loadNewerItems(currentSnapshot: Snapshot, completion: @escaping (LoadResult) -> Void) {
guard let newer = newer else { guard let newer = newer else {
completion([]) completion(.failure(.noNewer))
return return
} }
let request = Client.getStatuses(timeline: timeline, range: newer) let request = Client.getStatuses(timeline: timeline, range: newer)
mastodonController.run(request) { response in
mastodonController.run(request) { (response) in switch response {
guard case let .success(statuses, pagination) = response else { case let .failure(error):
completion([]) completion(.failure(.client(error)))
return
} case let .success(statuses, pagination):
// if there are no new statuses, pagination is nil
// if there are no new statuses, pagination is nil // if we were to then overwrite self.newer, future refresh would fail
// if we were to then overwrite self.newer, future refreshes would fail if let newer = pagination?.newer {
if let newer = pagination?.newer { self.newer = newer
self.newer = newer }
}
self.mastodonController.persistentContainer.addAll(statuses: statuses) {
self.mastodonController?.persistentContainer.addAll(statuses: statuses) { var snapshot = currentSnapshot
completion(statuses.map { ($0.id, .unknown) }) let identifiers = statuses.map { Item.status(id: $0.id, state: .unknown) }
if let first = snapshot.itemIdentifiers(inSection: .statuses).first {
snapshot.insertItems(identifiers, beforeItem: first)
} else {
snapshot.appendItems(identifiers, toSection: .statuses)
}
completion(.success(snapshot))
}
} }
} }
} }
// MARK: - UITableViewDataSource override func willRemoveItems(_ items: [Item]) {
for case let .status(id: id, state: _) in items {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { mastodonController.persistentContainer.status(for: id)?.decrementReferenceCount()
guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? TimelineStatusTableViewCell else { fatalError() } }
let (id, state) = item(for: indexPath)
cell.delegate = self
cell.updateUI(statusID: id, state: state)
return cell
} }
} }
extension TimelineTableViewController {
enum Section: Hashable, CaseIterable {
case statuses
case footer
}
enum Item: Hashable {
case status(id: String, state: StatusState)
}
}
extension TimelineTableViewController: TuskerNavigationDelegate { extension TimelineTableViewController: TuskerNavigationDelegate {
var apiController: MastodonController { mastodonController } var apiController: MastodonController { mastodonController }
} }
@ -161,17 +188,23 @@ extension TimelineTableViewController: StatusTableViewCellDelegate {
extension TimelineTableViewController: UITableViewDataSourcePrefetching, StatusTablePrefetching { extension TimelineTableViewController: UITableViewDataSourcePrefetching, StatusTablePrefetching {
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) { func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
let ids = indexPaths.map { item(for: $0).id }
prefetchStatuses(with: ids)
}
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
let ids: [String] = indexPaths.compactMap { let ids: [String] = indexPaths.compactMap {
guard $0.section < sections.count, if case let .status(id: id, state: _) = dataSource.itemIdentifier(for: $0) {
$0.row < sections[$0.section].count else { return id
} else {
return nil
}
}
prefetchStatuses(with: ids)
}
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
let ids: [String] = indexPaths.compactMap {
if case let .status(id: id, state: _) = dataSource.itemIdentifier(for: $0) {
return id
} else {
return nil return nil
} }
return item(for: $0).id
} }
cancelPrefetchingStatuses(with: ids) cancelPrefetchingStatuses(with: ids)
} }