167 lines
6.4 KiB
Swift
167 lines
6.4 KiB
Swift
//
|
|
// 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"
|
|
|
|
weak var mastodonController: MastodonController!
|
|
|
|
let actionType: ActionType
|
|
let statusID: String
|
|
var statusState: StatusState
|
|
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.
|
|
- Parameter mastodonController The `MastodonController` instance this view controller uses.
|
|
*/
|
|
init(actionType: ActionType, statusID: String, statusState: StatusState, accountIDs: [String]?, mastodonController: MastodonController) {
|
|
self.mastodonController = mastodonController
|
|
|
|
self.actionType = actionType
|
|
self.statusID = statusID
|
|
self.statusState = statusState
|
|
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")
|
|
}
|
|
|
|
deinit {
|
|
if let accountIDs = self.accountIDs, let container = mastodonController?.persistentContainer {
|
|
container.backgroundContext.perform {
|
|
for id in accountIDs {
|
|
container.account(for: id, in: container.backgroundContext)?.decrementReferenceCount()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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 let accountIDs = accountIDs {
|
|
accountIDs.forEach { (id) in
|
|
self.mastodonController.persistentContainer.account(for: id)?.incrementReferenceCount()
|
|
}
|
|
} else {
|
|
// account IDs haven't been set, so perform a request to load them
|
|
guard let status = mastodonController.persistentContainer.status(for: statusID) else {
|
|
fatalError("Missing cached status \(statusID)")
|
|
}
|
|
|
|
tableView.tableFooterView = UIActivityIndicatorView(style: .large)
|
|
|
|
let request = actionType == .favorite ? Status.getFavourites(status.id) : Status.getReblogs(status.id)
|
|
mastodonController.run(request) { (response) in
|
|
guard case let .success(accounts, _) = response else { fatalError() }
|
|
self.mastodonController.persistentContainer.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.delegate = self
|
|
cell.updateUI(statusID: statusID, state: statusState)
|
|
return cell
|
|
case 1:
|
|
guard let accountIDs = accountIDs,
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: accountCell, for: indexPath) as? AccountTableViewCell else { fatalError() }
|
|
cell.delegate = self
|
|
cell.updateUI(accountID: accountIDs[indexPath.row])
|
|
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 {
|
|
var apiController: MastodonController { mastodonController }
|
|
func statusCellCollapsedStateChanged(_ cell: BaseStatusTableViewCell) {
|
|
// causes the table view to recalculate the cell heights
|
|
tableView.beginUpdates()
|
|
tableView.endUpdates()
|
|
}
|
|
}
|