2019-09-06 03:16:45 +00:00
//
// S t a t u s A c t i o n A c c o u n t L i s t T a b l e V i e w C o n t r o l l e r . s w i f t
// T u s k e r
//
// C r e a t e d b y S h a d o w f a c t s o n 9 / 5 / 1 9 .
// C o p y r i g h t © 2 0 1 9 S h a d o w f a c t s . A l l r i g h t s r e s e r v e d .
//
import UIKit
2019-09-07 21:10:58 +00:00
import Pachyderm
2019-09-06 03:16:45 +00:00
2019-09-06 21:45:49 +00:00
class StatusActionAccountListTableViewController : EnhancedTableViewController {
2019-09-06 03:16:45 +00:00
private let statusCell = " statusCell "
private let accountCell = " accountCell "
2020-05-13 22:57:04 +00:00
weak var mastodonController : MastodonController !
2020-01-05 20:25:07 +00:00
2019-09-07 21:10:58 +00:00
let actionType : ActionType
2019-09-06 03:16:45 +00:00
let statusID : String
2019-11-28 23:36:58 +00:00
var statusState : StatusState
2019-09-07 21:10:58 +00:00
var accountIDs : [ String ] ? {
didSet {
tableView . reloadData ( )
}
}
// / I f ` t r u e ` , a w a r n i n g w i l l b e s h o w n b e l o w t h e a c c o u n t l i s t d e s c r i b i n g t h a t t h e t o t a l f a v s / r e b l o g s m a y b e i n n a c u r a t e .
var showInacurateCountWarning = false
2019-09-06 03:16:45 +00:00
2019-09-07 21:10:58 +00:00
/* *
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 .
2020-01-05 20:25:07 +00:00
- Parameter mastodonController The ` MastodonController ` instance this view controller uses .
2019-09-07 21:10:58 +00:00
*/
2020-01-05 20:25:07 +00:00
init ( actionType : ActionType , statusID : String , statusState : StatusState , accountIDs : [ String ] ? , mastodonController : MastodonController ) {
self . mastodonController = mastodonController
2019-09-07 21:10:58 +00:00
self . actionType = actionType
2019-09-06 03:16:45 +00:00
self . statusID = statusID
2019-11-28 23:36:58 +00:00
self . statusState = statusState
2019-09-06 03:16:45 +00:00
self . accountIDs = accountIDs
2019-09-06 21:45:49 +00:00
super . init ( style : . grouped )
2019-09-07 21:10:58 +00:00
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 " )
}
2019-09-06 03:16:45 +00:00
}
2019-09-07 21:10:58 +00:00
2019-09-06 03:16:45 +00:00
required init ? ( coder : NSCoder ) {
fatalError ( " init(coder:) has not been implemented " )
}
2020-05-12 01:59:46 +00:00
deinit {
2020-05-13 22:58:11 +00:00
if let accountIDs = self . accountIDs , let container = mastodonController ? . persistentContainer {
2020-05-12 01:59:46 +00:00
container . backgroundContext . perform {
for id in accountIDs {
container . account ( for : id , in : container . backgroundContext ) ? . decrementReferenceCount ( )
}
}
}
}
2019-09-06 03:16:45 +00:00
override func viewDidLoad ( ) {
super . viewDidLoad ( )
2019-11-19 17:08:11 +00:00
tableView . register ( UINib ( nibName : " TimelineStatusTableViewCell " , bundle : . main ) , forCellReuseIdentifier : statusCell )
2019-09-06 03:16:45 +00:00
tableView . register ( UINib ( nibName : " AccountTableViewCell " , bundle : . main ) , forCellReuseIdentifier : accountCell )
tableView . rowHeight = UITableView . automaticDimension
tableView . estimatedRowHeight = 66 // h e i g h t o f a c c o u n t c e l l , w h i c h w i l l b e t h e m o s t c o m m o n
tableView . alwaysBounceVertical = true
tableView . tableHeaderView = UIView ( frame : CGRect ( x : 0 , y : 0 , width : 0 , height : CGFloat . leastNormalMagnitude ) )
2019-09-07 21:10:58 +00:00
2020-05-12 01:59:46 +00:00
if let accountIDs = accountIDs {
accountIDs . forEach { ( id ) in
self . mastodonController . persistentContainer . account ( for : id ) ? . incrementReferenceCount ( )
}
} else {
2019-09-07 21:10:58 +00:00
// a c c o u n t I D s h a v e n ' t b e e n s e t , s o p e r f o r m a r e q u e s t t o l o a d t h e m
2020-04-27 23:20:09 +00:00
guard let status = mastodonController . persistentContainer . status ( for : statusID ) else {
2019-09-07 21:10:58 +00:00
fatalError ( " Missing cached status \( statusID ) " )
}
tableView . tableFooterView = UIActivityIndicatorView ( style : . large )
2020-04-27 23:20:09 +00:00
let request = actionType = = . favorite ? Status . getFavourites ( status . id ) : Status . getReblogs ( status . id )
2020-01-05 20:25:07 +00:00
mastodonController . run ( request ) { ( response ) in
2019-09-07 21:10:58 +00:00
guard case let . success ( accounts , _ ) = response else { fatalError ( ) }
2020-05-10 19:04:22 +00:00
self . mastodonController . persistentContainer . addAll ( accounts : accounts ) {
DispatchQueue . main . async {
self . accountIDs = accounts . map { $0 . id }
self . tableView . tableFooterView = nil
}
2019-09-07 21:10:58 +00:00
}
}
}
2019-09-06 03:16:45 +00:00
}
// MARK: - T a b l e v i e w d a t a s o u r c e
override func numberOfSections ( in tableView : UITableView ) -> Int {
return 2
}
override func tableView ( _ tableView : UITableView , numberOfRowsInSection section : Int ) -> Int {
switch section {
case 0 : // s t a t u s
return 1
case 1 : // a c c o u n t s
2019-09-07 21:10:58 +00:00
if let accountIDs = accountIDs {
return accountIDs . count
} else {
return 0
}
2019-09-06 03:16:45 +00:00
default :
fatalError ( " Invalid section \( section ) " )
}
}
override func tableView ( _ tableView : UITableView , cellForRowAt indexPath : IndexPath ) -> UITableViewCell {
switch indexPath . section {
case 0 :
2019-11-19 17:08:11 +00:00
guard let cell = tableView . dequeueReusableCell ( withIdentifier : statusCell , for : indexPath ) as ? TimelineStatusTableViewCell else { fatalError ( ) }
2019-09-06 03:16:45 +00:00
cell . delegate = self
2020-01-06 00:54:28 +00:00
cell . updateUI ( statusID : statusID , state : statusState )
2019-09-06 03:16:45 +00:00
return cell
case 1 :
2019-09-07 21:10:58 +00:00
guard let accountIDs = accountIDs ,
let cell = tableView . dequeueReusableCell ( withIdentifier : accountCell , for : indexPath ) as ? AccountTableViewCell else { fatalError ( ) }
2019-09-06 03:16:45 +00:00
cell . delegate = self
2020-01-06 00:54:28 +00:00
cell . updateUI ( accountID : accountIDs [ indexPath . row ] )
2019-09-06 03:16:45 +00:00
return cell
default :
fatalError ( " Invalid section \( indexPath . section ) " )
}
}
2019-09-07 21:10:58 +00:00
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 " )
2019-09-06 03:16:45 +00:00
}
2019-09-07 21:10:58 +00:00
enum ActionType {
case favorite , reblog
2019-09-06 03:16:45 +00:00
}
}
2019-09-09 22:40:23 +00:00
extension StatusActionAccountListTableViewController : StatusTableViewCellDelegate {
2020-01-05 20:25:07 +00:00
var apiController : MastodonController { mastodonController }
2019-11-28 23:36:58 +00:00
func statusCellCollapsedStateChanged ( _ cell : BaseStatusTableViewCell ) {
2019-09-09 22:40:23 +00:00
// c a u s e s t h e t a b l e v i e w t o r e c a l c u l a t e t h e c e l l h e i g h t s
tableView . beginUpdates ( )
tableView . endUpdates ( )
}
}