Skip to content

Commit 7a81397

Browse files
committed
Update versionCheck protocol for async
1 parent d65a48a commit 7a81397

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

Loop.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4751,6 +4751,7 @@
47514751
GCC_WARN_UNUSED_LABEL = YES;
47524752
GCC_WARN_UNUSED_PARAMETER = YES;
47534753
GCC_WARN_UNUSED_VARIABLE = YES;
4754+
IPHONEOS_DEPLOYMENT_TARGET = 15.1;
47544755
LOCALIZED_STRING_MACRO_NAMES = (
47554756
NSLocalizedString,
47564757
CFLocalizedString,
@@ -4860,6 +4861,7 @@
48604861
GCC_WARN_UNUSED_LABEL = YES;
48614862
GCC_WARN_UNUSED_PARAMETER = YES;
48624863
GCC_WARN_UNUSED_VARIABLE = YES;
4864+
IPHONEOS_DEPLOYMENT_TARGET = 15.1;
48634865
LOCALIZED_STRING_MACRO_NAMES = (
48644866
NSLocalizedString,
48654867
CFLocalizedString,

Loop/Managers/LoopAppManager.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import Intents
1111
import Combine
1212
import LoopKit
1313
import LoopKitUI
14-
import TidepoolKit
1514
import MockKit
1615
import HealthKit
1716
import WidgetKit

Loop/Managers/SupportManager.swift

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ extension SupportManager {
103103
// MARK: Version checking
104104
extension SupportManager {
105105
func performCheck() {
106-
checkVersion { [weak self] versionUpdate in
107-
self?.notify(versionUpdate)
106+
Task { @MainActor in
107+
let versionUpdate = await checkVersion()
108+
self.notify(versionUpdate)
108109
}
109110
}
110111

@@ -114,37 +115,41 @@ extension SupportManager {
114115
}
115116
}
116117

117-
func checkVersion(completion: @escaping (VersionUpdate) -> Void) {
118-
let group = DispatchGroup()
119-
var results = [String: Result<VersionUpdate?, Error>]()
120-
supports.value.values.forEach { support in
121-
group.enter()
122-
support.checkVersion(bundleIdentifier: Bundle.main.bundleIdentifier!, currentVersion: Bundle.main.shortVersionString) { result in
123-
results[support.identifier] = result
124-
group.leave()
118+
func checkVersion() async -> VersionUpdate {
119+
120+
let results = await withTaskGroup(of: (VersionUpdate?,String).self) { group in
121+
var updatesFromServices = [(VersionUpdate?,String)]()
122+
123+
supports.value.values.forEach { support in
124+
group.addTask {
125+
return (await support.checkVersion(bundleIdentifier: Bundle.main.bundleIdentifier!, currentVersion: Bundle.main.shortVersionString), support.identifier)
126+
}
125127
}
128+
129+
for await update in group {
130+
updatesFromServices.append(update)
131+
}
132+
133+
return updatesFromServices
126134
}
127-
group.notify(queue: DispatchQueue.main) { [weak self] in
128-
guard let self = self else { return }
129-
self.saveState()
130-
let (identifierWithHighestVersionUpdate, aggregatedVersionUpdate) = self.aggregate(results: results)
131-
self.identifierWithHighestVersionUpdate = identifierWithHighestVersionUpdate
132-
completion(aggregatedVersionUpdate)
133-
}
135+
136+
self.saveState()
137+
let (identifierWithHighestVersionUpdate, aggregatedVersionUpdate) = self.aggregate(results: results)
138+
self.identifierWithHighestVersionUpdate = identifierWithHighestVersionUpdate
139+
return aggregatedVersionUpdate
134140
}
135141

136-
private func aggregate(results: [String : Result<VersionUpdate?, Error>]) -> (String?, VersionUpdate) {
142+
private func aggregate(results: [(VersionUpdate?,String)]) -> (String?, VersionUpdate) {
137143
var aggregatedVersionUpdate = VersionUpdate.default
138144
var identifierWithHighestVersionUpdate: String?
139-
results.forEach { key, value in
140-
switch value {
141-
case .failure(let error):
142-
self.log.error("Error from version check %{public}@: %{public}@", key, error.localizedDescription)
143-
case .success(let versionUpdate):
144-
if let versionUpdate = versionUpdate, versionUpdate > aggregatedVersionUpdate {
145+
results.forEach { versionUpdate, identifier in
146+
if let versionUpdate {
147+
if versionUpdate > aggregatedVersionUpdate {
145148
aggregatedVersionUpdate = versionUpdate
146-
identifierWithHighestVersionUpdate = key
149+
identifierWithHighestVersionUpdate = identifier
147150
}
151+
} else {
152+
self.log.error("Version check failed for %{public}@", identifier)
148153
}
149154
}
150155
return (identifierWithHighestVersionUpdate, aggregatedVersionUpdate)
@@ -225,8 +230,8 @@ extension SupportManager {
225230
fileprivate extension Result where Success == VersionUpdate? {
226231
var value: VersionUpdate {
227232
switch self {
228-
case .failure: return .none
229-
case .success(let val): return val ?? .none
233+
case .failure: return .noUpdateNeeded
234+
case .success(let val): return val ?? .noUpdateNeeded
230235
}
231236
}
232237
}

LoopTests/Managers/SupportManagerTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ class SupportManagerTests: XCTestCase {
7272
}
7373

7474
func testVersionCheckOneService() throws {
75-
XCTAssertEqual(VersionUpdate.none, getVersion())
75+
XCTAssertEqual(VersionUpdate.noUpdateNeeded, getVersion())
7676
mockSupport.mockResult = .success(.required)
7777
XCTAssertEqual(.required, getVersion())
7878
}
7979

8080
func testVersionCheckOneServiceError() throws {
8181
// Error doesn't really do anything but log
8282
mockSupport.mockResult = .failure(MockError.nothing)
83-
XCTAssertEqual(VersionUpdate.none, getVersion())
83+
XCTAssertEqual(VersionUpdate.noUpdateNeeded, getVersion())
8484
}
8585

8686
func testVersionCheckMultipleServices() throws {
8787
let anotherSupport = AnotherMockSupport()
8888
supportManager.addSupport(anotherSupport)
89-
XCTAssertEqual(VersionUpdate.none, getVersion())
89+
XCTAssertEqual(VersionUpdate.noUpdateNeeded, getVersion())
9090
anotherSupport.mockResult = .success(.required)
9191
XCTAssertEqual(.required, getVersion())
9292
mockSupport.mockResult = .success(.recommended)

0 commit comments

Comments
 (0)