2017-02-17 13:44:25 +08:00
//
// P G P K e y A r m o r S e t t i n g 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 1 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
import SwiftyUserDefaults
2017-04-04 00:03:21 +08:00
class PGPKeyArmorSettingTableViewController : UITableViewController , UITextViewDelegate {
2017-02-17 13:44:25 +08:00
@IBOutlet weak var armorPublicKeyTextView : UITextView !
@IBOutlet weak var armorPrivateKeyTextView : UITextView !
2017-02-17 14:58:27 +08:00
var pgpPassphrase : String ?
2017-03-16 22:39:03 -07:00
let passwordStore = PasswordStore . shared
2017-02-17 13:44:25 +08:00
2017-04-04 00:03:21 +08:00
private var recentPastedText = " "
2017-02-17 13:44:25 +08:00
override func viewDidLoad ( ) {
super . viewDidLoad ( )
armorPublicKeyTextView . text = Defaults [ . pgpPublicKeyArmor ]
armorPrivateKeyTextView . text = Defaults [ . pgpPrivateKeyArmor ]
2017-03-16 22:39:03 -07:00
pgpPassphrase = passwordStore . pgpKeyPassphrase
2017-02-17 14:58:27 +08:00
}
2017-04-08 23:51:43 +08:00
private func createSavePassphraseAndSegueAlert ( ) -> UIAlertController {
2017-02-28 12:25:52 +08:00
let savePassphraseAlert = UIAlertController ( title : " Passphrase " , message : " Do you want to save the passphrase for later decryption? " , preferredStyle : UIAlertControllerStyle . alert )
savePassphraseAlert . addAction ( UIAlertAction ( title : " No " , style : UIAlertActionStyle . default ) { _ in
Defaults [ . isRememberPassphraseOn ] = false
self . performSegue ( withIdentifier : " savePGPKeySegue " , sender : self )
} )
savePassphraseAlert . addAction ( UIAlertAction ( title : " Save " , style : UIAlertActionStyle . destructive ) { _ in
Defaults [ . isRememberPassphraseOn ] = true
self . performSegue ( withIdentifier : " savePGPKeySegue " , sender : self )
} )
return savePassphraseAlert
}
2017-04-10 02:15:49 +08:00
override func shouldPerformSegue ( withIdentifier identifier : String , sender : Any ? ) -> Bool {
if identifier = = " savePGPKeySegue " {
if armorPublicKeyTextView . text . isEmpty {
Utils . alert ( title : " Cannot Save " , message : " Please set public key first. " , controller : self , completion : nil )
return false
}
if armorPrivateKeyTextView . text . isEmpty {
Utils . alert ( title : " Cannot Save " , message : " Please set private key first. " , controller : self , completion : nil )
return false
}
}
return true
}
2017-02-17 14:58:27 +08:00
@IBAction func save ( _ sender : Any ) {
2017-02-28 12:25:52 +08:00
let alert = UIAlertController ( title : " Passphrase " , message : " Please fill in the passphrase of your PGP secret key. " , preferredStyle : UIAlertControllerStyle . alert )
2017-02-17 14:58:27 +08:00
alert . addAction ( UIAlertAction ( title : " OK " , style : UIAlertActionStyle . default , handler : { _ in
self . pgpPassphrase = alert . textFields ? . first ? . text
2017-04-08 23:51:43 +08:00
let savePassphraseAndSegueAlert = self . createSavePassphraseAndSegueAlert ( )
self . present ( savePassphraseAndSegueAlert , animated : true , completion : nil )
2017-02-17 14:58:27 +08:00
} ) )
alert . addTextField ( configurationHandler : { ( textField : UITextField ! ) in
textField . text = self . pgpPassphrase
textField . isSecureTextEntry = true
} )
self . present ( alert , animated : true , completion : nil )
2017-02-17 13:44:25 +08:00
}
2017-04-04 00:03:21 +08:00
func textView ( _ textView : UITextView , shouldChangeTextIn range : NSRange , replacementText text : String ) -> Bool {
if text = = UIPasteboard . general . string {
2017-04-08 23:51:43 +08:00
// u s e r p a s t e s s o m e t h i n g , g e t r e a d y t o c l e a r i n 1 0 s
2017-04-04 00:03:21 +08:00
recentPastedText = text
DispatchQueue . global ( qos : . background ) . asyncAfter ( deadline : DispatchTime . now ( ) + 10 ) { [ weak weakSelf = self ] in
if let pasteboardString = UIPasteboard . general . string ,
pasteboardString = = weakSelf ? . recentPastedText {
UIPasteboard . general . string = " "
}
}
}
return true
}
2017-02-17 13:44:25 +08:00
}