Improve startup UX with placeholders and prefetch

This commit is contained in:
Micha
2025-11-19 23:28:12 +01:00
parent 562023519a
commit 6b8d458605
4 changed files with 150 additions and 31 deletions

View File

@@ -103,16 +103,26 @@ struct MainView: View {
}
}
.onAppear {
let initialID: UUID?
if let storedID = UserDefaults.standard.string(forKey: "selectedServerID"),
let uuid = UUID(uuidString: storedID),
servers.contains(where: { $0.id == uuid }) {
print("✅ [MainView] Restored selected server \(uuid)")
selectedServerID = uuid
} else if selectedServerID == nil, let first = servers.first {
initialID = uuid
} else if let first = servers.first {
print("✅ [MainView] Selecting first server \(first.hostname)")
selectedServerID = first.id
initialID = first.id
} else {
print(" [MainView] No stored selection")
initialID = nil
}
selectedServerID = initialID
if let initialID {
Task {
await prefetchOtherServers(activeID: initialID)
}
}
pingAllServers()
pingTimer = Timer.scheduledTimer(withTimeInterval: 10.0, repeats: true) { _ in
@@ -157,6 +167,39 @@ struct MainView: View {
}
}
}
private func prefetchOtherServers(activeID: UUID) async {
let others = servers.filter { $0.id != activeID }
await withTaskGroup(of: Void.self) { group in
for server in others {
group.addTask {
await fetchServerInfoAsync(for: server.id)
}
}
}
}
private func fetchServerInfoAsync(for id: UUID) async {
guard let server = servers.first(where: { $0.id == id }) else { return }
guard let apiKey = KeychainHelper.loadApiKey(for: server.hostname)?.trimmingCharacters(in: .whitespacesAndNewlines),
!apiKey.isEmpty,
let baseURL = URL(string: "https://\(server.hostname)")
else { return }
do {
let api = try await APIFactory.detectAndCreateAPI(baseURL: baseURL, apiKey: apiKey)
let info = try await api.fetchServerSummary(apiKey: apiKey)
await MainActor.run {
if let index = servers.firstIndex(where: { $0.id == id }) {
var updated = servers[index]
updated.info = info
servers[index] = updated
}
}
} catch {
print("❌ Prefetch failed for \(server.hostname): \(error)")
}
}
private func moveServer(from source: IndexSet, to destination: Int) {
servers.move(fromOffsets: source, toOffset: destination)