Skip to content

Commit d7f961e

Browse files
committed
add values and total methods to ProgressReporter
1 parent 87de3d2 commit d7f961e

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

Sources/FoundationEssentials/ProgressManager/ProgressManager.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,11 @@ internal struct AnyMetatypeWrapper: Hashable, Equatable, Sendable {
297297
}
298298
}
299299

300-
301300
/// Returns an array of values for specified property in subtree.
302301
/// - Parameter metatype: Type of property.
303302
/// - Returns: Array of values for property.
304303
public func values<P: Property>(of property: P.Type) -> [P.Value?] {
304+
_$observationRegistrar.access(self, keyPath: \.state)
305305
return state.withLock { state in
306306
let childrenValues = getFlattenedChildrenValues(property: property, state: &state)
307307
return [state.otherProperties[AnyMetatypeWrapper(metatype: property)] as? P.Value ?? P.defaultValue] + childrenValues.map { $0 ?? P.defaultValue }

Sources/FoundationEssentials/ProgressManager/ProgressReporter.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ import Observation
5555
return try manager.getAdditionalProperties(closure)
5656
}
5757

58+
/// Returns an array of values for specified property in subtree.
59+
/// - Parameter metatype: Type of property.
60+
/// - Returns: Array of values for property.
61+
public func values<P: ProgressManager.Property>(of property: P.Type) -> [P.Value?] {
62+
manager.values(of: property)
63+
}
64+
65+
/// Returns the aggregated result of values.
66+
/// - Parameters:
67+
/// - property: Type of property.
68+
public func total<P: ProgressManager.Property>(of property: P.Type) -> P.Value where P.Value: AdditiveArithmetic {
69+
manager.total(of: property)
70+
}
71+
5872
internal let manager: ProgressManager
5973

6074
internal init(manager: ProgressManager) {

Tests/FoundationEssentialsTests/ProgressManager/ProgressManagerTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,12 @@ class TestProgressReporter: XCTestCase {
617617
properties.totalFileCount = 6
618618
}
619619
XCTAssertEqual(reporter.withProperties(\.totalFileCount), 6)
620+
621+
let totalFileCount = manager.total(of: ProgressManager.Properties.TotalFileCount.self)
622+
XCTAssertEqual(totalFileCount, 6)
623+
624+
let totalFileCountValues = manager.values(of: ProgressManager.Properties.TotalFileCount.self)
625+
XCTAssertEqual(totalFileCountValues, [6])
620626
}
621627

622628
func testAddProgressReporterAsChild() {

0 commit comments

Comments
 (0)