fix: restore iOS navigation with unsaved forms
This commit is contained in:
@@ -43,6 +43,7 @@ final class WorkspaceViewController: UIViewController, WKNavigationDelegate, WKU
|
||||
private var settingsAvailable = false
|
||||
private var toolbarHeight: NSLayoutConstraint!
|
||||
private var appearance: UISegmentedControl!
|
||||
private var javaScriptConfirmation: (alert: UIAlertController, completion: (Bool) -> Void)?
|
||||
|
||||
init(defaults: UserDefaults = .standard) {
|
||||
self.defaults = defaults
|
||||
@@ -51,6 +52,8 @@ final class WorkspaceViewController: UIViewController, WKNavigationDelegate, WKU
|
||||
|
||||
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
||||
|
||||
deinit { javaScriptConfirmation?.completion(false) }
|
||||
|
||||
private func text(_ key: String) -> String {
|
||||
NSLocalizedString(key, comment: "KanbanGit iOS workspace")
|
||||
}
|
||||
@@ -199,6 +202,7 @@ final class WorkspaceViewController: UIViewController, WKNavigationDelegate, WKU
|
||||
|
||||
func open(_ url: URL) {
|
||||
if server?.url == url, connection.ready { showWorkspace(); return }
|
||||
completeJavaScriptConfirmation(false)
|
||||
let selected = WorkspaceServer.selecting(url, current: server)
|
||||
guard let data = try? JSONEncoder().encode(selected) else { status.text = text("storageError"); return }
|
||||
defaults.set(data, forKey: serverKey)
|
||||
@@ -265,6 +269,7 @@ final class WorkspaceViewController: UIViewController, WKNavigationDelegate, WKU
|
||||
}
|
||||
|
||||
@objc private func showSettings() {
|
||||
completeJavaScriptConfirmation(false)
|
||||
loading.stopAnimating()
|
||||
view.endEditing(true)
|
||||
launcher.isHidden = false
|
||||
@@ -320,6 +325,39 @@ final class WorkspaceViewController: UIViewController, WKNavigationDelegate, WKU
|
||||
}
|
||||
}
|
||||
|
||||
func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String,
|
||||
initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
|
||||
guard webView === workspace, viewIfLoaded?.window != nil,
|
||||
presentedViewController == nil, javaScriptConfirmation == nil else {
|
||||
completionHandler(false)
|
||||
return
|
||||
}
|
||||
let alert = UIAlertController(title: frame.request.url?.host ?? text("appName"),
|
||||
message: message, preferredStyle: .alert)
|
||||
let cancel = UIAlertAction(title: text("cancel"), style: .cancel) { [weak self] _ in
|
||||
self?.completeJavaScriptConfirmation(false)
|
||||
}
|
||||
alert.addAction(cancel)
|
||||
alert.addAction(UIAlertAction(title: text("ok"), style: .default) { [weak self] _ in
|
||||
self?.completeJavaScriptConfirmation(true)
|
||||
})
|
||||
alert.preferredAction = cancel
|
||||
javaScriptConfirmation = (alert, completionHandler)
|
||||
present(alert, animated: true)
|
||||
}
|
||||
|
||||
func completeJavaScriptConfirmation(_ accepted: Bool) {
|
||||
guard let confirmation = javaScriptConfirmation else { return }
|
||||
javaScriptConfirmation = nil
|
||||
if confirmation.alert.presentingViewController == nil {
|
||||
confirmation.completion(accepted)
|
||||
} else {
|
||||
confirmation.alert.dismiss(animated: false) {
|
||||
confirmation.completion(accepted)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func webView(_ webView: WKWebView, decidePolicyFor action: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
|
||||
guard webView === workspace, let url = action.request.url else { decisionHandler(.cancel); return }
|
||||
guard server?.allows(url) == true else {
|
||||
|
||||
Reference in New Issue
Block a user