Skip to content

Commit

Permalink
Added time estimation API
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-Spencer committed Jul 15, 2020
1 parent a1997f0 commit db6b8ad
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Sources/ProgressReporter/ProgressReporter.swift
Expand Up @@ -105,6 +105,27 @@ open class ProgressCoordinator: ProgressCensus {
public static let shared = ProgressCoordinator()
public var watcher: ProgressWatcher?

/// A rough estimate of the amount of time (seconds) it will take for a single
/// increment of progress when reported via `reportProgress(...)`
///
/// This value should be coordinated with the `anticipatedIncrementBatchSize`.
/// `ProgressCoordinator` uses this in concert with the batch size to provide time estimates.
public var anticipatedTimeForIncrement: TimeInterval = 0.5

/// An estimated batch size used to calculate an estimate of remaining time.
/// The default value is 1, meaning the batch size corresponds to one increment
/// of progress.
///
/// This is useful if you perform concurrent, asynchronous operations. For example,
/// if you use an `OperationQueue` to run multiple operations, you should specify
/// your expected maximum concurrent operation size.
///
/// Remember that the maximum concurrency sizes for an `OperationQueue` are much larger
/// in the iOS / iPadOS Simulator than they are on an actual device. Test this on-device
/// for the most accurate results.
public var anticipatedIncrementBatchSize: Int = 1

@Published public var timeRemaining: TimeInterval = 0.0
@Published public var rawProgress: Float = 0.0
public var progress: TangibleProgress {
get {
Expand Down Expand Up @@ -142,6 +163,21 @@ open class ProgressCoordinator: ProgressCensus {
updateReporterSafely()
}

@discardableResult
public func estimateTimeRemaining() -> TimeInterval {
let batch = Double(anticipatedIncrementBatchSize)
let count = Double(totalSteps)
let increment = anticipatedTimeForIncrement

let batches = count / batch
let totalDuration = increment * batches
let remainingProgress = Double(progress.progress)

let remainingDuration = totalDuration * remainingProgress
timeRemaining = TimeInterval(exactly: remainingDuration) ?? 0
return remainingDuration
}

private func updateReporterSafely() {
if Thread.isMainThread {
watcher?.hasProgressToReport(report: progress)
Expand Down

0 comments on commit db6b8ad

Please sign in to comment.