// // StatusesTableViewController.swift // Tusker // // Created by Shadowfacts on 8/15/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import UIKit import Pachyderm class TimelineTableViewController: EnhancedTableViewController { 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 timelineSegments: [TimelineSegment] = [] { didSet { DispatchQueue.main.async { self.tableView.reloadData() } } } var newer: RequestRange? var older: RequestRange? init(for timeline: Timeline) { self.timeline = timeline super.init(style: .plain) title = timeline.title tabBarItem.image = timeline.tabBarImage self.refreshControl = UIRefreshControl() refreshControl!.addTarget(self, action: #selector(refreshStatuses(_:)), for: .valueChanged) userActivity = UserActivityManager.showTimelineActivity(timeline: timeline) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func statusID(for indexPath: IndexPath) -> String { return timelineSegments[indexPath.section][indexPath.row] } override func viewDidLoad() { super.viewDidLoad() tableView.rowHeight = UITableView.automaticDimension tableView.estimatedRowHeight = 140 tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell") tableView.prefetchDataSource = self guard MastodonController.client?.accessToken != nil else { return } let request = MastodonController.client.getStatuses(timeline: timeline) MastodonController.client.run(request) { response in guard case let .success(statuses, pagination) = response else { fatalError() } MastodonCache.addAll(statuses: statuses) self.timelineSegments.insert(TimelineSegment(objects: statuses), at: 0) self.newer = pagination?.newer self.older = pagination?.older } } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return timelineSegments.count } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return timelineSegments[section].count } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else { fatalError() } cell.updateUI(statusID: statusID(for: indexPath)) cell.delegate = self return cell } // MARK: - Table view delegate override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if indexPath.section == timelineSegments.count - 1, indexPath.row == timelineSegments[indexPath.section].count - 1 { let older = self.older ?? RequestRange.before(id: timelineSegments.last!.last!, count: nil) let request = MastodonController.client.getStatuses(timeline: timeline, range: older) MastodonController.client.run(request) { response in guard case let .success(newStatuses, pagination) = response else { fatalError() } self.older = pagination?.older MastodonCache.addAll(statuses: newStatuses) self.timelineSegments[self.timelineSegments.count - 1].append(objects: newStatuses) } } } override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true } 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() } @objc func refreshStatuses(_ sender: Any) { let newer = self.newer ?? RequestRange.after(id: timelineSegments.first!.first!, count: nil) let request = MastodonController.client.getStatuses(timeline: timeline, range: newer) MastodonController.client.run(request) { response in guard case let .success(newStatuses, pagination) = response else { fatalError() } self.newer = pagination?.newer MastodonCache.addAll(statuses: newStatuses) self.timelineSegments[0].insertAtBeginning(objects: newStatuses) 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) } } } @objc func composePressed(_ sender: Any) { compose() } } extension TimelineTableViewController: StatusTableViewCellDelegate { func statusCollapsedStateChanged() { // causes the table view to recalculate the cell heights tableView.beginUpdates() tableView.endUpdates() } } extension TimelineTableViewController: UITableViewDataSourcePrefetching { func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) { for indexPath in indexPaths { guard let status = MastodonCache.status(for: statusID(for: indexPath)) else { continue } 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 { guard let status = MastodonCache.status(for: statusID(for: indexPath)) else { continue } ImageCache.avatars.cancel(status.account.avatar) for attachment in status.attachments { ImageCache.attachments.cancel(attachment.url) } } } }