2019-12-04 05:17:06 +01:00
final class JoinPublicChatVC : UIViewController , UIPageViewControllerDataSource , UIPageViewControllerDelegate , OWSQRScannerDelegate {
private let pageVC = UIPageViewController ( transitionStyle : . scroll , navigationOrientation : . horizontal , options : nil )
private var pages : [ UIViewController ] = [ ]
private var isJoining = false
private var targetVCIndex : Int ?
2019-12-04 06:54:46 +01:00
// MARK: S e t t i n g s
override var preferredStatusBarStyle : UIStatusBarStyle { return . lightContent }
2019-12-04 05:17:06 +01:00
// MARK: C o m p o n e n t s
private lazy var tabBar : TabBar = {
let tabs = [
2020-01-23 05:01:31 +01:00
TabBar . Tab ( title : NSLocalizedString ( " Open Group URL " , comment : " " ) ) { [ weak self ] in
2019-12-04 05:17:06 +01:00
guard let self = self else { return }
self . pageVC . setViewControllers ( [ self . pages [ 0 ] ] , direction : . forward , animated : false , completion : nil )
} ,
TabBar . Tab ( title : NSLocalizedString ( " Scan QR Code " , comment : " " ) ) { [ weak self ] in
guard let self = self else { return }
self . pageVC . setViewControllers ( [ self . pages [ 1 ] ] , direction : . forward , animated : false , completion : nil )
}
]
return TabBar ( tabs : tabs )
} ( )
private lazy var enterChatURLVC : EnterChatURLVC = {
let result = EnterChatURLVC ( )
result . joinPublicChatVC = self
return result
} ( )
private lazy var scanQRCodePlaceholderVC : ScanQRCodePlaceholderVC = {
let result = ScanQRCodePlaceholderVC ( )
result . joinPublicChatVC = self
return result
} ( )
private lazy var scanQRCodeWrapperVC : ScanQRCodeWrapperVC = {
2020-01-23 05:01:31 +01:00
let message = NSLocalizedString ( " Scan the QR code of the open group you'd like to join " , comment : " " )
2019-12-04 05:17:06 +01:00
let result = ScanQRCodeWrapperVC ( message : message )
result . delegate = self
return result
} ( )
// MARK: L i f e c y c l e
override func viewDidLoad ( ) {
// 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
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 ( )
2020-01-23 05:01:31 +01:00
titleLabel . text = NSLocalizedString ( " Join Open Group " , comment : " " )
2019-12-04 05:17:06 +01:00
titleLabel . textColor = Colors . text
2019-12-05 04:31:45 +01:00
titleLabel . font = . boldSystemFont ( ofSize : Values . veryLargeFontSize )
2019-12-04 05:17:06 +01:00
navigationItem . titleView = titleLabel
// S e t u p p a g e V C
let hasCameraAccess = ( AVCaptureDevice . authorizationStatus ( for : . video ) = = . authorized )
pages = [ enterChatURLVC , ( hasCameraAccess ? scanQRCodeWrapperVC : scanQRCodePlaceholderVC ) ]
pageVC . dataSource = self
pageVC . delegate = self
pageVC . setViewControllers ( [ enterChatURLVC ] , direction : . forward , animated : false , completion : nil )
// S e t u p t a b b a r
view . addSubview ( tabBar )
tabBar . pin ( . leading , to : . leading , of : view )
2020-01-21 01:30:01 +01:00
let tabBarInset : CGFloat
if #available ( iOS 13 , * ) {
tabBarInset = navigationBar . height ( )
} else {
tabBarInset = 0
}
tabBar . pin ( . top , to : . top , of : view , withInset : tabBarInset )
2019-12-04 05:17:06 +01:00
view . pin ( . trailing , to : . trailing , of : tabBar )
// S e t u p p a g e V C c o n s t r a i n t s
let pageVCView = pageVC . view !
view . addSubview ( pageVCView )
pageVCView . pin ( . leading , to : . leading , of : view )
pageVCView . pin ( . top , to : . bottom , of : tabBar )
view . pin ( . trailing , to : . trailing , of : pageVCView )
view . pin ( . bottom , to : . bottom , of : pageVCView )
let screen = UIScreen . main . bounds
pageVCView . set ( . width , to : screen . width )
2020-01-21 01:30:01 +01:00
let height : CGFloat
if #available ( iOS 13 , * ) {
height = navigationController ! . view . bounds . height - navigationBar . height ( ) - Values . tabBarHeight
} else {
2020-01-22 00:10:16 +01:00
let statusBarHeight = UIApplication . shared . statusBarFrame . height
2020-01-21 01:30:01 +01:00
height = navigationController ! . view . bounds . height - navigationBar . height ( ) - Values . tabBarHeight - statusBarHeight
}
2019-12-04 05:17:06 +01:00
pageVCView . set ( . height , to : height )
enterChatURLVC . constrainHeight ( to : height )
scanQRCodePlaceholderVC . constrainHeight ( to : height )
}
// MARK: G e n e r a l
func pageViewController ( _ pageViewController : UIPageViewController , viewControllerBefore viewController : UIViewController ) -> UIViewController ? {
guard let index = pages . firstIndex ( of : viewController ) , index != 0 else { return nil }
return pages [ index - 1 ]
}
func pageViewController ( _ pageViewController : UIPageViewController , viewControllerAfter viewController : UIViewController ) -> UIViewController ? {
guard let index = pages . firstIndex ( of : viewController ) , index != ( pages . count - 1 ) else { return nil }
return pages [ index + 1 ]
}
fileprivate func handleCameraAccessGranted ( ) {
pages [ 1 ] = scanQRCodeWrapperVC
pageVC . setViewControllers ( [ scanQRCodeWrapperVC ] , direction : . forward , animated : false , completion : nil )
}
// MARK: U p d a t i n g
func pageViewController ( _ pageViewController : UIPageViewController , willTransitionTo pendingViewControllers : [ UIViewController ] ) {
guard let targetVC = pendingViewControllers . first , let index = pages . firstIndex ( of : targetVC ) else { return }
targetVCIndex = index
}
func pageViewController ( _ pageViewController : UIPageViewController , didFinishAnimating isFinished : Bool , previousViewControllers : [ UIViewController ] , transitionCompleted isCompleted : Bool ) {
guard isCompleted , let index = targetVCIndex else { return }
tabBar . selectTab ( at : index )
}
// MARK: I n t e r a c t i o n
@objc private func close ( ) {
dismiss ( animated : true , completion : nil )
}
func controller ( _ controller : OWSQRCodeScanningViewController , didDetectQRCodeWith string : String ) {
let chatURL = string
joinPublicChatIfPossible ( with : chatURL )
}
fileprivate func joinPublicChatIfPossible ( with chatURL : String ) {
guard ! isJoining else { return }
guard let url = URL ( string : chatURL ) , let scheme = url . scheme , scheme = = " https " , url . host != nil else {
return showError ( title : NSLocalizedString ( " Invalid URL " , comment : " " ) , message : NSLocalizedString ( " Please check the URL you entered and try again " , comment : " " ) )
}
isJoining = true
let channelID : UInt64 = 1
let urlAsString = url . absoluteString
let displayName = OWSProfileManager . shared ( ) . localProfileName ( )
// TODO: P r o f i l e p i c t u r e & p r o f i l e k e y
LokiPublicChatManager . shared . addChat ( server : urlAsString , channel : channelID )
. done ( on : . main ) { [ weak self ] _ in
let _ = LokiPublicChatAPI . getMessages ( for : channelID , on : urlAsString )
let _ = LokiPublicChatAPI . setDisplayName ( to : displayName , on : urlAsString )
2020-01-21 23:53:29 +01:00
let _ = LokiPublicChatAPI . join ( channelID , on : urlAsString )
2019-12-04 05:17:06 +01:00
self ? . presentingViewController ! . dismiss ( animated : true , completion : nil )
}
. catch ( on : . main ) { [ weak self ] _ in
self ? . isJoining = false
self ? . showError ( title : NSLocalizedString ( " Couldn't Join " , comment : " " ) )
}
}
// MARK: C o n v e n i e n c e
private func showError ( title : String , message : String = " " ) {
let alert = UIAlertController ( title : title , message : message , preferredStyle : . alert )
alert . addAction ( UIAlertAction ( title : NSLocalizedString ( " OK " , comment : " " ) , style : . default , handler : nil ) )
presentAlert ( alert )
}
}
private final class EnterChatURLVC : UIViewController {
weak var joinPublicChatVC : JoinPublicChatVC !
private var bottomConstraint : NSLayoutConstraint !
// MARK: C o m p o n e n t s
private lazy var chatURLTextField : TextField = {
2020-02-07 03:01:23 +01:00
let result = TextField ( placeholder : " Enter an open group URL " )
2019-12-04 05:17:06 +01:00
result . keyboardType = . URL
result . autocapitalizationType = . none
return result
} ( )
// MARK: L i f e c y c l e
override func viewDidLoad ( ) {
// R e m o v e b a c k g r o u n d c o l o r
view . backgroundColor = . clear
2020-02-07 03:01:23 +01:00
// 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 . withAlphaComponent ( Values . unimportantElementOpacity )
explanationLabel . font = . systemFont ( ofSize : Values . verySmallFontSize )
explanationLabel . text = NSLocalizedString ( " Open groups can be joined by anyone and do not provide full metadata protection " , comment : " " )
explanationLabel . numberOfLines = 0
explanationLabel . textAlignment = . center
explanationLabel . lineBreakMode = . byWordWrapping
2019-12-04 05:17:06 +01:00
// N e x t b u t t o n
2019-12-06 04:42:43 +01:00
let nextButton = Button ( style : . prominentOutline , size : . large )
2019-12-04 05:17:06 +01:00
nextButton . setTitle ( NSLocalizedString ( " Next " , comment : " " ) , for : UIControl . State . normal )
nextButton . addTarget ( self , action : #selector ( joinPublicChatIfPossible ) , 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-12-05 05:28:32 +01:00
// S e t u p s t a c k v i e w
2020-02-07 03:01:23 +01:00
let stackView = UIStackView ( arrangedSubviews : [ chatURLTextField , UIView . vStretchingSpacer ( ) , nextButtonContainer , UIView . spacer ( withHeight : Values . smallSpacing ) , explanationLabel ] )
2019-12-04 05:17:06 +01:00
stackView . axis = . vertical
stackView . alignment = . fill
2020-02-01 04:21:32 +01:00
let bottomSpacing = isSmallScreen ? Values . smallSpacing : Values . largeSpacing
stackView . layoutMargins = UIEdgeInsets ( top : Values . largeSpacing , left : Values . largeSpacing , bottom : bottomSpacing , right : Values . largeSpacing )
2019-12-04 05:17:06 +01:00
stackView . isLayoutMarginsRelativeArrangement = true
view . addSubview ( stackView )
stackView . pin ( . leading , to : . leading , of : view )
stackView . pin ( . top , to : . top , of : view )
view . pin ( . trailing , to : . trailing , of : stackView )
bottomConstraint = view . pin ( . bottom , to : . bottom , of : stackView )
// S e t u p w i d t h c o n s t r a i n t
view . set ( . width , to : UIScreen . main . bounds . width )
// 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 )
// L i s t e n t o k e y b o a r d n o t i f i c a t i o n s
let notificationCenter = NotificationCenter . default
notificationCenter . addObserver ( self , selector : #selector ( handleKeyboardWillChangeFrameNotification ( _ : ) ) , name : UIResponder . keyboardWillChangeFrameNotification , object : nil )
notificationCenter . addObserver ( self , selector : #selector ( handleKeyboardWillHideNotification ( _ : ) ) , name : UIResponder . keyboardWillHideNotification , object : nil )
}
deinit {
NotificationCenter . default . removeObserver ( self )
}
// MARK: G e n e r a l
func constrainHeight ( to height : CGFloat ) {
view . set ( . height , to : height )
}
@objc private func dismissKeyboard ( ) {
chatURLTextField . resignFirstResponder ( )
}
// MARK: U p d a t i n g
@objc private func handleKeyboardWillChangeFrameNotification ( _ notification : Notification ) {
guard let newHeight = ( notification . userInfo ? [ UIResponder . keyboardFrameEndUserInfoKey ] as ? NSValue ) ? . cgRectValue . size . height else { return }
bottomConstraint . constant = newHeight
UIView . animate ( withDuration : 0.25 ) {
self . view . layoutIfNeeded ( )
}
}
@objc private func handleKeyboardWillHideNotification ( _ notification : Notification ) {
bottomConstraint . constant = 0
UIView . animate ( withDuration : 0.25 ) {
self . view . layoutIfNeeded ( )
}
}
// MARK: I n t e r a c t i o n
@objc private func joinPublicChatIfPossible ( ) {
2020-02-07 06:57:50 +01:00
var chatURL = chatURLTextField . text ? . trimmingCharacters ( in : . whitespaces ) ? ? " "
2020-02-09 23:28:24 +01:00
if ! chatURL . lowercased ( ) . starts ( with : " http " ) {
2020-02-07 06:57:50 +01:00
chatURL = " https:// " + chatURL
}
2019-12-04 05:17:06 +01:00
joinPublicChatVC . joinPublicChatIfPossible ( with : chatURL )
}
}
private final class ScanQRCodePlaceholderVC : UIViewController {
weak var joinPublicChatVC : JoinPublicChatVC !
override func viewDidLoad ( ) {
// R e m o v e b a c k g r o u n d c o l o r
view . backgroundColor = . clear
// 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-01-08 06:10:12 +01:00
explanationLabel . text = NSLocalizedString ( " Session needs camera access to scan QR codes " , comment : " " )
2019-12-04 05:17:06 +01:00
explanationLabel . numberOfLines = 0
explanationLabel . textAlignment = . center
explanationLabel . lineBreakMode = . byWordWrapping
// S e t u p c a l l t o a c t i o n b u t t o n
let callToActionButton = UIButton ( )
callToActionButton . titleLabel ! . font = . boldSystemFont ( ofSize : Values . mediumFontSize )
callToActionButton . setTitleColor ( Colors . accent , for : UIControl . State . normal )
callToActionButton . setTitle ( NSLocalizedString ( " Enable Camera Access " , comment : " " ) , for : UIControl . State . normal )
callToActionButton . addTarget ( self , action : #selector ( requestCameraAccess ) , for : UIControl . Event . touchUpInside )
// S e t u p s t a c k v i e w
let stackView = UIStackView ( arrangedSubviews : [ explanationLabel , callToActionButton ] )
stackView . axis = . vertical
stackView . spacing = Values . mediumSpacing
stackView . alignment = . center
// S e t u p c o n s t r a i n t s
view . set ( . width , to : UIScreen . main . bounds . width )
view . addSubview ( stackView )
stackView . pin ( . leading , to : . leading , of : view , withInset : Values . massiveSpacing )
view . pin ( . trailing , to : . trailing , of : stackView , withInset : Values . massiveSpacing )
let verticalCenteringConstraint = stackView . center ( . vertical , in : view )
verticalCenteringConstraint . constant = - 16 // M a k e s t h i n g s a p p e a r c e n t e r e d v i s u a l l y
}
func constrainHeight ( to height : CGFloat ) {
view . set ( . height , to : height )
}
@objc private func requestCameraAccess ( ) {
ows_ask ( forCameraPermissions : { [ weak self ] hasCameraAccess in
if hasCameraAccess {
self ? . joinPublicChatVC . handleCameraAccessGranted ( )
} else {
// D o n o t h i n g
}
} )
}
}