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)
|
dismiss(animated: true)
|
||||||
|
|
||||||
// todo: show loading indicator
|
// todo: show loading indicator
|
||||||
reloadInitialItems()
|
reloadInitial()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,96 +55,125 @@ 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
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override func loadInitialItems(completion: @escaping ([TimelineEntry]) -> Void) {
|
|
||||||
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) })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override func loadOlder(completion: @escaping ([TimelineEntry]) -> Void) {
|
|
||||||
guard let older = older else {
|
|
||||||
completion([])
|
|
||||||
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) })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override func loadNewer(completion: @escaping ([TimelineEntry]) -> Void) {
|
|
||||||
guard let newer = newer else {
|
|
||||||
completion([])
|
|
||||||
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) })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.delegate = self
|
||||||
|
|
||||||
cell.updateUI(statusID: id, state: state)
|
cell.updateUI(statusID: id, state: state)
|
||||||
|
|
||||||
return cell
|
return cell
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
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 loadOlderItems(currentSnapshot: Snapshot, completion: @escaping (LoadResult) -> Void) {
|
||||||
|
guard let older = older else {
|
||||||
|
completion(.failure(.noOlder))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let request = Client.getStatuses(timeline: timeline, range: older)
|
||||||
|
|
||||||
|
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 loadNewerItems(currentSnapshot: Snapshot, completion: @escaping (LoadResult) -> Void) {
|
||||||
|
guard let newer = newer else {
|
||||||
|
completion(.failure(.noNewer))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let request = Client.getStatuses(timeline: timeline, range: newer)
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
extension TimelineTableViewController: TuskerNavigationDelegate {
|
||||||
|
@ -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 }
|
let ids: [String] = indexPaths.compactMap {
|
||||||
|
if case let .status(id: id, state: _) = dataSource.itemIdentifier(for: $0) {
|
||||||
|
return id
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
prefetchStatuses(with: ids)
|
prefetchStatuses(with: ids)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
return item(for: $0).id
|
|
||||||
}
|
}
|
||||||
cancelPrefetchingStatuses(with: ids)
|
cancelPrefetchingStatuses(with: ids)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue