-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
54 changed files
with
1,907 additions
and
879 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
Sources/RadiantKit/BushelProgressUI/CopyOperation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
// | ||
// CopyOperation.swift | ||
// RadiantKit | ||
// | ||
// Created by Leo Dion. | ||
// Copyright © 2024 BrightDigit. | ||
// | ||
// Permission is hereby granted, free of charge, to any person | ||
// obtaining a copy of this software and associated documentation | ||
// files (the “Software”), to deal in the Software without | ||
// restriction, including without limitation the rights to use, | ||
// copy, modify, merge, publish, distribute, sublicense, and/or | ||
// sell copies of the Software, and to permit persons to whom the | ||
// Software is furnished to do so, subject to the following | ||
// conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
// OTHER DEALINGS IN THE SOFTWARE. | ||
// | ||
|
||
#if canImport(Observation) && (os(macOS) || os(iOS)) | ||
public import Foundation | ||
import Observation | ||
|
||
public import OSLog | ||
|
||
@MainActor @Observable | ||
public final class CopyOperation<ValueType: BinaryInteger & Sendable>: Identifiable { | ||
private let sourceURL: URL | ||
private let destinationURL: URL | ||
public let totalValue: ValueType? | ||
private let timeInterval: TimeInterval | ||
private nonisolated let getSize: @Sendable (URL) throws -> ValueType? | ||
private let copyFile: @Sendable (CopyPaths) async throws -> Void | ||
public var currentValue = ValueType.zero | ||
private var timer: Timer? | ||
private let logger: Logger? | ||
|
||
public nonisolated var id: URL { sourceURL } | ||
|
||
public init( | ||
sourceURL: URL, | ||
destinationURL: URL, | ||
totalValue: ValueType?, | ||
timeInterval: TimeInterval, | ||
logger: Logger?, | ||
getSize: @escaping @Sendable (URL) throws -> ValueType?, | ||
copyFile: @escaping @Sendable (CopyPaths) async throws -> Void | ||
) { | ||
self.sourceURL = sourceURL | ||
self.destinationURL = destinationURL | ||
self.totalValue = totalValue | ||
self.timeInterval = timeInterval | ||
self.getSize = getSize | ||
self.copyFile = copyFile | ||
self.logger = logger | ||
} | ||
|
||
private nonisolated func updateValue(_ currentValue: ValueType) { | ||
Task { await self.updatingValue(currentValue) } | ||
} | ||
|
||
private func updatingValue(_ currentValue: ValueType) { | ||
self.currentValue = currentValue | ||
if let totalValue = self.totalValue { | ||
guard self.currentValue < totalValue else { | ||
self.logger?.debug("Copy is complete based on size. Quitting timer.") | ||
self.killTimer() | ||
return | ||
} | ||
} | ||
else { | ||
self.logger?.warning("Total size is missing") | ||
} | ||
} | ||
|
||
private func starTimer() { | ||
timer = Timer.scheduledTimer(withTimeInterval: timeInterval, repeats: true) { | ||
[weak self] timer in | ||
guard let weakSelf = self else { | ||
timer.invalidate() | ||
return | ||
} | ||
weakSelf.logger?.debug("Timer On") | ||
assert(weakSelf.logger != nil) | ||
Task { | ||
let currentValue: ValueType? | ||
do { currentValue = try weakSelf.getSize(weakSelf.destinationURL) } | ||
catch { | ||
weakSelf.logger?.error("Unable to get size: \(error)") | ||
assertionFailure("Unable to get size: \(error)") | ||
currentValue = nil | ||
} | ||
if let currentValue { | ||
weakSelf.updateValue(currentValue) | ||
weakSelf.logger?.debug("Updating size to: \(currentValue)") | ||
} | ||
else { | ||
weakSelf.logger?.warning("Unable to get size") | ||
} | ||
} | ||
} | ||
} | ||
|
||
public func execute() async throws { | ||
self.logger?.debug("Starting Copy operating") | ||
await starTimer() | ||
do { try await self.copyFile(.init(fromURL: sourceURL, toURL: destinationURL)) } | ||
catch { | ||
self.logger?.error("Error Copying: \(error)") | ||
self.killTimer() | ||
throw error | ||
} | ||
self.logger?.debug("Copy is done. Quitting timer.") | ||
self.killTimer() | ||
} | ||
|
||
private func killTimer() { | ||
timer?.invalidate() | ||
timer = nil | ||
} | ||
} | ||
|
||
extension CopyOperation: ProgressOperation {} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// CopyPaths.swift | ||
// RadiantKit | ||
// | ||
// Created by Leo Dion. | ||
// Copyright © 2024 BrightDigit. | ||
// | ||
// Permission is hereby granted, free of charge, to any person | ||
// obtaining a copy of this software and associated documentation | ||
// files (the “Software”), to deal in the Software without | ||
// restriction, including without limitation the rights to use, | ||
// copy, modify, merge, publish, distribute, sublicense, and/or | ||
// sell copies of the Software, and to permit persons to whom the | ||
// Software is furnished to do so, subject to the following | ||
// conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
// OTHER DEALINGS IN THE SOFTWARE. | ||
// | ||
|
||
public import Foundation | ||
|
||
public struct CopyPaths { | ||
public let fromURL: URL | ||
public let toURL: URL | ||
} |
76 changes: 76 additions & 0 deletions
76
Sources/RadiantKit/BushelProgressUI/DownloadOperation.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// DownloadOperation.swift | ||
// RadiantKit | ||
// | ||
// Created by Leo Dion. | ||
// Copyright © 2024 BrightDigit. | ||
// | ||
// Permission is hereby granted, free of charge, to any person | ||
// obtaining a copy of this software and associated documentation | ||
// files (the “Software”), to deal in the Software without | ||
// restriction, including without limitation the rights to use, | ||
// copy, modify, merge, publish, distribute, sublicense, and/or | ||
// sell copies of the Software, and to permit persons to whom the | ||
// Software is furnished to do so, subject to the following | ||
// conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
// OTHER DEALINGS IN THE SOFTWARE. | ||
// | ||
|
||
#if canImport(Observation) && (os(macOS) || os(iOS)) | ||
public import Foundation | ||
import Observation | ||
|
||
@MainActor @Observable | ||
public final class DownloadOperation<ValueType: BinaryInteger & Sendable>: | ||
|
||
Identifiable, ProgressOperation, Sendable | ||
{ | ||
private let download: ObservableDownloader | ||
private let sourceURL: URL | ||
private let destinationURL: URL | ||
|
||
public nonisolated var id: URL { sourceURL } | ||
|
||
public var currentValue: ValueType { .init(download.totalBytesWritten) } | ||
|
||
public var totalValue: ValueType? { download.totalBytesExpectedToWrite.map(ValueType.init(_:)) } | ||
|
||
public init( | ||
sourceURL: URL, | ||
destinationURL: URL, | ||
totalBytesExpectedToWrite: ValueType?, | ||
configuration: URLSessionConfiguration? = nil, | ||
queue: OperationQueue? = nil | ||
) { | ||
assert(!sourceURL.isFileURL) | ||
assert(totalBytesExpectedToWrite != nil) | ||
self.sourceURL = sourceURL | ||
self.destinationURL = destinationURL | ||
self.download = .init( | ||
totalBytesExpectedToWrite: totalBytesExpectedToWrite, | ||
configuration: configuration, | ||
queue: queue | ||
) | ||
} | ||
|
||
public func execute() async throws { | ||
try await withCheckedThrowingContinuation { continuation in | ||
self.download.begin(from: self.sourceURL, to: self.destinationURL) { result in | ||
continuation.resume(with: result) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#endif |
50 changes: 50 additions & 0 deletions
50
Sources/RadiantKit/BushelProgressUI/FileOperationProgress.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// FileOperationProgress.swift | ||
// RadiantKit | ||
// | ||
// Created by Leo Dion. | ||
// Copyright © 2024 BrightDigit. | ||
// | ||
// Permission is hereby granted, free of charge, to any person | ||
// obtaining a copy of this software and associated documentation | ||
// files (the “Software”), to deal in the Software without | ||
// restriction, including without limitation the rights to use, | ||
// copy, modify, merge, publish, distribute, sublicense, and/or | ||
// sell copies of the Software, and to permit persons to whom the | ||
// Software is furnished to do so, subject to the following | ||
// conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be | ||
// included in all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, | ||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
// OTHER DEALINGS IN THE SOFTWARE. | ||
// | ||
|
||
#if canImport(Observation) && (os(macOS) || os(iOS)) | ||
public import Foundation | ||
import Observation | ||
|
||
@MainActor @Observable | ||
public final class FileOperationProgress<ValueType: BinaryInteger>: Identifiable { | ||
public let operation: any ProgressOperation<ValueType> | ||
|
||
public nonisolated var id: URL { operation.id } | ||
|
||
public var totalValueBytes: Int64? { operation.totalValue.map(Int64.init) } | ||
|
||
public var currentValueBytes: Int64 { Int64(operation.currentValue) } | ||
|
||
internal var currentValue: Double { Double(operation.currentValue) } | ||
|
||
internal var totalValue: Double? { operation.totalValue.map(Double.init) } | ||
|
||
public init(_ operation: any ProgressOperation<ValueType>) { self.operation = operation } | ||
} | ||
#endif |
Oops, something went wrong.