Tusker/Tusker/Screens/Conversation/ConversationViewController....

140 lines
5.4 KiB
Swift
Raw Normal View History

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
class ConversationViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
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
}
@IBOutlet weak var tableView: UITableView!
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]
guard let mainStatus = StatusCache.get(id: mainStatusID) else { fatalError("Missing cached status \(mainStatusID!)") }
2018-08-28 18:29:06 +00:00
let request = Status.getContext(mainStatus)
MastodonController.shared.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)
StatusCache.addAll(parents)
StatusCache.addAll(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)
2018-08-28 18:29:06 +00:00
DispatchQueue.main.async {
self.tableView.scrollToRow(at: indexPath, at: .middle, animated: false)
}
}
}
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-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
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
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
}
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-09-03 20:54:03 +00:00
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
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return tableView.cellForRow(at: indexPath) is TableViewSwipeActionProvider
}
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.leadingSwipeActionsConfiguration()
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return (tableView.cellForRow(at: indexPath) as? TableViewSwipeActionProvider)?.trailingSwipeActionsConfiguration()
}
2018-08-28 18:29:06 +00:00
}
extension ConversationViewController: StatusTableViewCellDelegate {}
extension ConversationViewController: LargeImageViewControllerDelegate {}