Add PromiseKit dependency

- Added PromiseKit dependency
This commit is contained in:
2018-11-15 22:08:00 -04:00
parent 2689d86c18
commit be7b6b5881
541 changed files with 46282 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
#import "UIView+AnyPromise.h"
#import "UIViewController+AnyPromise.h"
typedef NS_OPTIONS(NSInteger, PMKAnimationOptions) {
PMKAnimationOptionsNone = 1 << 0,
PMKAnimationOptionsAppear = 1 << 1,
PMKAnimationOptionsDisappear = 1 << 2,
};

View File

@@ -0,0 +1,80 @@
#if !PMKCocoaPods
import PromiseKit
#endif
import UIKit
#if !os(tvOS)
extension UIViewController {
#if swift(>=4.2)
/// Presents the UIImagePickerController, resolving with the user action.
public func promise(_ vc: UIImagePickerController, animate: PMKAnimationOptions = [.appear, .disappear], completion: (() -> Void)? = nil) -> Promise<[UIImagePickerController.InfoKey: Any]> {
let animated = animate.contains(.appear)
let proxy = UIImagePickerControllerProxy()
vc.delegate = proxy
present(vc, animated: animated, completion: completion)
return proxy.promise.ensure {
vc.presentingViewController?.dismiss(animated: animated, completion: nil)
}
}
#else
/// Presents the UIImagePickerController, resolving with the user action.
public func promise(_ vc: UIImagePickerController, animate: PMKAnimationOptions = [.appear, .disappear], completion: (() -> Void)? = nil) -> Promise<[String: Any]> {
let animated = animate.contains(.appear)
let proxy = UIImagePickerControllerProxy()
vc.delegate = proxy
present(vc, animated: animated, completion: completion)
return proxy.promise.ensure {
vc.presentingViewController?.dismiss(animated: animated, completion: nil)
}
}
#endif
}
@objc private class UIImagePickerControllerProxy: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
#if swift(>=4.2)
let (promise, seal) = Promise<[UIImagePickerController.InfoKey: Any]>.pending()
#else
let (promise, seal) = Promise<[String: Any]>.pending()
#endif
var retainCycle: AnyObject?
required override init() {
super.init()
retainCycle = self
}
#if swift(>=4.2)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
seal.fulfill(info)
retainCycle = nil
}
#else
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
seal.fulfill(info)
retainCycle = nil
}
#endif
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
seal.reject(UIImagePickerController.PMKError.cancelled)
retainCycle = nil
}
}
extension UIImagePickerController {
/// Errors representing PromiseKit UIImagePickerController failures
public enum PMKError: CancellableError {
/// The user cancelled the UIImagePickerController.
case cancelled
/// - Returns: true
public var isCancelled: Bool {
switch self {
case .cancelled:
return true
}
}
}
}
#endif

View File

@@ -0,0 +1,80 @@
#import <PromiseKit/AnyPromise.h>
#import <UIKit/UIView.h>
// Created by Masafumi Yoshida on 2014/07/11.
// Copyright (c) 2014年 DeNA. All rights reserved.
/**
To import the `UIView` category:
use_frameworks!
pod "PromiseKit/UIKit"
Or `UIKit` is one of the categories imported by the umbrella pod:
use_frameworks!
pod "PromiseKit"
And then in your sources:
@import PromiseKit;
*/
@interface UIView (PromiseKit)
/**
Animate changes to one or more views using the specified duration.
@param duration The total duration of the animations, measured in
seconds. If you specify a negative value or 0, the changes are made
without animating them.
@param animations A block object containing the changes to commit to the
views.
@return A promise that fulfills with a boolean NSNumber indicating
whether or not the animations actually finished.
*/
+ (AnyPromise *)promiseWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_REFINED_FOR_SWIFT;
/**
Animate changes to one or more views using the specified duration, delay,
options, and completion handler.
@param duration The total duration of the animations, measured in
seconds. If you specify a negative value or 0, the changes are made
without animating them.
@param delay The amount of time (measured in seconds) to wait before
beginning the animations. Specify a value of 0 to begin the animations
immediately.
@param options A mask of options indicating how you want to perform the
animations. For a list of valid constants, see UIViewAnimationOptions.
@param animations A block object containing the changes to commit to the
views.
@return A promise that fulfills with a boolean NSNumber indicating
whether or not the animations actually finished.
*/
+ (AnyPromise *)promiseWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations NS_REFINED_FOR_SWIFT;
/**
Performs a view animation using a timing curve corresponding to the
motion of a physical spring.
@return A promise that fulfills with a boolean NSNumber indicating
whether or not the animations actually finished.
*/
+ (AnyPromise *)promiseWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void (^)(void))animations NS_REFINED_FOR_SWIFT;
/**
Creates an animation block object that can be used to set up
keyframe-based animations for the current view.
@return A promise that fulfills with a boolean NSNumber indicating
whether or not the animations actually finished.
*/
+ (AnyPromise *)promiseWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options keyframeAnimations:(void (^)(void))animations NS_REFINED_FOR_SWIFT;
@end

View File

@@ -0,0 +1,64 @@
//
// UIView+PromiseKit_UIAnimation.m
// YahooDenaStudy
//
// Created by Masafumi Yoshida on 2014/07/11.
// Copyright (c) 2014 DeNA. All rights reserved.
//
#import <PromiseKit/PromiseKit.h>
#import "UIView+AnyPromise.h"
#define CopyPasta \
NSAssert([NSThread isMainThread], @"UIKit animation must be performed on the main thread"); \
\
if (![NSThread isMainThread]) { \
id error = [NSError errorWithDomain:PMKErrorDomain code:PMKInvalidUsageError userInfo:@{NSLocalizedDescriptionKey: @"Animation was attempted on a background thread"}]; \
return [AnyPromise promiseWithValue:error]; \
} \
\
PMKResolver resolve = nil; \
AnyPromise *promise = [[AnyPromise alloc] initWithResolver:&resolve];
@implementation UIView (PromiseKit)
+ (AnyPromise *)promiseWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations {
return [self promiseWithDuration:duration delay:0 options:0 animations:animations];
}
+ (AnyPromise *)promiseWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void(^)(void))animations
{
CopyPasta;
[UIView animateWithDuration:duration delay:delay options:options animations:animations completion:^(BOOL finished) {
resolve(@(finished));
}];
return promise;
}
+ (AnyPromise *)promiseWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay usingSpringWithDamping:(CGFloat)dampingRatio initialSpringVelocity:(CGFloat)velocity options:(UIViewAnimationOptions)options animations:(void(^)(void))animations
{
CopyPasta;
[UIView animateWithDuration:duration delay:delay usingSpringWithDamping:dampingRatio initialSpringVelocity:velocity options:options animations:animations completion:^(BOOL finished) {
resolve(@(finished));
}];
return promise;
}
+ (AnyPromise *)promiseWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewKeyframeAnimationOptions)options keyframeAnimations:(void(^)(void))animations
{
CopyPasta;
[UIView animateKeyframesWithDuration:duration delay:delay options:options animations:animations completion:^(BOOL finished) {
resolve(@(finished));
}];
return promise;
}
@end

View File

@@ -0,0 +1,115 @@
import UIKit.UIView
#if !PMKCocoaPods
import PromiseKit
#endif
/**
To import the `UIView` category:
use_frameworks!
pod "PromiseKit/UIKit"
Or `UIKit` is one of the categories imported by the umbrella pod:
use_frameworks!
pod "PromiseKit"
And then in your sources:
import PromiseKit
*/
public extension UIView {
#if swift(>=4.2)
/**
Animate changes to one or more views using the specified duration, delay,
options, and completion handler.
- Parameter duration: The total duration of the animations, measured in
seconds. If you specify a negative value or 0, the changes are made
without animating them.
- Parameter delay: The amount of time (measured in seconds) to wait before
beginning the animations. Specify a value of 0 to begin the animations
immediately.
- Parameter options: A mask of options indicating how you want to perform the
animations. For a list of valid constants, see UIViewAnimationOptions.
- Parameter animations: A block object containing the changes to commit to the
views.
- Returns: A promise that fulfills with a boolean NSNumber indicating
whether or not the animations actually finished.
*/
@discardableResult
static func animate(_: PMKNamespacer, duration: TimeInterval, delay: TimeInterval = 0, options: UIView.AnimationOptions = [], animations: @escaping () -> Void) -> Guarantee<Bool> {
return Guarantee { animate(withDuration: duration, delay: delay, options: options, animations: animations, completion: $0) }
}
@discardableResult
static func animate(_: PMKNamespacer, duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping damping: CGFloat, initialSpringVelocity: CGFloat, options: UIView.AnimationOptions = [], animations: @escaping () -> Void) -> Guarantee<Bool> {
return Guarantee { animate(withDuration: duration, delay: delay, usingSpringWithDamping: damping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: $0) }
}
@discardableResult
static func transition(_: PMKNamespacer, with view: UIView, duration: TimeInterval, options: UIView.AnimationOptions = [], animations: (() -> Void)?) -> Guarantee<Bool> {
return Guarantee { transition(with: view, duration: duration, options: options, animations: animations, completion: $0) }
}
@discardableResult
static func transition(_: PMKNamespacer, from: UIView, to: UIView, duration: TimeInterval, options: UIView.AnimationOptions = []) -> Guarantee<Bool> {
return Guarantee { transition(from: from, to: to, duration: duration, options: options, completion: $0) }
}
@discardableResult
static func perform(_: PMKNamespacer, animation: UIView.SystemAnimation, on views: [UIView], options: UIView.AnimationOptions = [], animations: (() -> Void)?) -> Guarantee<Bool> {
return Guarantee { perform(animation, on: views, options: options, animations: animations, completion: $0) }
}
#else
/**
Animate changes to one or more views using the specified duration, delay,
options, and completion handler.
- Parameter duration: The total duration of the animations, measured in
seconds. If you specify a negative value or 0, the changes are made
without animating them.
- Parameter delay: The amount of time (measured in seconds) to wait before
beginning the animations. Specify a value of 0 to begin the animations
immediately.
- Parameter options: A mask of options indicating how you want to perform the
animations. For a list of valid constants, see UIViewAnimationOptions.
- Parameter animations: A block object containing the changes to commit to the
views.
- Returns: A promise that fulfills with a boolean NSNumber indicating
whether or not the animations actually finished.
*/
@discardableResult
static func animate(_: PMKNamespacer, duration: TimeInterval, delay: TimeInterval = 0, options: UIViewAnimationOptions = [], animations: @escaping () -> Void) -> Guarantee<Bool> {
return Guarantee { animate(withDuration: duration, delay: delay, options: options, animations: animations, completion: $0) }
}
@discardableResult
static func animate(_: PMKNamespacer, duration: TimeInterval, delay: TimeInterval, usingSpringWithDamping damping: CGFloat, initialSpringVelocity: CGFloat, options: UIViewAnimationOptions = [], animations: @escaping () -> Void) -> Guarantee<Bool> {
return Guarantee { animate(withDuration: duration, delay: delay, usingSpringWithDamping: damping, initialSpringVelocity: initialSpringVelocity, options: options, animations: animations, completion: $0) }
}
@discardableResult
static func transition(_: PMKNamespacer, with view: UIView, duration: TimeInterval, options: UIViewAnimationOptions = [], animations: (() -> Void)?) -> Guarantee<Bool> {
return Guarantee { transition(with: view, duration: duration, options: options, animations: animations, completion: $0) }
}
@discardableResult
static func transition(_: PMKNamespacer, from: UIView, to: UIView, duration: TimeInterval, options: UIViewAnimationOptions = []) -> Guarantee<Bool> {
return Guarantee { transition(from: from, to: to, duration: duration, options: options, completion: $0) }
}
@discardableResult
static func perform(_: PMKNamespacer, animation: UISystemAnimation, on views: [UIView], options: UIViewAnimationOptions = [], animations: (() -> Void)?) -> Guarantee<Bool> {
return Guarantee { perform(animation, on: views, options: options, animations: animations, completion: $0) }
}
#endif
}

View File

@@ -0,0 +1,71 @@
#import <PromiseKit/PromiseKit.h>
#import <UIKit/UIViewController.h>
/**
To import the `UIViewController` category:
use_frameworks!
pod "PromiseKit/UIKit"
Or `UIKit` is one of the categories imported by the umbrella pod:
use_frameworks!
pod "PromiseKit"
And then in your sources:
@import PromiseKit;
*/
@interface UIViewController (PromiseKit)
/**
Presents a view controller modally.
If the view controller is one of the following:
- MFMailComposeViewController
- MFMessageComposeViewController
- UIImagePickerController
- SLComposeViewController
Then PromiseKit presents the view controller returning a promise that is
resolved as per the documentation for those classes. Eg. if you present a
`UIImagePickerController` the view controller will be presented for you
and the returned promise will resolve with the media the user selected.
[self promiseViewController:[MFMailComposeViewController new] animated:YES completion:nil].then(^{
//…
});
Otherwise PromiseKit expects your view controller to implement a
`promise` property. This promise will be returned from this method and
presentation and dismissal of the presented view controller will be
managed for you.
\@interface MyViewController: UIViewController
@property (readonly) AnyPromise *promise;
@end
@implementation MyViewController {
PMKResolver resolve;
}
- (void)viewDidLoad {
_promise = [[AnyPromise alloc] initWithResolver:&resolve];
}
- (void)later {
resolve(@"some fulfilled value");
}
@end
[self promiseViewController:[MyViewController new] aniamted:YES completion:nil].then(^(id value){
// value == @"some fulfilled value"
});
@return A promise that can be resolved by the presented view controller.
*/
- (AnyPromise *)promiseViewController:(UIViewController *)vc animated:(BOOL)animated completion:(void (^)(void))block NS_REFINED_FOR_SWIFT;
@end

View File

@@ -0,0 +1,140 @@
#import <UIKit/UINavigationController.h>
#import "UIViewController+AnyPromise.h"
#import <PromiseKit/PromiseKit.h>
#if PMKImagePickerController
#import <UIKit/UIImagePickerController.h>
#endif
@interface PMKGenericDelegate : NSObject <UINavigationControllerDelegate> {
@public
PMKResolver resolve;
}
+ (instancetype)delegateWithPromise:(AnyPromise **)promise;
@end
@interface UIViewController ()
- (AnyPromise*) promise;
@end
@implementation UIViewController (PromiseKit)
- (AnyPromise *)promiseViewController:(UIViewController *)vc animated:(BOOL)animated completion:(void (^)(void))block {
__kindof UIViewController *vc2present = vc;
AnyPromise *promise = nil;
if ([vc isKindOfClass:NSClassFromString(@"MFMailComposeViewController")]) {
PMKGenericDelegate *delegate = [PMKGenericDelegate delegateWithPromise:&promise];
[vc setValue:delegate forKey:@"mailComposeDelegate"];
}
else if ([vc isKindOfClass:NSClassFromString(@"MFMessageComposeViewController")]) {
PMKGenericDelegate *delegate = [PMKGenericDelegate delegateWithPromise:&promise];
[vc setValue:delegate forKey:@"messageComposeDelegate"];
}
#ifdef PMKImagePickerController
else if ([vc isKindOfClass:[UIImagePickerController class]]) {
PMKGenericDelegate *delegate = [PMKGenericDelegate delegateWithPromise:&promise];
[vc setValue:delegate forKey:@"delegate"];
}
#endif
else if ([vc isKindOfClass:NSClassFromString(@"SLComposeViewController")]) {
PMKResolver resolve;
promise = [[AnyPromise alloc] initWithResolver:&resolve];
[vc setValue:^(NSInteger result){
if (result == 0) {
resolve([NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]);
} else {
resolve(@(result));
}
} forKey:@"completionHandler"];
}
else if ([vc isKindOfClass:[UINavigationController class]])
vc = [(id)vc viewControllers].firstObject;
if (!vc) {
id userInfo = @{NSLocalizedDescriptionKey: @"nil or effective nil passed to promiseViewController"};
id err = [NSError errorWithDomain:PMKErrorDomain code:PMKInvalidUsageError userInfo:userInfo];
return [AnyPromise promiseWithValue:err];
}
if (!promise) {
if (![vc respondsToSelector:@selector(promise)]) {
id userInfo = @{NSLocalizedDescriptionKey: @"ViewController is not promisable"};
id err = [NSError errorWithDomain:PMKErrorDomain code:PMKInvalidUsageError userInfo:userInfo];
return [AnyPromise promiseWithValue:err];
}
promise = [vc valueForKey:@"promise"];
if (![promise isKindOfClass:[AnyPromise class]]) {
id userInfo = @{NSLocalizedDescriptionKey: @"The promise property is nil or not of type AnyPromise"};
id err = [NSError errorWithDomain:PMKErrorDomain code:PMKInvalidUsageError userInfo:userInfo];
return [AnyPromise promiseWithValue:err];
}
}
if (!promise.pending)
return promise;
[self presentViewController:vc2present animated:animated completion:block];
promise.ensure(^{
[vc2present.presentingViewController dismissViewControllerAnimated:animated completion:nil];
});
return promise;
}
@end
@implementation PMKGenericDelegate {
id retainCycle;
}
+ (instancetype)delegateWithPromise:(AnyPromise **)promise; {
PMKGenericDelegate *d = [PMKGenericDelegate new];
d->retainCycle = d;
*promise = [[AnyPromise alloc] initWithResolver:&d->resolve];
return d;
}
- (void)mailComposeController:(id)controller didFinishWithResult:(int)result error:(NSError *)error {
if (error != nil) {
resolve(error);
} else if (result == 0) {
resolve([NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]);
} else {
resolve(@(result));
}
retainCycle = nil;
}
- (void)messageComposeViewController:(id)controller didFinishWithResult:(int)result {
if (result == 2) {
id userInfo = @{NSLocalizedDescriptionKey: @"The attempt to save or send the message was unsuccessful."};
id error = [NSError errorWithDomain:PMKErrorDomain code:PMKOperationFailed userInfo:userInfo];
resolve(error);
} else {
resolve(@(result));
}
retainCycle = nil;
}
#ifdef PMKImagePickerController
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
id img = info[UIImagePickerControllerEditedImage] ?: info[UIImagePickerControllerOriginalImage];
resolve(PMKManifold(img, info));
retainCycle = nil;
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
resolve([NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]);
retainCycle = nil;
}
#endif
@end

View File

@@ -0,0 +1,14 @@
#if !PMKCocoaPods
import PromiseKit
#endif
import UIKit
@available(iOS 10, tvOS 10, *)
public extension UIViewPropertyAnimator {
func startAnimation(_: PMKNamespacer) -> Guarantee<UIViewAnimatingPosition> {
return Guarantee {
addCompletion($0)
startAnimation()
}
}
}