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 {
|
|
|
|
|
|
|
|
static func create(for mainStatus: Status) -> ConversationViewController {
|
|
|
|
guard let conversationController = UIStoryboard(name: "Conversation", bundle: nil).instantiateInitialViewController() as? ConversationViewController else { fatalError() }
|
|
|
|
conversationController.mainStatus = mainStatus
|
|
|
|
return conversationController
|
|
|
|
}
|
|
|
|
|
|
|
|
@IBOutlet weak var tableView: UITableView!
|
|
|
|
|
|
|
|
var mainStatus: Status!
|
|
|
|
|
|
|
|
var statuses: [Status] = [] {
|
|
|
|
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")
|
|
|
|
|
|
|
|
statuses = [mainStatus]
|
|
|
|
|
2018-09-11 14:52:21 +00:00
|
|
|
mainStatus.getContext { response in
|
|
|
|
guard case let .success(context, _) = response else { fatalError() }
|
2018-08-28 18:29:06 +00:00
|
|
|
var statuses = self.getDirectParents(of: self.mainStatus, from: context.ancestors)
|
|
|
|
statuses.append(self.mainStatus)
|
|
|
|
statuses.append(contentsOf: context.descendants)
|
|
|
|
self.statuses = statuses
|
|
|
|
let indexPath = IndexPath(row: statuses.firstIndex(of: self.mainStatus)!, section: 0)
|
|
|
|
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) {
|
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-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 {
|
|
|
|
return statuses.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
let status = statuses[indexPath.row]
|
|
|
|
|
|
|
|
if status == mainStatus {
|
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "mainStatusCell", for: indexPath) as? ConversationMainStatusTableViewCell else { fatalError() }
|
|
|
|
cell.selectionStyle = .none
|
|
|
|
cell.updateUI(for: status)
|
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
|
|
|
} else {
|
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: "statusCell", for: indexPath) as? StatusTableViewCell else { fatalError() }
|
|
|
|
cell.updateUI(for: status)
|
|
|
|
cell.delegate = self
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-03 20:54:03 +00:00
|
|
|
func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
|
2018-08-28 18:29:06 +00:00
|
|
|
let status = statuses[indexPath.row]
|
2018-09-03 20:54:03 +00:00
|
|
|
return status == mainStatus ? nil : indexPath
|
2018-08-28 18:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-09-03 22:46:20 +00:00
|
|
|
|
|
|
|
extension ConversationViewController: StatusTableViewCellDelegate {}
|
|
|
|
extension ConversationViewController: LargeImageViewControllerDelegate {}
|