Skip to content

Commit a25cca3

Browse files
committed
replace totalCount and completedCount methods with setCounts
1 parent 677d891 commit a25cca3

File tree

4 files changed

+52
-40
lines changed

4 files changed

+52
-40
lines changed

Sources/FoundationEssentials/ProgressManager/ProgressManager+Interop.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ internal final class NSProgressBridge: Progress, @unchecked Sendable {
187187
self.progress = progress
188188
super.init(parent: nil, userInfo: nil)
189189

190-
managerBridge.completedCount(Int(progress.completedUnitCount))
190+
managerBridge.setCounts { completed, total in
191+
completed = Int(progress.completedUnitCount)
192+
}
191193

192194
let position = manager.addChild(
193195
child: managerBridge,
@@ -200,8 +202,10 @@ internal final class NSProgressBridge: Progress, @unchecked Sendable {
200202
// Overrides the _updateChild func that Foundation.Progress calls to update parent
201203
// so that the parent that gets updated is the ProgressManager parent
202204
override func _updateChild(_ child: Foundation.Progress, fraction: _NSProgressFractionTuple, portion: Int64) {
203-
managerBridge.totalCount(Int(fraction.next.total))
204-
managerBridge.completedCount(Int(fraction.next.completed))
205+
managerBridge.setCounts { completed, total in
206+
completed = Int(fraction.next.completed)
207+
total = Int(fraction.next.total)
208+
}
205209
managerBridge.markSelfDirty()
206210
}
207211
}

Sources/FoundationEssentials/ProgressManager/ProgressManager.swift

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -196,39 +196,29 @@ internal import _FoundationCollections
196196
}
197197
}
198198

199-
public func completedCount(_ count: Int) {
199+
public func setCounts(_ counts: (_ completed: inout Int, _ total: inout Int?) -> Void) {
200200
_$observationRegistrar.withMutation(of: self, keyPath: \.fractionCompleted) {
201201
_$observationRegistrar.withMutation(of: self, keyPath: \.completedCount) {
202-
let parents: [ParentState]? = state.withLock { state in
203-
guard state.selfFraction.completed != count else {
204-
return nil
202+
_$observationRegistrar.withMutation(of: self, keyPath: \.totalCount) {
203+
let parents: [ParentState]? = state.withLock { state in
204+
var completed = state.selfFraction.completed
205+
var total = state.selfFraction.total
206+
207+
counts(&completed, &total)
208+
209+
guard state.selfFraction.completed != completed || state.selfFraction.total != total else {
210+
return nil
211+
}
212+
213+
state.selfFraction.completed = completed
214+
state.selfFraction.total = total
215+
216+
return state.parents
205217
}
206218

207-
state.completedCount(count)
208-
209-
return state.parents
210-
}
211-
if let parents = parents {
212-
markSelfDirty(parents: parents)
213-
}
214-
}
215-
}
216-
}
217-
218-
public func totalCount(_ count: Int?) {
219-
_$observationRegistrar.withMutation(of: self, keyPath: \.fractionCompleted) {
220-
_$observationRegistrar.withMutation(of: self, keyPath: \.totalCount) {
221-
let parents: [ParentState]? = state.withLock { state in
222-
guard state.selfFraction.total != count else {
223-
return nil
219+
if let parents = parents {
220+
markSelfDirty(parents: parents)
224221
}
225-
226-
state.totalCount(count)
227-
228-
return state.parents
229-
}
230-
if let parents = parents {
231-
markSelfDirty(parents: parents)
232222
}
233223
}
234224
}

Tests/FoundationEssentialsTests/ProgressManager/ProgressManagerTests.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,19 @@ extension Tag {
6969
#expect(overall.fractionCompleted == 0.5)
7070
#expect(overall.isIndeterminate == false)
7171

72-
overall.totalCount(nil)
72+
overall.setCounts { _, total in
73+
total = nil
74+
}
7375
overall.complete(count: 1)
7476
#expect(overall.completedCount == 6)
7577
#expect(overall.totalCount == nil)
7678
#expect(overall.fractionCompleted == 0.0)
7779
#expect(overall.isIndeterminate == true)
7880
#expect(overall.isFinished == false)
7981

80-
overall.totalCount(12)
82+
overall.setCounts { _, total in
83+
total = 12
84+
}
8185
overall.complete(count: 2)
8286
#expect(overall.completedCount == 8)
8387
#expect(overall.totalCount == 12)
@@ -110,7 +114,9 @@ extension Tag {
110114
#expect(overall.isIndeterminate == true)
111115
#expect(overall.isFinished == false)
112116

113-
overall.totalCount(5)
117+
overall.setCounts { _, total in
118+
total = 5
119+
}
114120
#expect(overall.completedCount == 2)
115121
#expect(overall.totalCount == 5)
116122
#expect(overall.fractionCompleted == 0.4)
@@ -142,13 +148,17 @@ extension Tag {
142148
#expect(overall.fractionCompleted == 0.5)
143149
#expect(childManager.isIndeterminate == false)
144150

145-
childManager.totalCount(nil)
151+
childManager.setCounts { _, total in
152+
total = nil
153+
}
146154

147155
#expect(overall.fractionCompleted == 0.0)
148156
#expect(childManager.isIndeterminate == true)
149157
#expect(childManager.completedCount == 2)
150158

151-
childManager.totalCount(5)
159+
childManager.setCounts { _, total in
160+
total = 5
161+
}
152162
childManager.complete(count: 2)
153163

154164
#expect(overall.fractionCompleted == 0.8)
@@ -173,7 +183,9 @@ extension Tag {
173183
let manager = ProgressManager(totalCount: nil)
174184
#expect(manager.isIndeterminate == true)
175185

176-
manager.totalCount(10)
186+
manager.setCounts { _, total in
187+
total = 10
188+
}
177189
#expect(manager.isIndeterminate == false)
178190
#expect(manager.totalCount == 10)
179191

@@ -631,7 +643,9 @@ extension Tag {
631643
// Task 3: Change to determinate after a delay
632644
group.addTask {
633645
try? await Task.sleep(nanoseconds: 1_000_000)
634-
manager.totalCount(20)
646+
manager.setCounts { _, total in
647+
total = 20
648+
}
635649

636650
for _ in 1...30 {
637651
let _ = manager.fractionCompleted

Tests/FoundationEssentialsTests/ProgressManager/ProgressReporterTests.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ import Testing
7373
overall.assign(count: 20, to: child2.reporter)
7474
child2.complete(count: 20)
7575

76-
overall.totalCount(30)
76+
overall.setCounts { _, total in
77+
total = 30
78+
}
7779
#expect(overall.completedCount == 20)
7880
#expect(overall.fractionCompleted == Double(25) / Double(30))
7981

@@ -94,7 +96,9 @@ import Testing
9496

9597
#expect(reporter2.fractionCompleted == 0.5)
9698

97-
overall.totalCount(30)
99+
overall.setCounts { _, total in
100+
total = 30
101+
}
98102

99103
#expect(overall.totalCount == 30)
100104
#expect(overall.fractionCompleted == 0.5)

0 commit comments

Comments
 (0)