Skip to content

Commit 7c319f9

Browse files
committed
add fileURL additional property
1 parent 9d1d0a3 commit 7c319f9

File tree

6 files changed

+108
-3
lines changed

6 files changed

+108
-3
lines changed

Sources/FoundationEssentials/ProgressManager/ProgressManager+Properties+SpecializedGet.swift

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ extension ProgressManager {
316316

317317
internal func getUpdatedEstimatedTimeRemaining() -> Duration {
318318
return state.withLock { state in
319-
// Get self's throughput as part of summary
319+
// Get self's estimatedTimeRemaining as part of summary
320320
var value: Duration = Duration.seconds(0)
321321
ProgressManager.Properties.EstimatedTimeRemaining.reduce(into: &value, value: state.estimatedTimeRemaining)
322322

@@ -335,7 +335,35 @@ extension ProgressManager {
335335
}
336336
} else {
337337
// Merge non-dirty, updated value
338-
value = ProgressManager.Properties.EstimatedTimeRemaining.merge(value, state.estimatedTimeRemaining)
338+
value = ProgressManager.Properties.EstimatedTimeRemaining.merge(value, childState.estimatedTimeRemaining.value)
339+
}
340+
}
341+
return value
342+
}
343+
}
344+
345+
internal func getUpdatedFileURL() -> [URL] {
346+
return state.withLock { state in
347+
// Get self's estimatedTimeRemaining as part of summary
348+
var value: [URL] = ProgressManager.Properties.FileURL.defaultSummary
349+
ProgressManager.Properties.FileURL.reduce(into: &value, value: state.fileURL)
350+
351+
guard !state.children.isEmpty else {
352+
return value
353+
}
354+
355+
for (idx, childState) in state.children.enumerated() {
356+
if childState.fileURL.isDirty {
357+
// Update dirty path
358+
if let child = childState.child {
359+
let updatedSummary = child.getUpdatedFileURL()
360+
let newFileURL = PropertyStateURL(value: updatedSummary, isDirty: false)
361+
state.children[idx].fileURL = newFileURL
362+
value = ProgressManager.Properties.FileURL.merge(value, updatedSummary)
363+
}
364+
} else {
365+
// Merge non-dirty, updated value
366+
value = ProgressManager.Properties.FileURL.merge(value, childState.fileURL.value)
339367
}
340368
}
341369
return value

Sources/FoundationEssentials/ProgressManager/ProgressManager+Properties+SpecializedSet.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ extension ProgressManager {
7070
}
7171
}
7272

73+
internal func markSelfDirty(property: ProgressManager.Properties.FileURL.Type, parents: [ParentState]) {
74+
for parentState in parents {
75+
parentState.parent.markChildDirty(property: property, at: parentState.positionInParent)
76+
}
77+
}
78+
7379
internal func markChildDirty(property: MetatypeWrapper<Int>, at position: Int) {
7480
let parents = state.withLock { state in
7581
state.children[position].childPropertiesInt[property]?.isDirty = true
@@ -142,6 +148,14 @@ extension ProgressManager {
142148
markSelfDirty(property: property, parents: parents)
143149
}
144150

151+
internal func markChildDirty(property: ProgressManager.Properties.FileURL.Type, at position: Int) {
152+
let parents = state.withLock { state in
153+
state.children[position].fileURL.isDirty = true
154+
return state.parents
155+
}
156+
markSelfDirty(property: property, parents: parents)
157+
}
158+
145159
//MARK: Methods to preserve values of properties upon deinit
146160
internal func setChildRemainingPropertiesInt(_ properties: [MetatypeWrapper<Int>: Int], at position: Int) {
147161
state.withLock { state in

Sources/FoundationEssentials/ProgressManager/ProgressManager+Properties.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,34 @@ extension ProgressManager {
191191
public static func merge(_ summary1: Duration, _ summary2: Duration) -> Duration {
192192
return max(summary1, summary2)
193193
}
194-
194+
}
195+
196+
public var fileURL: FileURL.Type { FileURL.self }
197+
public struct FileURL: Sendable, Property {
198+
199+
public typealias Value = URL?
200+
201+
public typealias Summary = [URL]
202+
203+
public static var key: String { return "FileURL" }
204+
205+
public static var defaultValue: URL? { return nil }
206+
207+
public static var defaultSummary: [URL] { return [] }
208+
209+
public typealias T = URL
210+
211+
public static func reduce(into summary: inout [URL], value: URL?) {
212+
guard let value else {
213+
return
214+
}
215+
summary.append(value)
216+
}
217+
218+
public static func merge(_ summary1: [URL], _ summary2: [URL]) -> [URL] {
219+
return summary1 + summary2
220+
}
221+
195222
}
196223
}
197224
}

Sources/FoundationEssentials/ProgressManager/ProgressManager+State.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ extension ProgressManager {
7676
var isDirty: Bool
7777
}
7878

79+
internal struct PropertyStateURL {
80+
var value: [URL]
81+
var isDirty: Bool
82+
}
83+
7984
internal struct ChildState {
8085
weak var child: ProgressManager?
8186
var remainingPropertiesInt: [MetatypeWrapper<Int>: Int]?
@@ -90,6 +95,7 @@ extension ProgressManager {
9095
var completedByteCount: PropertyStateInt64
9196
var throughput: PropertyStateThroughput
9297
var estimatedTimeRemaining: PropertyStateDuration
98+
var fileURL: PropertyStateURL
9399
var childPropertiesInt: [MetatypeWrapper<Int>: PropertyStateInt]
94100
var childPropertiesDouble: [MetatypeWrapper<Double>: PropertyStateDouble]
95101
var childPropertiesString: [MetatypeWrapper<String>: PropertyStateString]
@@ -143,6 +149,7 @@ extension ProgressManager {
143149
var completedByteCount: Int64
144150
var throughput: Int64
145151
var estimatedTimeRemaining: Duration
152+
var fileURL: URL?
146153
var propertiesInt: [MetatypeWrapper<Int>: Int]
147154
var propertiesDouble: [MetatypeWrapper<Double>: Double]
148155
var propertiesString: [MetatypeWrapper<String>: String]
@@ -192,6 +199,7 @@ extension ProgressManager {
192199
completedByteCount: children[idx].completedByteCount,
193200
throughput: children[idx].throughput,
194201
estimatedTimeRemaining: children[idx].estimatedTimeRemaining,
202+
fileURL: children[idx].fileURL,
195203
childPropertiesInt: children[idx].childPropertiesInt,
196204
childPropertiesDouble: children[idx].childPropertiesDouble,
197205
childPropertiesString: children[idx].childPropertiesString)
@@ -212,6 +220,7 @@ extension ProgressManager {
212220
completedByteCount: children[idx].completedByteCount,
213221
throughput: children[idx].throughput,
214222
estimatedTimeRemaining: children[idx].estimatedTimeRemaining,
223+
fileURL: children[idx].fileURL,
215224
childPropertiesInt: children[idx].childPropertiesInt,
216225
childPropertiesDouble: children[idx].childPropertiesDouble,
217226
childPropertiesString: children[idx].childPropertiesString)

Sources/FoundationEssentials/ProgressManager/ProgressManager+Values.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extension ProgressManager {
2525
internal var completedByteCountDirty = false
2626
internal var throughputDirty = false
2727
internal var estimatedTimeRemainingDirty = false
28+
internal var fileURLDirty = false
2829
internal var dirtyPropertiesInt: [MetatypeWrapper<Int>] = []
2930
internal var dirtyPropertiesDouble: [MetatypeWrapper<Double>] = []
3031
internal var dirtyPropertiesString: [MetatypeWrapper<String>] = []
@@ -171,6 +172,22 @@ extension ProgressManager {
171172
}
172173
}
173174

175+
public subscript(dyanamicMember key: KeyPath<ProgressManager.Properties, ProgressManager.Properties.FileURL.Type>) -> URL? {
176+
get {
177+
return state.fileURL
178+
}
179+
180+
set {
181+
guard newValue != state.fileURL else {
182+
return
183+
}
184+
185+
state.fileURL = newValue
186+
187+
fileURLDirty = true
188+
}
189+
}
190+
174191
public subscript<P: Property>(dynamicMember key: KeyPath<ProgressManager.Properties, P.Type>) -> Int where P.Value == Int, P.Summary == Int {
175192
get {
176193
return state.propertiesInt[MetatypeWrapper(P.self)] ?? P.defaultValue

Sources/FoundationEssentials/ProgressManager/ProgressManager.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ internal import _FoundationCollections
101101
completedByteCount: ProgressManager.Properties.CompletedByteCount.defaultValue,
102102
throughput: ProgressManager.Properties.Throughput.defaultValue,
103103
estimatedTimeRemaining: ProgressManager.Properties.EstimatedTimeRemaining.defaultValue,
104+
fileURL: ProgressManager.Properties.FileURL.defaultValue,
104105
propertiesInt: [:],
105106
propertiesDouble: [:],
106107
propertiesString: [:],
@@ -223,6 +224,10 @@ internal import _FoundationCollections
223224
return getUpdatedEstimatedTimeRemaining()
224225
}
225226

227+
public func summary(of property: ProgressManager.Properties.FileURL.Type) -> [URL] {
228+
return getUpdatedFileURL()
229+
}
230+
226231
/// Mutates any settable properties that convey information about progress.
227232
public func withProperties<T, E: Error>(
228233
_ closure: (inout sending Values) throws(E) -> sending T
@@ -276,6 +281,10 @@ internal import _FoundationCollections
276281
markSelfDirty(property: Properties.EstimatedTimeRemaining.self, parents: values.state.parents)
277282
}
278283

284+
if values.fileURLDirty {
285+
markSelfDirty(property: Properties.FileURL.self, parents: values.state.parents)
286+
}
287+
279288
if values.dirtyPropertiesInt.count > 0 {
280289
for property in values.dirtyPropertiesInt {
281290
markSelfDirty(property: property, parents: values.state.parents)
@@ -370,6 +379,7 @@ internal import _FoundationCollections
370379
completedByteCount: PropertyStateInt64(value: ProgressManager.Properties.CompletedByteCount.defaultSummary, isDirty: false),
371380
throughput: PropertyStateThroughput(value: ProgressManager.Properties.Throughput.defaultSummary, isDirty: false),
372381
estimatedTimeRemaining: PropertyStateDuration(value: ProgressManager.Properties.EstimatedTimeRemaining.defaultSummary, isDirty: false),
382+
fileURL: PropertyStateURL(value: ProgressManager.Properties.FileURL.defaultSummary, isDirty: false),
373383
childPropertiesInt: [:],
374384
childPropertiesDouble: [:],
375385
childPropertiesString: [:])

0 commit comments

Comments
 (0)