2018-03-01 20:40:35 +01:00
//
2018-03-01 20:41:45 +01:00
// C o p y r i g h t ( c ) 2 0 1 8 O p e n W h i s p e r S y s t e m s . A l l r i g h t s r e s e r v e d .
2018-03-01 20:40:35 +01:00
//
2018-03-01 20:41:45 +01:00
import UIKit
@objc
public class OWS2FAReminderViewController : UIViewController , PinEntryViewDelegate {
private var ows2FAManager : OWS2FAManager {
return OWS2FAManager . shared ( )
}
var pinEntryView : PinEntryView !
@objc
2018-05-17 04:42:00 +02:00
public class func wrappedInNavController ( ) -> OWSNavigationController {
let navController = OWSNavigationController ( )
2018-03-01 20:41:45 +01:00
navController . pushViewController ( OWS2FAReminderViewController ( ) , animated : false )
return navController
}
override public func viewDidAppear ( _ animated : Bool ) {
super . viewDidAppear ( animated )
pinEntryView . makePinTextFieldFirstResponder ( )
}
override public func loadView ( ) {
assert ( ows2FAManager . pinCode != nil )
2018-03-26 15:37:43 +02:00
self . navigationItem . title = NSLocalizedString ( " REMINDER_2FA_NAV_TITLE " , comment : " Navbar title for when user is periodically prompted to enter their registration lock PIN " )
2018-03-01 20:41:45 +01:00
self . navigationItem . leftBarButtonItem = UIBarButtonItem ( barButtonSystemItem : . stop , target : self , action : #selector ( didPressCloseButton ) )
let view = UIView ( )
self . view = view
view . backgroundColor = . white
let pinEntryView = PinEntryView ( )
self . pinEntryView = pinEntryView
pinEntryView . delegate = self
2018-03-09 18:08:46 +01:00
2018-03-26 15:37:43 +02:00
let instructionsTextHeader = NSLocalizedString ( " REMINDER_2FA_BODY_HEADER " , comment : " Body header for when user is periodically prompted to enter their registration lock PIN " )
let instructionsTextBody = NSLocalizedString ( " REMINDER_2FA_BODY " , comment : " Body text for when user is periodically prompted to enter their registration lock PIN " )
2018-03-09 18:08:46 +01:00
2018-05-25 18:54:25 +02:00
let attributes = [ NSAttributedStringKey . font : pinEntryView . boldLabelFont ]
2018-03-09 18:08:46 +01:00
let attributedInstructionsText = NSAttributedString ( string : instructionsTextHeader , attributes : attributes ) . rtlSafeAppend ( " " , referenceView : pinEntryView ) . rtlSafeAppend ( instructionsTextBody , referenceView : pinEntryView )
pinEntryView . attributedInstructionsText = attributedInstructionsText
2018-03-01 20:41:45 +01:00
view . addSubview ( pinEntryView )
pinEntryView . autoPinWidthToSuperview ( withMargin : 20 )
2018-03-09 18:08:46 +01:00
pinEntryView . autoPin ( toTopLayoutGuideOf : self , withInset : ScaleFromIPhone5 ( 16 ) )
2018-03-01 20:41:45 +01:00
pinEntryView . autoPin ( toBottomLayoutGuideOf : self , withInset : 0 )
}
// MARK: P i n E n t r y V i e w D e l e g a t e
public func pinEntryView ( _ entryView : PinEntryView , submittedPinCode pinCode : String ) {
Logger . info ( " \( logTag ) in \( #function ) " )
if checkResult ( pinCode : pinCode ) {
didSubmitCorrectPin ( )
} else {
didSubmitWrongPin ( )
}
}
// t e x t F i e l d ( _ t e x t F i e l d : U I T e x t F i e l d , s h o u l d C h a n g e C h a r a c t e r s I n r a n g e : N S R a n g e , r e p l a c e m e n t S t r i n g s t r i n g : S t r i n g ) - > B o o l {
public func pinEntryView ( _ entryView : PinEntryView , pinCodeDidChange pinCode : String ) {
// o p t i m i s t i c a l l y m a t c h , w i t h o u t h a v i n g t o p r e s s " d o n e "
if checkResult ( pinCode : pinCode ) {
didSubmitCorrectPin ( )
}
}
public func pinEntryViewForgotPinLinkTapped ( _ entryView : PinEntryView ) {
Logger . info ( " \( logTag ) in \( #function ) " )
let alertBody = NSLocalizedString ( " REMINDER_2FA_FORGOT_PIN_ALERT_MESSAGE " ,
comment : " Alert message explaining what happens if you forget your 'two-factor auth pin' " )
2018-03-26 15:37:43 +02:00
OWSAlerts . showAlert ( title : nil , message : alertBody )
2018-03-01 20:41:45 +01:00
}
// MARK: H e l p e r s
@objc
private func didPressCloseButton ( sender : UIButton ) {
Logger . info ( " \( logTag ) in \( #function ) " )
// W e ' l l a s k a g a i n n e x t t i m e t h e y l a u n c h
self . dismiss ( animated : true )
}
private func checkResult ( pinCode : String ) -> Bool {
return pinCode = = ows2FAManager . pinCode
}
private func didSubmitCorrectPin ( ) {
Logger . info ( " \( logTag ) in \( #function ) noWrongGuesses: \( noWrongGuesses ) " )
self . dismiss ( animated : true )
OWS2FAManager . shared ( ) . updateRepetitionInterval ( withWasSuccessful : noWrongGuesses )
}
var noWrongGuesses = true
private func didSubmitWrongPin ( ) {
noWrongGuesses = false
Logger . info ( " \( logTag ) in \( #function ) " )
let alertTitle = NSLocalizedString ( " REMINDER_2FA_WRONG_PIN_ALERT_TITLE " ,
comment : " Alert title after wrong guess for 'two-factor auth pin' reminder activity " )
let alertBody = NSLocalizedString ( " REMINDER_2FA_WRONG_PIN_ALERT_BODY " ,
comment : " Alert body after wrong guess for 'two-factor auth pin' reminder activity " )
OWSAlerts . showAlert ( title : alertTitle , message : alertBody )
self . pinEntryView . clearText ( )
}
}