@ -4,6 +4,7 @@ import NVActivityIndicatorView
@objc ( LKDeviceLinkingModal )
@objc ( LKDeviceLinkingModal )
final class DeviceLinkingModal : UIViewController , LokiDeviceLinkingSessionDelegate {
final class DeviceLinkingModal : UIViewController , LokiDeviceLinkingSessionDelegate {
private var deviceLink : LokiDeviceLink ?
// MARK: C o m p o n e n t s
// MARK: C o m p o n e n t s
private lazy var contentView : UIView = {
private lazy var contentView : UIView = {
@ -17,6 +18,8 @@ final class DeviceLinkingModal : UIViewController, LokiDeviceLinkingSessionDeleg
return result
return result
} ( )
} ( )
private lazy var topSpacer = UIView . spacer ( withHeight : 8 )
private lazy var spinner = NVActivityIndicatorView ( frame : CGRect . zero , type : . circleStrokeSpin , color : . white , padding : nil )
private lazy var spinner = NVActivityIndicatorView ( frame : CGRect . zero , type : . circleStrokeSpin , color : . white , padding : nil )
private lazy var titleLabel : UILabel = {
private lazy var titleLabel : UILabel = {
@ -41,6 +44,23 @@ final class DeviceLinkingModal : UIViewController, LokiDeviceLinkingSessionDeleg
return result
return result
} ( )
} ( )
private lazy var mnemonicLabel : UILabel = {
let result = UILabel ( )
result . textColor = Theme . primaryColor
result . font = UIFont . ows_dynamicTypeCaption1Clamped
result . text = " word word word "
result . numberOfLines = 0
result . lineBreakMode = . byWordWrapping
result . textAlignment = . center
return result
} ( )
private lazy var authorizeButton : OWSFlatButton = {
let result = OWSFlatButton . button ( title : NSLocalizedString ( " Authorize " , comment : " " ) , font : . ows_dynamicTypeBodyClamped , titleColor : . white , backgroundColor : . clear , target : self , selector : #selector ( authorizeDeviceLink ) )
result . setBackgroundColors ( upColor : . clear , downColor : . clear )
return result
} ( )
private lazy var cancelButton : OWSFlatButton = {
private lazy var cancelButton : OWSFlatButton = {
let result = OWSFlatButton . button ( title : NSLocalizedString ( " Cancel " , comment : " " ) , font : . ows_dynamicTypeBodyClamped , titleColor : . white , backgroundColor : . clear , target : self , selector : #selector ( cancel ) )
let result = OWSFlatButton . button ( title : NSLocalizedString ( " Cancel " , comment : " " ) , font : . ows_dynamicTypeBodyClamped , titleColor : . white , backgroundColor : . clear , target : self , selector : #selector ( cancel ) )
result . setBackgroundColors ( upColor : . clear , downColor : . clear )
result . setBackgroundColors ( upColor : . clear , downColor : . clear )
@ -51,46 +71,45 @@ final class DeviceLinkingModal : UIViewController, LokiDeviceLinkingSessionDeleg
override func viewDidLoad ( ) {
override func viewDidLoad ( ) {
super . viewDidLoad ( )
super . viewDidLoad ( )
setUpViewHierarchy ( )
setUpViewHierarchy ( )
LokiDeviceLinkingSession . startListeningForLinkingRequests ( with : self )
let _ = LokiDeviceLinkingSession . startListeningForLinkingRequests ( with : self )
}
}
private func setUpViewHierarchy ( ) {
private func setUpViewHierarchy ( ) {
view . backgroundColor = . clear
view . backgroundColor = . clear
// C o n t e n t v i e w
view . addSubview ( contentView )
view . addSubview ( contentView )
contentView . leadingAnchor . constraint ( equalTo : view . leadingAnchor , constant : 32 ) . isActive = true
contentView . leadingAnchor . constraint ( equalTo : view . leadingAnchor , constant : 32 ) . isActive = true
view . trailingAnchor . constraint ( equalTo : contentView . trailingAnchor , constant : 32 ) . isActive = true
view . trailingAnchor . constraint ( equalTo : contentView . trailingAnchor , constant : 32 ) . isActive = true
contentView . center ( . vertical , in : view )
contentView . center ( . vertical , in : view )
// S p i n n e r
let buttonStackView = UIStackView ( arrangedSubviews : [ authorizeButton , cancelButton ] )
contentView . addSubview ( spinner )
buttonStackView . axis = . horizontal
spinner . center ( . horizontal , in : contentView )
buttonStackView . distribution = . fillEqually
spinner . pin ( . top , to : . top , of : contentView , withInset : 32 )
let stackView = UIStackView ( arrangedSubviews : [ topSpacer , spinner , UIView . spacer ( withHeight : 8 ) , titleLabel , subtitleLabel , mnemonicLabel , buttonStackView ] )
spinner . set ( . width , to : 64 )
stackView . spacing = 16
contentView . addSubview ( stackView )
stackView . axis = . vertical
stackView . pin ( . leading , to : . leading , of : contentView , withInset : 16 )
stackView . pin ( . top , to : . top , of : contentView , withInset : 16 )
contentView . pin ( . trailing , to : . trailing , of : stackView , withInset : 16 )
contentView . pin ( . bottom , to : . bottom , of : stackView , withInset : 16 )
spinner . set ( . height , to : 64 )
spinner . set ( . height , to : 64 )
spinner . startAnimating ( )
spinner . startAnimating ( )
// T i t l e l a b e l
mnemonicLabel . isHidden = true
contentView . addSubview ( titleLabel )
let buttonHeight = cancelButton . button . titleLabel ! . font . pointSize * 48 / 17
titleLabel . pin ( . leading , to : . leading , of : contentView , withInset : 16 )
authorizeButton . set ( . height , to : buttonHeight )
titleLabel . pin ( . top , to : . bottom , of : spinner , withInset : 32 )
cancelButton . set ( . height , to : buttonHeight )
contentView . pin ( . trailing , to : . trailing , of : titleLabel , withInset : 16 )
authorizeButton . isHidden = true
// S u b t i t l e l a b e l
contentView . addSubview ( subtitleLabel )
subtitleLabel . pin ( . leading , to : . leading , of : contentView , withInset : 16 )
subtitleLabel . pin ( . top , to : . bottom , of : titleLabel , withInset : 32 )
contentView . pin ( . trailing , to : . trailing , of : subtitleLabel , withInset : 16 )
// C a n c e l b u t t o n
contentView . addSubview ( cancelButton )
cancelButton . pin ( . leading , to : . leading , of : contentView , withInset : 16 )
cancelButton . pin ( . top , to : . bottom , of : subtitleLabel , withInset : 16 )
contentView . pin ( . trailing , to : . trailing , of : cancelButton , withInset : 16 )
contentView . pin ( . bottom , to : . bottom , of : cancelButton , withInset : 16 )
let cancelButtonHeight = cancelButton . button . titleLabel ! . font . pointSize * 48 / 17
cancelButton . set ( . height , to : cancelButtonHeight )
}
}
// MARK: D e v i c e L i n k i n g
// MARK: D e v i c e L i n k i n g
func requestUserAuthorization ( for deviceLink : LokiDeviceLink ) {
func requestUserAuthorization ( for deviceLink : LokiDeviceLink ) {
self . deviceLink = deviceLink
self . topSpacer . isHidden = true
self . spinner . stopAnimating ( )
self . spinner . isHidden = true
self . titleLabel . text = NSLocalizedString ( " Linking Request Received " , comment : " " )
self . subtitleLabel . text = NSLocalizedString ( " Please check that the words below match the words shown on the device being linked. " , comment : " " )
self . mnemonicLabel . isHidden = false
self . authorizeButton . isHidden = false
}
}
// MARK: I n t e r a c t i o n
// MARK: I n t e r a c t i o n
@ -104,6 +123,14 @@ final class DeviceLinkingModal : UIViewController, LokiDeviceLinkingSessionDeleg
}
}
}
}
@objc private func authorizeDeviceLink ( ) {
let deviceLink = self . deviceLink !
let session = LokiDeviceLinkingSession . current !
session . authorizeDeviceLink ( deviceLink )
session . stopListeningForLinkingRequests ( )
dismiss ( animated : true , completion : nil )
}
@objc private func cancel ( ) {
@objc private func cancel ( ) {
LokiDeviceLinkingSession . current ? . stopListeningForLinkingRequests ( )
LokiDeviceLinkingSession . current ? . stopListeningForLinkingRequests ( )
dismiss ( animated : true , completion : nil )
dismiss ( animated : true , completion : nil )