2018-08-28 18:29:06 +00:00
|
|
|
//
|
|
|
|
// ConversationTableViewController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 8/28/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2018-09-11 14:52:21 +00:00
|
|
|
import Pachyderm
|
2018-08-28 18:29:06 +00:00
|
|
|
|
2018-10-12 01:10:25 +00:00
|
|
|
class ConversationViewController: UITableViewController {
|
2018-08-28 18:29:06 +00:00
|
|
|
|
2018-09-18 01:57:46 +00:00
|
|
|
static func create(for mainStatusID: String) -> ConversationViewController {
|
2018-08-28 18:29:06 +00:00
|
|
|
guard let conversationController = UIStoryboard(name: "Conversation", bundle: nil).instantiateInitialViewController() as? ConversationViewController else { fatalError() }
|
2018-09-18 01:57:46 +00:00
|
|
|
conversationController.mainStatusID = mainStatusID
|
2018-08-28 18:29:06 +00:00
|
|
|
return conversationController
|
|
|
|
}
|
|
|
|
|
2018-09-18 01:57:46 +00:00
|
|
|
var mainStatusID: String!
|
2018-08-28 18:29:06 +00:00
|
|
|
|
2018-09-18 01:57:46 +00:00
|
|
|
var statusIDs: [String] = [] {
|
2018-08-28 18:29:06 +00:00
|
|
|
didSet {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.tableView.reloadData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2018-09-18 01:57:46 +00:00
|
|
|
statusIDs = [mainStatusID]
|
|
|
|
|
2018-09-18 16:59:07 +00:00
|
|
|
guard let mainStatus = MastodonCache.status(for: mainStatusID) else { fatalError("Missing cached status \(mainStatusID!)") }
|
2018-08-28 18:29:06 +00:00
|
|
|
|
2018-09-17 23:22:37 +00:00
|
|
|
let request = Status.getContext(mainStatus)
|
2018-10-02 23:31:00 +00:00
|
|
|
MastodonController.client.run(request) { response in
|
2018-09-11 14:52:21 +00:00
|
|
|
guard case let .success(context, _) = response else { fatalError() }
|
2018-09-18 01:57:46 +00:00
|
|
|
let parents = self.getDirectParents(of: mainStatus, from: context.ancestors)
|
2018-09-18 16:59:07 +00:00
|
|
|
MastodonCache.addAll(statuses: parents)
|
|
|
|
MastodonCache.addAll(statuses: context.descendants)
|
2018-09-18 01:57:46 +00:00
|
|
|
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)
|
2018-08-28 18:29:06 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.tableView.scrollToRow(at: indexPath, at: .middle, animated: false)
|
|
|
|
}
|
|
|
|
}
|
2018-10-12 01:20:58 +00:00
|
|
|
|
|
|
|
registerForPreviewing(with: self, sourceView: view)
|
2018-08-28 18:29:06 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 23:49:31 +00:00
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
2018-08-29 01:46:57 +00:00
|
|
|
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-10-12 01:52:11 +00:00
|
|
|
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.viewController(forURL: 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
|
|
|
|
}
|
|
|
|
|
2018-08-28 18:29:06 +00:00
|
|
|
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: - 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
|
|
|
|
|
2018-10-12 01:10:25 +00:00
|
|
|
override func numberOfSections(in tableView: UITableView) -> Int {
|
2018-08-28 18:29:06 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2018-10-12 01:10:25 +00:00
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2018-09-18 01:57:46 +00:00
|
|
|
return statusIDs.count
|
2018-08-28 18:29:06 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 01:10:25 +00:00
|
|
|
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
2018-09-18 01:57:46 +00:00
|
|
|
let statusID = statusIDs[indexPath.row]
|
2018-08-28 18:29:06 +00:00
|
|
|
|
2018-09-18 01:57:46 +00:00
|
|
|
if statusID == mainStatusID {
|
2018-08-28 18:29:06 +00:00
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "mainStatusCell", for: indexPath) as? ConversationMainStatusTableViewCell else { fatalError() }
|
|
|
|
cell.selectionStyle = .none
|
2018-09-18 01:57:46 +00:00
|
|
|
cell.updateUI(for: statusID)
|
2018-08-28 18:29:06 +00:00
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
|
|
|
} else {
|
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else { fatalError() }
|
2018-09-18 01:57:46 +00:00
|
|
|
cell.updateUI(for: statusID)
|
2018-08-28 18:29:06 +00:00
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 01:10:25 +00:00
|
|
|
override func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
|
2018-09-18 01:57:46 +00:00
|
|
|
let statusID = statusIDs[indexPath.row]
|
|
|
|
return statusID == mainStatusID ? nil : indexPath
|
2018-08-28 18:29:06 +00:00
|
|
|
}
|
2018-09-15 17:01:13 +00:00
|
|
|
|
2018-10-12 01:10:25 +00:00
|
|
|
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
|
2018-09-15 17:01:13 +00:00
|
|
|
return tableView.cellForRow(at: indexPath) is TableViewSwipeActionProvider
|
|
|
|
}
|
|
|
|
|
2018-10-12 01:10:25 +00:00
|
|
|
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
2018-09-15 17:01:13 +00:00
|
|
|
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.leadingSwipeActionsConfiguration()
|
|
|
|
}
|
|
|
|
|
2018-10-12 01:10:25 +00:00
|
|
|
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
2018-09-15 17:01:13 +00:00
|
|
|
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration()
|
|
|
|
}
|
2018-08-28 18:29:06 +00:00
|
|
|
|
|
|
|
}
|
2018-09-03 22:46:20 +00:00
|
|
|
|
|
|
|
extension ConversationViewController: StatusTableViewCellDelegate {}
|
|
|
|
extension ConversationViewController: LargeImageViewControllerDelegate {}
|