From 50b3057b5c5f6123f748bf156f5ad0ecba1e6010 Mon Sep 17 00:00:00 2001 From: mattreaganmozilla <145381717+mattreaganmozilla@users.noreply.github.com> Date: Tue, 9 Jan 2024 06:37:40 -0800 Subject: [PATCH] Bugfix FXIOS-8105 [v121] Fix Send to Device failing intermittently (#18042) * [8105] Fix issue where Send to Device could fail because RustFXAccounts startup flow not yet completed * [8105] Additional work to fix issue with Send to Device sometimes failing, be sure we check for nil account manager not hasAccount(), the latter will be false when users are signed out * [8105] Cleanup, remove unnecessary code and explicit self references * [8105] Additional cleanup, restore self references where needed to avoid local variable shadowing (cherry picked from commit 86ad3adbe83040568bf39567e33fbd8e4797c8b6) --- Extensions/ShareTo/ShareViewController.swift | 61 +++++++++++++++----- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/Extensions/ShareTo/ShareViewController.swift b/Extensions/ShareTo/ShareViewController.swift index dae48800704d..77039b31151f 100644 --- a/Extensions/ShareTo/ShareViewController.swift +++ b/Extensions/ShareTo/ShareViewController.swift @@ -68,6 +68,7 @@ class ShareViewController: UIViewController { var shareItem: ExtensionUtils.ExtractedShareItem? private var viewsShownDuringDoneAnimation = [UIView]() private var stackView: UIStackView! + private var spinner: UIActivityIndicatorView? private var actionDoneRow: (row: UIStackView, label: UILabel)! private var sendToDevice: SendToDevice? private var pageInfoHeight: NSLayoutConstraint? @@ -93,6 +94,25 @@ class ShareViewController: UIViewController { setupNavBar() setupStackView() + + if RustFirefoxAccounts.shared.accountManager == nil { + // Show brief spinner in UI while startup is finishing + showProgressIndicator() + + let profile = BrowserProfile(localName: "profile") + Viaduct.shared.useReqwestBackend() + RustFirefoxAccounts.startup(prefs: profile.prefs) { [weak self] _ in + // Hide spinner and finish UI setup (Note: this completion + // block is currently guaranteed to arrive on main thread.) + self?.hideProgressIndicator() + self?.finalizeUISetup() + } + } else { + finalizeUISetup() + } + } + + private func finalizeUISetup() { setupRows() guard let shareItem = shareItem else { return } @@ -104,10 +124,6 @@ class ShareViewController: UIViewController { case .rawText(let text): self.pageInfoRowTitleLabel?.text = text.quoted } - - let profile = BrowserProfile(localName: "profile") - Viaduct.shared.useReqwestBackend() - RustFirefoxAccounts.startup(prefs: profile.prefs) { _ in } } private func setupRows() { @@ -320,6 +336,27 @@ class ShareViewController: UIViewController { stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor) ]) } + + private func showProgressIndicator() { + let indicator = UIActivityIndicatorView(style: .large) + let defaultSize = CGSize(width: 40.0, height: 40.0) + view.addSubview(indicator) + indicator.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + indicator.centerXAnchor.constraint(equalTo: view.centerXAnchor), + indicator.centerYAnchor.constraint(equalTo: view.centerYAnchor), + indicator.widthAnchor.constraint(equalToConstant: defaultSize.width), + indicator.heightAnchor.constraint(equalToConstant: defaultSize.height), + ]) + indicator.startAnimating() + spinner = indicator + } + + private func hideProgressIndicator() { + spinner?.stopAnimating() + spinner?.removeFromSuperview() + spinner = nil + } } extension ShareViewController { @@ -385,16 +422,12 @@ extension ShareViewController { guard let shareItem = shareItem, case .shareItem(let item) = shareItem else { return } gesture.isEnabled = false - view.isUserInteractionEnabled = false - if RustFirefoxAccounts.shared.accountManager != nil { - self.view.isUserInteractionEnabled = true - self.sendToDevice = SendToDevice() - guard let sendToDevice = self.sendToDevice else { return } - sendToDevice.sharedItem = item - sendToDevice.delegate = self.delegate - let vc = sendToDevice.initialViewController() - self.navigationController?.pushViewController(vc, animated: true) - } + self.sendToDevice = SendToDevice() + guard let sendToDevice = self.sendToDevice else { return } + sendToDevice.sharedItem = item + sendToDevice.delegate = self.delegate + let vc = sendToDevice.initialViewController() + navigationController?.pushViewController(vc, animated: true) } func openFirefox(withUrl url: String, isSearch: Bool) {