Skip to content

Commit c3467e8

Browse files
authored
Merge pull request #3 from SillyLittleTech/deepsource-autofix-41753c47
refactor: replace unused parameters in closure with underscore
2 parents a6d0a2e + 0679402 commit c3467e8

1 file changed

Lines changed: 64 additions & 29 deletions

File tree

mos/Flean/ViewController.swift

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,80 @@ let extensionBundleIdentifier = "slf.Flean.Extension"
1313

1414
class ViewController: NSViewController, WKNavigationDelegate, WKScriptMessageHandler {
1515

16-
@IBOutlet var webView: WKWebView!
16+
@IBOutlet var webView: WKWebView!
1717

18-
override func viewDidLoad() {
19-
super.viewDidLoad()
18+
override func viewDidLoad() {
19+
super.viewDidLoad()
2020

21-
self.webView.navigationDelegate = self
21+
self.webView.navigationDelegate = self
2222

23-
self.webView.configuration.userContentController.add(self, name: "controller")
23+
self.webView.configuration.userContentController.add(self, name: "controller")
2424

25-
self.webView.loadFileURL(Bundle.main.url(forResource: "Main", withExtension: "html")!, allowingReadAccessTo: Bundle.main.resourceURL!)
25+
guard
26+
let mainPageURL = Bundle.main.url(forResource: "Main", withExtension: "html"),
27+
let resourceURL = Bundle.main.resourceURL
28+
else {
29+
let message = "Failed to resolve bundled web resources."
30+
#if DEBUG
31+
assertionFailure(message)
32+
#endif
33+
NSLog("%@", message)
34+
let fallbackHTML = """
35+
<!doctype html>
36+
<html lang="en">
37+
<head>
38+
<meta charset="utf-8">
39+
<title>Error</title>
40+
<style>
41+
body { font-family: -apple-system, system-ui, sans-serif; margin: 2rem; }
42+
h1 { font-size: 1.6rem; margin-bottom: 0.5rem; }
43+
p { color: #555; }
44+
</style>
45+
</head>
46+
<body>
47+
<h1>Unable to load content</h1>
48+
<p>The app could not find its bundled web resources. Please reinstall the app or contact support.</p>
49+
</body>
50+
</html>
51+
"""
52+
self.webView.loadHTMLString(fallbackHTML, baseURL: Bundle.main.bundleURL)
53+
return
2654
}
2755

28-
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
29-
SFSafariExtensionManager.getStateOfSafariExtension(withIdentifier: extensionBundleIdentifier) { (state, error) in
30-
guard let state = state, error == nil else {
31-
// Insert code to inform the user that something went wrong.
32-
return
33-
}
34-
35-
DispatchQueue.main.async {
36-
if #available(macOS 13, *) {
37-
webView.evaluateJavaScript("show(\(state.isEnabled), true)")
38-
} else {
39-
webView.evaluateJavaScript("show(\(state.isEnabled), false)")
40-
}
41-
}
56+
self.webView.loadFileURL(mainPageURL, allowingReadAccessTo: resourceURL)
57+
}
58+
59+
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation?) {
60+
SFSafariExtensionManager.getStateOfSafariExtension(withIdentifier: extensionBundleIdentifier) {
61+
(state, error) in
62+
guard let state = state, error == nil else {
63+
// Insert code to inform the user that something went wrong.
64+
return
65+
}
66+
67+
DispatchQueue.main.async {
68+
if #available(macOS 13, *) {
69+
webView.evaluateJavaScript("show(\(state.isEnabled), true)")
70+
} else {
71+
webView.evaluateJavaScript("show(\(state.isEnabled), false)")
4272
}
73+
}
4374
}
75+
}
4476

45-
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
46-
if (message.body as! String != "open-preferences") {
47-
return;
48-
}
77+
func userContentController(
78+
_ userContentController: WKUserContentController, didReceive message: WKScriptMessage
79+
) {
80+
guard let action = message.body as? String, action == "open-preferences" else {
81+
return
82+
}
4983

50-
SFSafariApplication.showPreferencesForExtension(withIdentifier: extensionBundleIdentifier) { error in
51-
DispatchQueue.main.async {
52-
NSApplication.shared.terminate(nil)
53-
}
54-
}
84+
SFSafariApplication.showPreferencesForExtension(withIdentifier: extensionBundleIdentifier) {
85+
_ in
86+
DispatchQueue.main.async {
87+
NSApplication.shared.terminate(nil)
88+
}
5589
}
90+
}
5691

5792
}

0 commit comments

Comments
 (0)