forked from shadowfacts/Tusker
172 lines
6.7 KiB
Swift
172 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: UITableViewController {
|
|
|
|
static func create(for timeline: Timeline) -> UIViewController {
|
|
guard let timelineController = UIStoryboard(name: "Timeline", bundle: nil).instantiateInitialViewController() as? TimelineTableViewController else { fatalError() }
|
|
timelineController.timeline = timeline
|
|
|
|
let title: String
|
|
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"
|
|
}
|
|
timelineController.title = title
|
|
|
|
return timelineController
|
|
}
|
|
|
|
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))
|
|
}
|
|
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))
|
|
}
|
|
|
|
var timeline: Timeline!
|
|
|
|
var statusIDs: [String] = [] {
|
|
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 }
|
|
let request = MastodonController.shared.client.getStatuses(timeline: timeline)
|
|
MastodonController.shared.client.run(request) { response in
|
|
guard case let .success(statuses, pagination) = response else { fatalError() }
|
|
self.statusIDs = statuses.map { $0.id }
|
|
MastodonCache.addAll(statuses: statuses)
|
|
self.newer = pagination?.newer
|
|
self.older = pagination?.older
|
|
}
|
|
}
|
|
|
|
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 statusIDs.count
|
|
}
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else { fatalError() }
|
|
|
|
let statusID = statusIDs[indexPath.row]
|
|
|
|
cell.updateUI(for: statusID)
|
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
|
if indexPath.row == statusIDs.count - 1 {
|
|
guard let older = older else { return }
|
|
|
|
let request = MastodonController.shared.client.getStatuses(timeline: timeline, range: older)
|
|
MastodonController.shared.client.run(request) { response in
|
|
guard case let .success(newStatuses, pagination) = response else { fatalError() }
|
|
self.older = pagination?.older
|
|
MastodonCache.addAll(statuses: newStatuses)
|
|
self.statusIDs.append(contentsOf: newStatuses.map { $0.id })
|
|
}
|
|
}
|
|
}
|
|
|
|
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
|
return tableView.cellForRow(at: indexPath) is TableViewSwipeActionProvider
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
@IBAction func refreshStatuses(_ sender: Any) {
|
|
guard let newer = newer else { return }
|
|
|
|
let request = MastodonController.shared.client.getStatuses(timeline: timeline, range: newer)
|
|
MastodonController.shared.client.run(request) { response in
|
|
guard case let .success(newStatuses, pagination) = response else { fatalError() }
|
|
self.newer = pagination?.newer
|
|
MastodonCache.addAll(statuses: newStatuses)
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension TimelineTableViewController: StatusTableViewCellDelegate {}
|
|
extension TimelineTableViewController: LargeImageViewControllerDelegate {}
|