2019-12-18 03:56:53 +00:00
//
// E d i t L i s t A c c o u n t s 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 1 2 / 1 7 / 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
import Pachyderm
class EditListAccountsViewController : EnhancedTableViewController {
2020-01-05 20:25:07 +00:00
let mastodonController : MastodonController
2019-12-18 03:56:53 +00:00
let list : List
var dataSource : DataSource !
var nextRange : RequestRange ?
var searchResultsController : SearchResultsViewController !
var searchController : UISearchController !
2020-01-05 20:25:07 +00:00
init ( list : List , mastodonController : MastodonController ) {
2019-12-18 03:56:53 +00:00
self . list = list
2020-01-05 20:25:07 +00:00
self . mastodonController = mastodonController
2019-12-18 03:56:53 +00:00
super . init ( style : . plain )
title = String ( format : NSLocalizedString ( " Edit %@ " , comment : " edit list screen title " ) , list . title )
}
required init ? ( coder : NSCoder ) {
fatalError ( " init(coder:) has not been implemeneted " )
}
override func viewDidLoad ( ) {
super . viewDidLoad ( )
tableView . register ( UINib ( nibName : " AccountTableViewCell " , bundle : . main ) , forCellReuseIdentifier : " accountCell " )
tableView . rowHeight = UITableView . automaticDimension
tableView . estimatedRowHeight = 66
dataSource = DataSource ( tableView : tableView , cellProvider : { ( tableView , indexPath , item ) -> UITableViewCell ? in
guard case let . account ( id ) = item else { fatalError ( ) }
let cell = tableView . dequeueReusableCell ( withIdentifier : " accountCell " , for : indexPath ) as ! AccountTableViewCell
2021-02-06 19:35:34 +00:00
cell . delegate = self
2019-12-18 03:56:53 +00:00
cell . updateUI ( accountID : id )
return cell
} )
dataSource . editListAccountsController = self
2020-10-16 23:14:29 +00:00
searchResultsController = SearchResultsViewController ( mastodonController : mastodonController , resultTypes : [ . accounts ] )
2019-12-18 03:56:53 +00:00
searchResultsController . delegate = self
searchController = UISearchController ( searchResultsController : searchResultsController )
searchController . hidesNavigationBarDuringPresentation = false
searchController . searchResultsUpdater = searchResultsController
searchController . searchBar . autocapitalizationType = . none
searchController . searchBar . placeholder = NSLocalizedString ( " Search for accounts to add " , comment : " edit list search field placeholder " )
searchController . searchBar . delegate = searchResultsController
definesPresentationContext = true
navigationItem . searchController = searchController
navigationItem . hidesSearchBarWhenScrolling = false
navigationItem . rightBarButtonItem = UIBarButtonItem ( title : NSLocalizedString ( " Rename " , comment : " rename list button title " ) , style : . plain , target : self , action : #selector ( renameButtonPressed ) )
loadAccounts ( )
}
func loadAccounts ( ) {
let request = List . getAccounts ( list )
2020-01-05 20:25:07 +00:00
mastodonController . run ( request ) { ( response ) in
2019-12-18 03:56:53 +00:00
guard case let . success ( accounts , pagination ) = response else {
fatalError ( )
}
self . nextRange = pagination ? . older
2020-05-02 23:52:35 +00:00
self . mastodonController . persistentContainer . addAll ( accounts : accounts )
2019-12-18 03:56:53 +00:00
var snapshot = self . dataSource . snapshot ( )
snapshot . deleteSections ( [ . accounts ] )
snapshot . appendSections ( [ . accounts ] )
snapshot . appendItems ( accounts . map { . account ( id : $0 . id ) } )
DispatchQueue . main . async {
self . dataSource . apply ( snapshot )
}
}
}
// MARK: - T a b l e v i e w d e l e g a t e
override func tableView ( _ tableView : UITableView , editingStyleForRowAt indexPath : IndexPath ) -> UITableViewCell . EditingStyle {
return . delete
}
// MARK: - I n t e r a c t i o n
@objc func renameButtonPressed ( ) {
let alert = UIAlertController ( title : NSLocalizedString ( " Rename List " , comment : " rename list alert title " ) , message : nil , preferredStyle : . alert )
alert . addTextField { ( textField ) in
textField . text = self . list . title
}
alert . addAction ( UIAlertAction ( title : NSLocalizedString ( " Cancel " , comment : " rename list alert cancel button " ) , style : . cancel , handler : nil ) )
alert . addAction ( UIAlertAction ( title : NSLocalizedString ( " Rename " , comment : " renaem list alert rename button " ) , style : . default , handler : { ( _ ) in
guard let text = alert . textFields ? . first ? . text else {
fatalError ( )
}
let request = List . update ( self . list , title : text )
2020-01-05 20:25:07 +00:00
self . mastodonController . run ( request ) { ( response ) in
2019-12-18 03:56:53 +00:00
guard case . success ( _ , _ ) = response else {
fatalError ( )
}
// t o d o : s h o w s u c c e s s m e s s a g e s o m e h o w
}
} ) )
present ( alert , animated : true )
}
}
extension EditListAccountsViewController {
enum Section : Hashable {
case accounts
}
enum Item : Hashable {
case account ( id : String )
}
class DataSource : UITableViewDiffableDataSource < Section , Item > {
weak var editListAccountsController : EditListAccountsViewController ?
override func tableView ( _ tableView : UITableView , canEditRowAt indexPath : IndexPath ) -> Bool {
return true
}
override func tableView ( _ tableView : UITableView , commit editingStyle : UITableViewCell . EditingStyle , forRowAt indexPath : IndexPath ) {
guard editingStyle = = . delete ,
case let . account ( id ) = itemIdentifier ( for : indexPath ) else {
return
}
let request = List . remove ( editListAccountsController ! . list , accounts : [ id ] )
2020-01-05 20:25:07 +00:00
editListAccountsController ! . mastodonController . run ( request ) { ( response ) in
2019-12-18 03:56:53 +00:00
guard case . success ( _ , _ ) = response else {
fatalError ( )
}
self . editListAccountsController ? . loadAccounts ( )
}
}
}
}
extension EditListAccountsViewController : SearchResultsViewControllerDelegate {
func selectedSearchResult ( account accountID : String ) {
let request = List . add ( list , accounts : [ accountID ] )
2020-01-05 20:25:07 +00:00
mastodonController . run ( request ) { ( response ) in
2019-12-18 03:56:53 +00:00
guard case . success ( _ , _ ) = response else {
fatalError ( )
}
self . loadAccounts ( )
DispatchQueue . main . async {
self . searchController . isActive = false
}
}
}
}
2021-02-06 19:35:34 +00:00
extension EditListAccountsViewController : TuskerNavigationDelegate {
var apiController : MastodonController { mastodonController }
}