-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSLToast.m
More file actions
363 lines (297 loc) · 11.7 KB
/
Copy pathSLToast.m
File metadata and controls
363 lines (297 loc) · 11.7 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
// SLToast.m
// SLToastKit
//
// Created by Gregory Combs on 7/10/16.
// Copyright (C) 2016 Gregory Combs [gcombs at gmail]
// See LICENSE.txt for details.
//
#import "SLToast.h"
#import "SLTypeCheck.h"
const struct SLToastKeys SLToastKeys = {
.identifier = @"toastId",
.type = @"type",
.title = @"title",
.subtitle = @"subtitle",
.image = @"image",
.duration = @"duration",
.status = @"status",
};
@interface SLToast ()
@property (nonatomic,copy) NSString *toastId;
@property (nonatomic,assign) SLToastType type;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *subtitle;
@property (nonatomic,strong) UIImage *image;
@property (nonatomic,assign) NSTimeInterval duration;
@end
NSUInteger SLValueHash(NSUInteger hash, NSUInteger hashIndex) {
static size_t const uintBitSize = (__CHAR_BIT__ * sizeof(NSUInteger));
size_t howmuch = uintBitSize / (hashIndex+1);
hash = (hash) ?: 31; // accounts for nil objects
return ((hash << howmuch) | (hash >> (uintBitSize - howmuch)));
}
@implementation SLToast
- (instancetype)initWithIdentifier:(nonnull NSString *)toastId
type:(SLToastType)type
title:(nullable NSString *)title
subtitle:(nullable NSString *)subtitle
image:(nullable UIImage *)image
duration:(NSTimeInterval)duration
{
self = [super init];
if (self)
{
_toastId = [SLTypeNonEmptyStringOrNil(toastId) copy];
_title = [SLTypeStringOrNil(title) copy];
_subtitle = [SLTypeStringOrNil(subtitle) copy];
_image = SLTypeImageOrNil(image);
_type = type;
_duration = duration;
if (!self.isValid)
return nil;
}
return self;
}
+ (instancetype)toastWithIdentifier:(NSString *)itemId type:(SLToastType)type title:(NSString *)title subtitle:(NSString *)subtitle image:(UIImage *)image duration:(NSTimeInterval)duration
{
return [[self alloc] initWithIdentifier:itemId type:type title:title subtitle:subtitle image:image duration:duration];
}
- (instancetype)init
{
self = [self initWithIdentifier:@"<INVALID_INITIALIZER>"
type:SLToastTypeError
title:nil
subtitle:nil
image:nil
duration:0];
NSAssert(FALSE, @"Must use the designated initializer, as this is an immutable class");
return self;
}
- (instancetype)initWithDictionary:(NSDictionary *)dictionary
{
if (!SLTypeDictionaryOrNil(dictionary))
return nil;
struct SLToastKeys keys = SLToastKeys;
NSString *toastId = SLTypeNonEmptyStringOrNil(dictionary[keys.identifier]);
if (!toastId)
return nil;
NSString *title = SLTypeStringOrNil(dictionary[keys.title]);
NSString *subtitle = SLTypeStringOrNil(dictionary[keys.subtitle]);
UIImage *image = SLTypeImageOrNil(dictionary[keys.image]);
NSTimeInterval duration = [SLTypeNumberOrNil(dictionary[keys.duration]) doubleValue];
SLToastType type = [SLTypeNumberOrNil(dictionary[keys.type]) unsignedIntegerValue];
if (type < SLToastTypeInfo || type > SLToastTypeActivity)
type = SLToastTypeError; // invalid toast type
SLToastStatus status = [SLTypeNumberOrNil(dictionary[keys.status]) unsignedIntegerValue];
if (status < SLToastStatusUnknown || status > SLToastStatusFinished)
status = SLToastStatusUnknown; // invalid toast status
self = [self initWithIdentifier:toastId
type:type
title:title
subtitle:subtitle
image:image
duration:duration];
if (self)
self.status = status;
return self;
}
- (BOOL)isValid
{
SLToastStatus status = self.status;
SLToastType type = self.type;
return (SLTypeNonEmptyStringOrNil(self.toastId) != nil
&& status >= SLToastStatusMinimumValue
&& status <= SLToastStatusMaximumValue
&& type >= SLToastTypeMinimumValue
&& type <= SLToastTypeMaximumValue);
}
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone
{
NSString *toastId = [self.toastId copyWithZone:zone];
NSString *title = [self.title copyWithZone:zone];
NSString *subtitle = [self.subtitle copyWithZone:zone];
SLToast *copy = [[SLToast allocWithZone:zone] initWithIdentifier:toastId type:self.type title:title subtitle:subtitle image:self.image duration:self.duration];
if (!copy)
return copy;
copy.status = self.status;
return copy;
}
#pragma mark - NSCoding / NSSecureCoding
+ (BOOL)supportsSecureCoding
{
return YES;
}
+ (NSDictionary *)codableKeysAndClasses
{
struct SLToastKeys keys = SLToastKeys;
/* Don't add any mutables here (i.e. status) or risk the changing `-hash` inside a collection (bad). */
return @{keys.identifier:[NSString class],
keys.type: [NSNumber class],
keys.title: [NSString class],
keys.subtitle: [NSString class],
keys.image: [UIImage class],
keys.duration: [NSNumber class]};
}
- (instancetype)initWithCoder:(NSCoder *)decoder
{
NSSet *allowedString = [NSSet setWithObjects:[NSString class], [NSNull class], nil];
NSSet *allowedNumber = [NSSet setWithObjects:[NSNumber class], [NSNull class], nil];
NSSet *allowedImage = [NSSet setWithObjects:[UIImage class], [NSNull class], nil];
NSString *toastId = nil;
SLToastType type = SLToastTypeInfo;
NSTimeInterval duration = 0;
NSString *title = nil;
NSString *subtitle = nil;
UIImage *image = nil;
SLToastStatus status = SLToastStatusUnknown;
struct SLToastKeys keys = SLToastKeys;
@try {
if ([decoder containsValueForKey:keys.identifier])
toastId = [decoder decodeObjectOfClasses:allowedString forKey:keys.identifier];
if ([decoder containsValueForKey:SLToastKeys.type])
type = [[decoder decodeObjectOfClasses:allowedNumber forKey:keys.type] unsignedIntegerValue];
if ([decoder containsValueForKey:keys.duration])
duration = [[decoder decodeObjectOfClasses:allowedNumber forKey:keys.duration] unsignedIntegerValue];
if ([decoder containsValueForKey:keys.title])
title = [decoder decodeObjectOfClasses:allowedString forKey:SLToastKeys.title];
if ([decoder containsValueForKey:keys.subtitle])
subtitle = [decoder decodeObjectOfClasses:allowedString forKey:keys.subtitle];
if ([decoder containsValueForKey:keys.image])
image = [decoder decodeObjectOfClasses:allowedImage forKey:SLToastKeys.image];
if ([decoder containsValueForKey:keys.status])
status = [[decoder decodeObjectOfClasses:allowedNumber forKey:keys.status] unsignedIntegerValue];
}
@catch (NSException *exception) {
NSLog(@"Exception while decoding plist: %@", exception);
}
self = [self initWithIdentifier:toastId
type:type
title:title
subtitle:subtitle
image:image
duration:duration];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
if (!encoder)
return;
struct SLToastKeys keys = SLToastKeys;
[encoder encodeObject:@(self.type) forKey:keys.type];
[encoder encodeObject:@(self.status) forKey:keys.status];
[encoder encodeObject:@(self.duration) forKey:keys.duration];
NSString *toastId = self.toastId;
if (toastId)
[encoder encodeObject:toastId forKey:keys.identifier];
NSString *title = self.title;
if (title)
[encoder encodeObject:title forKey:keys.title];
NSString *subtitle = self.subtitle;
if (subtitle)
[encoder encodeObject:subtitle forKey:keys.subtitle];
UIImage *image = self.image;
if (image)
[encoder encodeObject:image forKey:keys.image];
}
#pragma mark - Equality & hash
- (BOOL)isEqual:(nullable id)obj
{
if (!obj || ![obj isKindOfClass:[self class]])
return NO;
return [[self dictionaryRepresentation] isEqualToDictionary:[obj dictionaryRepresentation]];
}
- (NSUInteger)hash
{
__block NSUInteger current = 31;
__block NSUInteger hashIndex = 1;
__weak typeof(self) weakSelf = self;
// WARNING: You must ensure that no mutable properties are encoded into the hash
[[SLToast codableKeysAndClasses] enumerateKeysAndObjectsUsingBlock:^(NSString *key, Class valueClass, BOOL *stop) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf)
return;
current = SLValueHash([[strongSelf valueForKey:key] hash], hashIndex) ^ current;
hashIndex++;
}];
return current;
}
#pragma mark - Description and Dictionary Representation
- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
__weak typeof(self) weakSelf = self;
[[[self class] codableKeysAndClasses] enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
__strong typeof(weakSelf) strongSelf = weakSelf;
id value = [strongSelf valueForKey:key];
if (value)
{
dict[key] = value;
}
}];
dict[SLToastKeys.status] = @(self.status);
return [dict copy];
}
- (NSString *)description
{
NSMutableString *description = [[NSMutableString alloc] initWithFormat:@"[%@] - ", NSStringFromClass(self.class)];
__weak typeof(self) weakSelf = self;
[[self dictionaryRepresentation] enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
__strong typeof(weakSelf) strongSelf = weakSelf;
id value = nil;
@try {
value = [strongSelf valueForKey:key];
}
@catch (NSException *exception) {
}
if (!value)
value = @"<nil>";
[description appendFormat:@" %@: %@\n", key, value];
}];
return description;
}
#pragma mark - Keyed Subscripting
- (nullable id)objectForKeyedSubscript:(nonnull NSString *)key
{
if (!SLTypeNonEmptyStringOrNil(key))
return nil;
// Status cannot be included in codableKeysAndClasses due to mutability
if ([key isEqualToString:SLToastKeys.status])
return @(self.status);
if (![[self class] codableKeysAndClasses][key])
return nil;
return [self valueForKey:key];
}
#if INFO_SHOULD_BE_MUTABLE
- (void)setDictionaryRepresentation:(NSDictionary *)dictionary
{
if (!SLTypeDictionaryOrNil(dictionary))
return;
struct SLToastKeys keys = SLToastKeys;
_toastId = [SLTypeNonEmptyStringOrNil(dictionary[keys.identifier]) copy];
_type = [SLTypeNumberOrNil(dictionary[keys.type]) unsignedIntegerValue];
_title = [SLTypeStringOrNil(dictionary[keys.title]) copy];
_subtitle = [SLTypeStringOrNil(dictionary[keys.subtitle]) copy];
_image = SLTypeImageOrNil(dictionary[keys.image]);
_duration = [SLTypeNumberOrNil(dictionary[keys.duration]) doubleValue];
_status = [SLTypeNumberOrNil(dictionary[keys.status]) unsignedIntegerValue];
}
- (void)setObject:(nullable id)object forKeyedSubscript:(nonnull NSString *)key
{
if (!SLTypeNonEmptyStringOrNil(key))
return;
BOOL secureSupported = [[self class] supportsSecureCoding];
NSDictionary *codableProperties = [[self class] codableKeysAndClasses];
Class valueClass = codableProperties[key];
if (!valueClass)
return;
if (secureSupported && object && ![object isKindOfClass:valueClass])
{
NSLog(@"Expected '%@' to be a %@, but was actually a %@", key, valueClass, [object class]);
return;
}
[self setValue:object forKey:key];
}
#endif
@end