// 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 Sodium
import SessionUIKit
import SignalUtilitiesKit
import SessionUtilitiesKit
final class RegisterVC : BaseVC {
private var seed : Data ! { didSet { updateKeyPair ( ) } }
private var ed25519KeyPair : KeyPair !
private var x25519KeyPair : KeyPair ! { didSet { updatePublicKeyLabel ( ) } }
// MARK: - C o m p o n e n t s
private lazy var publicKeyLabel : UILabel = {
let result = UILabel ( )
result . font = Fonts . spaceMono ( ofSize : isIPhone5OrSmaller ? Values . mediumFontSize : 20 )
result . themeTextColor = . textPrimary
result . accessibilityLabel = " Session ID "
result . isAccessibilityElement = true
result . lineBreakMode = . byCharWrapping
result . numberOfLines = 0
return result
} ( )
private lazy var copyPublicKeyButton : SessionButton = {
let result = SessionButton ( style : . bordered , size : . large )
result . accessibilityLabel = " Copy "
result . isAccessibilityElement = true
result . setTitle ( " copy " . localized ( ) , for : . normal )
result . addTarget ( self , action : #selector ( copyPublicKey ) , for : . touchUpInside )
return result
} ( )
private lazy var legalLabel : UILabel = {
let result = UILabel ( )
result . font = . systemFont ( ofSize : Values . verySmallFontSize )
result . themeTextColor = . textPrimary
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 . 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_register_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_register_explanation " . localized ( )
explanationLabel . themeTextColor = . textPrimary
explanationLabel . lineBreakMode = . byWordWrapping
explanationLabel . numberOfLines = 0
// S e t u p p u b l i c k e y l a b e l c o n t a i n e r
let publicKeyLabelContainer = UIView ( )
publicKeyLabelContainer . addSubview ( publicKeyLabel )
publicKeyLabel . pin ( to : publicKeyLabelContainer , withInset : Values . mediumSpacing )
publicKeyLabelContainer . layer . cornerRadius = TextField . cornerRadius
publicKeyLabelContainer . layer . borderWidth = 1
publicKeyLabelContainer . themeBorderColor = . textPrimary
// S e t u p s p a c e r s
let topSpacer = UIView . vStretchingSpacer ( )
let bottomSpacer = UIView . vStretchingSpacer ( )
// S e t u p r e g i s t e r b u t t o n
let registerButton = SessionButton ( style : . filled , size : . large )
registerButton . accessibilityLabel = " Continue "
registerButton . setTitle ( " continue_2 " . localized ( ) , for : . normal )
registerButton . addTarget ( self , action : #selector ( register ) , 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 : [ registerButton , copyPublicKeyButton ] )
buttonStackView . axis = . vertical
buttonStackView . spacing = isIPhone5OrSmaller ? Values . smallSpacing : Values . mediumSpacing
buttonStackView . alignment = . fill
if UIDevice . current . isIPad {
registerButton . set ( . width , to : Values . iPadButtonWidth )
copyPublicKeyButton . set ( . width , to : Values . iPadButtonWidth )
buttonStackView . alignment = . center
}
// S e t u p b u t t o n s t a c k v i e w c o n t a i n e r
let buttonStackViewContainer = UIView ( )
buttonStackViewContainer . addSubview ( buttonStackView )
buttonStackView . pin ( . leading , to : . leading , of : buttonStackViewContainer , withInset : Values . massiveSpacing )
buttonStackView . pin ( . top , to : . top , of : buttonStackViewContainer )
buttonStackViewContainer . pin ( . trailing , to : . trailing , of : buttonStackView , withInset : Values . massiveSpacing )
buttonStackViewContainer . pin ( . bottom , to : . bottom , of : buttonStackView )
// S e t u p l e g a l l a b e l
legalLabel . isUserInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer ( target : self , action : #selector ( handleLegalLabelTapped ) )
legalLabel . addGestureRecognizer ( tapGestureRecognizer )
// S e t u p l e g a l l a b e l c o n t a i n e r
let legalLabelContainer = UIView ( )
legalLabelContainer . set ( . height , to : Values . onboardingButtonBottomOffset )
legalLabelContainer . addSubview ( legalLabel )
legalLabel . pin ( . leading , to : . leading , of : legalLabelContainer , withInset : Values . massiveSpacing )
legalLabel . pin ( . top , to : . top , of : legalLabelContainer )
legalLabelContainer . pin ( . trailing , to : . trailing , of : legalLabel , withInset : Values . massiveSpacing )
legalLabelContainer . pin ( . bottom , to : . bottom , of : legalLabel , withInset : isIPhone5OrSmaller ? 6 : 10 )
// S e t u p t o p s t a c k v i e w
let topStackView = UIStackView ( arrangedSubviews : [ titleLabel , explanationLabel , publicKeyLabelContainer ] )
topStackView . axis = . vertical
topStackView . spacing = isIPhone5OrSmaller ? Values . smallSpacing : Values . veryLargeSpacing
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 , buttonStackViewContainer , legalLabelContainer ] )
mainStackView . axis = . vertical
mainStackView . alignment = . fill
view . addSubview ( mainStackView )
mainStackView . pin ( to : view )
topSpacer . heightAnchor . constraint ( equalTo : bottomSpacer . heightAnchor , multiplier : 1 ) . isActive = true
// P e f o r m i n i t i a l s e e d u p d a t e
updateSeed ( )
}
override func viewWillAppear ( _ animated : Bool ) {
super . viewWillAppear ( animated )
Onboarding . Flow . register . unregister ( )
}
// MARK: G e n e r a l
@objc private func enableCopyButton ( ) {
copyPublicKeyButton . isUserInteractionEnabled = true
UIView . transition ( with : copyPublicKeyButton , duration : 0.25 , options : . transitionCrossDissolve , animations : {
self . copyPublicKeyButton . setTitle ( " copy " . localized ( ) , for : . normal )
} , completion : nil )
}
// MARK: U p d a t i n g
private func updateSeed ( ) {
seed = try ! Randomness . generateRandomBytes ( numberBytes : 16 )
}
private func updateKeyPair ( ) {
( ed25519KeyPair , x25519KeyPair ) = try ! Identity . generate ( from : seed )
}
private func updatePublicKeyLabel ( ) {
let hexEncodedPublicKey = x25519KeyPair . hexEncodedPublicKey
publicKeyLabel . accessibilityLabel = hexEncodedPublicKey
publicKeyLabel . accessibilityIdentifier = " Session ID "
publicKeyLabel . isAccessibilityElement = true
let characterCount = hexEncodedPublicKey . count
var count = 0
let limit = 32
func animate ( ) {
let numberOfIndexesToShuffle = 32 - count
let indexesToShuffle = ( 0. . < characterCount ) . shuffled ( ) [ 0. . < numberOfIndexesToShuffle ]
var mangledHexEncodedPublicKey = hexEncodedPublicKey
for index in indexesToShuffle {
let startIndex = mangledHexEncodedPublicKey . index ( mangledHexEncodedPublicKey . startIndex , offsetBy : index )
let endIndex = mangledHexEncodedPublicKey . index ( after : startIndex )
mangledHexEncodedPublicKey . replaceSubrange ( startIndex . . < endIndex , with : " 0123456789abcdef__ " . shuffled ( ) [ 0. . < 1 ] )
}
count += 1
if count < limit {
publicKeyLabel . text = mangledHexEncodedPublicKey
Timer . scheduledTimer ( withTimeInterval : 0.032 , repeats : false ) { _ in
animate ( )
}
} else {
publicKeyLabel . text = hexEncodedPublicKey
}
}
animate ( )
}
// MARK: - I n t e r a c t i o n
@objc private func register ( ) {
Onboarding . Flow . register
. preregister (
with : seed ,
ed25519KeyPair : ed25519KeyPair ,
x25519KeyPair : x25519KeyPair
)
let displayNameVC : DisplayNameVC = DisplayNameVC ( flow : . register )
self . navigationController ? . pushViewController ( displayNameVC , animated : true )
}
@objc private func copyPublicKey ( ) {
UIPasteboard . general . string = x25519KeyPair . hexEncodedPublicKey
copyPublicKeyButton . isUserInteractionEnabled = false
copyPublicKeyButton . accessibilityLabel = " Copy session id "
copyPublicKeyButton . isAccessibilityElement = true
UIView . transition ( with : copyPublicKeyButton , duration : 0.25 , options : . transitionCrossDissolve , animations : {
self . copyPublicKeyButton . setTitle ( " copied " . localized ( ) , for : . normal )
} , completion : nil )
Timer . scheduledTimer ( timeInterval : 4 , target : self , selector : #selector ( enableCopyButton ) , userInfo : nil , repeats : false )
}
@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 )
}
}
}