From 5b8e97287e32580292e4075c6208434abd03a5c6 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 20 Jun 2021 22:27:38 -0400 Subject: [PATCH] Convert TimelineTableViewController to use DiffableTimelineLikeTableViewController --- .../Lists/ListTimelineViewController.swift | 2 +- .../TimelineTableViewController.swift | 173 +++++++++++------- 2 files changed, 104 insertions(+), 71 deletions(-) diff --git a/Tusker/Screens/Lists/ListTimelineViewController.swift b/Tusker/Screens/Lists/ListTimelineViewController.swift index 36af70eb..a7a7ab0a 100644 --- a/Tusker/Screens/Lists/ListTimelineViewController.swift +++ b/Tusker/Screens/Lists/ListTimelineViewController.swift @@ -58,7 +58,7 @@ class ListTimelineViewController: TimelineTableViewController { dismiss(animated: true) // todo: show loading indicator - reloadInitialItems() + reloadInitial() } } diff --git a/Tusker/Screens/Timeline/TimelineTableViewController.swift b/Tusker/Screens/Timeline/TimelineTableViewController.swift index b2905246..3848feaa 100644 --- a/Tusker/Screens/Timeline/TimelineTableViewController.swift +++ b/Tusker/Screens/Timeline/TimelineTableViewController.swift @@ -11,7 +11,7 @@ import Pachyderm typealias TimelineEntry = (id: String, state: StatusState) -class TimelineTableViewController: TimelineLikeTableViewController { +class TimelineTableViewController: DiffableTimelineLikeTableViewController { let timeline: Timeline weak var mastodonController: MastodonController! @@ -44,10 +44,8 @@ class TimelineTableViewController: TimelineLikeTableViewController String { return NSLocalizedString("Refresh Statuses", comment: "refresh status command discoverability title") } - - override func willRemoveRows(at indexPaths: [IndexPath]) { - for indexPath in indexPaths { - let id = item(for: indexPath).id - mastodonController.persistentContainer.status(for: id)?.decrementReferenceCount() + + override func cellProvider(_ tableView: UITableView, _ indexPath: IndexPath, _ item: Item) -> UITableViewCell? { + guard case let .status(id: id, state: state) = item, + let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? TimelineStatusTableViewCell else { + 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) - mastodonController?.run(request) { (response) in - guard case let .success(statuses, pagination) = response else { - completion([]) - return - } - - self.newer = pagination?.newer - self.older = pagination?.older - - self.mastodonController?.persistentContainer.addAll(statuses: statuses) { - completion(statuses.map { ($0.id, .unknown) }) + mastodonController.run(request) { response in + switch response { + case let .failure(error): + completion(.failure(.client(error))) + + case let .success(statuses, pagination): + self.newer = pagination?.newer + self.older = pagination?.older + + self.mastodonController.persistentContainer.addAll(statuses: statuses) { + 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 { - completion([]) + completion(.failure(.noOlder)) return } let request = Client.getStatuses(timeline: timeline, range: older) - mastodonController.run(request) { (response) in - guard case let .success(statuses, pagination) = response else { - completion([]) - return - } - - self.older = pagination?.older - - self.mastodonController?.persistentContainer.addAll(statuses: statuses) { - completion(statuses.map { ($0.id, .unknown) }) + mastodonController.run(request) { response in + switch response { + case let .failure(error): + completion(.failure(.client(error))) + + case let .success(statuses, pagination): + self.older = pagination?.older + + self.mastodonController.persistentContainer.addAll(statuses: statuses) { + 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 { - completion([]) + completion(.failure(.noNewer)) return } let request = Client.getStatuses(timeline: timeline, range: newer) - - mastodonController.run(request) { (response) in - guard case let .success(statuses, pagination) = response else { - completion([]) - return - } - - // if there are no new statuses, pagination is nil - // if we were to then overwrite self.newer, future refreshes would fail - if let newer = pagination?.newer { - self.newer = newer - } - - self.mastodonController?.persistentContainer.addAll(statuses: statuses) { - completion(statuses.map { ($0.id, .unknown) }) + mastodonController.run(request) { response in + switch response { + case let .failure(error): + completion(.failure(.client(error))) + + case let .success(statuses, pagination): + // if there are no new statuses, pagination is nil + // if we were to then overwrite self.newer, future refresh would fail + if let newer = pagination?.newer { + self.newer = newer + } + + self.mastodonController.persistentContainer.addAll(statuses: statuses) { + var snapshot = currentSnapshot + 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 tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - 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 + override func willRemoveItems(_ items: [Item]) { + for case let .status(id: id, state: _) in items { + mastodonController.persistentContainer.status(for: id)?.decrementReferenceCount() + } } } +extension TimelineTableViewController { + enum Section: Hashable, CaseIterable { + case statuses + case footer + } + enum Item: Hashable { + case status(id: String, state: StatusState) + } +} + extension TimelineTableViewController: TuskerNavigationDelegate { var apiController: MastodonController { mastodonController } } @@ -161,17 +188,23 @@ extension TimelineTableViewController: StatusTableViewCellDelegate { extension TimelineTableViewController: UITableViewDataSourcePrefetching, StatusTablePrefetching { 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 { - guard $0.section < sections.count, - $0.row < sections[$0.section].count else { + if case let .status(id: id, state: _) = dataSource.itemIdentifier(for: $0) { + 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 item(for: $0).id } cancelPrefetchingStatuses(with: ids) }