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,66 @@
//
// Created by merowing on 09/05/2014.
//
//
//
#import <PromiseKit/AnyPromise.h>
#import <Accounts/ACAccountStore.h>
/**
To import the `ACAccountStore` category:
use_frameworks!
pod "PromiseKit/Accounts"
And then in your sources:
@import PromiseKit;
*/
@interface ACAccountStore (PromiseKit)
/**
Obtains permission to access protected user properties.
@param type The account type.
@param options Can be nil.
@return A promise that resolves when the requested permissions have been
successfully obtained. The promise thens all accounts of the specified
type.
@see requestAccessToAccountsWithType:options:completion:
*/
- (AnyPromise *)requestAccessToAccountsWithType:(ACAccountType *)type options:(NSDictionary *)options NS_REFINED_FOR_SWIFT;
/**
Renews account credentials when the credentials are no longer valid.
@param account The account to renew credentials.
@return A promise that thens the `ACAccountCredentialRenewResult`.
*/
- (AnyPromise *)renewCredentialsForAccount:(ACAccount *)account NS_REFINED_FOR_SWIFT;
/**
Saves an account to the Accounts database.
@param account The account to save.
@return A promise that resolves when the account has been successfully
saved.
*/
- (AnyPromise *)saveAccount:(ACAccount *)account NS_REFINED_FOR_SWIFT;
/**
Removes an account from the account store.
@param account The account to remove.
@return A promise that resolves when the account has been successfully
removed.
*/
- (AnyPromise *)removeAccount:(ACAccount *)account NS_REFINED_FOR_SWIFT;
@end

View File

@@ -0,0 +1,48 @@
#import "ACAccountStore+AnyPromise.h"
#import <PromiseKit/PromiseKit.h>
@implementation ACAccountStore (PromiseKit)
- (AnyPromise *)requestAccessToAccountsWithType:(ACAccountType *)type options:(NSDictionary *)options {
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
[self requestAccessToAccountsWithType:type options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
resolve([self accountsWithAccountType:type]);
} else if (error) {
resolve(error);
} else {
error = [NSError errorWithDomain:PMKErrorDomain code:PMKAccessDeniedError userInfo:@{
NSLocalizedDescriptionKey: @"Access to the requested social service has been denied. Please enable access in your device settings."
}];
resolve(error);
}
}];
}];
}
- (AnyPromise *)renewCredentialsForAccount:(ACAccount *)account {
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
[self renewCredentialsForAccount:account completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) {
resolve(error ?: @(renewResult));
}];
}];
}
- (AnyPromise *)saveAccount:(ACAccount *)account {
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
[self saveAccount:account withCompletionHandler:^(BOOL success, NSError *error) {
resolve(error);
}];
}];
}
- (AnyPromise *)removeAccount:(ACAccount *)account {
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
[self removeAccount:account withCompletionHandler:^(BOOL success, NSError *error) {
resolve(error);
}];
}];
}
@end

View File

@@ -0,0 +1,59 @@
import Accounts
#if !PMKCocoaPods
import PromiseKit
#endif
/**
To import the `ACAccountStore` category:
use_frameworks!
pod "PromiseKit/ACAccountStore"
And then in your sources:
import PromiseKit
*/
extension ACAccountStore {
/// Renews account credentials when the credentials are no longer valid.
public func renewCredentials(for account: ACAccount) -> Promise<ACAccountCredentialRenewResult> {
return Promise { renewCredentials(for: account, completion: $0.resolve) }
}
/// Obtains permission to access protected user properties.
public func requestAccessToAccounts(with type: ACAccountType, options: [AnyHashable: Any]? = nil) -> Promise<Void> {
return Promise { seal in
requestAccessToAccounts(with: type, options: options, completion: { granted, error in
if granted {
seal.fulfill(())
} else if let error = error {
seal.reject(error)
} else {
seal.reject(PMKError.accessDenied)
}
})
}
}
/// Saves an account to the Accounts database.
public func saveAccount(_ account: ACAccount) -> Promise<Void> {
return Promise { saveAccount(account, withCompletionHandler: $0.resolve) }.asVoid()
}
/// Removes an account from the account store.
public func removeAccount(_ account: ACAccount) -> Promise<Void> {
return Promise { removeAccount(account, withCompletionHandler: $0.resolve) }.asVoid()
}
/// PromiseKit ACAccountStore errors
public enum PMKError: Error, CustomStringConvertible {
/// The request for accounts access was denied.
case accessDenied
public var description: String {
switch self {
case .accessDenied:
return "Access to the requested social service has been denied. Please enable access in your device settings."
}
}
}
}