2019-11-20 02:06:41 +01:00
@objc ( LKDeviceNameModal )
final class DeviceNameModal : Modal {
2019-11-20 04:18:38 +01:00
@objc public var device : DeviceLink . Device !
2019-11-20 02:06:41 +01:00
@objc public var delegate : DeviceNameModalDelegate ?
// MARK: C o m p o n e n t s
2019-12-05 04:31:45 +01:00
private lazy var nameTextField : UITextField = {
2019-11-20 02:06:41 +01:00
let result = UITextField ( )
2019-12-05 04:31:45 +01:00
result . textColor = Colors . text
result . font = . systemFont ( ofSize : Values . mediumFontSize )
2019-11-20 04:18:38 +01:00
result . textAlignment = . center
2019-11-20 02:06:41 +01:00
let placeholder = NSMutableAttributedString ( string : NSLocalizedString ( " Enter a Name " , comment : " " ) )
2019-12-05 04:31:45 +01:00
placeholder . addAttribute ( . foregroundColor , value : Colors . text . withAlphaComponent ( Values . unimportantElementOpacity ) , range : NSRange ( location : 0 , length : placeholder . length ) )
2019-11-20 02:06:41 +01:00
result . attributedPlaceholder = placeholder
2019-12-05 04:31:45 +01:00
result . tintColor = Colors . accent
2019-11-20 02:06:41 +01:00
result . keyboardAppearance = . dark
return result
} ( )
// MARK: L i f e c y c l e
2019-11-20 04:18:38 +01:00
override func viewDidLoad ( ) {
super . viewDidLoad ( )
2019-12-05 04:31:45 +01:00
let notificationCenter = NotificationCenter . default
notificationCenter . addObserver ( self , selector : #selector ( handleKeyboardWillChangeFrameNotification ( _ : ) ) , name : UIResponder . keyboardWillChangeFrameNotification , object : nil )
notificationCenter . addObserver ( self , selector : #selector ( handleKeyboardWillHideNotification ( _ : ) ) , name : UIResponder . keyboardWillHideNotification , object : nil )
2019-11-20 04:18:38 +01:00
}
2019-11-20 02:06:41 +01:00
override func populateContentView ( ) {
2019-12-05 04:31:45 +01:00
// S e t u p t i t l e l a b e l
2019-11-20 02:06:41 +01:00
let titleLabel = UILabel ( )
2019-12-05 04:31:45 +01:00
titleLabel . textColor = Colors . text
titleLabel . font = . boldSystemFont ( ofSize : Values . mediumFontSize )
2019-11-20 02:06:41 +01:00
titleLabel . text = NSLocalizedString ( " Change Device Name " , comment : " " )
titleLabel . numberOfLines = 0
titleLabel . lineBreakMode = . byWordWrapping
titleLabel . textAlignment = . center
2019-12-05 04:31:45 +01:00
// S e t u p e x p l a n a t i o n l a b e l
2019-11-20 02:06:41 +01:00
let explanationLabel = UILabel ( )
2019-12-05 04:31:45 +01:00
explanationLabel . textColor = Colors . text
explanationLabel . font = . systemFont ( ofSize : Values . smallFontSize )
2019-11-20 02:06:41 +01:00
explanationLabel . text = NSLocalizedString ( " Enter the new display name for your device below " , comment : " " )
explanationLabel . numberOfLines = 0
explanationLabel . textAlignment = . center
explanationLabel . lineBreakMode = . byWordWrapping
2019-12-05 04:31:45 +01:00
// S e t u p O K b u t t o n
let okButton = UIButton ( )
okButton . set ( . height , to : Values . mediumButtonHeight )
okButton . layer . cornerRadius = Values . modalButtonCornerRadius
okButton . backgroundColor = Colors . accent
okButton . titleLabel ! . font = . systemFont ( ofSize : Values . smallFontSize )
okButton . setTitleColor ( Colors . text , for : UIControl . State . normal )
okButton . setTitle ( NSLocalizedString ( " OK " , comment : " " ) , for : UIControl . State . normal )
okButton . addTarget ( self , action : #selector ( changeName ) , for : UIControl . Event . touchUpInside )
// S e t u p b u t t o n s t a c k v i e w
let buttonStackView = UIStackView ( arrangedSubviews : [ cancelButton , okButton ] )
2019-11-20 02:06:41 +01:00
buttonStackView . axis = . horizontal
2019-12-05 04:31:45 +01:00
buttonStackView . spacing = Values . mediumSpacing
2019-11-20 02:06:41 +01:00
buttonStackView . distribution = . fillEqually
// S t a c k v i e w
2019-12-05 04:31:45 +01:00
let stackView = UIStackView ( arrangedSubviews : [ titleLabel , explanationLabel , nameTextField , buttonStackView ] )
2019-11-20 02:06:41 +01:00
stackView . axis = . vertical
2019-12-05 04:31:45 +01:00
stackView . spacing = Values . largeSpacing
2019-11-20 02:06:41 +01:00
contentView . addSubview ( stackView )
2019-12-05 04:31:45 +01:00
stackView . pin ( . leading , to : . leading , of : contentView , withInset : Values . largeSpacing )
stackView . pin ( . top , to : . top , of : contentView , withInset : Values . largeSpacing )
contentView . pin ( . trailing , to : . trailing , of : stackView , withInset : Values . largeSpacing )
contentView . pin ( . bottom , to : . bottom , of : stackView , withInset : Values . largeSpacing )
2019-11-20 02:06:41 +01:00
}
2019-11-20 04:18:38 +01:00
deinit {
NotificationCenter . default . removeObserver ( self )
}
// MARK: U p d a t i n g
@objc private func handleKeyboardWillChangeFrameNotification ( _ notification : Notification ) {
guard let newHeight = ( notification . userInfo ? [ UIResponder . keyboardFrameEndUserInfoKey ] as ? NSValue ) ? . cgRectValue . size . height else { return }
verticalCenteringConstraint . constant = - ( newHeight / 2 )
UIView . animate ( withDuration : 0.25 ) {
self . view . layoutIfNeeded ( )
}
}
2019-12-05 04:31:45 +01:00
@objc private func handleKeyboardWillHideNotification ( _ notification : Notification ) {
verticalCenteringConstraint . constant = 0
UIView . animate ( withDuration : 0.25 ) {
self . view . layoutIfNeeded ( )
}
}
2019-11-20 02:06:41 +01:00
// MARK: I n t e r a c t i o n
@objc private func changeName ( ) {
2019-12-05 04:31:45 +01:00
let name = nameTextField . text ! . trimmingCharacters ( in : CharacterSet . whitespacesAndNewlines )
2019-11-20 04:18:38 +01:00
if ! name . isEmpty {
UserDefaults . standard . set ( name , forKey : " \( device . hexEncodedPublicKey ) _display_name " )
delegate ? . handleDeviceNameChanged ( to : name , for : device )
} else {
let alert = UIAlertController ( title : NSLocalizedString ( " Error " , comment : " " ) , message : NSLocalizedString ( " Please pick a name " , comment : " " ) , preferredStyle : . alert )
alert . addAction ( UIAlertAction ( title : NSLocalizedString ( " OK " , comment : " " ) , accessibilityIdentifier : nil , style : . default , handler : nil ) )
present ( alert , animated : true , completion : nil )
}
2019-11-20 02:06:41 +01:00
}
}