Skip to content

Commit

Permalink
Combine cancellation flag with progress tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
jwelton committed Jun 9, 2016
1 parent 3b492da commit 8e75ae3
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions Zip/Zip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public class Zip {
internal static var customFileExtensions: Set<String> = []

/**
Property used for cancellation of process
Static progress tracker (also used for cancellation)
*/
private static var operationShouldContinue = true
private static var progressTracker: NSProgress?

// MARK: Lifecycle

Expand Down Expand Up @@ -97,10 +97,10 @@ public class Zip {
totalSize += attributeFileSize
}

let progressTracker = NSProgress(totalUnitCount: Int64(totalSize))
progressTracker.cancellable = false
progressTracker.pausable = false
progressTracker.kind = NSProgressKindFile
progressTracker = NSProgress(totalUnitCount: Int64(totalSize))
progressTracker?.cancellable = true
progressTracker?.pausable = false
progressTracker?.kind = NSProgressKindFile

// Begin unzipping
let zip = unzOpen64(path)
Expand Down Expand Up @@ -188,12 +188,11 @@ public class Zip {
progressHandler(progress: (currentPosition/totalSize))
}

progressTracker.completedUnitCount = Int64(currentPosition)
progressTracker?.completedUnitCount = Int64(currentPosition)

} while (ret == UNZ_OK && ret != UNZ_END_OF_LIST_OF_FILE) && operationShouldContinue
} while (ret == UNZ_OK && ret != UNZ_END_OF_LIST_OF_FILE) && !(progressTracker?.cancelled ?? false)

guard operationShouldContinue else {
operationShouldContinue = true
guard !(progressTracker?.cancelled ?? false) else {
throw ZipError.OperationCancelled
}

Expand All @@ -202,7 +201,7 @@ public class Zip {
progressHandler(progress: 1.0)
}

progressTracker.completedUnitCount = Int64(totalSize)
progressTracker?.completedUnitCount = Int64(totalSize)

}

Expand Down Expand Up @@ -252,10 +251,10 @@ public class Zip {
catch {}
}

let progressTracker = NSProgress(totalUnitCount: Int64(totalSize))
progressTracker.cancellable = false
progressTracker.pausable = false
progressTracker.kind = NSProgressKindFile
progressTracker = NSProgress(totalUnitCount: Int64(totalSize))
progressTracker?.cancellable = true
progressTracker?.pausable = false
progressTracker?.kind = NSProgressKindFile

// Begin Zipping
let zip = zipOpen(destinationPath, APPEND_STATUS_CREATE)
Expand Down Expand Up @@ -298,8 +297,7 @@ public class Zip {
}
var length: Int = 0
while (feof(input) == 0) {
guard operationShouldContinue else {
operationShouldContinue = true
guard !(progressTracker?.cancelled ?? false) else {
throw ZipError.OperationCancelled
}

Expand All @@ -312,7 +310,7 @@ public class Zip {
progressHandler(progress: (currentPosition/totalSize))
}

progressTracker.completedUnitCount = Int64(currentPosition)
progressTracker?.completedUnitCount = Int64(currentPosition)

zipCloseFileInZip(zip)
free(buffer)
Expand All @@ -326,14 +324,14 @@ public class Zip {
progressHandler(progress: 1.0)
}

progressTracker.completedUnitCount = Int64(totalSize)
progressTracker?.completedUnitCount = Int64(totalSize)
}

/**
Set the cancel operation flag
*/
public class func cancelCurrentOperation() {
operationShouldContinue = false
progressTracker?.cancel()
}

/**
Expand Down

0 comments on commit 8e75ae3

Please sign in to comment.