forked from shadowfacts/Tusker
Convert TimelineTableViewController to use DiffableTimelineLikeTableViewController
This commit is contained in:
parent
49572c1fec
commit
5b8e97287e
|
@ -58,7 +58,7 @@ class ListTimelineViewController: TimelineTableViewController {
|
|||
dismiss(animated: true)
|
||||
|
||||
// todo: show loading indicator
|
||||
reloadInitialItems()
|
||||
reloadInitial()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ import Pachyderm
|
|||
|
||||
typealias TimelineEntry = (id: String, state: StatusState)
|
||||
|
||||
class TimelineTableViewController: TimelineLikeTableViewController<TimelineEntry> {
|
||||
class TimelineTableViewController: DiffableTimelineLikeTableViewController<TimelineTableViewController.Section, TimelineTableViewController.Item> {
|
||||
|
||||
let timeline: Timeline
|
||||
weak var mastodonController: MastodonController!
|
||||
|
@ -44,10 +44,8 @@ class TimelineTableViewController: TimelineLikeTableViewController<TimelineEntry
|
|||
// decrement reference counts of any statuses we still have
|
||||
// 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(_:)
|
||||
for section in sections {
|
||||
for (id, _) in section {
|
||||
persistentContainer.status(for: id)?.decrementReferenceCount()
|
||||
}
|
||||
for case let .status(id: id, state: _) in dataSource.snapshot().itemIdentifiers {
|
||||
persistentContainer.status(for: id)?.decrementReferenceCount()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,98 +55,127 @@ class TimelineTableViewController: TimelineLikeTableViewController<TimelineEntry
|
|||
tableView.register(UINib(nibName: "TimelineStatusTableViewCell", bundle: .main), forCellReuseIdentifier: "statusCell")
|
||||
}
|
||||
|
||||
// MARK: - DiffableTimelineLikeTableViewController
|
||||
|
||||
override class func refreshCommandTitle() -> 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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue