Skip to content

'Cancel' for PromiseKit option 2 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
github "mxcl/PromiseKit" ~> 6.0
#github "mxcl/PromiseKit" ~> 6.0
github "dougzilla32/PromiseKit" "PMKCancel"
#github "PromiseKit/Cancel" ~> 1.0
github "dougzilla32/Cancel" ~> 1.0
3 changes: 2 additions & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github "mxcl/PromiseKit" "6.3.3"
github "dougzilla32/Cancel" "1.0.0"
github "dougzilla32/PromiseKit" "a0217bd7b69af68237dcdeee0197e63259b9d445"
6 changes: 6 additions & 0 deletions PMKUIKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
63C9C4581D5D339B00101ECE /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 630B2DF51D5D0AD400DC10E9 /* [email protected] */; };
63C9C45E1D5D341600101ECE /* PMKUIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 63C7FFA71D5BEE09003BAE60 /* PMKUIKit.framework */; };
63C9C45F1D5D341600101ECE /* PMKUIKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 63C7FFA71D5BEE09003BAE60 /* PMKUIKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
911E6FC620F571F7006F49B9 /* TestUIViewPropertyAnimator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 911E6FC520F571F7006F49B9 /* TestUIViewPropertyAnimator.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -95,6 +96,7 @@
63C9C4451D5D334700101ECE /* PMKTestsHost.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PMKTestsHost.app; sourceTree = BUILT_PRODUCTS_DIR; };
63CCF8121D5C0C4E00503216 /* Cartfile */ = {isa = PBXFileReference; lastKnownFileType = text; path = Cartfile; sourceTree = "<group>"; };
63CCF8171D5C11B500503216 /* Carthage.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Carthage.xcconfig; sourceTree = "<group>"; };
911E6FC520F571F7006F49B9 /* TestUIViewPropertyAnimator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestUIViewPropertyAnimator.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -186,6 +188,7 @@
637E2C8E1D5C2E720043E370 /* TestUIImagePickerController.swift */,
637E2C8F1D5C2E720043E370 /* TestUIViewController.m */,
6332142A1D83CD17009F67CE /* TestUIView.swift */,
911E6FC520F571F7006F49B9 /* TestUIViewPropertyAnimator.swift */,
637E2C9A1D5C2F600043E370 /* infrastructure.swift */,
);
path = Tests;
Expand Down Expand Up @@ -352,6 +355,7 @@
);
inputPaths = (
PromiseKit,
PMKCancel,
);
name = "Embed Carthage Frameworks";
outputPaths = (
Expand All @@ -367,6 +371,7 @@
);
inputPaths = (
PromiseKit,
PMKCancel,
);
name = "Embed Carthage Frameworks";
outputPaths = (
Expand Down Expand Up @@ -402,6 +407,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
911E6FC620F571F7006F49B9 /* TestUIViewPropertyAnimator.swift in Sources */,
637E2C951D5C2E720043E370 /* TestUIImagePickerController.swift in Sources */,
637E2C961D5C2E720043E370 /* TestUIViewController.m in Sources */,
637E2C9B1D5C2F600043E370 /* infrastructure.swift in Sources */,
Expand Down
21 changes: 21 additions & 0 deletions Sources/UIViewPropertyAnimator+Promise.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if !PMKCocoaPods
import PMKCancel
import PromiseKit
#endif
import UIKit
Expand All @@ -12,3 +13,23 @@ public extension UIViewPropertyAnimator {
}
}
}

//////////////////////////////////////////////////////////// Cancellation

@available(iOS 10, tvOS 10, *)
extension UIViewPropertyAnimator: CancellableTask {
public func cancel() {
stopAnimation(true)
}

public var isCancelled: Bool {
return (state == .inactive) && (fractionComplete < 1.0)
}

public func startAnimationCC(_: PMKNamespacer) -> CancellablePromise<UIViewAnimatingPosition> {
return CancellablePromise(task: self) {
addCompletion($0.fulfill)
startAnimation()
}
}
}
47 changes: 47 additions & 0 deletions Tests/TestUIViewPropertyAnimator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import PromiseKit
import PMKCancel
import PMKUIKit
import XCTest
import UIKit

@available(iOS 10, tvOS 10, *)
class UIViewPropertyAnimatorTests: XCTestCase {
func test() {
let ex1 = expectation(description: "")
let ex2 = expectation(description: "")

let animator = UIViewPropertyAnimator(duration: 0.1, curve: .easeIn, animations: { [weak self] in
ex1.fulfill()
})
animator.startAnimation(.promise).done { _ in
ex2.fulfill()
}

waitForExpectations(timeout: 1)
}
}

//////////////////////////////////////////////////////////// Cancellation

@available(iOS 10, tvOS 10, *)
extension UIViewPropertyAnimatorTests {
func testCancel() {
let ex1 = expectation(description: "")
let ex2 = expectation(description: "")

let animator = UIViewPropertyAnimator(duration: 0.1, curve: .easeIn, animations: { [weak self] in
ex1.fulfill()
})
let p = animator.startAnimationCC(.promise).done { _ in
XCTFail()
}.catch(policy: .allErrors) { error in
error.isCancelled ? ex2.fulfill() : XCTFail("Error: \(error)")
}
p.cancel()

XCTAssert(animator.isCancelled)
XCTAssert(p.isCancelled)

waitForExpectations(timeout: 1)
}
}