2019-10-21 18:51:33 +02:00
//
2019-10-24 02:58:18 +02:00
// A c c o u n t I n s p e c t o r V i e w C o n t r o l l e r . s w i f t
2019-10-21 18:51:33 +02:00
// N e t N e w s W i r e - i O S
//
// C r e a t e d b y M a u r i c e P a r k e r o n 5 / 1 7 / 1 9 .
// C o p y r i g h t © 2 0 1 9 R a n c h e r o S o f t w a r e . A l l r i g h t s r e s e r v e d .
//
import UIKit
import Account
2019-10-24 02:58:18 +02:00
class AccountInspectorViewController : UITableViewController {
static let preferredContentSizeForFormSheetDisplay = CGSize ( width : 460.0 , height : 400.0 )
2019-10-21 18:51:33 +02:00
@IBOutlet weak var nameTextField : UITextField !
@IBOutlet weak var activeSwitch : UISwitch !
2021-04-12 20:48:03 +02:00
@IBOutlet weak var deleteAccountButton : VibrantButton !
2019-10-21 18:51:33 +02:00
2019-10-24 02:58:18 +02:00
var isModal = false
2019-10-21 18:51:33 +02:00
weak var account : Account ?
override func viewDidLoad ( ) {
super . viewDidLoad ( )
guard let account = account else { return }
nameTextField . placeholder = account . defaultName
nameTextField . text = account . name
nameTextField . delegate = self
activeSwitch . isOn = account . isActive
2019-10-24 02:58:18 +02:00
2019-10-24 03:29:22 +02:00
navigationItem . title = account . nameForDisplay
2021-04-12 20:48:03 +02:00
if account . type != . onMyMac {
deleteAccountButton . setTitle ( NSLocalizedString ( " Remove Account " , comment : " Remove Account " ) , for : . normal )
}
2019-10-24 02:58:18 +02:00
if isModal {
let doneBarButtonItem = UIBarButtonItem ( barButtonSystemItem : . done , target : self , action : #selector ( done ) )
navigationItem . leftBarButtonItem = doneBarButtonItem
}
2019-11-16 22:47:12 +01:00
tableView . register ( ImageHeaderView . self , forHeaderFooterViewReuseIdentifier : " SectionHeader " )
2019-11-03 11:01:58 +01:00
2019-10-24 02:58:18 +02:00
}
2019-10-21 18:51:33 +02:00
override func viewWillDisappear ( _ animated : Bool ) {
account ? . name = nameTextField . text
account ? . isActive = activeSwitch . isOn
}
2019-10-24 02:58:18 +02:00
@objc func done ( ) {
dismiss ( animated : true )
}
2019-10-21 18:51:33 +02:00
2019-10-22 09:31:07 +02:00
@IBAction func credentials ( _ sender : Any ) {
guard let account = account else { return }
switch account . type {
case . feedbin :
2019-10-24 02:58:18 +02:00
let navController = UIStoryboard . account . instantiateViewController ( withIdentifier : " FeedbinAccountNavigationViewController " ) as ! UINavigationController
2019-10-22 09:31:07 +02:00
let addViewController = navController . topViewController as ! FeedbinAccountViewController
addViewController . account = account
navController . modalPresentationStyle = . currentContext
present ( navController , animated : true )
2020-10-29 20:53:52 +01:00
case . newsBlur :
let navController = UIStoryboard . account . instantiateViewController ( withIdentifier : " NewsBlurAccountNavigationViewController " ) as ! UINavigationController
let addViewController = navController . topViewController as ! NewsBlurAccountViewController
addViewController . account = account
navController . modalPresentationStyle = . currentContext
present ( navController , animated : true )
case . inoreader , . bazQux , . theOldReader , . freshRSS :
let navController = UIStoryboard . account . instantiateViewController ( withIdentifier : " ReaderAPIAccountNavigationViewController " ) as ! UINavigationController
let addViewController = navController . topViewController as ! ReaderAPIAccountViewController
addViewController . accountType = account . type
addViewController . account = account
navController . modalPresentationStyle = . currentContext
present ( navController , animated : true )
2019-10-22 09:31:07 +02:00
default :
break
}
}
@IBAction func deleteAccount ( _ sender : Any ) {
2019-11-11 08:42:31 +01:00
guard let account = account else {
return
}
2021-04-24 09:16:20 +02:00
let title = NSLocalizedString ( " Remove Account " , comment : " Remove Account " )
2019-11-11 08:42:31 +01:00
let message : String = {
switch account . type {
case . feedly :
2021-04-24 09:16:20 +02:00
return NSLocalizedString ( " Are you sure you want to remove this account? NetNewsWire will no longer be able to access articles and feeds unless the account is added again. " , comment : " Log Out and Remove Account " )
2019-11-11 08:42:31 +01:00
default :
2021-04-24 09:16:20 +02:00
return NSLocalizedString ( " Are you sure you want to remove this account? This cannot be undone. " , comment : " Remove Account " )
2019-11-11 08:42:31 +01:00
}
} ( )
2019-10-22 09:31:07 +02:00
let alertController = UIAlertController ( title : title , message : message , preferredStyle : . alert )
let cancelTitle = NSLocalizedString ( " Cancel " , comment : " Cancel " )
let cancelAction = UIAlertAction ( title : cancelTitle , style : . cancel )
alertController . addAction ( cancelAction )
2021-04-24 09:16:20 +02:00
let markTitle = NSLocalizedString ( " Remove " , comment : " Remove " )
2019-10-22 09:31:07 +02:00
let markAction = UIAlertAction ( title : markTitle , style : . default ) { [ weak self ] ( action ) in
2019-11-05 04:10:02 +01:00
guard let self = self , let account = self . account else { return }
2019-10-22 09:31:07 +02:00
AccountManager . shared . deleteAccount ( account )
2019-11-05 04:10:02 +01:00
if self . isModal {
self . dismiss ( animated : true )
} else {
self . navigationController ? . popViewController ( animated : true )
}
2019-10-22 09:31:07 +02:00
}
alertController . addAction ( markAction )
2021-01-12 02:57:14 +01:00
alertController . preferredAction = markAction
2019-10-22 09:31:07 +02:00
present ( alertController , animated : true )
}
2019-10-21 18:51:33 +02:00
}
2019-11-03 11:01:58 +01:00
// MARK: T a b l e V i e w
2019-10-24 02:58:18 +02:00
extension AccountInspectorViewController {
2019-11-11 08:42:31 +01:00
var hidesCredentialsSection : Bool {
guard let account = account else {
return true
}
switch account . type {
2020-03-30 10:00:52 +02:00
case . onMyMac , . cloudKit , . feedly :
2019-11-11 08:42:31 +01:00
return true
default :
return false
}
}
2019-10-21 18:51:33 +02:00
override func numberOfSections ( in tableView : UITableView ) -> Int {
guard let account = account else { return 0 }
if account = = AccountManager . shared . defaultAccount {
return 1
2019-11-11 08:42:31 +01:00
} else if hidesCredentialsSection {
2019-10-21 18:51:33 +02:00
return 2
} else {
return super . numberOfSections ( in : tableView )
}
}
2019-11-03 11:01:58 +01:00
override func tableView ( _ tableView : UITableView , heightForHeaderInSection section : Int ) -> CGFloat {
2019-11-22 22:11:15 +01:00
return section = = 0 ? ImageHeaderView . rowHeight : super . tableView ( tableView , heightForHeaderInSection : section )
2019-11-03 11:01:58 +01:00
}
override func tableView ( _ tableView : UITableView , viewForHeaderInSection section : Int ) -> UIView ? {
guard let account = account else { return nil }
if section = = 0 {
2019-11-16 22:47:12 +01:00
let headerView = tableView . dequeueReusableHeaderFooterView ( withIdentifier : " SectionHeader " ) as ! ImageHeaderView
2019-11-03 11:01:58 +01:00
headerView . imageView . image = AppAssets . image ( for : account . type )
return headerView
} else {
return super . tableView ( tableView , viewForHeaderInSection : section )
}
}
2019-10-21 18:51:33 +02:00
override func tableView ( _ tableView : UITableView , cellForRowAt indexPath : IndexPath ) -> UITableViewCell {
let cell : UITableViewCell
2019-11-11 08:42:31 +01:00
if indexPath . section = = 1 , hidesCredentialsSection {
2019-10-21 18:51:33 +02:00
cell = super . tableView ( tableView , cellForRowAt : IndexPath ( row : 0 , section : 2 ) )
} else {
cell = super . tableView ( tableView , cellForRowAt : indexPath )
}
return cell
}
override func tableView ( _ tableView : UITableView , shouldHighlightRowAt indexPath : IndexPath ) -> Bool {
if indexPath . section > 0 {
return true
}
return false
}
override func tableView ( _ tableView : UITableView , didSelectRowAt indexPath : IndexPath ) {
tableView . selectRow ( at : nil , animated : true , scrollPosition : . none )
}
}
2019-11-03 11:01:58 +01:00
// MARK: U I T e x t F i e l d D e l e g a t e
2019-10-24 02:58:18 +02:00
extension AccountInspectorViewController : UITextFieldDelegate {
2019-10-21 18:51:33 +02:00
func textFieldShouldReturn ( _ textField : UITextField ) -> Bool {
textField . resignFirstResponder ( )
return true
}
}