|
| 1 | +import UIKit |
| 2 | + |
| 3 | +/// Configuration and actions for an ULErrorViewController, |
| 4 | +/// modeling an error when Jetpack is not installed or is not connected |
| 5 | +/// Displayed as an entry point to the native Jetpack setup flow. |
| 6 | +/// |
| 7 | +struct JetpackSetupRequiredViewModel: ULErrorViewModel { |
| 8 | + private let siteURL: String |
| 9 | + private let connectionOnly: Bool |
| 10 | + private let authentication: Authentication |
| 11 | + private let analytics: Analytics |
| 12 | + |
| 13 | + init(siteURL: String, |
| 14 | + connectionOnly: Bool, |
| 15 | + authentication: Authentication = ServiceLocator.authenticationManager, |
| 16 | + analytics: Analytics = ServiceLocator.analytics) { |
| 17 | + self.connectionOnly = connectionOnly |
| 18 | + self.siteURL = siteURL |
| 19 | + self.authentication = authentication |
| 20 | + self.analytics = analytics |
| 21 | + } |
| 22 | + |
| 23 | + // MARK: - Data and configuration |
| 24 | + let title: String? = Localization.title |
| 25 | + |
| 26 | + var image: UIImage { |
| 27 | + connectionOnly ? .jetpackConnectionImage : .jetpackSetupImage |
| 28 | + } |
| 29 | + |
| 30 | + var text: NSAttributedString { |
| 31 | + let font: UIFont = .body |
| 32 | + let boldFont: UIFont = font.bold |
| 33 | + |
| 34 | + let boldSiteAddress = NSAttributedString(string: siteURL.trimHTTPScheme(), |
| 35 | + attributes: [.font: boldFont]) |
| 36 | + let message = NSMutableAttributedString(string: connectionOnly ? Localization.connectionErrorMessage : Localization.setupErrorMessage) |
| 37 | + |
| 38 | + message.replaceFirstOccurrence(of: "%@", with: boldSiteAddress) |
| 39 | + |
| 40 | + let subtitle = connectionOnly ? Localization.connectionSubtitle : Localization.setupSubtitle |
| 41 | + let subtitleAttributedString = NSAttributedString(string: "\n\n" + subtitle, |
| 42 | + attributes: [.font: UIFont.body, |
| 43 | + .foregroundColor: UIColor.secondaryLabel]) |
| 44 | + message.append(subtitleAttributedString) |
| 45 | + return message |
| 46 | + } |
| 47 | + |
| 48 | + let isAuxiliaryButtonHidden = true |
| 49 | + |
| 50 | + let auxiliaryButtonTitle = "" |
| 51 | + |
| 52 | + var primaryButtonTitle: String { |
| 53 | + connectionOnly ? Localization.connectJetpack : Localization.installJetpack |
| 54 | + } |
| 55 | + |
| 56 | + let secondaryButtonTitle = "" |
| 57 | + |
| 58 | + let isSecondaryButtonHidden = true |
| 59 | + |
| 60 | + // Configures `Help` button title |
| 61 | + let rightBarButtonItemTitle: String? = Localization.helpBarButtonItemTitle |
| 62 | + |
| 63 | + var termsLabelText: NSAttributedString? { |
| 64 | + let content = String.localizedStringWithFormat(Localization.termsContent, Localization.termsOfService, Localization.shareDetails) |
| 65 | + let paragraph = NSMutableParagraphStyle() |
| 66 | + paragraph.alignment = .center |
| 67 | + |
| 68 | + let mutableAttributedText = NSMutableAttributedString( |
| 69 | + string: content, |
| 70 | + attributes: [.font: UIFont.caption1, |
| 71 | + .foregroundColor: UIColor.text, |
| 72 | + .paragraphStyle: paragraph] |
| 73 | + ) |
| 74 | + |
| 75 | + mutableAttributedText.setAsLink(textToFind: Localization.termsOfService, |
| 76 | + linkURL: Links.jetpackTerms + self.siteURL) |
| 77 | + mutableAttributedText.setAsLink(textToFind: Localization.shareDetails, |
| 78 | + linkURL: Links.jetpackShareDetails + self.siteURL) |
| 79 | + return mutableAttributedText |
| 80 | + } |
| 81 | + |
| 82 | + func viewDidLoad(_ viewController: UIViewController?) { |
| 83 | + // TODO: add tracks |
| 84 | + } |
| 85 | + |
| 86 | + func didTapPrimaryButton(in viewController: UIViewController?) { |
| 87 | + // TODO: handle Jetpack setup natively |
| 88 | + } |
| 89 | + |
| 90 | + func didTapSecondaryButton(in viewController: UIViewController?) { |
| 91 | + // no-op |
| 92 | + } |
| 93 | + |
| 94 | + func didTapAuxiliaryButton(in viewController: UIViewController?) { |
| 95 | + // no-op |
| 96 | + } |
| 97 | + |
| 98 | + func didTapRightBarButtonItem(in viewController: UIViewController?) { |
| 99 | + guard let viewController = viewController else { |
| 100 | + return |
| 101 | + } |
| 102 | + authentication.presentSupport(from: viewController, screen: .jetpackRequired) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +extension JetpackSetupRequiredViewModel { |
| 107 | + enum Localization { |
| 108 | + static let title = NSLocalizedString("Connect Store", comment: "Title of the Jetpack setup required screen") |
| 109 | + static let installJetpack = NSLocalizedString( |
| 110 | + "Install Jetpack", |
| 111 | + comment: "Button to install Jetpack from the Jetpack setup required screen" |
| 112 | + ) |
| 113 | + static let connectJetpack = NSLocalizedString( |
| 114 | + "Connect Jetpack", |
| 115 | + comment: "Button to authorize Jetpack connection from the Jetpack setup required screen" |
| 116 | + ) |
| 117 | + static let setupErrorMessage = NSLocalizedString( |
| 118 | + "To use this app for %@ you'll need the free Jetpack plugin installed and connected on your store.", |
| 119 | + comment: "Error message on the Jetpack setup required screen." + |
| 120 | + "Reads like: To use this app for test.com you'll need..." |
| 121 | + ) |
| 122 | + static let connectionErrorMessage = NSLocalizedString( |
| 123 | + "To use this app for %@ you'll need to connect your store to Jetpack.", |
| 124 | + comment: "Error message on the Jetpack setup required screen when Jetpack connection is missing." + |
| 125 | + "Reads like: To use this app for test.com you'll need..." |
| 126 | + ) |
| 127 | + static let setupSubtitle = NSLocalizedString( |
| 128 | + "You’ll need your store credentials to begin the installation.", |
| 129 | + comment: "Subtitle on the Jetpack setup required screen" |
| 130 | + ) |
| 131 | + static let connectionSubtitle = NSLocalizedString( |
| 132 | + "You’ll need your store credentials to begin the connection.", |
| 133 | + comment: "Subtitle on the Jetpack setup required screen when only Jetpack connection is missing" |
| 134 | + ) |
| 135 | + static let helpBarButtonItemTitle = NSLocalizedString("Help", comment: "Help button on Jetpack setup required screen.") |
| 136 | + static let termsContent = NSLocalizedString( |
| 137 | + "By tapping the Connect Jetpack button, you agree to our %1$@ and to %2$@ with WordPress.com.", |
| 138 | + comment: "Content of the label at the end of the Jetpack setup required screen. " + |
| 139 | + "Reads like: By tapping the Connect Jetpack button, you agree to our Terms of Service and to share details with WordPress.com." |
| 140 | + ) |
| 141 | + static let termsOfService = NSLocalizedString( |
| 142 | + "Terms of Service", |
| 143 | + comment: "The terms to be agreed upon when tapping the Connect Jetpack button on the Jetpack setup required screen." |
| 144 | + ) |
| 145 | + static let shareDetails = NSLocalizedString( |
| 146 | + "share details", |
| 147 | + comment: "The action to be agreed upon when tapping the Connect Jetpack button on the Jetpack setup required screen." |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + enum Links { |
| 152 | + static let jetpackTerms = "https://jetpack.com/redirect/?source=wpcom-tos&site=" |
| 153 | + static let jetpackShareDetails = "https://jetpack.com/redirect/?source=jetpack-support-what-data-does-jetpack-sync&site=" |
| 154 | + } |
| 155 | +} |
0 commit comments