2017-02-13 01:15:42 +08:00
//
// E d i t P a s s w o r d 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 2 / 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
class EditPasswordTableViewController : PasswordEditorTableViewController {
var password : Password ?
override func viewDidLoad ( ) {
tableData = [
[ [ . type : PasswordEditorCellType . textFieldCell , . title : " name " , . content : password ! . name ] ] ,
[ [ . type : PasswordEditorCellType . fillPasswordCell , . title : " password " , . content : password ! . password ] ] ,
[ [ . type : PasswordEditorCellType . textViewCell , . title : " additions " , . content : password ! . getAdditionsPlainText ( ) ] ] ,
]
sectionHeaderTitles = [ " name " , " password " , " additions " ] . map { $0 . uppercased ( ) }
2017-02-21 01:06:03 +08:00
sectionFooterTitles = [ " " , " " , " It is recommended to use \" key: value \" format to store additional fields as follows: \n url: https://www.apple.com \n username: passforios@gmail.com. " ]
2017-02-13 01:15:42 +08:00
super . viewDidLoad ( )
}
override func shouldPerformSegue ( withIdentifier identifier : String , sender : Any ? ) -> Bool {
if identifier = = " saveEditPasswordSegue " {
let nameCell = tableView . cellForRow ( at : IndexPath ( row : 0 , section : 0 ) ) as ! ContentTableViewCell
if nameCell . getContent ( ) != password ? . name {
2017-02-16 00:54:42 +08:00
let alertTitle = " Cannot Save Edit "
2017-02-13 01:15:42 +08:00
let alertMessage = " Editing name is not supported. "
2017-02-16 00:54:42 +08:00
Utils . alert ( title : alertTitle , message : alertMessage , controller : self ) {
2017-02-13 01:15:42 +08:00
nameCell . setContent ( content : self . password ! . name )
}
return false
}
}
return true
}
override func prepare ( for segue : UIStoryboardSegue , sender : Any ? ) {
2017-02-13 11:42:28 +08:00
if segue . identifier = = " saveEditPasswordSegue " {
let cells = tableView . visibleCells
var cellContents = [ String : String ] ( )
for cell in cells {
let indexPath = tableView . indexPath ( for : cell ) !
let contentCell = cell as ! ContentTableViewCell
let cellTitle = tableData [ indexPath . section ] [ indexPath . row ] [ . title ] as ! String
cellContents [ cellTitle ] = contentCell . getContent ( ) !
}
2017-02-15 21:25:26 +08:00
var plainText = " "
if cellContents [ " additions " ] ! != " " {
2017-03-03 20:15:16 +08:00
plainText = " \( cellContents [ " password " ] ! ) \n \( cellContents [ " additions " ] ! ) "
2017-02-15 21:25:26 +08:00
} else {
2017-03-03 20:15:16 +08:00
plainText = " \( cellContents [ " password " ] ! ) "
2017-02-15 21:25:26 +08:00
}
password ! . updatePassword ( name : cellContents [ " name " ] ! , plainText : plainText )
2017-02-13 01:15:42 +08:00
}
}
}