|
| 1 | +import Combine |
| 2 | +import Foundation |
| 3 | +import Yosemite |
| 4 | +import WordPressAuthenticator |
| 5 | +import class Networking.WordPressOrgNetwork |
| 6 | + |
| 7 | +final class JetpackConnectionErrorViewModel: ULErrorViewModel { |
| 8 | + private let siteURL: String |
| 9 | + private var jetpackConnectionURL: URL? |
| 10 | + private let stores: StoresManager |
| 11 | + private let isPrimaryButtonLoadingSubject = CurrentValueSubject<Bool, Never>(false) |
| 12 | + private let jetpackSetupCompletionHandler: (String) -> Void |
| 13 | + |
| 14 | + init(siteURL: String, |
| 15 | + credentials: WordPressOrgCredentials, |
| 16 | + stores: StoresManager = ServiceLocator.stores, |
| 17 | + onJetpackSetupCompletion: @escaping (String) -> Void) { |
| 18 | + self.siteURL = siteURL |
| 19 | + self.stores = stores |
| 20 | + self.jetpackSetupCompletionHandler = onJetpackSetupCompletion |
| 21 | + authenticate(with: credentials) |
| 22 | + fetchJetpackConnectionURL() |
| 23 | + } |
| 24 | + |
| 25 | + // MARK: - Data and configuration |
| 26 | + |
| 27 | + let image: UIImage = .productErrorImage |
| 28 | + |
| 29 | + var text: NSAttributedString { |
| 30 | + let font: UIFont = .body |
| 31 | + let boldFont: UIFont = font.bold |
| 32 | + |
| 33 | + let boldSiteAddress = NSAttributedString(string: siteURL.trimHTTPScheme(), |
| 34 | + attributes: [.font: boldFont]) |
| 35 | + let attributedString = NSMutableAttributedString(string: Localization.noJetpackEmail) |
| 36 | + attributedString.replaceFirstOccurrence(of: "%@", with: boldSiteAddress) |
| 37 | + |
| 38 | + return attributedString |
| 39 | + } |
| 40 | + |
| 41 | + let isAuxiliaryButtonHidden = true |
| 42 | + |
| 43 | + let auxiliaryButtonTitle = "" |
| 44 | + |
| 45 | + let primaryButtonTitle = Localization.primaryButtonTitle |
| 46 | + |
| 47 | + var isPrimaryButtonLoading: AnyPublisher<Bool, Never> { |
| 48 | + isPrimaryButtonLoadingSubject.eraseToAnyPublisher() |
| 49 | + } |
| 50 | + |
| 51 | + let secondaryButtonTitle = Localization.secondaryButtonTitle |
| 52 | + |
| 53 | + func viewDidLoad(_ viewController: UIViewController?) { |
| 54 | + // TODO: Tracks? |
| 55 | + } |
| 56 | + |
| 57 | + func didTapPrimaryButton(in viewController: UIViewController?) { |
| 58 | + showJetpackConnectionWebView(from: viewController) |
| 59 | + } |
| 60 | + |
| 61 | + func didTapSecondaryButton(in viewController: UIViewController?) { |
| 62 | + viewController?.navigationController?.popToRootViewController(animated: true) |
| 63 | + } |
| 64 | + |
| 65 | + func didTapAuxiliaryButton(in viewController: UIViewController?) { |
| 66 | + // no-op |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// MARK: - Private helpers |
| 71 | +private extension JetpackConnectionErrorViewModel { |
| 72 | + /// Presents a web view pointing to the Jetpack connection URL. |
| 73 | + /// |
| 74 | + func showJetpackConnectionWebView(from viewController: UIViewController?) { |
| 75 | + guard let viewController = viewController else { |
| 76 | + return |
| 77 | + } |
| 78 | + guard let url = jetpackConnectionURL else { |
| 79 | + DDLogWarn("⚠️ No Jetpack connection URL found") |
| 80 | + return |
| 81 | + } |
| 82 | + let viewModel = JetpackConnectionWebViewModel(initialURL: url, siteURL: siteURL, completion: { [weak self] in |
| 83 | + self?.fetchJetpackUser(in: viewController) |
| 84 | + }) |
| 85 | + let pluginViewController = AuthenticatedWebViewController(viewModel: viewModel) |
| 86 | + viewController.navigationController?.show(pluginViewController, sender: nil) |
| 87 | + } |
| 88 | + |
| 89 | + /// Prepares `JetpackConnectionStore` to authenticate subsequent requests to WP.org API. |
| 90 | + /// |
| 91 | + func authenticate(with credentials: WordPressOrgCredentials) { |
| 92 | + guard let authenticator = credentials.makeCookieNonceAuthenticator() else { |
| 93 | + return |
| 94 | + } |
| 95 | + let network = WordPressOrgNetwork(authenticator: authenticator) |
| 96 | + let action = JetpackConnectionAction.authenticate(siteURL: siteURL, network: network) |
| 97 | + stores.dispatch(action) |
| 98 | + } |
| 99 | + |
| 100 | + /// Fetches the URL for handling Jetpack connection in a web view |
| 101 | + /// |
| 102 | + func fetchJetpackConnectionURL() { |
| 103 | + isPrimaryButtonLoadingSubject.send(true) |
| 104 | + let action = JetpackConnectionAction.fetchJetpackConnectionURL { [weak self] result in |
| 105 | + guard let self = self else { return } |
| 106 | + self.isPrimaryButtonLoadingSubject.send(false) |
| 107 | + switch result { |
| 108 | + case .success(let url): |
| 109 | + self.jetpackConnectionURL = url |
| 110 | + case .failure(let error): |
| 111 | + DDLogWarn("⚠️ Error fetching Jetpack connection URL: \(error)") |
| 112 | + } |
| 113 | + } |
| 114 | + stores.dispatch(action) |
| 115 | + } |
| 116 | + |
| 117 | + /// Gets the connected WP.com email address if possible, or show error otherwise. |
| 118 | + /// |
| 119 | + func fetchJetpackUser(in viewController: UIViewController) { |
| 120 | + showInProgressView(in: viewController) |
| 121 | + let action = JetpackConnectionAction.fetchJetpackUser { [weak self] result in |
| 122 | + guard let self = self else { return } |
| 123 | + // dismisses the in-progress view |
| 124 | + viewController.navigationController?.dismiss(animated: true) |
| 125 | + |
| 126 | + switch result { |
| 127 | + case .success(let user): |
| 128 | + guard let emailAddress = user.wpcomUser?.email else { |
| 129 | + DDLogWarn("⚠️ Cannot find connected WPcom user") |
| 130 | + return self.showSetupErrorNotice(in: viewController) |
| 131 | + } |
| 132 | + self.jetpackSetupCompletionHandler(emailAddress) |
| 133 | + case .failure(let error): |
| 134 | + DDLogWarn("⚠️ Error fetching Jetpack user: \(error)") |
| 135 | + self.showSetupErrorNotice(in: viewController) |
| 136 | + } |
| 137 | + } |
| 138 | + stores.dispatch(action) |
| 139 | + } |
| 140 | + |
| 141 | + func showInProgressView(in viewController: UIViewController) { |
| 142 | + let viewProperties = InProgressViewProperties(title: Localization.inProgressMessage, message: "") |
| 143 | + let inProgressViewController = InProgressViewController(viewProperties: viewProperties) |
| 144 | + inProgressViewController.modalPresentationStyle = .overCurrentContext |
| 145 | + |
| 146 | + viewController.navigationController?.present(inProgressViewController, animated: true, completion: nil) |
| 147 | + } |
| 148 | + |
| 149 | + func showSetupErrorNotice(in viewController: UIViewController) { |
| 150 | + let message = Localization.setupErrorMessage |
| 151 | + let notice = Notice(title: message, feedbackType: .error) |
| 152 | + let noticePresenter = DefaultNoticePresenter() |
| 153 | + noticePresenter.presentingViewController = viewController |
| 154 | + noticePresenter.enqueue(notice: notice) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +private extension JetpackConnectionErrorViewModel { |
| 159 | + enum Localization { |
| 160 | + static let noJetpackEmail = NSLocalizedString( |
| 161 | + "It looks like your account is not connected to %@'s Jetpack", |
| 162 | + comment: "Message explaining that the entered site credentials belong to an account that is not connected to the site's Jetpack. " |
| 163 | + + "Reads like 'It looks like your account is not connected to awebsite.com's Jetpack") |
| 164 | + |
| 165 | + static let primaryButtonTitle = NSLocalizedString( |
| 166 | + "Connect Jetpack to your account", |
| 167 | + comment: "Button linking to web view for setting up Jetpack connection. " + |
| 168 | + "Presented when logging in with store credentials of an account not connected to the site's Jetpack") |
| 169 | + |
| 170 | + static let secondaryButtonTitle = NSLocalizedString( |
| 171 | + "Log In With Another Account", |
| 172 | + comment: "Action button that will restart the login flow." + |
| 173 | + "Presented when logging in with store credentials of an account not connected to the site's Jetpack" |
| 174 | + ) |
| 175 | + |
| 176 | + static let inProgressMessage = NSLocalizedString( |
| 177 | + "Verifying Jetpack connection...", |
| 178 | + comment: "Message displayed when checking whether Jetpack has been connected successfully" |
| 179 | + ) |
| 180 | + |
| 181 | + static let setupErrorMessage = NSLocalizedString( |
| 182 | + "Cannot verify your Jetpack connection. Please try again.", |
| 183 | + comment: "Error message displayed when failed to check for Jetpack connection." |
| 184 | + ) |
| 185 | + } |
| 186 | +} |
0 commit comments