Skip to content

Commit 8bdb37f

Browse files
committed
refactor updater initialization and settings management in FlitroApp and SettingsView
1 parent f51afd7 commit 8bdb37f

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

Flitro/FlitroApp.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
1616
}
1717

1818
func applicationDidFinishLaunching(_ notification: Notification) {
19-
updaterController = SPUStandardUpdaterController(startingUpdater: true, updaterDelegate: nil, userDriverDelegate: nil)
20-
2119
if let window = NSApp.windows.first {
2220
window.delegate = self
2321
}
@@ -57,6 +55,14 @@ struct ContentView: View {
5755
@main
5856
struct FlitroApp: App {
5957
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
58+
59+
init() {
60+
// Ensure updaterController is initialized before any scene
61+
if appDelegate.updaterController == nil {
62+
appDelegate.updaterController = SPUStandardUpdaterController(startingUpdater: true, updaterDelegate: nil, userDriverDelegate: nil)
63+
}
64+
}
65+
6066
var body: some Scene {
6167
WindowGroup {
6268
ContentView()
@@ -80,7 +86,11 @@ struct FlitroApp: App {
8086
MenuBarExtraContents().environmentObject(ContextManager.shared)
8187
}
8288
Settings {
83-
SettingsView()
89+
if let updater = appDelegate.updaterController?.updater {
90+
SettingsView(updater: updater)
91+
} else {
92+
Text("Updater not available")
93+
}
8494
}
8595
WindowGroup("About Flitro", id: "about") {
8696
AboutView()

Flitro/SettingsView.swift

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,36 @@
11
import SwiftUI
22
import ServiceManagement
3+
import Sparkle
34

45
struct SettingsView: View {
6+
let updater: SPUUpdater
57
@AppStorage("startAtLogin") private var startAtLogin: Bool = false
8+
@State private var automaticallyChecksForUpdates: Bool
9+
@State private var automaticallyDownloadsUpdates: Bool
10+
11+
init(updater: SPUUpdater) {
12+
self.updater = updater
13+
_automaticallyChecksForUpdates = State(wrappedValue: updater.automaticallyChecksForUpdates)
14+
_automaticallyDownloadsUpdates = State(wrappedValue: updater.automaticallyDownloadsUpdates)
15+
}
616

717
var body: some View {
818
Form {
919
Toggle(isOn: $startAtLogin) {
1020
Text("Start Flitro at login")
1121
}
12-
.onChange(of: startAtLogin) {
13-
setLaunchAtLogin(enabled: startAtLogin)
22+
.onChange(of: startAtLogin) { newValue, _ in
23+
setLaunchAtLogin(enabled: newValue)
1424
}
25+
Toggle("Automatically check for updates", isOn: $automaticallyChecksForUpdates)
26+
.onChange(of: automaticallyChecksForUpdates) { newValue, _ in
27+
updater.automaticallyChecksForUpdates = newValue
28+
}
29+
Toggle("Automatically download updates", isOn: $automaticallyDownloadsUpdates)
30+
.disabled(!automaticallyChecksForUpdates)
31+
.onChange(of: automaticallyDownloadsUpdates) { newValue, _ in
32+
updater.automaticallyDownloadsUpdates = newValue
33+
}
1534
}
1635
.padding()
1736
.frame(width: 320)
@@ -28,4 +47,4 @@ struct SettingsView: View {
2847
// Handle error (e.g., show an alert)
2948
}
3049
}
31-
}
50+
}

0 commit comments

Comments
 (0)