Tusker/Tusker/Screens/Timeline/TimelineTableViewController...

143 lines
5.0 KiB
Swift

//
// StatusesTableViewController.swift
// Tusker
//
// Created by Shadowfacts on 8/15/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
import MastodonKit
class TimelineTableViewController: UITableViewController {
static func create(for timeline: Timeline) -> UIViewController {
guard let navigationController = UIStoryboard(name: "Timeline", bundle: nil).instantiateInitialViewController() as? UINavigationController,
let timelineController = navigationController.topViewController as? TimelineTableViewController else { fatalError() }
timelineController.timeline = timeline
switch timeline {
case .home:
navigationController.tabBarItem.title = "Home"
timelineController.navigationItem.title = "Home"
case .local:
navigationController.tabBarItem.title = "Local"
timelineController.navigationItem.title = "Local"
case .federated:
navigationController.tabBarItem.title = "Federated"
timelineController.navigationItem.title = "Federated"
}
return navigationController
}
var timeline: Timeline!
var statuses: [Status] = [] {
didSet {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
var newer: RequestRange?
var older: RequestRange?
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 140
tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
guard MastodonController.shared.client?.accessToken != nil else { return }
MastodonController.shared.client.run(timeline.request()) { result in
guard case let .success(statuses, pagination) = result else { fatalError() }
self.statuses = statuses
self.newer = pagination?.previous
self.older = pagination?.next
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
for cell in tableView.visibleCells {
if let cell = cell as? PreferencesAdaptive {
cell.updateUIForPreferences()
}
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return statuses.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else { fatalError() }
let status = statuses[indexPath.row]
cell.updateUI(for: status)
cell.delegate = self
return cell
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == statuses.count - 1 {
guard let older = older else { return }
MastodonController.shared.client.run(timeline.request(range: older)) { result in
guard case let .success(newStatuses, pagination) = result else { fatalError() }
self.older = pagination?.next
self.statuses.append(contentsOf: newStatuses)
}
}
}
@IBAction func refreshStatuses(_ sender: Any) {
guard let newer = newer else { return }
MastodonController.shared.client.run(timeline.request(range: newer)) { result in
guard case let .success(newStatuses, pagination) = result else { fatalError() }
self.newer = pagination?.previous
self.statuses.insert(contentsOf: newStatuses, 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)
}
}
}
}
extension TimelineTableViewController: StatusTableViewCellDelegate {
func updatedStatus(for cell: StatusTableViewCell, status: Status) {
let indexPath = tableView.indexPath(for: cell)!
statuses[indexPath.row] = status
}
}
extension TimelineTableViewController: LargeImageViewControllerDelegate {}