session-ios/Signal/src/Loki/View Controllers/LandingVC.swift

176 lines
9.4 KiB
Swift
Raw Normal View History

2019-12-06 04:42:43 +01:00
2020-02-20 04:37:17 +01:00
final class LandingVC : BaseVC, LinkDeviceVCDelegate, DeviceLinkingModalDelegate {
2019-12-09 01:50:45 +01:00
private var fakeChatViewContentOffset: CGPoint!
2019-12-06 04:42:43 +01:00
2019-12-09 00:01:10 +01:00
// MARK: Components
private lazy var fakeChatView: FakeChatView = {
let result = FakeChatView()
result.set(.height, to: Values.fakeChatViewHeight)
return result
}()
2019-12-12 06:07:08 +01:00
private lazy var registerButton: Button = {
let result = Button(style: .prominentFilled, size: .large)
2020-01-23 05:01:31 +01:00
result.setTitle(NSLocalizedString("Create Session ID", comment: ""), for: UIControl.State.normal)
2019-12-12 06:07:08 +01:00
result.titleLabel!.font = .boldSystemFont(ofSize: Values.mediumFontSize)
result.addTarget(self, action: #selector(register), for: UIControl.Event.touchUpInside)
return result
}()
private lazy var restoreButton: Button = {
let result = Button(style: .prominentOutline, size: .large)
2019-12-17 18:04:31 +01:00
result.setTitle(NSLocalizedString("Continue your Session", comment: ""), for: UIControl.State.normal)
2019-12-12 06:07:08 +01:00
result.titleLabel!.font = .boldSystemFont(ofSize: Values.mediumFontSize)
result.addTarget(self, action: #selector(restore), for: UIControl.Event.touchUpInside)
return result
}()
private lazy var linkButton: Button = {
let result = Button(style: .regularBorderless, size: .small)
result.setTitle(NSLocalizedString("Link to an existing account", comment: ""), for: UIControl.State.normal)
result.titleLabel!.font = .systemFont(ofSize: Values.smallFontSize)
result.addTarget(self, action: #selector(linkDevice), for: UIControl.Event.touchUpInside)
return result
}()
2019-12-06 04:42:43 +01:00
// MARK: Lifecycle
override func viewDidLoad() {
2020-02-20 04:37:17 +01:00
super.viewDidLoad()
setUpGradientBackground()
setUpNavBarStyle()
setUpNavBarSessionIcon()
2019-12-06 04:42:43 +01:00
// Set up title label
let titleLabel = UILabel()
titleLabel.textColor = Colors.text
2020-01-17 05:53:56 +01:00
titleLabel.font = .boldSystemFont(ofSize: isSmallScreen ? Values.largeFontSize : Values.veryLargeFontSize)
2019-12-17 18:04:31 +01:00
titleLabel.text = NSLocalizedString("Your Session begins here...", comment: "")
2019-12-06 04:42:43 +01:00
titleLabel.numberOfLines = 0
titleLabel.lineBreakMode = .byWordWrapping
// Set up title label container
let titleLabelContainer = UIView()
titleLabelContainer.addSubview(titleLabel)
titleLabel.pin(.leading, to: .leading, of: titleLabelContainer, withInset: Values.veryLargeSpacing)
titleLabel.pin(.top, to: .top, of: titleLabelContainer)
titleLabelContainer.pin(.trailing, to: .trailing, of: titleLabel, withInset: Values.veryLargeSpacing)
titleLabelContainer.pin(.bottom, to: .bottom, of: titleLabel)
2019-12-09 00:01:10 +01:00
// Set up spacers
let topSpacer = UIView.vStretchingSpacer()
let bottomSpacer = UIView.vStretchingSpacer()
2019-12-12 06:07:08 +01:00
// Set up link button container
let linkButtonContainer = UIView()
linkButtonContainer.set(.height, to: Values.onboardingButtonBottomOffset)
linkButtonContainer.addSubview(linkButton)
linkButton.pin(.leading, to: .leading, of: linkButtonContainer, withInset: Values.massiveSpacing)
linkButton.pin(.top, to: .top, of: linkButtonContainer)
linkButtonContainer.pin(.trailing, to: .trailing, of: linkButton, withInset: Values.massiveSpacing)
2020-01-17 05:53:56 +01:00
linkButtonContainer.pin(.bottom, to: .bottom, of: linkButton, withInset: isSmallScreen ? 6 : 10)
2019-12-06 04:42:43 +01:00
// Set up button stack view
let buttonStackView = UIStackView(arrangedSubviews: [ registerButton, restoreButton ])
buttonStackView.axis = .vertical
2020-01-17 05:53:56 +01:00
buttonStackView.spacing = isSmallScreen ? Values.smallSpacing : Values.mediumSpacing
2019-12-06 04:42:43 +01:00
buttonStackView.alignment = .fill
2019-12-09 00:01:10 +01:00
// Set up button stack view container
let buttonStackViewContainer = UIView()
buttonStackViewContainer.addSubview(buttonStackView)
buttonStackView.pin(.leading, to: .leading, of: buttonStackViewContainer, withInset: Values.massiveSpacing)
buttonStackView.pin(.top, to: .top, of: buttonStackViewContainer)
buttonStackViewContainer.pin(.trailing, to: .trailing, of: buttonStackView, withInset: Values.massiveSpacing)
buttonStackViewContainer.pin(.bottom, to: .bottom, of: buttonStackView)
// Set up main stack view
2020-01-17 05:53:56 +01:00
let mainStackView = UIStackView(arrangedSubviews: [ topSpacer, titleLabelContainer, UIView.spacer(withHeight: isSmallScreen ? Values.smallSpacing : Values.mediumSpacing), fakeChatView, bottomSpacer, buttonStackViewContainer, linkButtonContainer ])
2019-12-09 00:01:10 +01:00
mainStackView.axis = .vertical
mainStackView.alignment = .fill
view.addSubview(mainStackView)
mainStackView.pin(to: view)
topSpacer.heightAnchor.constraint(equalTo: bottomSpacer.heightAnchor, multiplier: 1).isActive = true
2019-12-12 06:11:54 +01:00
// Show device unlinked alert if needed
2020-02-19 06:45:38 +01:00
if UserDefaults.standard[.wasUnlinked] {
2019-12-12 06:11:54 +01:00
let alert = UIAlertController(title: NSLocalizedString("Device Unlinked", comment: ""), message: NSLocalizedString("Your device was unlinked successfully", comment: ""), preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), accessibilityIdentifier: nil, style: .default, handler: nil))
present(alert, animated: true, completion: nil)
UserDefaults.removeAll()
}
2019-12-06 06:42:28 +01:00
}
2019-12-09 01:50:45 +01:00
override func viewDidDisappear(_ animated: Bool) {
super.viewDidAppear(animated)
2020-01-21 04:02:53 +01:00
if let fakeChatViewContentOffset = fakeChatViewContentOffset {
fakeChatView.contentOffset = fakeChatViewContentOffset
}
2019-12-09 01:50:45 +01:00
}
2019-12-06 06:42:28 +01:00
// MARK: Interaction
@objc private func register() {
2019-12-09 01:50:45 +01:00
fakeChatViewContentOffset = fakeChatView.contentOffset
DispatchQueue.main.async {
self.fakeChatView.contentOffset = self.fakeChatViewContentOffset
}
2019-12-09 02:33:40 +01:00
let registerVC = RegisterVC()
navigationController!.pushViewController(registerVC, animated: true)
}
@objc private func restore() {
fakeChatViewContentOffset = fakeChatView.contentOffset
DispatchQueue.main.async {
self.fakeChatView.contentOffset = self.fakeChatViewContentOffset
}
let restoreVC = RestoreVC()
navigationController!.pushViewController(restoreVC, animated: true)
2019-12-06 04:42:43 +01:00
}
2019-12-12 06:07:08 +01:00
@objc private func linkDevice() {
let linkDeviceVC = LinkDeviceVC()
linkDeviceVC.delegate = self
let navigationController = OWSNavigationController(rootViewController: linkDeviceVC)
present(navigationController, animated: true, completion: nil)
}
// MARK: Device Linking
func requestDeviceLink(with hexEncodedPublicKey: String) {
guard ECKeyPair.isValidHexEncodedPublicKey(candidate: hexEncodedPublicKey) else {
let alert = UIAlertController(title: NSLocalizedString("Invalid Session ID", comment: ""), message: NSLocalizedString("Please make sure the Session ID you entered is correct and try again.", comment: ""), preferredStyle: .alert)
2019-12-12 06:07:08 +01:00
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), accessibilityIdentifier: nil, style: .default, handler: nil))
return present(alert, animated: true, completion: nil)
}
let seed = Randomness.generateRandomBytes(16)!
let keyPair = Curve25519.generateKeyPair(fromSeed: seed + seed)
let identityManager = OWSIdentityManager.shared()
let databaseConnection = identityManager.value(forKey: "dbConnection") as! YapDatabaseConnection
databaseConnection.setObject(seed.toHexString(), forKey: "LKLokiSeed", inCollection: OWSPrimaryStorageIdentityKeyStoreCollection)
databaseConnection.setObject(keyPair, forKey: OWSPrimaryStorageIdentityKeyStoreIdentityKey, inCollection: OWSPrimaryStorageIdentityKeyStoreCollection)
TSAccountManager.sharedInstance().phoneNumberAwaitingVerification = keyPair.hexEncodedPublicKey
TSAccountManager.sharedInstance().didRegister()
2020-03-11 04:35:55 +01:00
let appDelegate = UIApplication.shared.delegate as! AppDelegate
2020-03-25 00:27:43 +01:00
appDelegate.startPollerIfNeeded()
2020-03-11 04:35:55 +01:00
let deviceLinkingModal = DeviceLinkingModal(mode: .slave, delegate: self)
deviceLinkingModal.modalPresentationStyle = .overFullScreen
deviceLinkingModal.modalTransitionStyle = .crossDissolve
self.present(deviceLinkingModal, animated: true, completion: nil)
let linkingRequestMessage = DeviceLinkingUtilities.getLinkingRequestMessage(for: hexEncodedPublicKey)
ThreadUtil.enqueue(linkingRequestMessage)
2019-12-12 06:07:08 +01:00
}
func handleDeviceLinkAuthorized(_ deviceLink: DeviceLink) {
2020-02-19 06:45:38 +01:00
UserDefaults.standard[.masterHexEncodedPublicKey] = deviceLink.master.hexEncodedPublicKey
2019-12-12 06:07:08 +01:00
fakeChatViewContentOffset = fakeChatView.contentOffset
DispatchQueue.main.async {
self.fakeChatView.contentOffset = self.fakeChatViewContentOffset
}
let homeVC = HomeVC()
navigationController!.setViewControllers([ homeVC ], animated: true)
}
func handleDeviceLinkingModalDismissed() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
2020-03-25 00:27:43 +01:00
appDelegate.stopPollerIfNeeded()
2019-12-12 06:07:08 +01:00
TSAccountManager.sharedInstance().resetForReregistration()
}
// MARK: Convenience
private func setUserInteractionEnabled(_ isEnabled: Bool) {
[ registerButton, restoreButton, linkButton ].forEach {
$0.isUserInteractionEnabled = isEnabled
}
}
2019-12-06 04:42:43 +01:00
}