This repository has been archived by the owner on Oct 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorageManager.m
94 lines (77 loc) · 2.4 KB
/
StorageManager.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
//
// StorageManager.m
// CoreLocationDemo
//
// Created by Piotr Tobolski on 03.05.2015.
// Copyright (c) 2015 BLStream. All rights reserved.
//
#import "StorageManager.h"
@interface StorageManager ()
@property (strong, nonatomic) NSUserDefaults *defaults;
@end
@implementation StorageManager
+ (instancetype)sharedInstance {
static id singleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
singleton = [[[self class] alloc] init];
});
return singleton;
}
- (instancetype)init {
if (self = [super init]) {
_defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.applewatch.blstream"];
}
return self;
}
- (NSArray *)locations {
NSData *data = [self.defaults objectForKey:@"locations"];
if (data == nil) {
return nil;
}
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
- (void)setLocations:(NSArray *)locations {
[self.defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:locations] forKey:@"locations"];
[self.defaults synchronize];
}
- (void)appendLocation:(Location *)location {
NSMutableArray *array = self.locations.mutableCopy;
if (array == nil) {
array = [[NSMutableArray alloc] init];
}
[array addObject:location];
self.locations = array;
}
- (NSArray *)messages {
NSData *data = [self.defaults objectForKey:@"messages"];
if (data == nil) {
return nil;
}
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
- (void)setMessages:(NSArray *)messages {
[self.defaults setObject:[NSKeyedArchiver archivedDataWithRootObject:messages] forKey:@"messages"];
[self.defaults synchronize];
}
- (void)appendMessage:(NSString *)message {
NSMutableArray *array = self.messages.mutableCopy;
if (array == nil) {
array = [[NSMutableArray alloc] init];
}
[array addObject:message];
self.messages = array;
}
- (BOOL)significantMonitoringEnabled {
return [[self.defaults objectForKey:@"significant"] boolValue];
}
- (void)setSignificantMonitoringEnabled:(BOOL)significantMonitoringEnabled {
[self.defaults setObject:@(significantMonitoringEnabled) forKey:@"significant"];
}
- (BOOL)visitMonitoringEnabled {
return [[self.defaults objectForKey:@"visit"] boolValue];
}
- (void)setVisitMonitoringEnabled:(BOOL)visitMonitoringEnabled {
[self.defaults setObject:@(visitMonitoringEnabled) forKey:@"visit"];
}
@end