-
Notifications
You must be signed in to change notification settings - Fork 1
/
CloudKit+Convenience.m
175 lines (129 loc) · 5.49 KB
/
CloudKit+Convenience.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// CloudKit+Convenience.m
// Ringtonic
//
// Created by Alexander Ivanov on 19.11.16.
// Copyright © 2016 Alexander Ivanov. All rights reserved.
//
#import "CloudKit+Convenience.h"
@implementation CKContainer (Convenience)
- (void)fetchUserRecordID:(void (^)(CKRecordID *))completionHandler {
[self fetchUserRecordIDWithCompletionHandler:^(CKRecordID * _Nullable recordID, NSError * _Nullable error) {
if (completionHandler)
completionHandler(recordID);
[error log:@"fetchUserRecordIDWithCompletionHandler:"];
}];
}
- (void)startOperation:(CKOperation *)operation allowsCellularAccess:(BOOL)allowsCellularAccess {
// operation.allowsCellularAccess = allowsCellularAccess;
// operation.container = self;
[operation start];
}
//- (CKModifyBadgeOperation *)modifyBadge:(NSUInteger)badgeValue completionHandler:(void (^)(BOOL success))completionHandler {
// CKModifyBadgeOperation *operation = [[CKModifyBadgeOperation alloc] initWithBadgeValue:badgeValue];
// operation.modifyBadgeCompletionBlock = ^(NSError *operationError) {
// if (completionHandler)
// completionHandler(operationError == Nil);
//
// [operationError log:@"modifyBadge:"];
// };
// [self startOperation:operation allowsCellularAccess:YES];
//
// return operation;
//}
- (CKFetchRecordsOperation *)fetchCurrentUserRecord:(void(^)(CKRecord *record))completionHandler {
CKFetchRecordsOperation *operation = [CKFetchRecordsOperation fetchCurrentUserRecordOperation];
operation.fetchRecordsCompletionBlock = ^(NSDictionary<CKRecordID *,CKRecord *> *recordsByRecordID, NSError *operationError) {
if (completionHandler)
completionHandler(recordsByRecordID.allValues.firstObject);
[operationError log:@"fetchCurrentUserRecord:"];
};
[self startOperation:operation allowsCellularAccess:YES];
return operation;
}
@end
@implementation CKNotification (Convenience)
- (NSString *)alertLocalization {
return self.alertLocalizationKey && self.alertLocalizationArgs ? [NSString stringWithFormat:self.alertLocalizationKey arguments:self.alertLocalizationArgs] : self.alertBody;
}
- (BOOL)hasAlert {
return self.alertBody.length || self.alertLocalizationKey.length;
}
@end
@implementation CKNotificationInfo (Convenience)
- (BOOL)hasAlert {
return self.alertBody.length || self.alertLocalizationKey.length;
}
@end
@implementation CKRecord (Comnvenience)
- (NSDictionary *)allKeyValuePairs {
NSArray *keys = [self allKeys];
NSMutableDictionary *pairs = [NSMutableDictionary dictionaryWithCapacity:keys.count];
for (NSString *key in keys)
pairs[key] = self[key];
return pairs;
}
@end
@implementation CKReference (Convenience)
- (instancetype)initWithRecord:(CKRecord *)record {
return [self initWithRecord:record action:CKReferenceActionNone];
}
- (instancetype)initWithRecordID:(CKRecordID *)recordID {
return [self initWithRecordID:recordID action:CKReferenceActionNone];
}
@end
#define KEY_EQUALS_TO @"%K == %@"
#define KEY_IS_IN @"%K IN %@"
#define KEY_GREATER_THAN @"%K > %@"
#define KEY_LESS_THAN @"%K < %@"
@implementation NSPredicate (Convenience)
+ (NSPredicate *)truePredicate {
return [self predicateWithValue:YES];
}
+ (NSPredicate *)falsePredicate {
return [self predicateWithValue:NO];
}
+ (NSPredicate *)predicateWithKeys:(NSDictionary<NSString *, __kindof id <CKRecordValue>> *)keys {
NSString *format = [[NSArray arrayWithObject:KEY_EQUALS_TO count:keys.count] componentsJoinedByString:@" AND "];
NSArray *array = [keys array];
return [self predicateWithFormat:format argumentArray:array];
}
+ (NSPredicate *)predicateWithKey:(NSString *)key value:(__kindof id <CKRecordValue>)value {
return [self predicateWithKeys:@{ key : value }];
}
+ (NSPredicate *)predicateWithKey:(NSString *)key values:(NSArray<__kindof id<CKRecordValue>> *)values {
return [self predicateWithFormat:KEY_IS_IN, key, values];
}
+ (NSPredicate *)predicateWithRecordID:(CKRecordID *)recordID {
return recordID ? [self predicateWithKeys:@{ KEY_RECORD_ID : [[CKReference alloc] initWithRecordID:recordID] }] : Nil;
}
+ (NSPredicate *)predicateWithCreatorUserRecordID:(CKRecordID *)recordID {
return recordID ? [self predicateWithKey:KEY_CREATOR_USER_RECORD_ID value:[[CKReference alloc] initWithRecordID:recordID]] : Nil;
}
+ (NSPredicate *)predicateWithCreatorUserRecordIDs:(NSArray<CKRecordID *> *)recordIDs {
return recordIDs.count ? [self predicateWithKey:KEY_CREATOR_USER_RECORD_ID values:[recordIDs map:^id(CKRecordID *obj) {
return [[CKReference alloc] initWithRecordID:obj];
}]] : Nil;
}
+ (NSPredicate *)predicateWithKey:(NSString *)key greaterThan:(__kindof id <CKRecordValue>)value {
return [self predicateWithFormat:KEY_GREATER_THAN, key, value];
}
+ (NSPredicate *)predicateWithKey:(NSString *)key lessThan:(__kindof id <CKRecordValue>)value {
return [self predicateWithFormat:KEY_LESS_THAN, key, value];
}
+ (NSPredicate *)predicateWithCreationDateGreaterThan:(NSDate *)date {
return [self predicateWithKey:KEY_CREATION_DATE greaterThan:date ? date : [NSDate date]];
}
+ (NSPredicate *)predicateWithCreationDateLessThan:(NSDate *)date {
return [self predicateWithKey:KEY_CREATION_DATE lessThan:date ? date : [NSDate date]];
}
+ (NSPredicate *)predicateWithModificationDateGreaterThan:(NSDate *)date {
return [self predicateWithKey:KEY_MODIFICATION_DATE greaterThan:date ? date : [NSDate date]];
}
+ (NSPredicate *)predicateWithModificationDateLessThan:(NSDate *)date {
return [self predicateWithKey:KEY_MODIFICATION_DATE lessThan:date ? date : [NSDate date]];
}
- (NSPredicate *)notPredicate {
return [NSCompoundPredicate notPredicateWithSubpredicate:self];
}
@end