-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathPHStorage.m
125 lines (93 loc) · 3.19 KB
/
PHStorage.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
/*
* Phoenix is released under the MIT License. Refer to https://github.com/kasper/phoenix/blob/master/LICENSE.md
*/
#import "PHStorage.h"
@interface PHStorage ()
@property(copy) NSString *path;
@property NSMutableDictionary<NSString *, id> *storage;
@property(getter=isPersisting) BOOL persisting;
@end
@implementation PHStorage
#if DEBUG
static NSString *const PHStoragePath = @"~/Library/Application Support/Phoenix/storage.debug.json";
#else
static NSString *const PHStoragePath = @"~/Library/Application Support/Phoenix/storage.json";
#endif
#pragma mark - Initialising
- (instancetype)init {
if (self = [super init]) {
self.path = PHStoragePath.stringByExpandingTildeInPath;
self.storage = [NSMutableDictionary dictionary];
[self load];
}
return self;
}
+ (instancetype)storage {
return [[self alloc] init];
}
#pragma mark - Loading
- (void)load {
// No storage file to load
if (![[NSFileManager defaultManager] fileExistsAtPath:self.path]) {
return;
}
NSError *error;
NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:self.path];
[stream open];
id data = [NSJSONSerialization JSONObjectWithStream:stream options:0 error:&error];
[stream close];
if (error) {
NSLog(@"Error: Loading storage failed. (%@)", error);
return;
}
[self.storage addEntriesFromDictionary:data];
}
#pragma mark - Persisting
- (BOOL)ensureStorageDirectory {
NSError *error;
NSString *directory = self.path.stringByDeletingLastPathComponent;
// Ensure storage directory exists
[[NSFileManager defaultManager] createDirectoryAtPath:directory
withIntermediateDirectories:YES
attributes:nil
error:&error];
if (error) {
NSLog(@"Error: Could not create storage directory to path “%@”. (%@)", directory, error);
return NO;
}
return YES;
}
- (void)persist {
self.persisting = YES;
if (![self ensureStorageDirectory]) {
self.persisting = NO;
return;
}
NSError *error;
NSOutputStream *stream = [NSOutputStream outputStreamToFileAtPath:self.path append:NO];
[stream open];
[NSJSONSerialization writeJSONObject:self.storage toStream:stream options:NSJSONWritingPrettyPrinted error:&error];
[stream close];
if (error) {
NSLog(@"Error: Persisting storage failed. (%@)", error);
}
self.persisting = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:PHStorageDidPersistNotification object:nil];
}
#pragma mark - Storing
- (void)forKey:(NSString *)key setObject:(id)object {
if (![NSJSONSerialization isValidJSONObject:@[object]]) {
NSLog(@"Error: Value for key “%@” is not a valid JSON-object.", key);
return;
}
self.storage[key] = object;
[self performSelectorInBackground:@selector(persist) withObject:nil];
}
- (id)objectForKey:(NSString *)key {
return self.storage[key];
}
- (void)removeObjectForKey:(NSString *)key {
[self.storage removeObjectForKey:key];
[self performSelectorInBackground:@selector(persist) withObject:nil];
}
@end