mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
5.3 KiB
Swift
139 lines
5.3 KiB
Swift
//
|
|
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import PromiseKit
|
|
|
|
@objc
|
|
public class OnboardingPermissionsViewController: OnboardingBaseViewController {
|
|
|
|
override public func loadView() {
|
|
super.loadView()
|
|
|
|
view.backgroundColor = Theme.backgroundColor
|
|
view.layoutMargins = UIEdgeInsets(top: 32, left: 32, bottom: 32, right: 32)
|
|
|
|
// TODO:
|
|
// navigationItem.title = NSLocalizedString("SETTINGS_BACKUP", comment: "Label for the backup view in app settings.")
|
|
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(title: NSLocalizedString("NAVIGATION_ITEM_SKIP_BUTTON", comment: "A button to skip a view."),
|
|
style: .plain,
|
|
target: self,
|
|
action: #selector(skipWasPressed))
|
|
|
|
let titleLabel = self.titleLabel(text: NSLocalizedString("ONBOARDING_PERMISSIONS_TITLE", comment: "Title of the 'onboarding permissions' view."))
|
|
view.addSubview(titleLabel)
|
|
titleLabel.autoPinEdges(toSuperviewMarginsExcludingEdge: .bottom)
|
|
|
|
// TODO: Finalize copy.
|
|
let explanationLabel = self.explanationLabel(explanationText: NSLocalizedString("ONBOARDING_PERMISSIONS_EXPLANATION",
|
|
comment: "Explanation in the 'onboarding permissions' view."),
|
|
linkText: NSLocalizedString("ONBOARDING_PERMISSIONS_LEARN_MORE_LINK",
|
|
comment: "Link to the 'learn more' in the 'onboarding permissions' view."),
|
|
selector: #selector(explanationLabelTapped))
|
|
|
|
// TODO: Make sure this all fits if dynamic font sizes are maxed out.
|
|
let giveAccessButton = self.button(title: NSLocalizedString("ONBOARDING_PERMISSIONS_GIVE_ACCESS_BUTTON",
|
|
comment: "Label for the 'give access' button in the 'onboarding permissions' view."),
|
|
selector: #selector(giveAccessPressed))
|
|
|
|
let notNowButton = self.button(title: NSLocalizedString("ONBOARDING_PERMISSIONS_NOT_NOW_BUTTON",
|
|
comment: "Label for the 'not now' button in the 'onboarding permissions' view."),
|
|
selector: #selector(notNowPressed))
|
|
|
|
let buttonStack = UIStackView(arrangedSubviews: [
|
|
giveAccessButton,
|
|
notNowButton
|
|
])
|
|
buttonStack.axis = .vertical
|
|
buttonStack.alignment = .fill
|
|
buttonStack.spacing = 12
|
|
|
|
let stackView = UIStackView(arrangedSubviews: [
|
|
explanationLabel,
|
|
buttonStack
|
|
])
|
|
stackView.axis = .vertical
|
|
stackView.alignment = .fill
|
|
stackView.spacing = 40
|
|
view.addSubview(stackView)
|
|
stackView.autoPinWidthToSuperviewMargins()
|
|
stackView.autoPinEdge(.top, to: .bottom, of: titleLabel, withOffset: 20, relation: .greaterThanOrEqual)
|
|
NSLayoutConstraint.autoSetPriority(.defaultHigh) {
|
|
stackView.autoVCenterInSuperview()
|
|
}
|
|
}
|
|
|
|
public override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
|
|
self.navigationController?.isNavigationBarHidden = false
|
|
}
|
|
|
|
public override func viewDidAppear(_ animated: Bool) {
|
|
super.viewDidAppear(animated)
|
|
|
|
self.navigationController?.isNavigationBarHidden = false
|
|
}
|
|
|
|
// MARK: Request Access
|
|
|
|
private func requestAccess() {
|
|
Logger.info("")
|
|
|
|
// TODO: We need to defer app's request notification permissions until onboarding is complete.
|
|
requestContactsAccess().then { _ in
|
|
return PushRegistrationManager.shared.registerUserNotificationSettings()
|
|
}.done { [weak self] in
|
|
guard let self = self else {
|
|
return
|
|
}
|
|
self.onboardingController.onboardingPermissionsDidComplete(viewController: self)
|
|
}.retainUntilComplete()
|
|
}
|
|
|
|
private func requestContactsAccess() -> Promise<Void> {
|
|
Logger.info("")
|
|
|
|
let (promise, resolver) = Promise<Void>.pending()
|
|
CNContactStore().requestAccess(for: CNEntityType.contacts) { (granted, error) -> Void in
|
|
if granted {
|
|
Logger.info("Granted.")
|
|
} else {
|
|
Logger.error("Error: \(String(describing: error)).")
|
|
}
|
|
// Always fulfill.
|
|
resolver.fulfill(())
|
|
}
|
|
return promise
|
|
}
|
|
|
|
// MARK: - Events
|
|
|
|
@objc func skipWasPressed() {
|
|
Logger.info("")
|
|
|
|
onboardingController.onboardingPermissionsWasSkipped(viewController: self)
|
|
}
|
|
|
|
@objc func explanationLabelTapped(sender: UIGestureRecognizer) {
|
|
guard sender.state == .recognized else {
|
|
return
|
|
}
|
|
// TODO:
|
|
}
|
|
|
|
@objc func giveAccessPressed() {
|
|
Logger.info("")
|
|
|
|
requestAccess()
|
|
}
|
|
|
|
@objc func notNowPressed() {
|
|
Logger.info("")
|
|
|
|
onboardingController.onboardingPermissionsWasSkipped(viewController: self)
|
|
}
|
|
}
|