session-ios/Signal/src/ViewControllers/Registration/OnboardingPermissionsViewCo...

110 lines
4.3 KiB
Swift

//
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
import UIKit
import PromiseKit
import Contacts
@objc
public class OnboardingPermissionsViewController: OnboardingBaseViewController {
override public func loadView() {
super.loadView()
view.backgroundColor = Theme.backgroundColor
view.layoutMargins = .zero
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.createTitleLabel(text: NSLocalizedString("Loki Messenger can let you know when you get a message (and who it is from)", comment: ""))
titleLabel.accessibilityIdentifier = "onboarding.permissions." + "titleLabel"
let explanationLabel = self.createExplanationLabel(text: NSLocalizedString("ONBOARDING_PERMISSIONS_EXPLANATION",
comment: "Explanation in the 'onboarding permissions' view."))
explanationLabel.accessibilityIdentifier = "onboarding.permissions." + "explanationLabel"
let giveAccessButton = self.createButton(title: NSLocalizedString("ONBOARDING_PERMISSIONS_ENABLE_PERMISSIONS_BUTTON",
comment: "Label for the 'give access' button in the 'onboarding permissions' view."),
selector: #selector(giveAccessPressed))
giveAccessButton.accessibilityIdentifier = "onboarding.permissions." + "giveAccessButton"
let notNowButton = self.createLinkButton(title: NSLocalizedString("ONBOARDING_PERMISSIONS_NOT_NOW_BUTTON",
comment: "Label for the 'not now' button in the 'onboarding permissions' view."),
selector: #selector(notNowPressed))
notNowButton.accessibilityIdentifier = "onboarding.permissions." + "notNowButton"
let stackView = UIStackView(arrangedSubviews: [
titleLabel,
UIView.spacer(withHeight: 20),
explanationLabel,
UIView.vStretchingSpacer(),
notNowButton,
UIView.spacer(withHeight: 12),
giveAccessButton
])
stackView.axis = .vertical
stackView.alignment = .fill
stackView.layoutMargins = UIEdgeInsets(top: 32, left: 32, bottom: 32, right: 32)
stackView.isLayoutMarginsRelativeArrangement = true
view.addSubview(stackView)
stackView.autoPinWidthToSuperview()
stackView.autoPin(toTopLayoutGuideOf: self, withInset: 0)
stackView.autoPin(toBottomLayoutGuideOf: self, withInset: 0)
}
// MARK: Request Access
private func requestAccess() {
Logger.info("")
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 giveAccessPressed() {
Logger.info("")
requestAccess()
}
@objc func notNowPressed() {
Logger.info("")
onboardingController.onboardingPermissionsWasSkipped(viewController: self)
}
}