2019-06-06 14:46:23 -07:00
//
// A c c o u n t V i e w C o n t r o l l e r . s w i f t
// A l t S t o r e
//
// C r e a t e d b y R i l e y T e s t u t o n 6 / 6 / 1 9 .
// C o p y r i g h t © 2 0 1 9 R i l e y T e s t u t . A l l r i g h t s r e s e r v e d .
//
import UIKit
import Roxas
2019-07-31 11:46:26 -07:00
class SettingsViewController : UITableViewController
2019-06-06 14:46:23 -07:00
{
private var team : Team ?
private lazy var placeholderView = self . makePlaceholderView ( )
@IBOutlet var accountNameLabel : UILabel !
@IBOutlet var accountEmailLabel : UILabel !
@IBOutlet var teamNameLabel : UILabel !
@IBOutlet var teamTypeLabel : UILabel !
override func viewDidLoad ( )
{
super . viewDidLoad ( )
self . update ( )
}
override func viewWillAppear ( _ animated : Bool )
{
super . viewWillAppear ( animated )
self . update ( )
}
}
2019-07-31 11:46:26 -07:00
private extension SettingsViewController
2019-06-06 14:46:23 -07:00
{
func makePlaceholderView ( ) -> RSTPlaceholderView
{
let placeholderView = RSTPlaceholderView ( )
placeholderView . autoresizingMask = [ . flexibleWidth , . flexibleHeight ]
placeholderView . textLabel . text = NSLocalizedString ( " Not Signed In " , comment : " " )
placeholderView . detailTextLabel . text = NSLocalizedString ( " Please sign in with your Apple ID to download and refresh apps. " , comment : " " )
let signInButton = UIButton ( type : . system )
signInButton . titleLabel ? . font = UIFont . preferredFont ( forTextStyle : . body )
signInButton . setTitle ( NSLocalizedString ( " Sign In " , comment : " " ) , for : . normal )
2019-07-31 11:46:26 -07:00
signInButton . addTarget ( self , action : #selector ( SettingsViewController . signIn ( _ : ) ) , for : . primaryActionTriggered )
2019-06-06 14:46:23 -07:00
placeholderView . stackView . addArrangedSubview ( signInButton )
return placeholderView
}
func update ( )
{
if let team = DatabaseManager . shared . activeTeam ( )
{
self . tableView . separatorStyle = . singleLine
self . tableView . isScrollEnabled = true
self . tableView . backgroundView = nil
self . navigationItem . rightBarButtonItem ? . isEnabled = true
self . accountNameLabel . text = team . account . localizedName
self . accountEmailLabel . text = team . account . appleID
self . teamNameLabel . text = team . name
self . teamTypeLabel . text = team . type . localizedDescription
self . team = team
}
else
{
self . tableView . separatorStyle = . none
self . tableView . isScrollEnabled = false
self . tableView . backgroundView = self . placeholderView
self . navigationItem . rightBarButtonItem ? . isEnabled = false
self . team = nil
}
if self . isViewLoaded
{
self . tableView . reloadData ( )
}
}
}
2019-07-31 11:46:26 -07:00
private extension SettingsViewController
2019-06-06 14:46:23 -07:00
{
@objc func signIn ( _ sender : UIButton )
{
sender . isIndicatingActivity = true
AppManager . shared . authenticate ( presentingViewController : self ) { ( result ) in
DispatchQueue . main . async {
sender . isIndicatingActivity = false
self . update ( )
}
}
}
@IBAction func signOut ( _ sender : UIBarButtonItem )
{
func signOut ( )
{
DatabaseManager . shared . signOut { ( error ) in
DispatchQueue . main . async {
if let error = error
{
let toastView = RSTToastView ( text : error . localizedDescription , detailText : nil )
toastView . tintColor = . red
toastView . show ( in : self . navigationController ? . view ? ? self . view , duration : 2.0 )
}
else
{
let toastView = RSTToastView ( text : NSLocalizedString ( " Successfully Signed Out! " , comment : " " ) , detailText : nil )
toastView . tintColor = . altPurple
toastView . show ( in : self . navigationController ? . view ? ? self . view , duration : 2.0 )
}
self . update ( )
}
}
}
let alertController = UIAlertController ( title : NSLocalizedString ( " Are you sure you want to sign out? " , comment : " " ) , message : NSLocalizedString ( " You will no longer be able to install or refresh apps once you sign out. " , comment : " " ) , preferredStyle : . actionSheet )
alertController . addAction ( UIAlertAction ( title : NSLocalizedString ( " Sign Out " , comment : " " ) , style : . destructive ) { _ in signOut ( ) } )
alertController . addAction ( . cancel )
self . present ( alertController , animated : true , completion : nil )
}
}
2019-07-31 11:46:26 -07:00
extension SettingsViewController
2019-06-06 14:46:23 -07:00
{
override func numberOfSections ( in tableView : UITableView ) -> Int
{
let count = ( self . team = = nil ) ? 0 : super . numberOfSections ( in : tableView )
return count
}
}