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

175 lines
6.6 KiB
Swift
Raw Normal View History

2018-08-16 11:46:19 +00:00
//
// StatusesTableViewController.swift
// Tusker
//
// Created by Shadowfacts on 8/15/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
2018-09-11 14:52:21 +00:00
import Pachyderm
2018-08-16 11:46:19 +00:00
2018-08-21 21:17:25 +00:00
class TimelineTableViewController: UITableViewController {
let router: AppRouter
2018-09-15 14:56:27 +00:00
lazy var favoriteActionImage: UIImage = UIGraphicsImageRenderer(size: CGSize(width: 30 * 137/131, height: 30)).image { _ in
UIImage(named: "Favorite")!.draw(in: CGRect(x: 0, y: 0, width: 30 * 137/131, height: 30))
}
lazy var reblogActionImage: UIImage = UIGraphicsImageRenderer(size: CGSize(width: 30 * 927/558, height: 30)).image { _ in
UIImage(named: "Reblog")!.draw(in: CGRect(x: 0, y: 0, width: 30 * 927/558, height: 30))
}
2018-09-15 15:37:20 +00:00
lazy var replyActionImage: UIImage = UIGraphicsImageRenderer(size: CGSize(width: 30 * 205/151, height: 30)).image { _ in
UIImage(named: "Reply")!.draw(in: CGRect(x: 0, y: 0, width: 30 * 205/151, height: 30))
}
lazy var moreActionImage: UIImage = UIGraphicsImageRenderer(size: CGSize(width: 30 * 2/1, height: 30)).image { _ in
UIImage(named: "More")!.draw(in: CGRect(x: 0, y: 0, width: 30 * 2/1, height: 30))
}
2018-09-15 14:56:27 +00:00
2018-08-21 21:17:25 +00:00
var timeline: Timeline!
2018-09-18 01:57:46 +00:00
var statusIDs: [String] = [] {
2018-08-17 02:39:16 +00:00
didSet {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
var newer: RequestRange?
var older: RequestRange?
init(for timeline: Timeline, router: AppRouter) {
2018-10-20 16:03:18 +00:00
self.timeline = timeline
self.router = router
super.init(style: .plain)
2018-10-20 16:03:18 +00:00
switch timeline {
case .home:
title = "Home"
case let .public(local):
title = local ? "Local" : "Federated"
case let .tag(hashtag):
title = "#\(hashtag)"
case .list:
title = "List"
case .direct:
title = "Direct"
}
self.refreshControl = UIRefreshControl()
refreshControl!.addTarget(self, action: #selector(refreshStatuses(_:)), for: .valueChanged)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: #selector(composePressed(_:)))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 140
tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
2018-08-28 01:27:34 +00:00
2018-10-02 23:31:00 +00:00
guard MastodonController.client?.accessToken != nil else { return }
let request = MastodonController.client.getStatuses(timeline: timeline)
MastodonController.client.run(request) { response in
2018-09-11 14:52:21 +00:00
guard case let .success(statuses, pagination) = response else { fatalError() }
2018-09-18 01:57:46 +00:00
self.statusIDs = statuses.map { $0.id }
2018-09-18 16:59:07 +00:00
MastodonCache.addAll(statuses: statuses)
2018-09-11 14:52:21 +00:00
self.newer = pagination?.newer
self.older = pagination?.older
2018-08-17 02:39:16 +00:00
}
2018-10-12 01:20:58 +00:00
registerForPreviewing(with: self, sourceView: view)
2018-08-17 02:39:16 +00:00
}
2018-08-28 23:49:31 +00:00
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
2018-08-28 23:49:31 +00:00
for cell in tableView.visibleCells {
if let cell = cell as? PreferencesAdaptive {
cell.updateUIForPreferences()
}
}
}
2018-08-16 11:46:19 +00:00
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
2018-08-17 02:39:16 +00:00
return 1
2018-08-16 11:46:19 +00:00
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
2018-09-18 01:57:46 +00:00
return statusIDs.count
2018-08-16 11:46:19 +00:00
}
2018-08-17 02:39:16 +00:00
2018-08-16 11:46:19 +00:00
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
2018-08-28 18:29:06 +00:00
guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else { fatalError() }
2018-08-17 02:39:16 +00:00
2018-09-18 01:57:46 +00:00
let statusID = statusIDs[indexPath.row]
2018-08-17 02:39:16 +00:00
2018-09-18 01:57:46 +00:00
cell.updateUI(for: statusID)
cell.delegate = self
2018-08-16 11:46:19 +00:00
return cell
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
2018-09-18 01:57:46 +00:00
if indexPath.row == statusIDs.count - 1 {
guard let older = older else { return }
2018-10-02 23:31:00 +00:00
let request = MastodonController.client.getStatuses(timeline: timeline, range: older)
MastodonController.client.run(request) { response in
2018-09-11 14:52:21 +00:00
guard case let .success(newStatuses, pagination) = response else { fatalError() }
self.older = pagination?.older
2018-09-18 16:59:07 +00:00
MastodonCache.addAll(statuses: newStatuses)
2018-09-18 01:57:46 +00:00
self.statusIDs.append(contentsOf: newStatuses.map { $0.id })
}
}
2018-08-16 11:46:19 +00:00
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return tableView.cellForRow(at: indexPath) is TableViewSwipeActionProvider
}
2018-09-15 14:56:27 +00:00
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.leadingSwipeActionsConfiguration()
2018-09-15 14:56:27 +00:00
}
2018-09-15 15:37:20 +00:00
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration()
2018-09-15 15:37:20 +00:00
}
2018-10-20 16:03:18 +00:00
@objc func refreshStatuses(_ sender: Any) {
guard let newer = newer else { return }
2018-10-02 23:31:00 +00:00
let request = MastodonController.client.getStatuses(timeline: timeline, range: newer)
MastodonController.client.run(request) { response in
2018-09-11 14:52:21 +00:00
guard case let .success(newStatuses, pagination) = response else { fatalError() }
self.newer = pagination?.newer
2018-09-18 16:59:07 +00:00
MastodonCache.addAll(statuses: newStatuses)
2018-09-18 01:57:46 +00:00
self.statusIDs.insert(contentsOf: newStatuses.map { $0.id }, 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)
}
}
2018-08-16 11:46:19 +00:00
}
2018-10-20 16:03:18 +00:00
@objc func composePressed(_ sender: Any) {
compose()
}
2018-08-16 11:46:19 +00:00
}
2018-09-18 01:57:46 +00:00
extension TimelineTableViewController: StatusTableViewCellDelegate {}