mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2025-06-29 04:39:53 +00:00
ios: asychronous subscription updates (#4707)
* ios: asychronous subscription updates * cleanup
This commit is contained in:
parent
3740805125
commit
3a0921c093
2 changed files with 23 additions and 20 deletions
|
@ -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
|
||||||
|
|
|
@ -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))")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue