diff --git a/PadelClub/Views/Navigation/Toolbox/APICallsListView.swift b/PadelClub/Views/Navigation/Toolbox/APICallsListView.swift index 0382d5f..a934fe3 100644 --- a/PadelClub/Views/Navigation/Toolbox/APICallsListView.swift +++ b/PadelClub/Views/Navigation/Toolbox/APICallsListView.swift @@ -12,20 +12,27 @@ import PadelClubData struct APICallsListView: View { @State var descriptors: [CollectionDescriptor] = [] + @State var total = 0 var body: some View { List { - ForEach(self.descriptors) { descriptor in - - NavigationLink { - if let syncedType = descriptor.type as? any SyncedStorable.Type { - APICallsView(name: descriptor.name, type: syncedType) + Section { + LabeledContent("Total count", value: "\(total)") + } + + Section { + ForEach(self.descriptors) { descriptor in + + NavigationLink { + if let syncedType = descriptor.type as? any SyncedStorable.Type { + APICallsView(name: descriptor.name, type: syncedType) + } + } label: { + LabeledContent(descriptor.name, value: descriptor.count.string) } - } label: { - LabeledContent(descriptor.name, value: descriptor.count.string) + } - } }.onAppear { self.load() @@ -49,6 +56,8 @@ struct APICallsListView: View { func loadCount(_ type: T.Type, _ descriptor: CollectionDescriptor) async { let calls = await StoreCenter.main.apiCalls(type: type) descriptor.count = calls.count + + self.total = total + descriptor.count // Logger.log("\(descriptor.name), count = \(calls.count)") } diff --git a/PadelClub/Views/Navigation/Toolbox/DebugSettingsView.swift b/PadelClub/Views/Navigation/Toolbox/DebugSettingsView.swift index 95ec2a3..874b833 100644 --- a/PadelClub/Views/Navigation/Toolbox/DebugSettingsView.swift +++ b/PadelClub/Views/Navigation/Toolbox/DebugSettingsView.swift @@ -10,6 +10,10 @@ import LeStorage import PadelClubData struct DebugSettingsView: View { + @State private var errorMessage: String? + @State private var showingError = false + @State private var isSynchronizing = false + var body: some View { List { @@ -17,7 +21,19 @@ struct DebugSettingsView: View { LabeledContent("Has Websocket Manager", value: self._hasWebSocketManager) LabeledContent("Websocket ping", value: self._wsPingStatus) LabeledContent("Websocket failure", value: self._wsFailure) - LabeledContent("Last sync date", value: self._lastSyncDate) + LabeledContent("Last synced object date", value: self._lastSyncDate) + if isSynchronizing { + HStack { + ProgressView() + .scaleEffect(0.8) + Text("Synchronizing...") + .foregroundColor(.secondary) + } + } else { + Button("Synchronize") { + self._synchronize() + } + } } Section("Settings") { @@ -40,6 +56,11 @@ struct DebugSettingsView: View { } } + .alert("Synchronization Error", isPresented: $showingError) { + Button("OK") { } + } message: { + Text(errorMessage ?? "An unknown error occurred") + } } fileprivate var _userId: String { @@ -79,6 +100,29 @@ struct DebugSettingsView: View { fileprivate var _lastSyncDate: String { return "\(StoreCenter.main.lastSyncDate)" } + + fileprivate func _synchronize() { + Logger.log("launch sync...") + Task { + await MainActor.run { + isSynchronizing = true + } + + do { + try await StoreCenter.main.synchronizeLastUpdates() + } catch { + Logger.error(error) + await MainActor.run { + errorMessage = error.localizedDescription + showingError = true + } + } + + await MainActor.run { + isSynchronizing = false + } + } + } } struct DebugPurchaseView: View {