|
| 1 | +import Foundation |
| 2 | +import UIKit |
| 3 | +import SafariServices |
| 4 | +import WordPressAuthenticator |
| 5 | + |
| 6 | + |
| 7 | +/// Displays the WooCommerce Prologue UI. |
| 8 | +/// |
| 9 | +class LoginPrologueViewController: UIViewController { |
| 10 | + |
| 11 | + /// White-Background View, to be placed surrounding the bottom area. |
| 12 | + /// |
| 13 | + @IBOutlet var backgroundView: UIView! |
| 14 | + |
| 15 | + /// Label to be displayed at the top of the Prologue. |
| 16 | + /// |
| 17 | + @IBOutlet var upperLabel: UILabel! |
| 18 | + |
| 19 | + /// Disclaimer Label |
| 20 | + /// |
| 21 | + @IBOutlet var disclaimerTextView: UITextView! |
| 22 | + |
| 23 | + /// Jetpack Logo ImageVIew |
| 24 | + /// |
| 25 | + @IBOutlet var jetpackImageView: UIImageView! |
| 26 | + |
| 27 | + /// Default Action Button. |
| 28 | + /// |
| 29 | + @IBOutlet var loginButton: UIButton! |
| 30 | + |
| 31 | + |
| 32 | + // MARK: - Overridden Properties |
| 33 | + |
| 34 | + override var supportedInterfaceOrientations: UIInterfaceOrientationMask { |
| 35 | + return .portrait |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + // MARK: - Overridden Methods |
| 40 | + |
| 41 | + override func viewDidLoad() { |
| 42 | + super.viewDidLoad() |
| 43 | + |
| 44 | + setupMainView() |
| 45 | + setupBackgroundView() |
| 46 | + setupJetpackImage() |
| 47 | + setupDisclaimerLabel() |
| 48 | + setupUpperLabel() |
| 49 | + setupLoginButton() |
| 50 | + } |
| 51 | + |
| 52 | + override func viewWillAppear(_ animated: Bool) { |
| 53 | + super.viewWillAppear(animated) |
| 54 | + navigationController?.setNavigationBarHidden(true, animated: animated) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +// MARK: - Initialization Methods |
| 60 | +// |
| 61 | +private extension LoginPrologueViewController { |
| 62 | + |
| 63 | + func setupMainView() { |
| 64 | + view.backgroundColor = StyleManager.wooCommerceBrandColor |
| 65 | + } |
| 66 | + |
| 67 | + func setupBackgroundView() { |
| 68 | + backgroundView.layer.masksToBounds = false |
| 69 | + backgroundView.layer.shadowOpacity = 0.2 |
| 70 | + } |
| 71 | + |
| 72 | + func setupUpperLabel() { |
| 73 | + upperLabel.text = NSLocalizedString("Check your WooCommerce store on the go, fulfill your orders and get notifications of new sales.", comment: "Login Prologue Legend") |
| 74 | + upperLabel.font = UIFont.font(forStyle: .subheadline, weight: .bold) |
| 75 | + upperLabel.textColor = .white |
| 76 | + } |
| 77 | + |
| 78 | + func setupJetpackImage() { |
| 79 | + jetpackImageView.image = UIImage.jetpackLogoImage.imageWithTintColor(.jetpackGreen) |
| 80 | + } |
| 81 | + |
| 82 | + func setupDisclaimerLabel() { |
| 83 | + disclaimerTextView.attributedText = disclaimerAttributedText |
| 84 | + disclaimerTextView.textContainerInset = .zero |
| 85 | + } |
| 86 | + |
| 87 | + func setupLoginButton() { |
| 88 | + let title = NSLocalizedString("Log in with Jetpack", comment: "Authentication Login Button") |
| 89 | + loginButton.setTitle(title, for: .normal) |
| 90 | + loginButton.backgroundColor = .clear |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +// MARK: - UITextViewDeletgate Conformance |
| 96 | +// |
| 97 | +extension LoginPrologueViewController: UITextViewDelegate { |
| 98 | + |
| 99 | + func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { |
| 100 | + displaySafariViewController(at: URL) |
| 101 | + return false |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +// MARK: - Action Handlers |
| 107 | +// |
| 108 | +extension LoginPrologueViewController { |
| 109 | + |
| 110 | + /// Opens SafariViewController at the specified URL. |
| 111 | + /// |
| 112 | + func displaySafariViewController(at url: URL) { |
| 113 | + let safariViewController = SafariViewController(url: url) |
| 114 | + safariViewController.modalPresentationStyle = .pageSheet |
| 115 | + present(safariViewController, animated: true, completion: nil) |
| 116 | + } |
| 117 | + |
| 118 | + /// Proceeds with the Login Flow. |
| 119 | + /// |
| 120 | + @IBAction func loginWasPressed() { |
| 121 | + let loginViewController = WordPressAuthenticator.signinForWordPress() |
| 122 | + loginViewController.restrictToWPCom = true |
| 123 | + loginViewController.offerSignupOption = false |
| 124 | + |
| 125 | + navigationController?.pushViewController(loginViewController, animated: true) |
| 126 | + navigationController?.setNavigationBarHidden(false, animated: true) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +// MARK: - Private Methods |
| 132 | +// |
| 133 | +private extension LoginPrologueViewController { |
| 134 | + |
| 135 | + /// Returns the Disclaimer Attributed Text (which contains a link to the Jetpack Setup URL). |
| 136 | + /// |
| 137 | + var disclaimerAttributedText: NSAttributedString { |
| 138 | + let disclaimerText = NSLocalizedString("This app requires Jetpack to be able to connect to your WooCommerce Store.\nFor configuration instructions, ", comment: "Login Disclaimer Text") |
| 139 | + let disclaimerAttributes: [NSAttributedStringKey: Any] = [ |
| 140 | + .font: UIFont.font(forStyle: .caption1, weight: .thin), |
| 141 | + .foregroundColor: UIColor.black |
| 142 | + ] |
| 143 | + |
| 144 | + let readText = NSLocalizedString("read here.", comment: "Login Disclaimer Linked Text") |
| 145 | + var readAttributes = disclaimerAttributes |
| 146 | + readAttributes[.link] = URL(string: WooConstants.jetpackSetupUrl) |
| 147 | + |
| 148 | + let readAttrText = NSMutableAttributedString(string: readText, attributes: readAttributes) |
| 149 | + let disclaimerAttrText = NSMutableAttributedString(string: disclaimerText, attributes: disclaimerAttributes) |
| 150 | + disclaimerAttrText.append(readAttrText) |
| 151 | + |
| 152 | + return disclaimerAttrText |
| 153 | + } |
| 154 | +} |
0 commit comments