Skip to content

Commit

Permalink
Add initial loading indicator when loading web UI
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaemepereira committed Feb 19, 2024
1 parent 2778f1f commit 78a33b0
Showing 1 changed file with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
}
}
Expand All @@ -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)
Expand All @@ -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()
}
}

0 comments on commit 78a33b0

Please sign in to comment.