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

172 lines
6.8 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 {
2018-08-16 11:46:19 +00:00
2018-10-02 23:23:50 +00:00
static func create(for timeline: Timeline) -> TimelineTableViewController {
guard let timelineController = UIStoryboard(name: "Timeline", bundle: nil).instantiateInitialViewController() as? TimelineTableViewController else { fatalError() }
2018-08-21 21:17:25 +00:00
timelineController.timeline = timeline
2018-09-11 14:52:21 +00:00
let title: String
2018-08-21 21:17:25 +00:00
switch timeline {
case .home:
2018-09-11 14:52:21 +00:00
title = "Home"
case let .public(local):
title = local ? "Local" : "Federated"
case let .tag(hashtag):
title = "#\(hashtag)"
case .list:
title = "List"
case .direct:
title = "Direct"
2018-08-21 21:17:25 +00:00
}
timelineController.title = title
2018-08-21 21:17:25 +00:00
return timelineController
2018-08-21 21:17:25 +00:00
}
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?
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-08-19 20:14:04 +00:00
guard MastodonController.shared.client?.accessToken != nil else { return }
2018-09-18 00:58:05 +00:00
let request = MastodonController.shared.client.getStatuses(timeline: timeline)
MastodonController.shared.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-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: - 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.
}
*/
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-09-18 00:58:05 +00:00
let request = MastodonController.shared.client.getStatuses(timeline: timeline, range: older)
MastodonController.shared.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
}
@IBAction func refreshStatuses(_ sender: Any) {
guard let newer = newer else { return }
2018-09-18 00:58:05 +00:00
let request = MastodonController.shared.client.getStatuses(timeline: timeline, range: newer)
MastodonController.shared.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-08-16 11:46:19 +00:00
}
2018-09-18 01:57:46 +00:00
extension TimelineTableViewController: StatusTableViewCellDelegate {}
extension TimelineTableViewController: LargeImageViewControllerDelegate {}