1 |
efrain |
1 |
/*
|
|
|
2 |
* Copyright 2021 Google LLC
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
#import "GoogleUtilities/Environment/Public/GoogleUtilities/GULHeartbeatDateStorageUserDefaults.h"
|
|
|
18 |
|
|
|
19 |
@interface GULHeartbeatDateStorageUserDefaults ()
|
|
|
20 |
|
|
|
21 |
/** The storage to store the date of the last sent heartbeat. */
|
|
|
22 |
@property(nonatomic, readonly) NSUserDefaults *userDefaults;
|
|
|
23 |
|
|
|
24 |
/** The key for user defaults to store heartbeat information. */
|
|
|
25 |
@property(nonatomic, readonly) NSString *key;
|
|
|
26 |
|
|
|
27 |
@end
|
|
|
28 |
|
|
|
29 |
@implementation GULHeartbeatDateStorageUserDefaults
|
|
|
30 |
|
|
|
31 |
- (instancetype)initWithDefaults:(NSUserDefaults *)defaults key:(NSString *)key {
|
|
|
32 |
self = [super init];
|
|
|
33 |
if (self) {
|
|
|
34 |
_userDefaults = defaults;
|
|
|
35 |
_key = key;
|
|
|
36 |
}
|
|
|
37 |
return self;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
- (NSMutableDictionary *)heartbeatDictionaryFromDefaults {
|
|
|
41 |
NSDictionary *heartbeatDict = [self.userDefaults objectForKey:self.key];
|
|
|
42 |
if (heartbeatDict != nil) {
|
|
|
43 |
return [heartbeatDict mutableCopy];
|
|
|
44 |
} else {
|
|
|
45 |
return [NSMutableDictionary dictionary];
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
- (nullable NSDate *)heartbeatDateForTag:(NSString *)tag {
|
|
|
50 |
NSDate *date = nil;
|
|
|
51 |
@synchronized(self.userDefaults) {
|
|
|
52 |
NSMutableDictionary *dict = [self heartbeatDictionaryFromDefaults];
|
|
|
53 |
date = dict[tag];
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
return date;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
- (BOOL)setHearbeatDate:(NSDate *)date forTag:(NSString *)tag {
|
|
|
60 |
@synchronized(self.userDefaults) {
|
|
|
61 |
NSMutableDictionary *dict = [self heartbeatDictionaryFromDefaults];
|
|
|
62 |
dict[tag] = date;
|
|
|
63 |
[self.userDefaults setObject:dict forKey:self.key];
|
|
|
64 |
}
|
|
|
65 |
return true;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
@end
|