forked from shadowfacts/Tusker
177 lines
6.7 KiB
Swift
177 lines
6.7 KiB
Swift
//
|
|
// StatusesTableViewController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 8/15/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Pachyderm
|
|
|
|
class TimelineTableViewController: EnhancedTableViewController {
|
|
|
|
var timeline: Timeline!
|
|
|
|
var timelineSegments: [[(id: String, state: StatusState)]] = [] {
|
|
didSet {
|
|
DispatchQueue.main.async {
|
|
self.tableView.reloadData()
|
|
}
|
|
}
|
|
}
|
|
|
|
var newer: RequestRange?
|
|
var older: RequestRange?
|
|
|
|
init(for timeline: Timeline) {
|
|
self.timeline = timeline
|
|
|
|
super.init(style: .plain)
|
|
|
|
title = timeline.title
|
|
tabBarItem.image = timeline.tabBarImage
|
|
|
|
self.refreshControl = UIRefreshControl()
|
|
refreshControl!.addTarget(self, action: #selector(refreshStatuses(_:)), for: .valueChanged)
|
|
|
|
userActivity = UserActivityManager.showTimelineActivity(timeline: timeline)
|
|
}
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
func statusID(for indexPath: IndexPath) -> String {
|
|
return timelineSegments[indexPath.section][indexPath.row].id
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
tableView.rowHeight = UITableView.automaticDimension
|
|
tableView.estimatedRowHeight = 140
|
|
|
|
tableView.register(UINib(nibName: "TimelineStatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
|
|
|
|
tableView.prefetchDataSource = self
|
|
|
|
guard MastodonController.client?.accessToken != nil else { return }
|
|
loadInitialStatuses()
|
|
}
|
|
|
|
func loadInitialStatuses() {
|
|
let request = MastodonController.client.getStatuses(timeline: timeline)
|
|
MastodonController.client.run(request) { response in
|
|
guard case let .success(statuses, pagination) = response else { fatalError() }
|
|
MastodonCache.addAll(statuses: statuses)
|
|
self.timelineSegments.insert(statuses.map { ($0.id, .unknown) }, at: 0)
|
|
self.newer = pagination?.newer
|
|
self.older = pagination?.older
|
|
}
|
|
}
|
|
|
|
// MARK: - Table view data source
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
|
return timelineSegments.count
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
return timelineSegments[section].count
|
|
}
|
|
|
|
|
|
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) = timelineSegments[indexPath.section][indexPath.row]
|
|
cell.updateUI(statusID: id, state: state)
|
|
cell.delegate = self
|
|
|
|
return cell
|
|
}
|
|
|
|
// MARK: - Table view delegate
|
|
|
|
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
|
if indexPath.section == timelineSegments.count - 1,
|
|
indexPath.row == timelineSegments[indexPath.section].count - 1 {
|
|
guard let older = older else { return }
|
|
|
|
let request = MastodonController.client.getStatuses(timeline: timeline, range: older)
|
|
MastodonController.client.run(request) { response in
|
|
guard case let .success(newStatuses, pagination) = response else { fatalError() }
|
|
self.older = pagination?.older
|
|
MastodonCache.addAll(statuses: newStatuses)
|
|
self.timelineSegments[self.timelineSegments.count - 1].append(contentsOf: newStatuses.map { ($0.id, .unknown) })
|
|
}
|
|
}
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
|
return true
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
|
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.leadingSwipeActionsConfiguration()
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
|
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration()
|
|
}
|
|
|
|
@objc func refreshStatuses(_ sender: Any) {
|
|
guard let newer = newer else { return }
|
|
|
|
let request = MastodonController.client.getStatuses(timeline: timeline, range: newer)
|
|
MastodonController.client.run(request) { response in
|
|
guard case let .success(newStatuses, pagination) = response else { fatalError() }
|
|
self.newer = pagination?.newer
|
|
MastodonCache.addAll(statuses: newStatuses)
|
|
self.timelineSegments[0].insert(contentsOf: newStatuses.map { ($0.id, .unknown) }, at: 0)
|
|
DispatchQueue.main.async {
|
|
self.refreshControl?.endRefreshing()
|
|
|
|
// maintain the current position in the list (don't scroll to the top)
|
|
self.tableView.scrollToRow(at: IndexPath(row: newStatuses.count, section: 0), at: .top, animated: false)
|
|
}
|
|
}
|
|
}
|
|
|
|
@objc func composePressed(_ sender: Any) {
|
|
compose()
|
|
}
|
|
|
|
}
|
|
|
|
extension TimelineTableViewController: StatusTableViewCellDelegate {
|
|
func statusCellCollapsedStateChanged(_ cell: BaseStatusTableViewCell) {
|
|
// causes the table view to recalculate the cell heights
|
|
tableView.beginUpdates()
|
|
tableView.endUpdates()
|
|
}
|
|
}
|
|
|
|
extension TimelineTableViewController: UITableViewDataSourcePrefetching {
|
|
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
|
for indexPath in indexPaths {
|
|
guard let status = MastodonCache.status(for: statusID(for: indexPath)) else { continue }
|
|
ImageCache.avatars.get(status.account.avatar, completion: nil)
|
|
for attachment in status.attachments {
|
|
ImageCache.attachments.get(attachment.url, completion: nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
|
|
for indexPath in indexPaths {
|
|
guard let status = MastodonCache.status(for: statusID(for: indexPath)) else { continue }
|
|
ImageCache.avatars.cancel(status.account.avatar)
|
|
for attachment in status.attachments {
|
|
ImageCache.attachments.cancel(attachment.url)
|
|
}
|
|
}
|
|
}
|
|
}
|