Tusker/Tusker/Screens/Conversation/ConversationTableViewContro...

151 lines
5.9 KiB
Swift

//
// ConversationTableViewController.swift
// Tusker
//
// Created by Shadowfacts on 8/28/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
import Pachyderm
class ConversationTableViewController: UITableViewController {
let router: AppRouter
var mainStatusID: String!
var statusIDs: [String] = [] {
didSet {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
init(for mainStatusID: String, router: AppRouter) {
self.mainStatusID = mainStatusID
self.router = router
super.init(style: .plain)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
tableView.register(UINib(nibName: "ConversationMainStatusTableViewCell", bundle: nil), forCellReuseIdentifier: "mainStatusCell")
statusIDs = [mainStatusID]
guard let mainStatus = MastodonCache.status(for: mainStatusID) else { fatalError("Missing cached status \(mainStatusID!)") }
let request = Status.getContext(mainStatus)
MastodonController.client.run(request) { response in
guard case let .success(context, _) = response else { fatalError() }
let parents = self.getDirectParents(of: mainStatus, from: context.ancestors)
MastodonCache.addAll(statuses: parents)
MastodonCache.addAll(statuses: context.descendants)
self.statusIDs = parents.map { $0.id } + [self.mainStatusID] + context.descendants.map { $0.id }
let indexPath = IndexPath(row: self.statusIDs.firstIndex(of: self.mainStatusID)!, section: 0)
DispatchQueue.main.async {
self.tableView.scrollToRow(at: indexPath, at: .middle, animated: false)
}
}
registerForPreviewing(with: self, sourceView: view)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
for cell in tableView.visibleCells {
if let cell = cell as? PreferencesAdaptive {
cell.updateUIForPreferences()
}
}
}
override var previewActionItems: [UIPreviewActionItem] {
var actions = [UIPreviewActionItem]()
if let status = MastodonCache.status(for: mainStatusID),
let url = status.url {
actions.append(UIPreviewAction(title: "Open in Safari", style: .default, handler: { (_, _) in
let vc = self.router.safariViewController(for: url)
UIApplication.shared.delegate!.window!!.rootViewController!.present(vc, animated: true)
}))
actions.append(UIPreviewAction(title: "Share", style: .default, handler: { (_, _) in
let vc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
UIApplication.shared.delegate!.window!!.rootViewController!.present(vc, animated: true)
}))
}
return actions
}
func getDirectParents(of status: Status, from statuses: [Status]) -> [Status] {
var statuses = statuses
var parents: [Status] = []
var currentStatus: Status? = status
while currentStatus != nil {
guard let index = statuses.firstIndex(where: { $0.id == currentStatus!.inReplyToID }) else { break }
let parent = statuses.remove(at: index)
parents.insert(parent, at: 0)
currentStatus = parent
}
return parents
}
// 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 {
let statusID = statusIDs[indexPath.row]
if statusID == mainStatusID {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "mainStatusCell", for: indexPath) as? ConversationMainStatusTableViewCell else { fatalError() }
cell.selectionStyle = .none
cell.updateUI(for: statusID)
cell.delegate = self
return cell
} else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else { fatalError() }
cell.updateUI(for: statusID)
cell.delegate = self
return cell
}
}
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
let statusID = statusIDs[indexPath.row]
return statusID == mainStatusID ? nil : indexPath
}
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()
}
}
extension ConversationTableViewController: StatusTableViewCellDelegate {}