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-11-10 16:48:09 +00:00
|
|
|
class TimelineTableViewController: EnhancedTableViewController {
|
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!
|
|
|
|
|
2019-07-31 23:33:48 +00:00
|
|
|
var timelineSegments: [TimelineSegment<Status>] = [] {
|
2018-08-17 02:39:16 +00:00
|
|
|
didSet {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.tableView.reloadData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-18 21:46:28 +00:00
|
|
|
var newer: RequestRange?
|
|
|
|
var older: RequestRange?
|
|
|
|
|
2019-01-19 19:31:31 +00:00
|
|
|
init(for timeline: Timeline) {
|
2018-10-20 16:03:18 +00:00
|
|
|
self.timeline = timeline
|
2018-10-21 02:07:04 +00:00
|
|
|
|
|
|
|
super.init(style: .plain)
|
2018-10-20 16:03:18 +00:00
|
|
|
|
2019-06-11 17:21:22 +00:00
|
|
|
title = timeline.title
|
|
|
|
tabBarItem.image = timeline.tabBarImage
|
2018-10-20 16:03:18 +00:00
|
|
|
|
|
|
|
self.refreshControl = UIRefreshControl()
|
|
|
|
refreshControl!.addTarget(self, action: #selector(refreshStatuses(_:)), for: .valueChanged)
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2019-07-31 23:33:48 +00:00
|
|
|
func statusID(for indexPath: IndexPath) -> String {
|
|
|
|
return timelineSegments[indexPath.section][indexPath.row]
|
|
|
|
}
|
|
|
|
|
2018-08-27 20:32:59 +00:00
|
|
|
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
|
|
|
|
2019-02-08 18:37:38 +00:00
|
|
|
tableView.prefetchDataSource = self
|
|
|
|
|
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 16:59:07 +00:00
|
|
|
MastodonCache.addAll(statuses: statuses)
|
2019-07-31 23:33:48 +00:00
|
|
|
self.timelineSegments.insert(TimelineSegment(objects: statuses), at: 0)
|
2018-09-11 14:52:21 +00:00
|
|
|
self.newer = pagination?.newer
|
|
|
|
self.older = pagination?.older
|
2018-08-17 02:39:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-03 00:05:47 +00:00
|
|
|
|
2018-08-16 11:46:19 +00:00
|
|
|
// MARK: - Table view data source
|
|
|
|
|
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2019-07-31 23:33:48 +00:00
|
|
|
return timelineSegments.count
|
2018-08-16 11:46:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2019-07-31 23:33:48 +00:00
|
|
|
return timelineSegments[section].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() }
|
2019-08-02 03:01:15 +00:00
|
|
|
|
|
|
|
cell.updateUI(statusID: statusID(for: indexPath))
|
2018-08-27 19:23:59 +00:00
|
|
|
cell.delegate = self
|
2018-08-16 11:46:19 +00:00
|
|
|
|
|
|
|
return cell
|
|
|
|
}
|
2018-08-18 21:46:28 +00:00
|
|
|
|
2019-06-04 21:04:37 +00:00
|
|
|
// MARK: - Table view delegate
|
|
|
|
|
2018-08-18 21:46:28 +00:00
|
|
|
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
|
2019-07-31 23:33:48 +00:00
|
|
|
if indexPath.section == timelineSegments.count - 1,
|
|
|
|
indexPath.row == timelineSegments[indexPath.section].count - 1 {
|
2018-08-18 21:46:28 +00:00
|
|
|
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)
|
2019-07-31 23:33:48 +00:00
|
|
|
self.timelineSegments[self.timelineSegments.count - 1].append(objects: newStatuses)
|
2018-08-18 21:46:28 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-16 11:46:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:01:13 +00:00
|
|
|
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
2019-06-14 02:31:36 +00:00
|
|
|
return true
|
2018-09-15 17:01:13 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 14:56:27 +00:00
|
|
|
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
2019-06-14 02:31:36 +00:00
|
|
|
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? {
|
2019-06-14 02:31:36 +00:00
|
|
|
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) {
|
2018-08-18 21:46:28 +00:00
|
|
|
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)
|
2019-07-31 23:33:48 +00:00
|
|
|
self.timelineSegments[0].insertAtBeginning(objects: newStatuses)
|
2018-08-18 21:46:28 +00:00
|
|
|
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-18 21:46:28 +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-03 22:46:20 +00:00
|
|
|
|
2018-09-18 01:57:46 +00:00
|
|
|
extension TimelineTableViewController: StatusTableViewCellDelegate {}
|
2019-02-08 18:37:38 +00:00
|
|
|
|
|
|
|
extension TimelineTableViewController: UITableViewDataSourcePrefetching {
|
|
|
|
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
|
|
|
for indexPath in indexPaths {
|
2019-07-31 23:33:48 +00:00
|
|
|
guard let status = MastodonCache.status(for: statusID(for: indexPath)) else { continue }
|
2019-02-08 18:37:38 +00:00
|
|
|
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 {
|
2019-07-31 23:33:48 +00:00
|
|
|
guard let status = MastodonCache.status(for: statusID(for: indexPath)) else { continue }
|
2019-02-08 18:37:38 +00:00
|
|
|
ImageCache.avatars.cancel(status.account.avatar)
|
|
|
|
for attachment in status.attachments {
|
|
|
|
ImageCache.attachments.cancel(attachment.url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|