// C o p y r i g h t © 2 0 2 2 R a n g e p r o o f P t y L t d . A l l r i g h t s r e s e r v e d .
import UIKit
import SessionUIKit
import SessionUtilitiesKit
import SignalUtilitiesKit
final class RestoreVC : BaseVC {
private var spacer1HeightConstraint : NSLayoutConstraint !
private var spacer2HeightConstraint : NSLayoutConstraint !
private var spacer3HeightConstraint : NSLayoutConstraint !
private var restoreButtonBottomOffsetConstraint : NSLayoutConstraint !
private var bottomConstraint : NSLayoutConstraint !
// MARK: - C o m p o n e n t s
private lazy var mnemonicTextView : TextView = {
let result = TextView ( placeholder : " vc_restore_seed_text_field_hint " . localized ( ) )
result . autocapitalizationType = . none
result . themeBorderColor = . textPrimary
result . accessibilityLabel = " Recovery phrase text view "
return result
} ( )
private lazy var legalLabel : UILabel = {
let result = UILabel ( )
result . font = . systemFont ( ofSize : Values . verySmallFontSize )
let text = " By using this service, you agree to our Terms of Service, End User License Agreement (EULA) and Privacy Policy "
let attributedText = NSMutableAttributedString (
string : text ,
attributes : [
. font : UIFont . systemFont ( ofSize : Values . verySmallFontSize )
]
)
attributedText . addAttribute ( . font , value : UIFont . boldSystemFont ( ofSize : Values . verySmallFontSize ) , range : ( text as NSString ) . range ( of : " Terms of Service " ) )
attributedText . addAttribute ( . font , value : UIFont . boldSystemFont ( ofSize : Values . verySmallFontSize ) , range : ( text as NSString ) . range ( of : " End User License Agreement (EULA) " ) )
attributedText . addAttribute ( . font , value : UIFont . boldSystemFont ( ofSize : Values . verySmallFontSize ) , range : ( text as NSString ) . range ( of : " Privacy Policy " ) )
result . themeTextColor = . textPrimary
result . attributedText = attributedText
result . textAlignment = . center
result . lineBreakMode = . byWordWrapping
result . numberOfLines = 0
return result
} ( )
// MARK: - L i f e c y c l e
override func viewDidLoad ( ) {
super . viewDidLoad ( )
setUpNavBarSessionIcon ( )
// S e t u p t i t l e l a b e l
let titleLabel = UILabel ( )
titleLabel . font = . boldSystemFont ( ofSize : isIPhone5OrSmaller ? Values . largeFontSize : Values . veryLargeFontSize )
titleLabel . text = " vc_restore_title " . localized ( )
titleLabel . themeTextColor = . textPrimary
titleLabel . lineBreakMode = . byWordWrapping
titleLabel . numberOfLines = 0
// S e t u p e x p l a n a t i o n l a b e l
let explanationLabel = UILabel ( )
explanationLabel . font = . systemFont ( ofSize : Values . smallFontSize )
explanationLabel . text = " vc_restore_explanation " . localized ( )
explanationLabel . themeTextColor = . textPrimary
explanationLabel . lineBreakMode = . byWordWrapping
explanationLabel . numberOfLines = 0
// S e t u p l e g a l l a b e l
legalLabel . isUserInteractionEnabled = true
let legalLabelTapGestureRecognizer = UITapGestureRecognizer ( target : self , action : #selector ( handleLegalLabelTapped ) )
legalLabel . addGestureRecognizer ( legalLabelTapGestureRecognizer )
// S e t u p s p a c e r s
let topSpacer = UIView . vStretchingSpacer ( )
let spacer1 = UIView ( )
spacer1HeightConstraint = spacer1 . set ( . height , to : isIPhone5OrSmaller ? Values . smallSpacing : Values . veryLargeSpacing )
let spacer2 = UIView ( )
spacer2HeightConstraint = spacer2 . set ( . height , to : isIPhone5OrSmaller ? Values . smallSpacing : Values . veryLargeSpacing )
let spacer3 = UIView ( )
spacer3HeightConstraint = spacer3 . set ( . height , to : isIPhone5OrSmaller ? Values . smallSpacing : Values . veryLargeSpacing )
let bottomSpacer = UIView . vStretchingSpacer ( )
let restoreButtonBottomOffsetSpacer = UIView ( )
restoreButtonBottomOffsetConstraint = restoreButtonBottomOffsetSpacer . set ( . height , to : Values . onboardingButtonBottomOffset )
// S e t u p r e s t o r e b u t t o n
let restoreButton = SessionButton ( style : . filled , size : . large )
restoreButton . setTitle ( " continue_2 " . localized ( ) , for : UIControl . State . normal )
restoreButton . addTarget ( self , action : #selector ( restore ) , for : UIControl . Event . touchUpInside )
// S e t u p r e s t o r e b u t t o n c o n t a i n e r
let restoreButtonContainer = UIView ( wrapping : restoreButton , withInsets : UIEdgeInsets ( top : 0 , leading : Values . massiveSpacing , bottom : 0 , trailing : Values . massiveSpacing ) , shouldAdaptForIPadWithWidth : Values . iPadButtonWidth )
// S e t u p t o p s t a c k v i e w
let topStackView = UIStackView ( arrangedSubviews : [ titleLabel , spacer1 , explanationLabel , spacer2 , mnemonicTextView , spacer3 , legalLabel ] )
topStackView . axis = . vertical
topStackView . alignment = . fill
// S e t u p t o p s t a c k v i e w c o n t a i n e r
let topStackViewContainer = UIView ( )
topStackViewContainer . addSubview ( topStackView )
topStackView . pin ( . leading , to : . leading , of : topStackViewContainer , withInset : Values . veryLargeSpacing )
topStackView . pin ( . top , to : . top , of : topStackViewContainer )
topStackViewContainer . pin ( . trailing , to : . trailing , of : topStackView , withInset : Values . veryLargeSpacing )
topStackViewContainer . pin ( . bottom , to : . bottom , of : topStackView )
// S e t u p m a i n s t a c k v i e w
let mainStackView = UIStackView ( arrangedSubviews : [ topSpacer , topStackViewContainer , bottomSpacer , restoreButtonContainer , restoreButtonBottomOffsetSpacer ] )
mainStackView . axis = . vertical
mainStackView . alignment = . fill
view . addSubview ( mainStackView )
mainStackView . pin ( . leading , to : . leading , of : view )
mainStackView . pin ( . top , to : . top , of : view )
mainStackView . pin ( . trailing , to : . trailing , of : view )
bottomConstraint = mainStackView . pin ( . bottom , to : . bottom , of : view )
topSpacer . heightAnchor . constraint ( equalTo : bottomSpacer . heightAnchor , multiplier : 1 ) . isActive = true
// D i s m i s s k e y b o a r d o n t a p
let tapGestureRecognizer = UITapGestureRecognizer ( target : self , action : #selector ( dismissKeyboard ) )
view . addGestureRecognizer ( tapGestureRecognizer )
// L i s t e n t o k e y b o a r d n o t i f i c a t i o n s
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 )
}
override func viewDidAppear ( _ animated : Bool ) {
super . viewDidAppear ( animated )
// O n s m a l l s c r e e n s w e h i d e t h e l e g a l l a b e l w h e n t h e k e y b o a r d i s u p , b u t i t ' s i m p o r t a n t t h a t t h e u s e r s e e s i t s o
// i n t h o s e i n s t a n c e s w e d o n ' t m a k e t h e k e y b o a r d c o m e u p a u t o m a t i c a l l y
if ! isIPhone5OrSmaller {
mnemonicTextView . becomeFirstResponder ( )
}
}
deinit {
NotificationCenter . default . removeObserver ( self )
}
// MARK: G e n e r a l
@objc private func dismissKeyboard ( ) {
mnemonicTextView . resignFirstResponder ( )
}
// 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 }
bottomConstraint . constant = - newHeight // N e g a t i v e d u e t o h o w t h e c o n s t r a i n t i s s e t u p
restoreButtonBottomOffsetConstraint . constant = isIPhone6OrSmaller ? Values . smallSpacing : Values . largeSpacing
spacer1HeightConstraint . constant = isIPhone6OrSmaller ? Values . smallSpacing : Values . mediumSpacing
spacer2HeightConstraint . constant = isIPhone6OrSmaller ? Values . smallSpacing : Values . mediumSpacing
spacer3HeightConstraint . constant = isIPhone6OrSmaller ? Values . smallSpacing : Values . mediumSpacing
if isIPhone5OrSmaller { legalLabel . isUserInteractionEnabled = false }
UIView . animate ( withDuration : 0.25 ) {
if isIPhone5OrSmaller { self . legalLabel . alpha = 0 }
self . view . layoutIfNeeded ( )
}
}
@objc private func handleKeyboardWillHideNotification ( _ notification : Notification ) {
bottomConstraint . constant = 0
restoreButtonBottomOffsetConstraint . constant = Values . onboardingButtonBottomOffset
spacer1HeightConstraint . constant = isIPhone5OrSmaller ? Values . smallSpacing : Values . veryLargeSpacing
spacer2HeightConstraint . constant = isIPhone5OrSmaller ? Values . smallSpacing : Values . veryLargeSpacing
spacer3HeightConstraint . constant = isIPhone5OrSmaller ? Values . smallSpacing : Values . veryLargeSpacing
if isIPhone5OrSmaller { legalLabel . isUserInteractionEnabled = true }
UIView . animate ( withDuration : 0.25 ) {
if isIPhone5OrSmaller { self . legalLabel . alpha = 1 }
self . view . layoutIfNeeded ( )
}
}
// MARK: - I n t e r a c t i o n
@objc private func restore ( ) {
func showError ( title : String , message : String = " " ) {
let modal : ConfirmationModal = ConfirmationModal (
info : ConfirmationModal . Info (
title : title ,
explanation : message ,
cancelTitle : " BUTTON_OK " . localized ( ) ,
cancelStyle : . alert_text
)
)
present ( modal , animated : true )
}
let seed : Data
let keyPairs : ( ed25519KeyPair : KeyPair , x25519KeyPair : KeyPair )
do {
let mnemonic : String = mnemonicTextView . text ! . lowercased ( )
let hexEncodedSeed : String = try Mnemonic . decode ( mnemonic : mnemonic )
seed = Data ( hex : hexEncodedSeed )
keyPairs = try Identity . generate ( from : seed )
}
catch let error {
let error = error as ? Mnemonic . DecodingError ? ? Mnemonic . DecodingError . generic
showError ( title : error . errorDescription ! )
return
}
// L o a d i n t h e u s e r c o n f i g a n d p r o g r e s s t o t h e n e x t s c r e e n
mnemonicTextView . resignFirstResponder ( )
Onboarding . Flow . recover
. preregister (
with : seed ,
ed25519KeyPair : keyPairs . ed25519KeyPair ,
x25519KeyPair : keyPairs . x25519KeyPair
)
let pnModeVC : PNModeVC = PNModeVC ( flow : . recover )
self . navigationController ? . pushViewController ( pnModeVC , animated : true )
}
@objc private func handleLegalLabelTapped ( _ tapGestureRecognizer : UITapGestureRecognizer ) {
let urlAsString : String ?
let tosRange = ( legalLabel . text ! as NSString ) . range ( of : " Terms of Service " )
let eulaRange = ( legalLabel . text ! as NSString ) . range ( of : " End User License Agreement (EULA) " )
let ppRange = ( legalLabel . text ! as NSString ) . range ( of : " Privacy Policy " )
let touchInLegalLabelCoordinates = tapGestureRecognizer . location ( in : legalLabel )
let characterIndex = legalLabel . characterIndex ( for : touchInLegalLabelCoordinates )
if tosRange . contains ( characterIndex ) {
urlAsString = " https://getsession.org/terms-of-service/ "
} else if eulaRange . contains ( characterIndex ) {
urlAsString = " https://getsession.org/terms-of-service/#eula "
} else if ppRange . contains ( characterIndex ) {
urlAsString = " https://getsession.org/privacy-policy/ "
} else {
urlAsString = nil
}
if let urlAsString = urlAsString {
let url = URL ( string : urlAsString ) !
UIApplication . shared . open ( url )
}
}
}