2019-10-09 07:19:58 +02:00
@objc ( LKNewPublicChatVC )
final class NewPublicChatVC : OWSViewController {
// MARK: C o m p o n e n t s
2019-10-14 04:41:32 +02:00
private lazy var urlTextField : UITextField = {
2019-10-09 07:19:58 +02:00
let result = UITextField ( )
result . textColor = Theme . primaryColor
2019-10-14 04:41:32 +02:00
result . font = . ows_dynamicTypeBodyClamped
let placeholder = NSMutableAttributedString ( string : NSLocalizedString ( " Enter a URL " , comment : " " ) )
2019-10-09 07:19:58 +02:00
placeholder . addAttribute ( . foregroundColor , value : Theme . placeholderColor , range : NSRange ( location : 0 , length : placeholder . length ) )
result . attributedPlaceholder = placeholder
2019-10-14 04:41:32 +02:00
result . tintColor = . lokiGreen ( )
2019-10-09 07:19:58 +02:00
result . keyboardAppearance = . dark
2019-10-14 04:41:32 +02:00
result . keyboardType = . URL
result . autocapitalizationType = . none
2019-10-09 07:19:58 +02:00
return result
} ( )
2019-10-14 04:41:32 +02:00
private lazy var addButton = OWSFlatButton . button ( title : NSLocalizedString ( " Add " , comment : " " ) , font : UIFont . ows_dynamicTypeBodyClamped . ows_mediumWeight ( ) , titleColor : . white , backgroundColor : . lokiGreen ( ) , target : self , selector : #selector ( handleAddButtonTapped ) )
2019-10-09 07:19:58 +02:00
// MARK: L i f e c y c l e
override func viewDidLoad ( ) {
// B a c k g r o u n d c o l o r & m a r g i n s
view . backgroundColor = Theme . backgroundColor
view . layoutMargins = . zero
// N a v i g a t i o n b a r
navigationItem . leftBarButtonItem = UIBarButtonItem ( barButtonSystemItem : . stop , target : self , action : #selector ( close ) )
2019-10-14 04:41:32 +02:00
title = NSLocalizedString ( " Add Public Chat " , comment : " " )
2019-10-09 07:19:58 +02:00
// S e p a r a t o r
let separator = UIView ( )
separator . autoSetDimension ( . height , toSize : 1 / UIScreen . main . scale )
separator . backgroundColor = Theme . hairlineColor
2019-10-14 04:41:32 +02:00
// E x p l a n a t i o n l a b e l
let explanationLabel = UILabel ( )
explanationLabel . textColor = Theme . primaryColor
explanationLabel . font = UIFont . ows_dynamicTypeSubheadlineClamped
explanationLabel . text = NSLocalizedString ( " Enter the URL of the public chat you'd like to join. The Loki Public Chat URL is https://chat.lokinet.org. " , comment : " " )
explanationLabel . numberOfLines = 0
explanationLabel . lineBreakMode = . byWordWrapping
// A d d b u t t o n
let addButtonHeight = addButton . button . titleLabel ! . font . pointSize * 48 / 17
addButton . autoSetDimension ( . height , toSize : addButtonHeight )
updateAddButton ( isConnecting : false )
2019-10-09 07:19:58 +02:00
// S t a c k v i e w
2019-10-14 04:41:32 +02:00
let stackView = UIStackView ( arrangedSubviews : [ urlTextField , UIView . spacer ( withHeight : 8 ) , separator , UIView . spacer ( withHeight : 24 ) , explanationLabel , UIView . vStretchingSpacer ( ) , addButton ] )
2019-10-09 07:19:58 +02:00
stackView . axis = . vertical
stackView . alignment = . fill
stackView . layoutMargins = UIEdgeInsets ( top : 16 , left : 16 , bottom : 16 , right : 16 )
stackView . isLayoutMarginsRelativeArrangement = true
view . addSubview ( stackView )
stackView . autoPinWidthToSuperview ( )
stackView . autoPin ( toTopLayoutGuideOf : self , withInset : 0 )
autoPinView ( toBottomOfViewControllerOrKeyboard : stackView , avoidNotch : true )
}
override func viewDidAppear ( _ animated : Bool ) {
super . viewDidAppear ( animated )
2019-10-14 04:41:32 +02:00
urlTextField . becomeFirstResponder ( )
}
// MARK: U p d a t i n g
private func updateAddButton ( isConnecting : Bool ) {
addButton . setEnabled ( ! isConnecting )
addButton . setTitle ( isConnecting ? NSLocalizedString ( " Connecting... " , comment : " " ) : NSLocalizedString ( " Add " , comment : " " ) )
}
// MARK: G e n e r a l
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 )
2019-10-09 07:19:58 +02:00
}
// MARK: I n t e r a c t i o n
@objc private func close ( ) {
dismiss ( animated : true , completion : nil )
}
2019-10-14 04:41:32 +02:00
@objc private func handleAddButtonTapped ( ) {
let uncheckedURL = ( urlTextField . text ? . trimmingCharacters ( in : . whitespaces ) ? ? " " ) . lowercased ( ) . replacingOccurrences ( of : " http:// " , with : " https:// " )
guard let url = URL ( string : uncheckedURL ) , 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 : " " ) )
2019-10-09 07:19:58 +02:00
}
2019-10-14 04:41:32 +02:00
updateAddButton ( isConnecting : true )
2019-10-09 07:19:58 +02:00
// TODO: U p o n a d d i n g w e s h o u l d f e t c h p r e v i o u s m e s s a g e s
2019-10-14 04:41:32 +02:00
LokiPublicChatManager . shared . addChat ( server : url . absoluteString , channel : 1 )
. done ( on : . main ) { [ weak self ] _ in
self ? . presentingViewController ! . dismiss ( animated : true , completion : nil )
2019-10-09 07:19:58 +02:00
}
2019-10-14 04:41:32 +02:00
. catch ( on : . main ) { [ weak self ] _ in
self ? . updateAddButton ( isConnecting : false )
self ? . showError ( title : NSLocalizedString ( " Couldn't Connect " , comment : " " ) )
2019-10-09 07:19:58 +02:00
}
}
}