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

264 lines
10 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-08-21 21:17:25 +00:00
static func create(for timeline: Timeline) -> UIViewController {
guard let navigationController = UIStoryboard(name: "Timeline", bundle: nil).instantiateInitialViewController() as? UINavigationController,
let timelineController = navigationController.topViewController as? TimelineTableViewController else { fatalError() }
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
}
2018-09-11 14:52:21 +00:00
navigationController.tabBarItem.title = title
timelineController.navigationItem.title = title
2018-08-21 21:17:25 +00:00
return navigationController
}
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-08-17 02:39:16 +00:00
var statuses: [Status] = [] {
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-11 14:52:21 +00:00
MastodonController.shared.client.getStatuses(timeline: timeline) { response in
guard case let .success(statuses, pagination) = response else { fatalError() }
2018-08-17 02:39:16 +00:00
self.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-08-17 02:39:16 +00:00
return statuses.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
let status = statuses[indexPath.row]
cell.updateUI(for: status)
cell.delegate = self
2018-08-16 11:46:19 +00:00
return cell
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == statuses.count - 1 {
guard let older = older else { return }
2018-09-11 14:52:21 +00:00
MastodonController.shared.client.getStatuses(timeline: timeline, range: older) { response in
guard case let .success(newStatuses, pagination) = response else { fatalError() }
self.older = pagination?.older
self.statuses.append(contentsOf: newStatuses)
}
}
2018-08-16 11:46:19 +00:00
}
2018-09-15 14:56:27 +00:00
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let status = statuses[indexPath.row]
2018-09-15 15:37:20 +00:00
let favorite: UIContextualAction
if status.favourited ?? false {
favorite = UIContextualAction(style: .normal, title: "Unfavorite", handler: { (action, view, completion) in
status.unfavourite(completion: { response in
DispatchQueue.main.async {
if case .success = response {
completion(true)
guard let cell = tableView.cellForRow(at: indexPath) as? StatusTableViewCell else { return }
cell.updateUI(for: cell.status)
} else {
completion(false)
}
2018-09-15 14:56:27 +00:00
}
2018-09-15 15:37:20 +00:00
})
2018-09-15 14:56:27 +00:00
})
2018-09-15 15:37:20 +00:00
favorite.backgroundColor = UIColor(displayP3Red: 235/255, green: 77/255, blue: 62/255, alpha: 1)
} else {
favorite = UIContextualAction(style: .normal, title: "Favorite") { (action, view, completion) in
status.favourite(completion: { response in
DispatchQueue.main.async {
if case .success = response {
completion(true)
guard let cell = tableView.cellForRow(at: indexPath) as? StatusTableViewCell else { return }
cell.updateUI(for: cell.status)
} else {
completion(false)
}
}
})
}
favorite.backgroundColor = UIColor(displayP3Red: 1, green: 204/255, blue: 0, alpha: 1)
2018-09-15 14:56:27 +00:00
}
favorite.image = favoriteActionImage
2018-09-15 15:37:20 +00:00
let reblog: UIContextualAction
if status.reblogged ?? false {
reblog = UIContextualAction(style: .normal, title: "Unreblog", handler: { (action, view, completion) in
status.unreblog(completion: { response in
DispatchQueue.main.async {
if case .success = response {
completion(true)
guard let cell = tableView.cellForRow(at: indexPath) as? StatusTableViewCell else { return }
cell.updateUI(for: cell.status)
} else {
completion(false)
}
2018-09-15 14:56:27 +00:00
}
2018-09-15 15:37:20 +00:00
})
2018-09-15 14:56:27 +00:00
})
2018-09-15 15:37:20 +00:00
reblog.backgroundColor = UIColor(displayP3Red: 235/255, green: 77/255, blue: 62/255, alpha: 1)
} else {
reblog = UIContextualAction(style: .normal, title: "Reblog") { (action, view, completion) in
status.reblog(completion: { response in
DispatchQueue.main.async {
if case .success = response {
completion(true)
guard let cell = tableView.cellForRow(at: indexPath) as? StatusTableViewCell else { return }
cell.updateUI(for: cell.status)
} else {
completion(false)
}
}
})
}
reblog.backgroundColor = view.tintColor
2018-09-15 14:56:27 +00:00
}
reblog.image = reblogActionImage
2018-09-15 15:37:20 +00:00
2018-09-15 14:56:27 +00:00
let actions = [
favorite,
reblog
]
return UISwipeActionsConfiguration(actions: actions)
}
2018-09-15 15:37:20 +00:00
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let status = statuses[indexPath.row]
let reply = UIContextualAction(style: .normal, title: "Reply") { (action, view, completion) in
completion(true)
self.reply(to: status)
}
reply.image = replyActionImage
reply.backgroundColor = view.tintColor
let more = UIContextualAction(style: .normal, title: "More") { (action, view, completion) in
completion(true)
self.showMoreOptions(status: status)
}
more.image = moreActionImage
more.backgroundColor = .gray
let actions = [
reply,
more
]
return UISwipeActionsConfiguration(actions: actions)
}
@IBAction func refreshStatuses(_ sender: Any) {
guard let newer = newer else { return }
2018-09-11 14:52:21 +00:00
MastodonController.shared.client.getStatuses(timeline: timeline, range: newer) { response in
guard case let .success(newStatuses, pagination) = response else { fatalError() }
self.newer = pagination?.newer
self.statuses.insert(contentsOf: newStatuses, 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-09 01:35:40 +00:00
extension TimelineTableViewController: StatusTableViewCellDelegate {
func updatedStatus(for cell: StatusTableViewCell, status: Status) {
let indexPath = tableView.indexPath(for: cell)!
statuses[indexPath.row] = status
}
}
extension TimelineTableViewController: LargeImageViewControllerDelegate {}