Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBP: Add initial loading indicator when loading web UI #2227

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ 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

Expand Down Expand Up @@ -63,9 +64,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 +77,39 @@ 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
loader.controlSize = .large
view.addSubview(loader)

NSLayoutConstraint.activate([
loader.centerXAnchor.constraint(equalTo: view.centerXAnchor),
loader.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
}

private func removeLoadingIndicator() {
loader.stopAnimation(nil)
loader.removeFromSuperview()
}

deinit {
if let reloadObserver {
NotificationCenter.default.removeObserver(reloadObserver)
Expand All @@ -98,3 +123,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()
}
}
Loading