2017-02-09 21:45:31 +08:00
//
// G e n e r a l 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 9 / 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-14 11:16:30 +08:00
import SwiftyUserDefaults
2017-02-09 21:45:31 +08:00
class GeneralSettingsTableViewController : BasicStaticTableViewController {
2017-02-14 11:16:30 +08:00
2017-02-28 12:25:52 +08:00
let hideUnknownSwitch : UISwitch = {
let uiSwitch = UISwitch ( )
uiSwitch . onTintColor = Globals . blue
uiSwitch . sizeToFit ( )
uiSwitch . addTarget ( self , action : #selector ( hideUnknownSwitchAction ( _ : ) ) , for : UIControlEvents . valueChanged )
return uiSwitch
} ( )
2017-03-02 15:01:52 +08:00
2017-03-03 17:12:25 +08:00
let hideOTPSwitch : UISwitch = {
let uiSwitch = UISwitch ( )
uiSwitch . onTintColor = Globals . blue
uiSwitch . sizeToFit ( )
uiSwitch . addTarget ( self , action : #selector ( hideOTPSwitchAction ( _ : ) ) , for : UIControlEvents . valueChanged )
return uiSwitch
} ( )
2017-02-28 12:25:52 +08:00
let rememberPassphraseSwitch : UISwitch = {
let uiSwitch = UISwitch ( )
uiSwitch . onTintColor = Globals . blue
uiSwitch . sizeToFit ( )
uiSwitch . addTarget ( self , action : #selector ( rememberPassphraseSwitchAction ( _ : ) ) , for : UIControlEvents . valueChanged )
uiSwitch . isOn = Defaults [ . isRememberPassphraseOn ]
return uiSwitch
} ( )
2017-03-02 15:01:52 +08:00
let showFolderSwitch : UISwitch = {
let uiSwitch = UISwitch ( )
uiSwitch . onTintColor = Globals . blue
uiSwitch . sizeToFit ( )
uiSwitch . addTarget ( self , action : #selector ( showFolderSwitchAction ( _ : ) ) , for : UIControlEvents . valueChanged )
uiSwitch . isOn = Defaults [ . isShowFolderOn ]
return uiSwitch
} ( )
2017-02-09 21:45:31 +08:00
override func viewDidLoad ( ) {
navigationItemTitle = " General "
2017-02-09 22:13:31 +08:00
tableData = [
// s e c t i o n 0
2017-02-10 16:44:59 +08:00
[ [ . title : " About Repository " , . action : " segue " , . link : " showAboutRepositorySegue " ] , ] ,
2017-02-25 11:40:12 +08:00
// s e c t i o n 1
2017-03-08 00:57:49 -08:00
[
[ . title : " Password Generator Flavor " , . action : " none " , . style : CellDataStyle . value1 ] ,
] ,
// s e c t i o n 2
2017-02-28 12:25:52 +08:00
[
2017-03-05 21:14:49 -08:00
[ . title : " Remember Passphrase " , . action : " none " , ] ,
2017-03-02 16:02:35 +08:00
] ,
[
2017-03-02 15:01:52 +08:00
[ . title : " Show Folder " , . action : " none " , ] ,
2017-02-28 12:25:52 +08:00
[ . title : " Hide Unknown Fields " , . action : " none " , ] ,
2017-03-08 00:58:10 -08:00
[ . title : " Hide OTP Fields " , . action : " none " , ] ,
2017-03-02 16:02:35 +08:00
] ,
2017-02-14 11:16:30 +08:00
2017-02-09 22:13:31 +08:00
]
2017-02-09 21:45:31 +08:00
super . viewDidLoad ( )
2017-02-25 11:40:12 +08:00
2017-02-14 11:16:30 +08:00
}
override func tableView ( _ tableView : UITableView , cellForRowAt indexPath : IndexPath ) -> UITableViewCell {
let cell = super . tableView ( tableView , cellForRowAt : indexPath )
2017-03-02 15:01:52 +08:00
switch cell . textLabel ! . text ! {
case " Hide Unknown Fields " :
2017-02-14 11:16:30 +08:00
cell . accessoryType = . none
2017-02-14 18:08:31 +08:00
let detailButton = UIButton ( type : . detailDisclosure )
2017-02-14 19:19:47 +08:00
hideUnknownSwitch . frame = CGRect ( x : detailButton . bounds . width + 10 , y : 0 , width : hideUnknownSwitch . bounds . width , height : hideUnknownSwitch . bounds . height )
detailButton . frame = CGRect ( x : 0 , y : 5 , width : detailButton . bounds . width , height : detailButton . bounds . height )
2017-02-14 18:08:31 +08:00
detailButton . addTarget ( self , action : #selector ( GeneralSettingsTableViewController . tapHideUnknownSwitchDetailButton ( _ : ) ) , for : UIControlEvents . touchDown )
let accessoryView = UIView ( frame : CGRect ( x : 0 , y : 0 , width : detailButton . bounds . width + hideUnknownSwitch . bounds . width + 10 , height : hideUnknownSwitch . bounds . height ) )
accessoryView . addSubview ( detailButton )
accessoryView . addSubview ( hideUnknownSwitch )
cell . accessoryView = accessoryView
2017-02-14 11:16:30 +08:00
cell . selectionStyle = . none
hideUnknownSwitch . isOn = Defaults [ . isHideUnknownOn ]
2017-03-08 00:57:49 -08:00
case " Hide OTP Fields " :
2017-03-03 17:12:25 +08:00
cell . accessoryType = . none
let detailButton = UIButton ( type : . detailDisclosure )
hideOTPSwitch . frame = CGRect ( x : detailButton . bounds . width + 10 , y : 0 , width : hideOTPSwitch . bounds . width , height : hideOTPSwitch . bounds . height )
detailButton . frame = CGRect ( x : 0 , y : 5 , width : detailButton . bounds . width , height : detailButton . bounds . height )
detailButton . addTarget ( self , action : #selector ( GeneralSettingsTableViewController . tapHideOTPSwitchDetailButton ( _ : ) ) , for : UIControlEvents . touchDown )
let accessoryView = UIView ( frame : CGRect ( x : 0 , y : 0 , width : detailButton . bounds . width + hideOTPSwitch . bounds . width + 10 , height : hideOTPSwitch . bounds . height ) )
accessoryView . addSubview ( detailButton )
accessoryView . addSubview ( hideOTPSwitch )
cell . accessoryView = accessoryView
cell . selectionStyle = . none
hideOTPSwitch . isOn = Defaults [ . isHideOTPOn ]
2017-03-05 21:14:49 -08:00
case " Remember Passphrase " :
2017-02-28 12:25:52 +08:00
cell . accessoryType = . none
cell . selectionStyle = . none
cell . accessoryView = rememberPassphraseSwitch
2017-03-02 15:01:52 +08:00
case " Show Folder " :
cell . accessoryType = . none
cell . selectionStyle = . none
cell . accessoryView = showFolderSwitch
2017-03-08 00:57:49 -08:00
case " Password Generator Flavor " :
cell . accessoryType = . disclosureIndicator
cell . detailTextLabel ? . text = Defaults [ . passwordGeneratorFlavor ]
2017-03-02 15:01:52 +08:00
default : break
2017-02-14 11:16:30 +08:00
}
return cell
}
2017-03-08 00:57:49 -08:00
override func tableView ( _ tableView : UITableView , didSelectRowAt indexPath : IndexPath ) {
super . tableView ( tableView , didSelectRowAt : indexPath )
let cell = tableView . cellForRow ( at : indexPath ) !
if cell . textLabel ! . text ! = = " Password Generator Flavor " {
tableView . deselectRow ( at : indexPath , animated : true )
showPasswordGeneratorFlavorActionSheet ( sourceCell : cell )
}
}
func showPasswordGeneratorFlavorActionSheet ( sourceCell : UITableViewCell ) {
let optionMenu = UIAlertController ( title : nil , message : nil , preferredStyle : . actionSheet )
var randomFlavorActionTitle = " "
var appleFlavorActionTitle = " "
if Defaults [ . passwordGeneratorFlavor ] = = " Random " {
randomFlavorActionTitle = " ✓ Random String "
appleFlavorActionTitle = " Apple's Keychain Style "
} else {
randomFlavorActionTitle = " Random String "
appleFlavorActionTitle = " ✓ Apple's Keychain Style "
}
let randomFlavorAction = UIAlertAction ( title : randomFlavorActionTitle , style : . default ) { _ in
Defaults [ . passwordGeneratorFlavor ] = " Random "
sourceCell . detailTextLabel ? . text = " Random "
}
let appleFlavorAction = UIAlertAction ( title : appleFlavorActionTitle , style : . default ) { _ in
Defaults [ . passwordGeneratorFlavor ] = " Apple "
sourceCell . detailTextLabel ? . text = " Apple "
}
let cancelAction = UIAlertAction ( title : " Cancel " , style : . cancel , handler : nil )
optionMenu . addAction ( randomFlavorAction )
optionMenu . addAction ( appleFlavorAction )
optionMenu . addAction ( cancelAction )
optionMenu . popoverPresentationController ? . sourceView = sourceCell
optionMenu . popoverPresentationController ? . sourceRect = sourceCell . bounds
self . present ( optionMenu , animated : true , completion : nil )
}
2017-02-14 18:08:31 +08:00
func tapHideUnknownSwitchDetailButton ( _ sender : Any ? ) {
2017-02-14 19:17:31 +08:00
let alertMessage = " Only \" key: value \" format in additional fields is supported. Unsupported fields will be given \" unkown \" keys. Turn on this switch to hide unsupported fields. "
2017-02-16 00:54:42 +08:00
let alertTitle = " Hide Unknown Fields "
Utils . alert ( title : alertTitle , message : alertMessage , controller : self , completion : nil )
2017-02-14 18:08:31 +08:00
}
2017-03-03 17:12:25 +08:00
func tapHideOTPSwitchDetailButton ( _ sender : Any ? ) {
let keywordsString = Password . otpKeywords . joined ( separator : " , " )
let alertMessage = " Turn on this switch to hide the fields related to one time passwords (i.e., \( keywordsString ) ). "
let alertTitle = " Hide One Time Password Fields "
Utils . alert ( title : alertTitle , message : alertMessage , controller : self , completion : nil )
}
2017-02-14 11:16:30 +08:00
func hideUnknownSwitchAction ( _ sender : Any ? ) {
Defaults [ . isHideUnknownOn ] = hideUnknownSwitch . isOn
2017-02-09 21:45:31 +08:00
}
2017-02-14 11:16:30 +08:00
2017-03-03 17:12:25 +08:00
func hideOTPSwitchAction ( _ sender : Any ? ) {
Defaults [ . isHideOTPOn ] = hideOTPSwitch . isOn
}
2017-02-28 12:25:52 +08:00
func rememberPassphraseSwitchAction ( _ sender : Any ? ) {
Defaults [ . isRememberPassphraseOn ] = rememberPassphraseSwitch . isOn
if rememberPassphraseSwitch . isOn = = false {
PasswordStore . shared . pgpKeyPassphrase = nil
}
}
2017-03-02 15:01:52 +08:00
func showFolderSwitchAction ( _ sender : Any ? ) {
Defaults [ . isShowFolderOn ] = showFolderSwitch . isOn
NotificationCenter . default . post ( Notification ( name : Notification . Name ( " passwordUpdated " ) ) )
}
2017-02-09 21:45:31 +08:00
}