From a3ad497206b6e0588a8f661a7533dd46992e0dbf Mon Sep 17 00:00:00 2001 From: Doug Date: Wed, 18 Jul 2018 17:44:50 -0700 Subject: [PATCH 1/3] 'Cancel' for PromiseKit --- Cartfile | 2 + Cartfile.resolved | 3 +- PMKUIKit.xcodeproj/project.pbxproj | 4 ++ Sources/UIViewPropertyAnimator+Promise.swift | 21 +++++++++ Tests/TestUIViewPropertyAnimator.swift | 47 ++++++++++++++++++++ 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 Tests/TestUIViewPropertyAnimator.swift diff --git a/Cartfile b/Cartfile index 2bfea98..c2d2433 100644 --- a/Cartfile +++ b/Cartfile @@ -1 +1,3 @@ github "mxcl/PromiseKit" ~> 6.0 +#github "PromiseKit/Cancel" ~> 1.0 +github "dougzilla32/Cancel" ~> 1.0 diff --git a/Cartfile.resolved b/Cartfile.resolved index a1be206..6d512a1 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1 +1,2 @@ -github "mxcl/PromiseKit" "6.3.3" +github "dougzilla32/Cancel" "1.0.0" +github "mxcl/PromiseKit" "6.3.4" diff --git a/PMKUIKit.xcodeproj/project.pbxproj b/PMKUIKit.xcodeproj/project.pbxproj index fff1565..8c228e8 100644 --- a/PMKUIKit.xcodeproj/project.pbxproj +++ b/PMKUIKit.xcodeproj/project.pbxproj @@ -25,6 +25,7 @@ 63C9C4581D5D339B00101ECE /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 630B2DF51D5D0AD400DC10E9 /* Default-568h@2x.png */; }; 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 */ @@ -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 = ""; }; 63CCF8171D5C11B500503216 /* Carthage.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = Carthage.xcconfig; sourceTree = ""; }; + 911E6FC520F571F7006F49B9 /* TestUIViewPropertyAnimator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestUIViewPropertyAnimator.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -186,6 +188,7 @@ 637E2C8E1D5C2E720043E370 /* TestUIImagePickerController.swift */, 637E2C8F1D5C2E720043E370 /* TestUIViewController.m */, 6332142A1D83CD17009F67CE /* TestUIView.swift */, + 911E6FC520F571F7006F49B9 /* TestUIViewPropertyAnimator.swift */, 637E2C9A1D5C2F600043E370 /* infrastructure.swift */, ); path = Tests; @@ -402,6 +405,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 */, diff --git a/Sources/UIViewPropertyAnimator+Promise.swift b/Sources/UIViewPropertyAnimator+Promise.swift index 34ee140..c442e68 100644 --- a/Sources/UIViewPropertyAnimator+Promise.swift +++ b/Sources/UIViewPropertyAnimator+Promise.swift @@ -1,4 +1,5 @@ #if !PMKCocoaPods +import PMKCancel import PromiseKit #endif import UIKit @@ -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 { + return CancellablePromise(task: self) { + addCompletion($0.fulfill) + startAnimation() + } + } +} diff --git a/Tests/TestUIViewPropertyAnimator.swift b/Tests/TestUIViewPropertyAnimator.swift new file mode 100644 index 0000000..292a8c5 --- /dev/null +++ b/Tests/TestUIViewPropertyAnimator.swift @@ -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) + } +} From 7c917f3e1a67fb5a098073228d74e994fca150df Mon Sep 17 00:00:00 2001 From: Doug Date: Wed, 18 Jul 2018 23:11:34 -0700 Subject: [PATCH 2/3] 'Cancel' for PromiseKit -- + for PMKCancel, change watchOS deployment target from 3.0 to 2.0 + include PMKCancel in the 'Embed Carthage Frameworks' build phase script for all extensions + include PMKCancel in SPM config for Alamofire and Foundation --- PMKUIKit.xcodeproj/project.pbxproj | 2 ++ 1 file changed, 2 insertions(+) diff --git a/PMKUIKit.xcodeproj/project.pbxproj b/PMKUIKit.xcodeproj/project.pbxproj index 8c228e8..4e7aaf4 100644 --- a/PMKUIKit.xcodeproj/project.pbxproj +++ b/PMKUIKit.xcodeproj/project.pbxproj @@ -355,6 +355,7 @@ ); inputPaths = ( PromiseKit, + PMKCancel, ); name = "Embed Carthage Frameworks"; outputPaths = ( @@ -370,6 +371,7 @@ ); inputPaths = ( PromiseKit, + PMKCancel, ); name = "Embed Carthage Frameworks"; outputPaths = ( From 50ba53de1383c6811089ba3ddeb39c94ca58d8c2 Mon Sep 17 00:00:00 2001 From: dougzilla Date: Thu, 19 Jul 2018 15:07:36 -0700 Subject: [PATCH 3/3] 'Cancel' for PromiseKit -- for all Extensions point Cartfile at the forked version of PromiseKit --- Cartfile | 3 ++- Cartfile.resolved | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cartfile b/Cartfile index c2d2433..cdd4145 100644 --- a/Cartfile +++ b/Cartfile @@ -1,3 +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 diff --git a/Cartfile.resolved b/Cartfile.resolved index 6d512a1..e1cadd5 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,2 +1,2 @@ github "dougzilla32/Cancel" "1.0.0" -github "mxcl/PromiseKit" "6.3.4" +github "dougzilla32/PromiseKit" "a0217bd7b69af68237dcdeee0197e63259b9d445"