Add PromiseKit dependency
- Added PromiseKit dependency
This commit is contained in:
1
Carthage/Checkouts/PromiseKit/Extensions/Social/Sources/PMKSocial.h
vendored
Normal file
1
Carthage/Checkouts/PromiseKit/Extensions/Social/Sources/PMKSocial.h
vendored
Normal file
@@ -0,0 +1 @@
|
||||
#import "SLRequest+AnyPromise.h"
|
||||
48
Carthage/Checkouts/PromiseKit/Extensions/Social/Sources/SLComposeViewController+Promise.swift
vendored
Normal file
48
Carthage/Checkouts/PromiseKit/Extensions/Social/Sources/SLComposeViewController+Promise.swift
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
#if !os(OSX)
|
||||
|
||||
import Social.SLComposeViewController
|
||||
import UIKit.UIViewController
|
||||
#if !PMKCocoaPods
|
||||
import PromiseKit
|
||||
#endif
|
||||
|
||||
/**
|
||||
To import this `UIViewController` category:
|
||||
|
||||
use_frameworks!
|
||||
pod "PromiseKit/Social"
|
||||
|
||||
And then in your sources:
|
||||
|
||||
import PromiseKit
|
||||
*/
|
||||
extension UIViewController {
|
||||
/// Presents the message view controller and resolves with the user action.
|
||||
public func promise(_ vc: SLComposeViewController, animated: Bool = true, completion: (() -> Void)? = nil) -> Promise<Void> {
|
||||
present(vc, animated: animated, completion: completion)
|
||||
return Promise { seal in
|
||||
vc.completionHandler = { result in
|
||||
if result == .cancelled {
|
||||
seal.reject(SLComposeViewController.PMKError.cancelled)
|
||||
} else {
|
||||
seal.fulfill(())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension SLComposeViewController {
|
||||
/// Errors representing PromiseKit SLComposeViewController failures
|
||||
public enum PMKError: CancellableError {
|
||||
/// The user cancelled the view controller.
|
||||
case cancelled
|
||||
|
||||
/// - Returns: true
|
||||
public var isCancelled: Bool {
|
||||
switch self { case .cancelled: return true }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
36
Carthage/Checkouts/PromiseKit/Extensions/Social/Sources/SLRequest+AnyPromise.h
vendored
Normal file
36
Carthage/Checkouts/PromiseKit/Extensions/Social/Sources/SLRequest+AnyPromise.h
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// Created by merowing on 09/05/2014.
|
||||
//
|
||||
//
|
||||
//
|
||||
|
||||
#import <PromiseKit/AnyPromise.h>
|
||||
#import <Social/SLRequest.h>
|
||||
|
||||
/**
|
||||
To import the `SLRequest` category:
|
||||
|
||||
use_frameworks!
|
||||
pod "PromiseKit/Social"
|
||||
|
||||
And then in your sources:
|
||||
|
||||
@import PromiseKit;
|
||||
*/
|
||||
@interface SLRequest (PromiseKit)
|
||||
|
||||
/**
|
||||
Performs the request asynchronously.
|
||||
|
||||
@return A promise that fulfills with three parameters:
|
||||
|
||||
1) The response decoded as JSON.
|
||||
2) The `NSHTTPURLResponse`.
|
||||
3) The raw `NSData` response.
|
||||
|
||||
@warning *Note* If PromiseKit determines the response is not JSON, the first
|
||||
parameter will instead be plain `NSData`.
|
||||
*/
|
||||
- (AnyPromise *)promise NS_REFINED_FOR_SWIFT;
|
||||
|
||||
@end
|
||||
51
Carthage/Checkouts/PromiseKit/Extensions/Social/Sources/SLRequest+AnyPromise.m
vendored
Normal file
51
Carthage/Checkouts/PromiseKit/Extensions/Social/Sources/SLRequest+AnyPromise.m
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Created by merowing on 09/05/2014.
|
||||
//
|
||||
//
|
||||
//
|
||||
|
||||
#import <PromiseKit/PromiseKit.h>
|
||||
#import "SLRequest+AnyPromise.h"
|
||||
|
||||
#if COCOAPODS
|
||||
#import "NSURLSession+AnyPromise.h"
|
||||
#else
|
||||
#import <PMKFoundation/NSURLSession+AnyPromise.h>
|
||||
#endif
|
||||
|
||||
@implementation SLRequest (PromiseKit)
|
||||
|
||||
- (AnyPromise *)promise {
|
||||
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
|
||||
[self performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
|
||||
|
||||
assert(![NSThread isMainThread]);
|
||||
|
||||
if (error)
|
||||
return resolve(error);
|
||||
|
||||
NSInteger const statusCode = urlResponse.statusCode;
|
||||
if (statusCode < 200 || statusCode >= 300) {
|
||||
id userInfo = [NSMutableDictionary new];
|
||||
userInfo[PMKURLErrorFailingURLResponseKey] = urlResponse;
|
||||
userInfo[NSLocalizedDescriptionKey] = [NSHTTPURLResponse localizedStringForStatusCode:statusCode];
|
||||
|
||||
if (responseData) {
|
||||
id str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
|
||||
userInfo[PMKURLErrorFailingStringKey] = str;
|
||||
}
|
||||
|
||||
resolve([NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:userInfo]);
|
||||
}
|
||||
else if (PMKHTTPURLResponseIsJSON(urlResponse)) {
|
||||
id err = nil;
|
||||
id json = [NSJSONSerialization JSONObjectWithData:responseData options:PMKJSONDeserializationOptions error:&err];
|
||||
resolve(err ?: PMKManifold(json, urlResponse, responseData));
|
||||
} else {
|
||||
resolve(PMKManifold(responseData, urlResponse, responseData));
|
||||
}
|
||||
}];
|
||||
}];
|
||||
}
|
||||
|
||||
@end
|
||||
37
Carthage/Checkouts/PromiseKit/Extensions/Social/Sources/SLRequest+Promise.swift
vendored
Normal file
37
Carthage/Checkouts/PromiseKit/Extensions/Social/Sources/SLRequest+Promise.swift
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
#if !PMKCocoaPods
|
||||
import PMKFoundation
|
||||
import PromiseKit
|
||||
#endif
|
||||
import Social
|
||||
|
||||
/**
|
||||
To import the `SLRequest` category:
|
||||
|
||||
use_frameworks!
|
||||
pod "PromiseKit/Social"
|
||||
|
||||
And then in your sources:
|
||||
|
||||
import PromiseKit
|
||||
*/
|
||||
extension SLRequest {
|
||||
/**
|
||||
Performs the request asynchronously.
|
||||
|
||||
- Returns: A promise that fulfills with the response.
|
||||
- SeeAlso: `URLDataPromise`
|
||||
*/
|
||||
public func perform() -> Promise<(data: Data, response: HTTPURLResponse)> {
|
||||
return Promise { seal in
|
||||
perform { data, rsp, error in
|
||||
if let data = data, let rsp = rsp {
|
||||
seal.fulfill((data, rsp))
|
||||
} else if let error = error {
|
||||
seal.reject(error)
|
||||
} else {
|
||||
seal.reject(PMKError.invalidCallingConvention)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user