2017-02-07 16:45:14 +08:00
//
// A d v a n c e d S e t t i n g s T a b l e V i e w C o n t r o l l e r . s w i f t
// p a s s
//
// C r e a t e d b y M i n g s h e n S u n o n 7 / 2 / 2 0 1 7 .
// C o p y r i g h t © 2 0 1 7 B o b S u n . A l l r i g h t s r e s e r v e d .
//
import UIKit
2017-02-07 16:52:07 +08:00
import SVProgressHUD
2017-03-23 19:38:43 -07:00
import SwiftyUserDefaults
2017-02-07 16:45:14 +08:00
class AdvancedSettingsTableViewController : UITableViewController {
2017-03-23 19:38:43 -07:00
@IBOutlet weak var encryptInASCIIArmoredTableViewCell : UITableViewCell !
2017-04-10 23:02:42 +08:00
@IBOutlet weak var gitSignatureTableViewCell : UITableViewCell !
2017-02-07 16:45:14 +08:00
@IBOutlet weak var eraseDataTableViewCell : UITableViewCell !
2017-03-04 01:15:28 +08:00
@IBOutlet weak var discardChangesTableViewCell : UITableViewCell !
2017-03-16 22:39:03 -07:00
let passwordStore = PasswordStore . shared
2017-03-23 19:38:43 -07:00
let encryptInASCIIArmoredSwitch : UISwitch = {
let uiSwitch = UISwitch ( )
uiSwitch . onTintColor = Globals . blue
uiSwitch . sizeToFit ( )
uiSwitch . addTarget ( self , action : #selector ( encryptInASCIIArmoredAction ( _ : ) ) , for : UIControlEvents . valueChanged )
return uiSwitch
} ( )
2017-03-16 22:39:03 -07:00
2017-02-07 16:45:14 +08:00
override func viewDidLoad ( ) {
super . viewDidLoad ( )
2017-03-23 19:38:43 -07:00
encryptInASCIIArmoredSwitch . isOn = Defaults [ . encryptInArmored ]
encryptInASCIIArmoredTableViewCell . accessoryView = encryptInASCIIArmoredSwitch
encryptInASCIIArmoredTableViewCell . selectionStyle = . none
2017-04-27 21:48:10 -07:00
setGitSignatureText ( )
}
private func setGitSignatureText ( ) {
if let gitConfigUserName = Defaults [ . gitConfigUserName ] ,
let gitConfigUserEmail = Defaults [ . gitConfigUserEmail ] {
self . gitSignatureTableViewCell . detailTextLabel ? . font = UIFont . systemFont ( ofSize : 14 )
self . gitSignatureTableViewCell . detailTextLabel ? . text = " \( gitConfigUserName ) < \( gitConfigUserEmail ) > "
2017-04-10 23:02:42 +08:00
} else {
gitSignatureTableViewCell . detailTextLabel ? . text = " Not Set "
}
2017-02-07 16:45:14 +08:00
}
override func tableView ( _ tableView : UITableView , didSelectRowAt indexPath : IndexPath ) {
2017-03-22 17:50:32 -07:00
tableView . deselectRow ( at : indexPath , animated : true )
2017-02-07 16:45:14 +08:00
if tableView . cellForRow ( at : indexPath ) = = eraseDataTableViewCell {
let alert = UIAlertController ( title : " Erase Password Store Data? " , message : " This will delete all local data and settings. Password store data on your remote server will not be affected. " , preferredStyle : UIAlertControllerStyle . alert )
alert . addAction ( UIAlertAction ( title : " Erase Password Data " , style : UIAlertActionStyle . destructive , handler : { [ unowned self ] ( action ) -> Void in
2017-02-07 16:52:07 +08:00
SVProgressHUD . show ( withStatus : " Erasing ... " )
2017-03-16 22:39:03 -07:00
self . passwordStore . erase ( )
2017-02-15 11:15:11 +08:00
self . navigationController ! . popViewController ( animated : true )
SVProgressHUD . showSuccess ( withStatus : " Done " )
SVProgressHUD . dismiss ( withDelay : 1 )
2017-02-07 16:45:14 +08:00
} ) )
alert . addAction ( UIAlertAction ( title : " Dismiss " , style : UIAlertActionStyle . cancel , handler : nil ) )
self . present ( alert , animated : true , completion : nil )
2017-03-04 01:15:28 +08:00
} else if tableView . cellForRow ( at : indexPath ) = = discardChangesTableViewCell {
let alert = UIAlertController ( title : " Discard All Changes? " , message : " Do you want to permanently discard all changes to the local copy of your password data? You cannot undo this action. " , preferredStyle : UIAlertControllerStyle . alert )
2017-03-11 02:14:53 +08:00
alert . addAction ( UIAlertAction ( title : " Discard All Changes " , style : UIAlertActionStyle . destructive , handler : { [ unowned self ] ( action ) -> Void in
2017-04-27 21:57:30 -07:00
SVProgressHUD . show ( withStatus : " Resetting ... " )
do {
let numberDiscarded = try self . passwordStore . reset ( )
self . navigationController ! . popViewController ( animated : true )
switch numberDiscarded {
case 0 :
SVProgressHUD . showSuccess ( withStatus : " No local commits " )
case 1 :
SVProgressHUD . showSuccess ( withStatus : " Discarded 1 commit " )
default :
SVProgressHUD . showSuccess ( withStatus : " Discarded \( numberDiscarded ) commits " )
2017-03-04 01:15:28 +08:00
}
2017-04-27 21:57:30 -07:00
SVProgressHUD . dismiss ( withDelay : 1 )
} catch {
Utils . alert ( title : " Error " , message : error . localizedDescription , controller : self , completion : nil )
2017-03-04 01:15:28 +08:00
}
2017-04-27 21:57:30 -07:00
2017-03-04 01:15:28 +08:00
} ) )
alert . addAction ( UIAlertAction ( title : " Dismiss " , style : UIAlertActionStyle . cancel , handler : nil ) )
self . present ( alert , animated : true , completion : nil )
2017-02-07 16:45:14 +08:00
}
}
2017-03-23 19:38:43 -07:00
func encryptInASCIIArmoredAction ( _ sender : Any ? ) {
Defaults [ . encryptInArmored ] = encryptInASCIIArmoredSwitch . isOn
}
2017-04-10 23:02:42 +08:00
@IBAction func cancelGitConfigSetting ( segue : UIStoryboardSegue ) {
}
@IBAction func saveGitConfigSetting ( segue : UIStoryboardSegue ) {
if let controller = segue . source as ? GitConfigSettingTableViewController {
2017-04-27 21:48:10 -07:00
if let gitConfigUserName = controller . nameTextField . text ,
let gitConfigUserEmail = controller . emailTextField . text {
Defaults [ . gitConfigUserName ] = gitConfigUserName
Defaults [ . gitConfigUserEmail ] = gitConfigUserEmail
2017-04-10 23:02:42 +08:00
}
2017-04-27 21:48:10 -07:00
setGitSignatureText ( )
2017-04-10 23:02:42 +08:00
}
}
2017-02-07 16:45:14 +08:00
}