2020-04-15 05:40:19 +02:00
import PromiseKit
2020-04-14 07:07:37 +02:00
final class PNModeVC : BaseVC , OptionViewDelegate {
private var optionViews : [ OptionView ] {
2020-04-15 06:39:55 +02:00
[ apnsOptionView , backgroundPollingOptionView ]
2020-04-14 07:07:37 +02:00
}
private var selectedOptionView : OptionView ? {
return optionViews . first { $0 . isSelected }
}
// MARK: C o m p o n e n t s
2020-05-29 02:11:01 +02:00
private lazy var apnsOptionView = OptionView ( title : NSLocalizedString ( " Apple Push Notification Service " , comment : " " ) , explanation : NSLocalizedString ( " Session will use the Apple Push Notification service to receive push notifications. You'll be notified of new messages reliably and immediately. Using APNs means that your IP address and device token will be exposed to Apple. If you use push notifications for other apps, this will already be the case. Your IP address and device token will also be exposed to Loki, but your messages will still be onion-routed and end-to-end encrypted, so the contents of your messages will remain completely private. " , comment : " " ) , delegate : self , isRecommended : true )
private lazy var backgroundPollingOptionView = OptionView ( title : NSLocalizedString ( " Background Polling " , comment : " " ) , explanation : NSLocalizedString ( " Session will occasionally check for new messages in the background. This guarantees full metadata protection, but message notifications may be significantly delayed. " , comment : " " ) , delegate : self )
2020-04-14 07:07:37 +02:00
// MARK: L i f e c y c l e
override func viewDidLoad ( ) {
super . viewDidLoad ( )
2020-05-30 00:20:30 +02:00
setUpGradientBackground ( )
setUpNavBarStyle ( )
setUpNavBarSessionIcon ( )
2020-04-14 07:07:37 +02:00
// S e t u p t i t l e l a b e l
let titleLabel = UILabel ( )
titleLabel . textColor = Colors . text
titleLabel . font = . boldSystemFont ( ofSize : isSmallScreen ? Values . largeFontSize : Values . veryLargeFontSize )
2020-04-15 07:55:32 +02:00
titleLabel . text = NSLocalizedString ( " Push Notifications " , comment : " " )
2020-04-14 07:07:37 +02:00
titleLabel . numberOfLines = 0
titleLabel . lineBreakMode = . byWordWrapping
// 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 . textColor = Colors . text
explanationLabel . font = . systemFont ( ofSize : Values . smallFontSize )
2020-04-15 07:55:32 +02:00
explanationLabel . text = NSLocalizedString ( " There are two ways Session can handle push notifications. Make sure to read the descriptions carefully before you choose. " , comment : " " )
2020-04-14 07:07:37 +02:00
explanationLabel . numberOfLines = 0
explanationLabel . lineBreakMode = . byWordWrapping
// S e t u p s p a c e r s
let topSpacer = UIView . vStretchingSpacer ( )
let bottomSpacer = UIView . vStretchingSpacer ( )
let registerButtonBottomOffsetSpacer = UIView ( )
registerButtonBottomOffsetSpacer . set ( . height , to : Values . onboardingButtonBottomOffset )
// S e t u p r e g i s t e r b u t t o n
let registerButton = Button ( style : . prominentFilled , size : . large )
registerButton . setTitle ( NSLocalizedString ( " Continue " , comment : " " ) , for : UIControl . State . normal )
registerButton . titleLabel ! . font = . boldSystemFont ( ofSize : Values . mediumFontSize )
registerButton . addTarget ( self , action : #selector ( register ) , for : UIControl . Event . touchUpInside )
// S e t u p r e g i s t e r b u t t o n c o n t a i n e r
let registerButtonContainer = UIView ( wrapping : registerButton , withInsets : UIEdgeInsets ( top : 0 , leading : Values . massiveSpacing , bottom : 0 , trailing : Values . massiveSpacing ) )
// S e t u p o p t i o n s s t a c k v i e w
let optionsStackView = UIStackView ( arrangedSubviews : optionViews )
optionsStackView . axis = . vertical
optionsStackView . spacing = Values . smallSpacing
optionsStackView . alignment = . fill
// S e t u p t o p s t a c k v i e w
let topStackView = UIStackView ( arrangedSubviews : [ titleLabel , explanationLabel , optionsStackView ] )
topStackView . axis = . vertical
2020-04-28 05:49:15 +02:00
let isMediumScreen = ( UIScreen . main . bounds . height - 667 ) < 1
topStackView . spacing = isSmallScreen ? Values . smallSpacing : ( isMediumScreen ? Values . mediumSpacing : Values . veryLargeSpacing )
2020-04-14 07:07:37 +02:00
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 ( wrapping : topStackView , withInsets : UIEdgeInsets ( top : 0 , leading : Values . veryLargeSpacing , bottom : 0 , trailing : Values . veryLargeSpacing ) )
// 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 , registerButtonContainer , registerButtonBottomOffsetSpacer ] )
mainStackView . axis = . vertical
mainStackView . alignment = . fill
view . addSubview ( mainStackView )
mainStackView . pin ( to : view )
topSpacer . heightAnchor . constraint ( equalTo : bottomSpacer . heightAnchor , multiplier : 1 ) . isActive = true
}
// MARK: I n t e r a c t i o n
2020-04-16 05:58:43 +02:00
func optionViewDidActivate ( _ optionView : OptionView ) {
2020-04-14 07:07:37 +02:00
optionViews . filter { $0 != optionView } . forEach { $0 . isSelected = false }
}
@objc private func register ( ) {
2020-04-14 08:57:32 +02:00
guard selectedOptionView != nil else {
let title = NSLocalizedString ( " Please Pick an Option " , comment : " " )
let alert = UIAlertController ( title : title , message : nil , preferredStyle : . alert )
alert . addAction ( UIAlertAction ( title : NSLocalizedString ( " OK " , comment : " " ) , style : . default , handler : nil ) )
return present ( alert , animated : true , completion : nil )
}
2020-04-14 08:32:10 +02:00
UserDefaults . standard [ . isUsingFullAPNs ] = ( selectedOptionView = = apnsOptionView )
2020-04-16 05:58:43 +02:00
UserDefaults . standard [ . hasSeenPNModeSheet ] = true // S h o u l d n ' t b e s h o w n t o u s e r s w h o ' v e d o n e t h e n e w o n b o a r d i n g
2020-04-14 08:11:22 +02:00
TSAccountManager . sharedInstance ( ) . didRegister ( )
let homeVC = HomeVC ( )
navigationController ! . setViewControllers ( [ homeVC ] , animated : true )
2020-04-17 05:55:24 +02:00
let syncTokensJob = SyncPushTokensJob ( accountManager : AppEnvironment . shared . accountManager , preferences : Environment . shared . preferences )
syncTokensJob . uploadOnlyIfStale = false
let _ : Promise < Void > = syncTokensJob . run ( )
2020-04-14 07:07:37 +02:00
}
}