Skip to content

Commit 2799401

Browse files
committed
'Cancel' for PromiseKit
1 parent 41bab77 commit 2799401

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

Cartfile.resolved

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github "mxcl/PromiseKit" "6.3.3"
1+
github "mxcl/PromiseKit" "6.3.4"

PMKUIKit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
63C9C4581D5D339B00101ECE /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 630B2DF51D5D0AD400DC10E9 /* [email protected] */; };
2626
63C9C45E1D5D341600101ECE /* PMKUIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 63C7FFA71D5BEE09003BAE60 /* PMKUIKit.framework */; };
2727
63C9C45F1D5D341600101ECE /* PMKUIKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 63C7FFA71D5BEE09003BAE60 /* PMKUIKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
28+
911E6FC620F571F7006F49B9 /* TestUIViewPropertyAnimator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 911E6FC520F571F7006F49B9 /* TestUIViewPropertyAnimator.swift */; };
2829
/* End PBXBuildFile section */
2930

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

100102
/* Begin PBXFrameworksBuildPhase section */
@@ -186,6 +188,7 @@
186188
637E2C8E1D5C2E720043E370 /* TestUIImagePickerController.swift */,
187189
637E2C8F1D5C2E720043E370 /* TestUIViewController.m */,
188190
6332142A1D83CD17009F67CE /* TestUIView.swift */,
191+
911E6FC520F571F7006F49B9 /* TestUIViewPropertyAnimator.swift */,
189192
637E2C9A1D5C2F600043E370 /* infrastructure.swift */,
190193
);
191194
path = Tests;
@@ -402,6 +405,7 @@
402405
isa = PBXSourcesBuildPhase;
403406
buildActionMask = 2147483647;
404407
files = (
408+
911E6FC620F571F7006F49B9 /* TestUIViewPropertyAnimator.swift in Sources */,
405409
637E2C951D5C2E720043E370 /* TestUIImagePickerController.swift in Sources */,
406410
637E2C961D5C2E720043E370 /* TestUIViewController.m in Sources */,
407411
637E2C9B1D5C2F600043E370 /* infrastructure.swift in Sources */,

Sources/UIViewPropertyAnimator+Promise.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,23 @@ public extension UIViewPropertyAnimator {
1212
}
1313
}
1414
}
15+
16+
//////////////////////////////////////////////////////////// Cancellation
17+
18+
@available(iOS 10, tvOS 10, *)
19+
extension UIViewPropertyAnimator: CancellableTask {
20+
public func cancel() {
21+
stopAnimation(true)
22+
}
23+
24+
public var isCancelled: Bool {
25+
return (state == .inactive) && (fractionComplete < 1.0)
26+
}
27+
28+
public func startAnimationCC(_: PMKNamespacer) -> CancellablePromise<UIViewAnimatingPosition> {
29+
return CancellablePromise(task: self) {
30+
addCompletion($0.fulfill)
31+
startAnimation()
32+
}
33+
}
34+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import PromiseKit
2+
import PMKUIKit
3+
import XCTest
4+
import UIKit
5+
6+
@available(iOS 10, tvOS 10, *)
7+
class UIViewPropertyAnimatorTests: XCTestCase {
8+
func test() {
9+
let ex1 = expectation(description: "")
10+
let ex2 = expectation(description: "")
11+
12+
let animator = UIViewPropertyAnimator(duration: 0.1, curve: .easeIn, animations: { [weak self] in
13+
ex1.fulfill()
14+
})
15+
animator.startAnimation(.promise).done { _ in
16+
ex2.fulfill()
17+
}
18+
19+
waitForExpectations(timeout: 1)
20+
}
21+
}
22+
23+
//////////////////////////////////////////////////////////// Cancellation
24+
25+
@available(iOS 10, tvOS 10, *)
26+
extension UIViewPropertyAnimatorTests {
27+
func testCancel() {
28+
let ex1 = expectation(description: "")
29+
let ex2 = expectation(description: "")
30+
31+
let animator = UIViewPropertyAnimator(duration: 0.1, curve: .easeIn, animations: { [weak self] in
32+
ex1.fulfill()
33+
})
34+
let p = animator.startAnimationCC(.promise).done { _ in
35+
XCTFail()
36+
}.catch(policy: .allErrors) { error in
37+
error.isCancelled ? ex2.fulfill() : XCTFail("Error: \(error)")
38+
}
39+
p.cancel()
40+
41+
XCTAssert(animator.isCancelled)
42+
XCTAssert(p.isCancelled)
43+
44+
waitForExpectations(timeout: 1)
45+
}
46+
}

0 commit comments

Comments
 (0)