fix: use AppStorage values for ping and refresh intervals

Make the ping and refresh intervals from preferences actually control
the timer frequencies. Settings now properly update timers when changed.
- Read pingInterval and refreshInterval from AppStorage
- Recreate timers when settings change
- Setup timers on app appearance with correct intervals
This commit is contained in:
Micha
2026-01-03 14:31:51 +01:00
parent 8cf974118b
commit e7b776942b

View File

@@ -6,6 +6,7 @@
//
import SwiftUI
import Combine
struct MainView: View {
@@ -17,7 +18,10 @@ struct MainView: View {
@State private var serverToDelete: Server?
@State private var showDeleteConfirmation = false
@State private var isFetchingInfo: Bool = false
@State private var refreshTimer = Timer.publish(every: 60, on: .main, in: .common).autoconnect()
@AppStorage("pingInterval") private var pingInterval: Int = 10
@AppStorage("refreshInterval") private var refreshInterval: Int = 60
@State private var refreshTimer: Timer.TimerPublisher?
@State private var refreshSubscription: AnyCancellable?
@State private var pingTimer: Timer?
private let serverOrderKey = MainView.serverOrderKeyStatic
private let storedServersKey = MainView.storedServersKeyStatic
@@ -97,11 +101,6 @@ struct MainView: View {
}
Button("Cancel", role: .cancel) {}
}
.onReceive(refreshTimer) { _ in
for server in servers {
fetchServerInfo(for: server.id)
}
}
.onAppear {
let initialID: UUID?
if let storedID = UserDefaults.standard.string(forKey: "selectedServerID"),
@@ -124,10 +123,13 @@ struct MainView: View {
await prefetchOtherServers(activeID: initialID)
}
}
pingAllServers()
pingTimer = Timer.scheduledTimer(withTimeInterval: 10.0, repeats: true) { _ in
pingAllServers()
}
setupTimers()
}
.onChange(of: pingInterval) { _, _ in
setupPingTimer()
}
.onChange(of: refreshInterval) { _, _ in
setupRefreshTimer()
}
.frame(minWidth: 800, minHeight: 450)
}
@@ -230,6 +232,29 @@ struct MainView: View {
}
}
private func setupTimers() {
setupPingTimer()
setupRefreshTimer()
}
private func setupPingTimer() {
pingTimer?.invalidate()
pingAllServers()
pingTimer = Timer.scheduledTimer(withTimeInterval: Double(pingInterval), repeats: true) { _ in
pingAllServers()
}
}
private func setupRefreshTimer() {
refreshSubscription = nil
refreshTimer = Timer.publish(every: Double(refreshInterval), on: .main, in: .common)
refreshSubscription = refreshTimer?.autoconnect().sink { _ in
for server in servers {
fetchServerInfo(for: server.id)
}
}
}
private static func loadStoredServers() -> [Server] {
let defaults = UserDefaults.standard
guard let data = defaults.data(forKey: storedServersKeyStatic) else {