From 78a33b00d3fb91cc37c3eaeb4a8e1f158431d447 Mon Sep 17 00:00:00 2001 From: Juan Manuel Pereira Date: Mon, 19 Feb 2024 10:52:19 -0300 Subject: [PATCH] Add initial loading indicator when loading web UI --- .../DataBrokerProtectionViewController.swift | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift index accf119bb7..2eeffe9661 100644 --- a/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift +++ b/LocalPackages/DataBrokerProtection/Sources/DataBrokerProtection/UI/DataBrokerProtectionViewController.swift @@ -26,12 +26,17 @@ final public class DataBrokerProtectionViewController: NSViewController { private let dataManager: DataBrokerProtectionDataManaging private let scheduler: DataBrokerProtectionScheduler private var webView: WKWebView? + private var loader: NSProgressIndicator! private let webUISettings: DataBrokerProtectionWebUIURLSettingsRepresentable private let webUIViewModel: DBPUIViewModel private let openURLHandler: (URL?) -> Void private var reloadObserver: NSObjectProtocol? + private enum Consts { + static let loaderDimension: CGFloat = 50.0 + } + public init(scheduler: DataBrokerProtectionScheduler, dataManager: DataBrokerProtectionDataManaging, privacyConfig: PrivacyConfigurationManaging? = nil, @@ -63,9 +68,10 @@ final public class DataBrokerProtectionViewController: NSViewController { public override func viewDidLoad() { super.viewDidLoad() + addLoadingIndicator() reloadObserver = NotificationCenter.default.addObserver(forName: DataBrokerProtectionNotifications.shouldReloadUI, - object: nil, - queue: .main) { [weak self] _ in + object: nil, + queue: .main) { [weak self] _ in self?.webView?.reload() } } @@ -75,16 +81,40 @@ final public class DataBrokerProtectionViewController: NSViewController { webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 1024, height: 768), configuration: configuration) webView?.uiDelegate = self + webView?.navigationDelegate = self view = webView! if let url = URL(string: webUISettings.selectedURL) { webView?.load(url) } else { + removeLoadingIndicator() assertionFailure("Selected URL is not valid \(webUISettings.selectedURL)") } } + private func addLoadingIndicator() { + loader = NSProgressIndicator() + loader.wantsLayer = true + loader.style = .spinning + loader.controlSize = .regular + loader.sizeToFit() + loader.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(loader) + + NSLayoutConstraint.activate([ + loader.centerXAnchor.constraint(equalTo: view.centerXAnchor), + loader.centerYAnchor.constraint(equalTo: view.centerYAnchor), + loader.widthAnchor.constraint(equalToConstant: Consts.loaderDimension), + loader.heightAnchor.constraint(equalToConstant: Consts.loaderDimension) + ]) + } + + private func removeLoadingIndicator() { + loader.stopAnimation(nil) + loader.removeFromSuperview() + } + deinit { if let reloadObserver { NotificationCenter.default.removeObserver(reloadObserver) @@ -98,3 +128,14 @@ extension DataBrokerProtectionViewController: WKUIDelegate { return nil } } + +extension DataBrokerProtectionViewController: WKNavigationDelegate { + + public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { + loader.startAnimation(nil) + } + + public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { + removeLoadingIndicator() + } +}