Skip to content

Commit f45fa99

Browse files
Merge pull request #641 from Adamant-im/dev/trello.com/c/AmlmMsUi
[trello.com/c/AmlmMsUi] Network layer fixes
2 parents c281ccb + d5e9f3b commit f45fa99

File tree

3 files changed

+56
-37
lines changed

3 files changed

+56
-37
lines changed

Adamant/Modules/Wallets/ERC20/ERC20WalletService.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,6 @@ final class ERC20WalletService: WalletCoreProtocol, @unchecked Sendable {
208208
}
209209
.store(in: &subscriptions)
210210

211-
NotificationCenter.default
212-
.notifications(named: .AdamantAccountService.accountDataUpdated, object: nil)
213-
.sink { @MainActor [weak self] _ in
214-
self?.update()
215-
}
216-
.store(in: &subscriptions)
217-
218211
NotificationCenter.default
219212
.notifications(named: .AdamantAccountService.userLoggedOut, object: nil)
220213
.sink { @MainActor [weak self] _ in

CommonKit/Sources/CommonKit/Services/HealthCheck/BlockchainHealthCheckWrapper.swift

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,26 @@ public final class BlockchainHealthCheckWrapper<
4848
}
4949
}
5050

51-
public override func healthCheckInternal() {
52-
super.healthCheckInternal()
51+
public override func healthCheckInternal() async {
52+
await super.healthCheckInternal()
53+
updateNodesAvailability(update: nil)
5354

54-
Task { @HealthCheckActor in
55-
updateNodesAvailability(update: nil)
56-
57-
await withTaskGroup(of: Void.self, returning: Void.self) { group in
58-
nodes.filter { $0.isEnabled }.forEach { node in
59-
group.addTask { @HealthCheckActor [weak self] in
60-
guard let self, !currentRequests.contains(node.id) else { return }
61-
62-
currentRequests.insert(node.id)
63-
defer { currentRequests.remove(node.id) }
64-
65-
let update = await updateNodeStatusInfo(node: node)
66-
updateNodesAvailability(update: update)
67-
}
55+
try? await withThrowingTaskGroup(of: Void.self, returning: Void.self) { group in
56+
nodes.filter { $0.isEnabled }.forEach { node in
57+
group.addTask { @HealthCheckActor [weak self] in
58+
guard let self, !currentRequests.contains(node.id) else { return }
59+
60+
currentRequests.insert(node.id)
61+
defer { currentRequests.remove(node.id) }
62+
63+
let update = await updateNodeStatusInfo(node: node)
64+
try Task.checkCancellation()
65+
updateNodesAvailability(update: update)
6866
}
69-
70-
await group.waitForAll()
71-
healthCheckPostProcessing()
7267
}
68+
69+
try await group.waitForAll()
70+
healthCheckPostProcessing()
7371
}
7472
}
7573
}

CommonKit/Sources/CommonKit/Services/HealthCheck/HealthCheckWrapper.swift

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ open class HealthCheckWrapper<Service: Sendable, Error: HealthCheckableError>: S
4545
private let crucialUpdateInterval: TimeInterval
4646
private let isActive: Bool
4747
private var healthCheckTimerSubscription: AnyCancellable?
48-
private var previousAppState: UIApplication.State?
48+
private var healthCheckSubscriptions = Set<AnyCancellable>()
49+
private var appState: AppState = .active
50+
private var performHealthCheckWhenBecomeActive = false
4951
private var lastUpdateTime: Date?
5052

5153
public nonisolated init(
@@ -112,17 +114,38 @@ open class HealthCheckWrapper<Service: Sendable, Error: HealthCheckableError>: S
112114

113115
nonisolated public func healthCheck() {
114116
Task { @HealthCheckActor in
115-
guard isActive else { return }
117+
guard canPerformHealthCheck else { return }
116118
lastUpdateTime = .now
117119
updateHealthCheckTimerSubscription()
118-
healthCheckInternal()
120+
121+
Task {
122+
await healthCheckInternal()
123+
guard Task.isCancelled else { return }
124+
performHealthCheckWhenBecomeActive = true
125+
}.store(in: &healthCheckSubscriptions)
119126
}
120127
}
121128

122-
open func healthCheckInternal() {}
129+
open func healthCheckInternal() async {}
123130
}
124131

125132
private extension HealthCheckWrapper {
133+
private enum AppState {
134+
case active
135+
case background
136+
}
137+
138+
var canPerformHealthCheck: Bool {
139+
guard isActive else { return false }
140+
141+
switch appState {
142+
case .active:
143+
return true
144+
case .background:
145+
return false
146+
}
147+
}
148+
126149
func configure(nodes: AnyObservable<[Node]>, connection: AnyObservable<Bool>) {
127150
let connection = connection
128151
.removeDuplicates()
@@ -149,7 +172,7 @@ private extension HealthCheckWrapper {
149172

150173
NotificationCenter.default
151174
.notifications(named: UIApplication.willResignActiveNotification, object: nil)
152-
.sink { @HealthCheckActor [weak self] _ in self?.previousAppState = .background }
175+
.sink { @HealthCheckActor [weak self] _ in self?.willResignActiveAction() }
153176
.store(in: &subscriptions)
154177
}
155178

@@ -172,17 +195,22 @@ private extension HealthCheckWrapper {
172195
}
173196

174197
func didBecomeActiveAction() {
175-
defer { previousAppState = .active }
198+
guard appState != .active else { return }
199+
appState = .active
176200

177-
guard
178-
previousAppState == .background,
179-
let timeToUpdate = lastUpdateTime?.addingTimeInterval(normalUpdateInterval / 3),
180-
Date.now > timeToUpdate
181-
else { return }
201+
let timeToUpdate = lastUpdateTime?.addingTimeInterval(normalUpdateInterval / 3)
202+
?? .adamantNullDate
182203

204+
guard performHealthCheckWhenBecomeActive || Date.now >= timeToUpdate else { return }
205+
performHealthCheckWhenBecomeActive = false
183206
healthCheck()
184207
}
185208

209+
func willResignActiveAction() {
210+
appState = .background
211+
healthCheckSubscriptions = .init()
212+
}
213+
186214
func updateNodes(_ newNodes: [Node]) {
187215
nodes = newNodes
188216
updateSortedNodes()

0 commit comments

Comments
 (0)