2017-02-13 01:15:42 +08:00
//
// P a s s w o r d E d i t o r 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
enum PasswordEditorCellType {
2017-03-09 03:19:36 +08:00
case textFieldCell , textViewCell , fillPasswordCell , passwordLengthCell
2017-02-13 01:15:42 +08:00
}
enum PasswordEditorCellKey {
case type , title , content , placeholders
}
2017-03-09 03:19:36 +08:00
class PasswordEditorTableViewController : UITableViewController , FillPasswordTableViewCellDelegate {
2017-02-13 01:15:42 +08:00
var navigationItemTitle : String ?
var tableData = [
[ Dictionary < PasswordEditorCellKey , Any > ]
] ( )
2017-03-09 03:19:36 +08:00
var sectionHeaderTitles = [ " name " , " password " , " additions " ] . map { $0 . uppercased ( ) }
var 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. " ]
var passwordLengthCell : SliderTableViewCell ?
2017-02-13 01:15:42 +08:00
override func viewDidLoad ( ) {
super . viewDidLoad ( )
navigationItem . title = navigationItemTitle
tableView . register ( UINib ( nibName : " TextFieldTableViewCell " , bundle : nil ) , forCellReuseIdentifier : " textFieldCell " )
tableView . register ( UINib ( nibName : " TextViewTableViewCell " , bundle : nil ) , forCellReuseIdentifier : " textViewCell " )
tableView . register ( UINib ( nibName : " FillPasswordTableViewCell " , bundle : nil ) , forCellReuseIdentifier : " fillPasswordCell " )
2017-03-09 03:19:36 +08:00
tableView . register ( UINib ( nibName : " SliderTableViewCell " , bundle : nil ) , forCellReuseIdentifier : " passwordLengthCell " )
2017-02-13 01:15:42 +08:00
tableView . rowHeight = UITableViewAutomaticDimension
tableView . estimatedRowHeight = 48
tableView . allowsSelection = false
2017-02-21 01:06:03 +08:00
self . tableView . sectionFooterHeight = UITableViewAutomaticDimension ;
self . tableView . estimatedSectionFooterHeight = 0 ;
2017-02-13 01:15:42 +08:00
}
override func tableView ( _ tableView : UITableView , cellForRowAt indexPath : IndexPath ) -> UITableViewCell {
let cellData = tableData [ indexPath . section ] [ indexPath . row ]
var cell = ContentTableViewCell ( )
switch cellData [ PasswordEditorCellKey . type ] as ! PasswordEditorCellType {
case . textViewCell :
cell = tableView . dequeueReusableCell ( withIdentifier : " textViewCell " , for : indexPath ) as ! ContentTableViewCell
case . fillPasswordCell :
2017-03-09 03:19:36 +08:00
let fillPasswordCell = tableView . dequeueReusableCell ( withIdentifier : " fillPasswordCell " , for : indexPath ) as ? FillPasswordTableViewCell
fillPasswordCell ? . delegate = self
cell = fillPasswordCell !
case . passwordLengthCell :
passwordLengthCell = tableView . dequeueReusableCell ( withIdentifier : " passwordLengthCell " , for : indexPath ) as ? SliderTableViewCell
passwordLengthCell ? . reset ( title : " Length " , minimumValue : 1 , maximumValue : Globals . passwordMaximumLength , defaultValue : Globals . passwordDefaultLength )
cell = passwordLengthCell !
2017-02-13 01:15:42 +08:00
default :
cell = tableView . dequeueReusableCell ( withIdentifier : " textFieldCell " , for : indexPath ) as ! ContentTableViewCell
}
if let content = cellData [ PasswordEditorCellKey . content ] as ? String {
cell . setContent ( content : content )
}
return cell
}
override func tableView ( _ tableView : UITableView , heightForHeaderInSection section : Int ) -> CGFloat {
return 44
}
override func numberOfSections ( in tableView : UITableView ) -> Int {
return tableData . count
}
override func tableView ( _ tableView : UITableView , numberOfRowsInSection section : Int ) -> Int {
return tableData [ section ] . count
}
override func tableView ( _ tableView : UITableView , titleForHeaderInSection section : Int ) -> String ? {
return sectionHeaderTitles [ section ]
}
2017-02-21 01:06:03 +08:00
override func tableView ( _ tableView : UITableView , titleForFooterInSection section : Int ) -> String ? {
return sectionFooterTitles [ section ]
}
2017-03-09 03:19:36 +08:00
func generatePassword ( ) -> String {
let length = passwordLengthCell ? . roundedValue ? ? Globals . passwordDefaultLength
return Utils . generatePassword ( length : length )
}
2017-02-13 01:15:42 +08:00
}