Skip to content

Commit c36df15

Browse files
style: format code with PHP CS Fixer, StandardJS and swift-format
This commit fixes the style issues introduced in f654093 according to the output from PHP CS Fixer, StandardJS and swift-format. Details: #22
1 parent f654093 commit c36df15

1 file changed

Lines changed: 138 additions & 128 deletions

File tree

Lines changed: 138 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,171 @@
11
// iOS-only web view container. Guard compilation so macOS builds don't attempt to
22
// compile UIKit/SwiftUI iOS-only APIs.
33
#if os(iOS)
4-
import SwiftUI
5-
import WebKit
6-
import UIKit
4+
import SwiftUI
5+
import WebKit
6+
import UIKit
77

8-
struct WebViewContainer: UIViewRepresentable {
8+
struct WebViewContainer: UIViewRepresentable {
99
func makeUIView(context: Context) -> WKWebView {
10-
let webCfg = WKWebViewConfiguration()
11-
let web = WKWebView(frame: .zero, configuration: webCfg)
10+
let webCfg = WKWebViewConfiguration()
11+
let web = WKWebView(frame: .zero, configuration: webCfg)
1212

13-
// Add script message handler compatible with the macOS app's controller name
14-
web.configuration.userContentController.add(context.coordinator, name: "controller")
15-
web.navigationDelegate = context.coordinator
13+
// Add script message handler compatible with the macOS app's controller name
14+
web.configuration.userContentController.add(context.coordinator, name: "controller")
15+
web.navigationDelegate = context.coordinator
1616

17-
// Give the coordinator a reference to the web view so it can inject
18-
// settings back into the page when requested.
19-
context.coordinator.webView = web
17+
// Give the coordinator a reference to the web view so it can inject
18+
// settings back into the page when requested.
19+
context.coordinator.webView = web
2020

21-
// Load bundled Main.html from the app bundle resources
22-
if let url = Bundle.main.url(forResource: "Main", withExtension: "html", subdirectory: "Base.lproj") {
23-
web.loadFileURL(url, allowingReadAccessTo: Bundle.main.resourceURL!)
24-
}
21+
// Load bundled Main.html from the app bundle resources
22+
if let url = Bundle.main.url(
23+
forResource: "Main", withExtension: "html", subdirectory: "Base.lproj")
24+
{
25+
web.loadFileURL(url, allowingReadAccessTo: Bundle.main.resourceURL!)
26+
}
2527

26-
return web
28+
return web
2729
}
2830

2931
func updateUIView(_ uiView: WKWebView, context: Context) {}
3032

3133
func makeCoordinator() -> Coordinator { Coordinator() }
3234

3335
class Coordinator: NSObject, WKScriptMessageHandler, WKNavigationDelegate {
34-
// Weak reference to avoid retain cycles
35-
weak var webView: WKWebView?
36-
37-
override init() {
38-
super.init()
39-
NotificationCenter.default.addObserver(self, selector: #selector(settingsChanged(_:)), name: Notification.Name("FleanSettingsDidChange"), object: nil)
36+
// Weak reference to avoid retain cycles
37+
weak var webView: WKWebView?
38+
39+
override init() {
40+
super.init()
41+
NotificationCenter.default.addObserver(
42+
self, selector: #selector(settingsChanged(_:)),
43+
name: Notification.Name("FleanSettingsDidChange"), object: nil)
44+
}
45+
46+
deinit {
47+
NotificationCenter.default.removeObserver(self)
48+
}
49+
50+
// MARK: - Settings storage in app container
51+
private func settingsFileURL() -> URL? {
52+
let fm = FileManager.default
53+
do {
54+
let appSupport = try fm.url(
55+
for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil,
56+
create: true)
57+
let dir = appSupport.appendingPathComponent("Flean", isDirectory: true)
58+
if !fm.fileExists(atPath: dir.path) {
59+
try fm.createDirectory(at: dir, withIntermediateDirectories: true, attributes: nil)
60+
}
61+
return dir.appendingPathComponent("settings.json")
62+
} catch {
63+
NSLog("Flean: failed to get application support directory: %s", String(describing: error))
64+
return nil
4065
}
66+
}
4167

42-
deinit {
43-
NotificationCenter.default.removeObserver(self)
44-
}
45-
46-
// MARK: - Settings storage in app container
47-
private func settingsFileURL() -> URL? {
48-
let fm = FileManager.default
49-
do {
50-
let appSupport = try fm.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
51-
let dir = appSupport.appendingPathComponent("Flean", isDirectory: true)
52-
if !fm.fileExists(atPath: dir.path) {
53-
try fm.createDirectory(at: dir, withIntermediateDirectories: true, attributes: nil)
54-
}
55-
return dir.appendingPathComponent("settings.json")
56-
} catch {
57-
NSLog("Flean: failed to get application support directory: %s", String(describing: error))
58-
return nil
59-
}
68+
private func loadSettings() -> [String: Any] {
69+
guard let url = settingsFileURL(), FileManager.default.fileExists(atPath: url.path) else {
70+
return [:]
6071
}
61-
62-
private func loadSettings() -> [String: Any] {
63-
guard let url = settingsFileURL(), FileManager.default.fileExists(atPath: url.path) else { return [:] }
64-
do {
65-
let data = try Data(contentsOf: url)
66-
if let obj = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
67-
return obj
68-
}
69-
} catch {
70-
NSLog("Flean: failed to load settings: %s", String(describing: error))
71-
}
72-
return [:]
72+
do {
73+
let data = try Data(contentsOf: url)
74+
if let obj = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] {
75+
return obj
76+
}
77+
} catch {
78+
NSLog("Flean: failed to load settings: %s", String(describing: error))
7379
}
74-
75-
private func saveSettings(_ dict: [String: Any]) -> Bool {
76-
guard let url = settingsFileURL() else { return false }
77-
do {
78-
let data = try JSONSerialization.data(withJSONObject: dict, options: [.prettyPrinted])
79-
try data.write(to: url, options: [.atomic])
80-
return true
81-
} catch {
82-
NSLog("Flean: failed to save settings: %s", String(describing: error))
83-
return false
84-
}
80+
return [:]
81+
}
82+
83+
private func saveSettings(_ dict: [String: Any]) -> Bool {
84+
guard let url = settingsFileURL() else { return false }
85+
do {
86+
let data = try JSONSerialization.data(withJSONObject: dict, options: [.prettyPrinted])
87+
try data.write(to: url, options: [.atomic])
88+
return true
89+
} catch {
90+
NSLog("Flean: failed to save settings: %s", String(describing: error))
91+
return false
8592
}
86-
87-
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
88-
// Expect either a string command or a dictionary with { action: "getSettings" | "setSettings", data: {...} }
89-
if let bodyStr = message.body as? String {
90-
if bodyStr == "open-preferences" {
91-
NSLog("Flean: open-preferences requested (iOS) — manual enable in Settings required")
92-
return
93-
}
94-
// legacy: handle simple 'get-settings' / 'set-settings' encoded strings
95-
if bodyStr == "get-settings" {
96-
sendSettingsToPage()
97-
return
98-
}
99-
}
100-
101-
if let body = message.body as? [String: Any], let action = body["action"] as? String {
102-
switch action {
103-
case "getSettings":
104-
sendSettingsToPage()
105-
case "setSettings":
106-
if let data = body["data"] as? [String: Any] {
107-
let ok = saveSettings(data)
108-
// Acknowledge back to the page
109-
let ack = ["action": "setSettingsAck", "success": ok] as [String : Any]
110-
sendJSONToPage(ack)
111-
}
112-
default:
113-
break
114-
}
115-
}
93+
}
94+
95+
func userContentController(
96+
_ userContentController: WKUserContentController, didReceive message: WKScriptMessage
97+
) {
98+
// Expect either a string command or a dictionary with { action: "getSettings" | "setSettings", data: {...} }
99+
if let bodyStr = message.body as? String {
100+
if bodyStr == "open-preferences" {
101+
NSLog("Flean: open-preferences requested (iOS) — manual enable in Settings required")
102+
return
103+
}
104+
// legacy: handle simple 'get-settings' / 'set-settings' encoded strings
105+
if bodyStr == "get-settings" {
106+
sendSettingsToPage()
107+
return
108+
}
116109
}
117110

118-
private func sendJSONToPage(_ obj: Any) {
119-
guard let web = webView else { return }
120-
do {
121-
let data = try JSONSerialization.data(withJSONObject: obj, options: [])
122-
if let json = String(data: data, encoding: .utf8) {
123-
// We dispatch a CustomEvent 'flean:message' with the JSON as detail
124-
let safeJSON = json.replacingOccurrences(of: "\\\"", with: "\\\\\"")
125-
let js = "window.dispatchEvent(new CustomEvent('flean:message', { detail: \(json) }));"
126-
web.evaluateJavaScript(js, completionHandler: nil)
127-
}
128-
} catch {
129-
NSLog("Flean: failed to serialize message to page: %s", String(describing: error))
111+
if let body = message.body as? [String: Any], let action = body["action"] as? String {
112+
switch action {
113+
case "getSettings":
114+
sendSettingsToPage()
115+
case "setSettings":
116+
if let data = body["data"] as? [String: Any] {
117+
let ok = saveSettings(data)
118+
// Acknowledge back to the page
119+
let ack = ["action": "setSettingsAck", "success": ok] as [String: Any]
120+
sendJSONToPage(ack)
130121
}
122+
default:
123+
break
124+
}
131125
}
132-
133-
private func sendSettingsToPage() {
134-
let settings = loadSettings()
135-
sendJSONToPage(["action": "settings", "data": settings])
136-
}
137-
138-
@objc private func settingsChanged(_ note: Notification) {
139-
// Push updated settings into the web view when the SettingsStore saves.
140-
sendSettingsToPage()
126+
}
127+
128+
private func sendJSONToPage(_ obj: Any) {
129+
guard let web = webView else { return }
130+
do {
131+
let data = try JSONSerialization.data(withJSONObject: obj, options: [])
132+
if let json = String(data: data, encoding: .utf8) {
133+
// We dispatch a CustomEvent 'flean:message' with the JSON as detail
134+
let safeJSON = json.replacingOccurrences(of: "\\\"", with: "\\\\\"")
135+
let js = "window.dispatchEvent(new CustomEvent('flean:message', { detail: \(json) }));"
136+
web.evaluateJavaScript(js, completionHandler: nil)
137+
}
138+
} catch {
139+
NSLog("Flean: failed to serialize message to page: %s", String(describing: error))
141140
}
141+
}
142+
143+
private func sendSettingsToPage() {
144+
let settings = loadSettings()
145+
sendJSONToPage(["action": "settings", "data": settings])
146+
}
147+
148+
@objc private func settingsChanged(_ note: Notification) {
149+
// Push updated settings into the web view when the SettingsStore saves.
150+
sendSettingsToPage()
151+
}
152+
153+
// MARK: - WKNavigationDelegate
154+
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
155+
// Update the UI to use iOS-appropriate text. Extension state cannot be
156+
// queried programmatically on iOS (no SFSafariExtensionManager equivalent),
157+
// so we pass null for the enabled state (shows "unknown") and true for
158+
// useSettingsInsteadOfPreferences so the correct iOS wording is displayed.
159+
webView.evaluateJavaScript("show(null, true)", completionHandler: nil)
160+
}
142161

143-
// MARK: - WKNavigationDelegate
144-
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
145-
// Update the UI to use iOS-appropriate text. Extension state cannot be
146-
// queried programmatically on iOS (no SFSafariExtensionManager equivalent),
147-
// so we pass null for the enabled state (shows "unknown") and true for
148-
// useSettingsInsteadOfPreferences so the correct iOS wording is displayed.
149-
webView.evaluateJavaScript("show(null, true)", completionHandler: nil)
150-
}
151-
152162
}
153-
}
163+
}
154164

155-
struct WebViewContainer_Previews: PreviewProvider {
165+
struct WebViewContainer_Previews: PreviewProvider {
156166
static var previews: some View {
157-
WebViewContainer()
167+
WebViewContainer()
158168
}
159-
}
169+
}
160170

161171
#endif

0 commit comments

Comments
 (0)