5 // Created by J Osborne on 7/11/13.
9 #import "PersistentState.h"
10 #import <Foundation/Foundation.h>
12 @interface PersistentState()
13 -(NSURL*)urlForStorage;
16 @implementation PersistentState
18 -(NSURL*)urlForStorage
20 return [NSURL fileURLWithPath:@"/var/mobile/Library/Preferences/com.apple.security.CircleJoinRequested.plist" isDirectory:NO];
23 -(unsigned int)defaultPendingApplicationReminderAlertInterval
28 +(instancetype)loadFromStorage
30 PersistentState *state = [[PersistentState alloc] init];
36 id plist = @{@"lastWritten": [NSDate distantPast]};
38 NSData *stateData = [NSData dataWithContentsOfURL:[state urlForStorage] options:0 error:&error];
40 NSLog(@"Can't read state data (p=%@, err=%@)", [state urlForStorage], error);
42 NSPropertyListFormat format;
43 id plistTmp = [NSPropertyListSerialization propertyListWithData:stateData options: NSPropertyListMutableContainersAndLeaves format:&format error:&error];
45 if (plistTmp == nil) {
46 NSLog(@"Can't deserialize %@, e=%@", stateData, error);
52 state.lastCircleStatus = plist[@"lastCircleStatus"] ? [plist[@"lastCircleStatus"] intValue] : kSOSCCCircleAbsent;
53 state.lastWritten = plist[@"lastWritten"];
54 state.pendingApplicationReminder = plist[@"pendingApplicationReminder"] ?: [NSDate distantFuture];
55 state.applicationDate = plist[@"applicationDate"] ?: [NSDate distantPast];
56 state.debugShowLeftReason = plist[@"debugShowLeftReason"];
57 state.pendingApplicationReminderAlertInterval = plist[@"pendingApplicationReminderAlertInterval"] ?
58 [plist[@"pendingApplicationReminderAlertInterval"] unsignedIntValue] :
59 [state defaultPendingApplicationReminderAlertInterval];
60 state.absentCircleWithNoReason = plist[@"absentCircleWithNoReason"] ? [plist[@"absentCircleWithNoReason"] intValue] : NO;
67 NSDictionary *plist = @{@"lastCircleStatus": [NSNumber numberWithInt:self.lastCircleStatus],
68 @"lastWritten": [NSDate date],
69 @"pendingApplicationReminder": self.pendingApplicationReminder ?: [NSDate distantFuture],
70 @"applicationDate": self.applicationDate ?: [NSDate distantPast],
71 @"pendingApplicationReminderAlertInterval": [NSNumber numberWithUnsignedInt:self.pendingApplicationReminderAlertInterval],
72 @"absentCircleWithNoReason": [NSNumber numberWithBool:self.absentCircleWithNoReason]};
73 if (self.debugShowLeftReason) {
74 NSMutableDictionary *tmp = [plist mutableCopy];
75 tmp[@"debugShowLeftReason"] = self.debugShowLeftReason;
78 NSLog(@"writeToStorage plist=%@", plist);
81 NSData *stateData = [NSPropertyListSerialization dataWithPropertyList:plist format:NSPropertyListXMLFormat_v1_0 options:kCFPropertyListImmutable error:&error];
83 NSLog(@"Can't serialize %@: %@", plist, error);
86 if (![stateData writeToURL:[self urlForStorage] options:NSDataWritingAtomic error:&error]) {
87 NSLog(@"Can't write to %@, error=%@", [self urlForStorage], error);