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

@ -1,46 +1,11 @@
import SwiftUI
enum ServiceStatus {
case pending
case operational
case degraded
case outage
var description: String {
switch self {
case .pending:
return "Fetching service status"
case .operational:
return "No issues detected"
case .degraded:
return "Performance degraded"
case .outage:
return "Service disruption"
}
}
var color: Color {
switch self {
case .pending:
return .gray
case .operational:
return .green
case .degraded:
return .orange
case .outage:
return .red
}
}
}
struct HomeView: View {
@EnvironmentObject
private var viewModel: ViewModel
@State private var serviceStatus: ServiceStatus = .operational
private let falsePositiveURL = URL(string: "https://www.sr2.uk/contact")!
private let statusURL = URL(string:
"https://status.sr2.uk/")!
@ -166,13 +131,13 @@ struct HomeView: View {
Link(destination: statusURL) {
HStack(spacing: 12) {
Circle()
.fill(serviceStatus.color)
.fill(viewModel.summaryStatus.color)
.frame(width: 12, height: 12)
VStack(alignment: .leading, spacing: 4) {
Text("Service Status")
.font(.headline)
Text(serviceStatus.description)
Text(viewModel.summaryStatus.description)
.font(.caption)
.foregroundStyle(.secondary)
}

58
dns/SummaryStatus.swift Normal file
View file

@ -0,0 +1,58 @@
//
// SummaryStatus.swift
// dns
//
// Created by Benjamin Erhart on 15.04.26.
//
import SwiftUI
struct Status: Codable {
let summaryStatus: SummaryStatus
}
enum SummaryStatus: String, Codable, CustomStringConvertible {
case pending
case ok
case notice
case disrupted
case down
var description: String {
switch self {
case .pending:
return "Fetching service status"
case .ok:
return "No issues detected"
case .notice:
return "In maintenance"
case .disrupted:
return "Service disruption"
case .down:
return "Service down"
}
}
var color: Color {
switch self {
case .pending:
return .gray
case .ok:
return .green
case .notice, .disrupted:
return .orange
case .down:
return .red
}
}
}

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)