Issue #5: Check server status on start.

This commit is contained in:
Benjamin Erhart 2026-04-15 14:13:47 +02:00
parent 603a23a1a8
commit f7ac3d9aed
3 changed files with 90 additions and 38 deletions

View file

@ -12,6 +12,8 @@ import OSLog
class ViewModel: NSObject, ObservableObject {
// MARK: Public Properties
@Published
var blocklist: BlocklistOption = .secure {
didSet {
@ -33,9 +35,14 @@ class ViewModel: NSObject, ObservableObject {
}
}
private var isProgrammaticChange = false
@Published
var summaryStatus: SummaryStatus = .pending
// MARK: Private Properties
private var isProgrammaticChange = false
private let manager = NEDNSSettingsManager.shared()
private let log = Logger(subsystem: String(describing: ViewModel.self), category: String(describing: ViewModel.self))
@ -72,9 +79,15 @@ class ViewModel: NSObject, ObservableObject {
}
}
}
Task {
await fetchServerStatus()
}
}
// MARK: Public Methods
func toggleDns() {
guard !isProgrammaticChange else {
// Reset, so next one is recognized as coming from the user again.
@ -110,6 +123,22 @@ class ViewModel: NSObject, ObservableObject {
}
}
func fetchServerStatus() async {
do {
let (data, _) = try await URLSession.shared.data(for: .init(url: .init(string: "https://status.sr2.uk/index.json")!))
let status = try JSONDecoder().decode(Status.self, from: data)
summaryStatus = status.summaryStatus
}
catch {
log.error("Error while checking status: \(error)")
}
}
// MARK: Private Methods
private func delayedToggle(_ enabled: Bool) {
Task {
try? await Task.sleep(nanoseconds: 500_000_000)