// // StatusActionAccountListTableViewController.swift // Tusker // // Created by Shadowfacts on 9/5/19. // Copyright © 2019 Shadowfacts. All rights reserved. // import UIKit import Pachyderm class StatusActionAccountListTableViewController: EnhancedTableViewController { private let statusCell = "statusCell" private let accountCell = "accountCell" let actionType: ActionType let statusID: String var accountIDs: [String]? { didSet { tableView.reloadData() } } /// If `true`, a warning will be shown below the account list describing that the total favs/reblogs may be innacurate. var showInacurateCountWarning = false /** Creates a new view controller showing the accounts that performed the given action on the given status. - Parameter actionType The action that this VC is for. - Parameter statusID The ID of the status to show. - Parameter accountIDs The accounts that will be shown. If `nil` is passed, a request will be performed to load the accounts. */ init(actionType: ActionType, statusID: String, accountIDs: [String]?) { self.actionType = actionType self.statusID = statusID self.accountIDs = accountIDs super.init(style: .grouped) switch actionType { case .favorite: title = NSLocalizedString("Favorited By", comment: "status favorited by accounts list title") case .reblog: title = NSLocalizedString("Reblogged By", comment: "status reblogged by accounts list title") } } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() tableView.register(UINib(nibName: "TimelineStatusTableViewCell", bundle: .main), forCellReuseIdentifier: statusCell) tableView.register(UINib(nibName: "AccountTableViewCell", bundle: .main), forCellReuseIdentifier: accountCell) tableView.rowHeight = UITableView.automaticDimension tableView.estimatedRowHeight = 66 // height of account cell, which will be the most common tableView.alwaysBounceVertical = true tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude)) if accountIDs == nil { // account IDs haven't been set, so perform a request to load them guard let status = MastodonCache.status(for: statusID) else { fatalError("Missing cached status \(statusID)") } tableView.tableFooterView = UIActivityIndicatorView(style: .large) let request = actionType == .favorite ? Status.getFavourites(status) : Status.getReblogs(status) MastodonController.client.run(request) { (response) in guard case let .success(accounts, _) = response else { fatalError() } MastodonCache.addAll(accounts: accounts) DispatchQueue.main.async { self.accountIDs = accounts.map { $0.id } self.tableView.tableFooterView = nil } } } } // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int { return 2 } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { switch section { case 0: // status return 1 case 1: // accounts if let accountIDs = accountIDs { return accountIDs.count } else { return 0 } default: fatalError("Invalid section \(section)") } } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { switch indexPath.section { case 0: guard let cell = tableView.dequeueReusableCell(withIdentifier: statusCell, for: indexPath) as? TimelineStatusTableViewCell else { fatalError() } cell.updateUI(statusID: statusID) cell.delegate = self return cell case 1: guard let accountIDs = accountIDs, let cell = tableView.dequeueReusableCell(withIdentifier: accountCell, for: indexPath) as? AccountTableViewCell else { fatalError() } cell.updateUI(accountID: accountIDs[indexPath.row]) cell.delegate = self return cell default: fatalError("Invalid section \(indexPath.section)") } } override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? { guard section == 1, showInacurateCountWarning else { return nil } return NSLocalizedString("Favorite and reblog counts for posts originating from instances other than your own may not be accurate.", comment: "shown on lists of status total actions") } enum ActionType { case favorite, reblog } } extension StatusActionAccountListTableViewController: StatusTableViewCellDelegate { func statusCollapsedStateChanged() { // causes the table view to recalculate the cell heights tableView.beginUpdates() tableView.endUpdates() } }