ios: asychronous subscription updates (#4707)

* ios: asychronous subscription updates

* cleanup
This commit is contained in:
Arturs Krumins 2024-08-17 15:26:56 +03:00 committed by GitHub
parent 3740805125
commit 3a0921c093
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 20 deletions

View file

@ -1420,9 +1420,9 @@ func apiGetVersion() throws -> CoreVersionInfo {
throw r throw r
} }
func getAgentSubsTotal() throws -> (SMPServerSubs, Bool) { func getAgentSubsTotal() async throws -> (SMPServerSubs, Bool) {
let userId = try currentUserId("getAgentSubsTotal") let userId = try currentUserId("getAgentSubsTotal")
let r = chatSendCmdSync(.getAgentSubsTotal(userId: userId), log: false) let r = await chatSendCmd(.getAgentSubsTotal(userId: userId))
if case let .agentSubsTotal(_, subsTotal, hasSession) = r { return (subsTotal, hasSession) } if case let .agentSubsTotal(_, subsTotal, hasSession) = r { return (subsTotal, hasSession) }
logger.error("getAgentSubsTotal error: \(String(describing: r))") logger.error("getAgentSubsTotal error: \(String(describing: r))")
throw r throw r

View file

@ -324,7 +324,7 @@ struct ChatListView: View {
struct SubsStatusIndicator: View { struct SubsStatusIndicator: View {
@State private var subs: SMPServerSubs = SMPServerSubs.newSMPServerSubs @State private var subs: SMPServerSubs = SMPServerSubs.newSMPServerSubs
@State private var hasSess: Bool = false @State private var hasSess: Bool = false
@State private var timer: Timer? = nil @State private var task: Task<Void, Never>?
@State private var showServersSummary = false @State private var showServersSummary = false
@AppStorage(DEFAULT_SHOW_SUBSCRIPTION_PERCENTAGE) private var showSubscriptionPercentage = false @AppStorage(DEFAULT_SHOW_SUBSCRIPTION_PERCENTAGE) private var showSubscriptionPercentage = false
@ -343,10 +343,10 @@ struct SubsStatusIndicator: View {
} }
.disabled(ChatModel.shared.chatRunning != true) .disabled(ChatModel.shared.chatRunning != true)
.onAppear { .onAppear {
startTimer() startTask()
} }
.onDisappear { .onDisappear {
stopTimer() stopTask()
} }
.appSheet(isPresented: $showServersSummary) { .appSheet(isPresented: $showServersSummary) {
ServersSummaryView() ServersSummaryView()
@ -354,25 +354,28 @@ struct SubsStatusIndicator: View {
} }
} }
private func startTimer() { private func startTask() {
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in task = Task {
if AppChatState.shared.value == .active { while !Task.isCancelled {
getSubsTotal() if AppChatState.shared.value == .active {
do {
let (subs, hasSess) = try await getAgentSubsTotal()
await MainActor.run {
self.subs = subs
self.hasSess = hasSess
}
} catch let error {
logger.error("getSubsTotal error: \(responseError(error))")
}
}
try? await Task.sleep(nanoseconds: 1_000_000_000) // Sleep for 1 second
} }
} }
} }
func stopTimer() { func stopTask() {
timer?.invalidate() task?.cancel()
timer = nil task = nil
}
private func getSubsTotal() {
do {
(subs, hasSess) = try getAgentSubsTotal()
} catch let error {
logger.error("getSubsTotal error: \(responseError(error))")
}
} }
} }