@ -1,5 +1,8 @@
final class SettingsVC : UIViewController {
final class SettingsVC : UIViewController , AvatarViewHelperDelegate {
private var profilePictureToBeUploaded : UIImage ?
private var displayNameToBeUploaded : String ?
private var isEditingDisplayName = false { didSet { handleIsEditingDisplayNameChanged ( ) } }
private lazy var userHexEncodedPublicKey : String = {
if let masterHexEncodedPublicKey = UserDefaults . standard . string ( forKey : " masterDeviceHexEncodedPublicKey " ) {
@ -22,11 +25,24 @@ final class SettingsVC : UIViewController {
return result
} ( )
private lazy var profilePictureUtilities : AvatarViewHelper = {
let result = AvatarViewHelper ( )
result . delegate = self
return result
} ( )
private lazy var displayNameLabel : UILabel = {
let result = UILabel ( )
result . textColor = Colors . text
result . font = . boldSystemFont ( ofSize : Values . veryLargeFontSize )
result . lineBreakMode = . byTruncatingTail
result . textAlignment = . center
return result
} ( )
private lazy var displayNameTextField : TextField = {
let result = TextField ( placeholder : NSLocalizedString ( " Enter a display name " , comment : " " ) , usesDefaultHeight : false )
result . textAlignment = . center
return result
} ( )
@ -50,15 +66,10 @@ final class SettingsVC : UIViewController {
navigationBar . isTranslucent = false
navigationBar . barTintColor = Colors . navigationBarBackground
// S e t u p n a v i g a t i o n b a r b u t t o n s
let closeButton = UIBarButtonItem ( image : # imageLiteral ( resourceName : " X " ) , style : . plain , target : self , action : #selector ( close ) )
closeButton . tintColor = Colors . text
navigationItem . leftBarButtonItem = closeButton
let backButton = UIBarButtonItem ( title : NSLocalizedString ( " Back " , comment : " " ) , style : . plain , target : nil , action : nil )
backButton . tintColor = Colors . text
navigationItem . backBarButtonItem = backButton
let qrCodeButton = UIBarButtonItem ( image : # imageLiteral ( resourceName : " QRCodeFilled " ) , style : . plain , target : self , action : #selector ( showQRCode ) )
qrCodeButton . tintColor = Colors . text
navigationItem . rightBarButtonItem = qrCodeButton
updateNavigationBarButtons ( )
// C u s t o m i z e t i t l e
let titleLabel = UILabel ( )
titleLabel . text = NSLocalizedString ( " Settings " , comment : " " )
@ -66,12 +77,24 @@ final class SettingsVC : UIViewController {
titleLabel . font = . boldSystemFont ( ofSize : Values . veryLargeFontSize )
navigationItem . titleView = titleLabel
// S e t u p p r o f i l e p i c t u r e v i e w
let profilePictureTapGestureRecognizer = UITapGestureRecognizer ( target : self , action : #selector ( showEditProfilePictureUI ) )
profilePictureView . addGestureRecognizer ( profilePictureTapGestureRecognizer )
profilePictureView . hexEncodedPublicKey = userHexEncodedPublicKey
profilePictureView . update ( )
// S e t u p d i s p l a y n a m e l a b e l
displayNameLabel . text = OWSProfileManager . shared ( ) . profileName ( forRecipientId : userHexEncodedPublicKey )
// S e t u p d i s p l a y n a m e c o n t a i n e r
let displayNameContainer = UIView ( )
displayNameContainer . addSubview ( displayNameLabel )
displayNameLabel . pin ( to : displayNameContainer )
displayNameContainer . addSubview ( displayNameTextField )
displayNameTextField . pin ( to : displayNameContainer )
displayNameContainer . set ( . height , to : 40 )
displayNameTextField . alpha = 0
let displayNameLabelTapGestureRecognizer = UITapGestureRecognizer ( target : self , action : #selector ( showEditDisplayNameUI ) )
displayNameContainer . addGestureRecognizer ( displayNameLabelTapGestureRecognizer )
// S e t u p h e a d e r v i e w
let headerStackView = UIStackView ( arrangedSubviews : [ profilePictureView , displayNameLabel ] )
let headerStackView = UIStackView ( arrangedSubviews : [ profilePictureView , displayName Container ] )
headerStackView . axis = . vertical
headerStackView . spacing = Values . smallSpacing
headerStackView . alignment = . center
@ -164,12 +187,6 @@ final class SettingsVC : UIViewController {
return result
}
override func viewWillAppear ( _ animated : Bool ) {
super . viewWillAppear ( animated )
profilePictureView . update ( )
displayNameLabel . text = OWSProfileManager . shared ( ) . profileName ( forRecipientId : userHexEncodedPublicKey )
}
// MARK: G e n e r a l
@objc private func enableCopyButton ( ) {
copyButton . isUserInteractionEnabled = true
@ -178,6 +195,91 @@ final class SettingsVC : UIViewController {
} , completion : nil )
}
func avatarActionSheetTitle ( ) -> String ? {
return NSLocalizedString ( " Update Profile Picture " , comment : " " )
}
func fromViewController ( ) -> UIViewController {
return self
}
func hasClearAvatarAction ( ) -> Bool {
return true
}
func clearAvatarActionLabel ( ) -> String {
return NSLocalizedString ( " Clear " , comment : " " )
}
// MARK: U p d a t i n g
private func handleIsEditingDisplayNameChanged ( ) {
updateNavigationBarButtons ( )
UIView . animate ( withDuration : 0.25 ) {
self . displayNameLabel . alpha = self . isEditingDisplayName ? 0 : 1
self . displayNameTextField . alpha = self . isEditingDisplayName ? 1 : 0
}
if isEditingDisplayName {
displayNameTextField . becomeFirstResponder ( )
} else {
displayNameTextField . resignFirstResponder ( )
}
}
private func updateNavigationBarButtons ( ) {
if isEditingDisplayName {
let cancelButton = UIBarButtonItem ( barButtonSystemItem : . cancel , target : self , action : #selector ( handleCancelDisplayNameEditingButtonTapped ) )
cancelButton . tintColor = Colors . text
navigationItem . leftBarButtonItem = cancelButton
let doneButton = UIBarButtonItem ( barButtonSystemItem : . done , target : self , action : #selector ( handleSaveDisplayNameButtonTapped ) )
doneButton . tintColor = Colors . text
navigationItem . rightBarButtonItem = doneButton
} else {
let closeButton = UIBarButtonItem ( image : # imageLiteral ( resourceName : " X " ) , style : . plain , target : self , action : #selector ( close ) )
closeButton . tintColor = Colors . text
navigationItem . leftBarButtonItem = closeButton
let qrCodeButton = UIBarButtonItem ( image : # imageLiteral ( resourceName : " QRCodeFilled " ) , style : . plain , target : self , action : #selector ( showQRCode ) )
qrCodeButton . tintColor = Colors . text
navigationItem . rightBarButtonItem = qrCodeButton
}
}
func avatarDidChange ( _ image : UIImage ) {
let maxSize = Int ( kOWSProfileManager_MaxAvatarDiameter )
profilePictureToBeUploaded = image . resizedImage ( toFillPixelSize : CGSize ( width : maxSize , height : maxSize ) )
updateProfile ( isUpdatingDisplayName : false , isUpdatingProfilePicture : true )
}
func clearAvatar ( ) {
profilePictureToBeUploaded = nil
updateProfile ( isUpdatingDisplayName : false , isUpdatingProfilePicture : true )
}
private func updateProfile ( isUpdatingDisplayName : Bool , isUpdatingProfilePicture : Bool ) {
let displayName = displayNameToBeUploaded ? ? OWSProfileManager . shared ( ) . profileName ( forRecipientId : userHexEncodedPublicKey )
let profilePicture = profilePictureToBeUploaded ? ? OWSProfileManager . shared ( ) . profileAvatar ( forRecipientId : userHexEncodedPublicKey )
ModalActivityIndicatorViewController . present ( fromViewController : navigationController ! , canCancel : false ) { [ weak self ] modalActivityIndicator in
OWSProfileManager . shared ( ) . updateLocalProfileName ( displayName , avatarImage : profilePicture , success : {
DispatchQueue . main . async {
modalActivityIndicator . dismiss {
guard let self = self else { return }
self . profilePictureView . update ( )
self . displayNameLabel . text = displayName
self . profilePictureToBeUploaded = nil
self . displayNameToBeUploaded = nil
}
}
} , failure : {
DispatchQueue . main . async {
modalActivityIndicator . dismiss {
let alert = UIAlertController ( title : NSLocalizedString ( " Couldn't Update Profile " , comment : " " ) , message : NSLocalizedString ( " Please check your internet connection and try again " , comment : " " ) , preferredStyle : . alert )
alert . addAction ( UIAlertAction ( title : NSLocalizedString ( " OK " , comment : " " ) , style : . default , handler : nil ) )
self ? . present ( alert , animated : true , completion : nil )
}
}
} )
}
}
// MARK: I n t e r a c t i o n
@objc private func close ( ) {
dismiss ( animated : true , completion : nil )
@ -188,6 +290,41 @@ final class SettingsVC : UIViewController {
navigationController ! . pushViewController ( qrCodeVC , animated : true )
}
@objc private func handleCancelDisplayNameEditingButtonTapped ( ) {
isEditingDisplayName = false
}
@objc private func handleSaveDisplayNameButtonTapped ( ) {
func showError ( title : String , message : String = " " ) {
let alert = UIAlertController ( title : title , message : message , preferredStyle : . alert )
alert . addAction ( UIAlertAction ( title : NSLocalizedString ( " OK " , comment : " " ) , style : . default , handler : nil ) )
presentAlert ( alert )
}
let displayName = displayNameTextField . text ! . trimmingCharacters ( in : CharacterSet . whitespacesAndNewlines )
guard ! displayName . isEmpty else {
return showError ( title : NSLocalizedString ( " Please pick a display name " , comment : " " ) )
}
let allowedCharacters = CharacterSet ( charactersIn : " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_ " )
let hasInvalidCharacters = ! displayName . allSatisfy { $0 . unicodeScalars . allSatisfy { allowedCharacters . contains ( $0 ) } }
guard ! hasInvalidCharacters else {
return showError ( title : NSLocalizedString ( " Please pick a display name that consists of only a-z, A-Z, 0-9 and _ characters " , comment : " " ) )
}
guard ! OWSProfileManager . shared ( ) . isProfileNameTooLong ( displayName ) else {
return showError ( title : NSLocalizedString ( " Please pick a shorter display name " , comment : " " ) )
}
isEditingDisplayName = false
displayNameToBeUploaded = displayName
updateProfile ( isUpdatingDisplayName : true , isUpdatingProfilePicture : false )
}
@objc private func showEditProfilePictureUI ( ) {
profilePictureUtilities . showChangeAvatarUI ( )
}
@objc private func showEditDisplayNameUI ( ) {
isEditingDisplayName = true
}
@objc private func copyPublicKey ( ) {
UIPasteboard . general . string = userHexEncodedPublicKey
copyButton . isUserInteractionEnabled = false