Skip to content

Commit 26e730f

Browse files
authored
Merge pull request #10 from SillyLittleTech/codex/fix-swift-code-issues-from-deepsource-review
Fix DeepSource Swift safety warnings in ViewController
2 parents dec346d + 746edd7 commit 26e730f

1 file changed

Lines changed: 34 additions & 5 deletions

File tree

mos/Flean/ViewController.swift

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,41 @@ class ViewController: NSViewController, WKNavigationDelegate, WKScriptMessageHan
2222

2323
self.webView.configuration.userContentController.add(self, name: "controller")
2424

25-
self.webView.loadFileURL(
26-
Bundle.main.url(forResource: "Main", withExtension: "html")!,
27-
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: nil)
53+
return
54+
}
55+
56+
self.webView.loadFileURL(mainPageURL, allowingReadAccessTo: resourceURL)
2857
}
2958

30-
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
59+
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation?) {
3160
SFSafariExtensionManager.getStateOfSafariExtension(withIdentifier: extensionBundleIdentifier) {
3261
(state, error) in
3362
guard let state = state, error == nil else {
@@ -48,7 +77,7 @@ class ViewController: NSViewController, WKNavigationDelegate, WKScriptMessageHan
4877
func userContentController(
4978
_ userContentController: WKUserContentController, didReceive message: WKScriptMessage
5079
) {
51-
if message.body as! String != "open-preferences" {
80+
guard let action = message.body as? String, action == "open-preferences" else {
5281
return
5382
}
5483

0 commit comments

Comments
 (0)