2019-06-17 08:20:09 +02:00
2019-09-24 06:20:22 +02:00
@objc ( LKNewConversationVC )
final class NewConversationVC : OWSViewController , OWSQRScannerDelegate {
2019-12-03 04:17:08 +01:00
private lazy var userHexEncodedPublicKey : String = {
let userDefaults = UserDefaults . standard
if let masterHexEncodedPublicKey = userDefaults . string ( forKey : " masterDeviceHexEncodedPublicKey " ) {
return masterHexEncodedPublicKey
} else {
return OWSIdentityManager . shared ( ) . identityKeyPair ( ) ! . hexEncodedPublicKey
}
} ( )
2019-06-17 08:20:09 +02:00
// MARK: C o m p o n e n t s
2019-12-03 04:17:08 +01:00
private lazy var publicKeyTextField = TextField ( placeholder : NSLocalizedString ( " Enter public key of recipient " , comment : " " ) )
private lazy var userPublicKeyLabel : UILabel = {
let result = UILabel ( )
result . textColor = Colors . text
result . font = Fonts . spaceMono ( ofSize : Values . mediumFontSize )
result . numberOfLines = 0
result . textAlignment = . center
result . lineBreakMode = . byCharWrapping
return result
} ( )
private lazy var copyButton : Button = {
let result = Button ( style : . unimportant )
result . setTitle ( NSLocalizedString ( " Copy " , comment : " " ) , for : UIControl . State . normal )
result . addTarget ( self , action : #selector ( copyPublicKey ) , for : UIControl . Event . touchUpInside )
2019-06-17 08:20:09 +02:00
return result
} ( )
// MARK: L i f e c y c l e
override func viewDidLoad ( ) {
2019-12-03 04:17:08 +01:00
// S e t g r a d i e n t b a c k g r o u n d
view . backgroundColor = . clear
let gradient = Gradients . defaultLokiBackground
view . setGradient ( gradient )
// S e t n a v i g a t i o n b a r b a c k g r o u n d c o l o r
if let navigationBar = navigationController ? . navigationBar {
navigationBar . setBackgroundImage ( UIImage ( ) , for : UIBarMetrics . default )
navigationBar . shadowImage = UIImage ( )
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
// C u s t o m i z e t i t l e
let titleLabel = UILabel ( )
titleLabel . text = NSLocalizedString ( " New Conversation " , comment : " " )
titleLabel . textColor = Colors . text
titleLabel . font = UIFont . boldSystemFont ( ofSize : Values . veryLargeFontSize )
navigationItem . titleView = titleLabel
// S e t u p e x p l a n a t i o n l a b e l
2019-06-17 08:20:09 +02:00
let explanationLabel = UILabel ( )
2019-12-03 04:17:08 +01:00
explanationLabel . textColor = Colors . text . withAlphaComponent ( Values . unimportantElementOpacity )
explanationLabel . font = . systemFont ( ofSize : Values . smallFontSize )
explanationLabel . text = NSLocalizedString ( " Users can share their public key by going into their account settings and tapping \" Share Public Key \" , or by sharing their QR code. " , comment : " " )
2019-06-17 08:20:09 +02:00
explanationLabel . numberOfLines = 0
2019-12-03 04:17:08 +01:00
explanationLabel . textAlignment = . center
2019-06-17 08:20:09 +02:00
explanationLabel . lineBreakMode = . byWordWrapping
2019-12-03 04:17:08 +01:00
// S e t u p s e p a r a t o r
let separator = Separator ( title : NSLocalizedString ( " Your Public Key " , comment : " " ) )
// S e t u p u s e r p u b l i c k e y l a b e l
userPublicKeyLabel . text = userHexEncodedPublicKey
// S e t u p s h a r e b u t t o n
let shareButton = Button ( style : . unimportant )
shareButton . setTitle ( NSLocalizedString ( " Share " , comment : " " ) , for : UIControl . State . normal )
shareButton . addTarget ( self , action : #selector ( sharePublicKey ) , for : UIControl . Event . touchUpInside )
// S e t u p b u t t o n c o n t a i n e r
let buttonContainer = UIStackView ( arrangedSubviews : [ copyButton , shareButton ] )
buttonContainer . axis = . horizontal
buttonContainer . spacing = Values . mediumSpacing
buttonContainer . distribution = . fillEqually
2019-08-23 07:12:08 +02:00
// N e x t b u t t o n
2019-12-03 04:17:08 +01:00
let nextButton = Button ( style : . prominent )
nextButton . setTitle ( NSLocalizedString ( " Next " , comment : " " ) , for : UIControl . State . normal )
nextButton . addTarget ( self , action : #selector ( handleNextButtonTapped ) , for : UIControl . Event . touchUpInside )
let nextButtonContainer = UIView ( )
nextButtonContainer . addSubview ( nextButton )
nextButton . pin ( . leading , to : . leading , of : nextButtonContainer , withInset : 80 )
nextButton . pin ( . top , to : . top , of : nextButtonContainer )
nextButtonContainer . pin ( . trailing , to : . trailing , of : nextButton , withInset : 80 )
nextButtonContainer . pin ( . bottom , to : . bottom , of : nextButton )
2019-06-17 08:20:09 +02:00
// S t a c k v i e w
2019-12-03 04:17:08 +01:00
let stackView = UIStackView ( arrangedSubviews : [ publicKeyTextField , UIView . spacer ( withHeight : Values . smallSpacing ) , explanationLabel , UIView . spacer ( withHeight : Values . veryLargeSpacing ) , separator , UIView . spacer ( withHeight : Values . veryLargeSpacing ) , userPublicKeyLabel , UIView . spacer ( withHeight : Values . veryLargeSpacing ) , buttonContainer , UIView . spacer ( withHeight : Values . veryLargeSpacing ) , nextButtonContainer , UIView . vStretchingSpacer ( ) ] )
2019-06-17 08:20:09 +02:00
stackView . axis = . vertical
stackView . alignment = . fill
2019-12-03 04:17:08 +01:00
stackView . layoutMargins = UIEdgeInsets ( top : Values . mediumSpacing , left : Values . largeSpacing , bottom : Values . mediumSpacing , right : Values . largeSpacing )
2019-06-17 08:20:09 +02:00
stackView . isLayoutMarginsRelativeArrangement = true
view . addSubview ( stackView )
2019-12-03 04:17:08 +01:00
stackView . pin ( to : view )
// 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 )
2019-06-17 08:20:09 +02:00
}
override func viewDidAppear ( _ animated : Bool ) {
super . viewDidAppear ( animated )
publicKeyTextField . becomeFirstResponder ( )
}
2019-12-03 04:17:08 +01:00
// MARK: G e n e r a l
@objc private func dismissKeyboard ( ) {
publicKeyTextField . resignFirstResponder ( )
}
@objc private func enableCopyButton ( ) {
copyButton . isUserInteractionEnabled = true
UIView . transition ( with : copyButton , duration : 0.25 , options : . transitionCrossDissolve , animations : {
self . copyButton . setTitle ( NSLocalizedString ( " Copy " , comment : " " ) , for : UIControl . State . normal )
} , completion : nil )
}
2019-06-17 08:20:09 +02:00
// MARK: I n t e r a c t i o n
@objc private func close ( ) {
dismiss ( animated : true , completion : nil )
}
2019-12-03 04:17:08 +01:00
@objc private func copyPublicKey ( ) {
UIPasteboard . general . string = userHexEncodedPublicKey
copyButton . isUserInteractionEnabled = false
UIView . transition ( with : copyButton , duration : 0.25 , options : . transitionCrossDissolve , animations : {
self . copyButton . setTitle ( NSLocalizedString ( " Copied " , comment : " " ) , for : UIControl . State . normal )
} , completion : nil )
Timer . scheduledTimer ( timeInterval : 4 , target : self , selector : #selector ( enableCopyButton ) , userInfo : nil , repeats : false )
2019-08-23 07:12:08 +02:00
}
2019-12-03 04:17:08 +01:00
@objc private func sharePublicKey ( ) {
let shareVC = UIActivityViewController ( activityItems : [ userHexEncodedPublicKey ] , applicationActivities : nil )
navigationController ? . present ( shareVC , animated : true , completion : nil )
2019-08-23 07:12:08 +02:00
}
2019-12-03 04:17:08 +01:00
// @ o b j c p r i v a t e f u n c s c a n Q R C o d e ( ) {
// o w s _ a s k ( f o r C a m e r a P e r m i s s i o n s : { [ w e a k s e l f ] h a s C a m e r a A c c e s s i n
// i f h a s C a m e r a A c c e s s {
// l e t m e s s a g e = N S L o c a l i z e d S t r i n g ( " S c a n t h e Q R c o d e o f t h e p e r s o n y o u ' d l i k e t o s e c u r e l y m e s s a g e . T h e y c a n f i n d t h e i r Q R c o d e b y g o i n g i n t o L o k i M e s s e n g e r ' s i n - a p p s e t t i n g s a n d c l i c k i n g \ " S h o w Q R C o d e \ " . " , c o m m e n t : " " )
// l e t s c a n Q R C o d e W r a p p e r V C = S c a n Q R C o d e W r a p p e r V C ( m e s s a g e : m e s s a g e )
// s c a n Q R C o d e W r a p p e r V C . d e l e g a t e = s e l f
// s e l f ? . n a v i g a t i o n C o n t r o l l e r ! . p u s h V i e w C o n t r o l l e r ( s c a n Q R C o d e W r a p p e r V C , a n i m a t e d : t r u e )
// } e l s e {
// / / D o n o t h i n g
// }
// } )
// }
//
// f u n c c o n t r o l l e r ( _ c o n t r o l l e r : O W S Q R C o d e S c a n n i n g V i e w C o n t r o l l e r , d i d D e t e c t Q R C o d e W i t h s t r i n g : S t r i n g ) {
// A n a l y t i c s . s h a r e d . t r a c k ( " Q R C o d e S c a n n e d " )
// l e t h e x E n c o d e d P u b l i c K e y = s t r i n g
// s t a r t N e w C o n v e r s a t i o n I f P o s s i b l e ( w i t h : h e x E n c o d e d P u b l i c K e y )
// }
2019-08-28 08:38:20 +02:00
@objc private func handleNextButtonTapped ( ) {
2019-06-17 08:20:09 +02:00
let hexEncodedPublicKey = publicKeyTextField . text ? . trimmingCharacters ( in : . whitespaces ) ? ? " "
2019-08-23 07:12:08 +02:00
startNewConversationIfPossible ( with : hexEncodedPublicKey )
}
2019-08-28 08:38:20 +02:00
private func startNewConversationIfPossible ( with hexEncodedPublicKey : String ) {
2019-06-17 08:20:09 +02:00
if ! ECKeyPair . isValidHexEncodedPublicKey ( candidate : hexEncodedPublicKey ) {
let alert = UIAlertController ( title : NSLocalizedString ( " Invalid Public Key " , comment : " " ) , message : NSLocalizedString ( " Please check the public key you entered and try again. " , comment : " " ) , preferredStyle : . alert )
alert . addAction ( UIAlertAction ( title : NSLocalizedString ( " OK " , comment : " " ) , style : . default , handler : nil ) )
presentAlert ( alert )
} else {
let thread = TSContactThread . getOrCreateThread ( contactId : hexEncodedPublicKey )
presentingViewController ! . dismiss ( animated : true , completion : nil )
2019-12-03 04:17:08 +01:00
SignalApp . shared ( ) . presentConversation ( for : thread , action : . compose , animated : false )
2019-06-17 08:20:09 +02:00
}
}
}