forked from youknowone/VisualJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVJDocumentHistory.m
More file actions
100 lines (82 loc) · 2.71 KB
/
VJDocumentHistory.m
File metadata and controls
100 lines (82 loc) · 2.71 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
//
// VJDocumentHistory.m
// VisualJSON
//
// Created by Jeong YunWon on 12. 11. 15..
// Copyright (c) 2012 youknowone.org. All rights reserved.
//
#import "VJDocumentHistory.h"
@implementation VJDocumentHistory
VJDocumentHistory *VJDocumentHistoryDefaultHistory = nil;
+ (void)initialize {
if (self == [VJDocumentHistory class]) {
VJDocumentHistoryDefaultHistory = [[VJDocumentHistory alloc] init];
}
}
- (id)init {
self = [super init];
if (self != nil) {
self->_history = [[[NSUserDefaults standardUserDefaults] objectForKey:@"history"] mutableCopy];
if (self->_history == nil) {
self->_history = [[NSMutableArray alloc] init];
}
}
return self;
}
- (void)dealloc {
[self->_history release];
[super dealloc];
}
- (BOOL)synchronize {
[[NSUserDefaults standardUserDefaults] setObject:self->_history forKey:@"history"];
return [[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSUInteger)count {
return self->_history.count;
}
- (VJDocumentHistoryItem *)itemAtIndex:(NSInteger)index {
return [VJDocumentHistoryItem historyWithDataDictionary:[self->_history objectAtIndex:index]];
}
- (void)addHistory:(NSString *)value {
NSUInteger count = self.count;
NSInteger index = NSNotFound;
for (NSInteger i = 0; i < count; i++) {
if ([[[self->_history objectAtIndex:i] objectForKey:@"address"] isEqualToString:value]) {
index = i;
break;
}
}
NSMutableDictionary *itemData;
if (index == NSNotFound) {
itemData = [NSMutableDictionary dictionary];
[itemData setObject:value forKey:@"address"];
[itemData setObject:[NSMutableArray arrayWithObject:[NSDate date]] forKey:@"dates"];
[self->_history insertObject:itemData atIndex:0];
} else {
itemData = [[self->_history objectAtIndex:index] retain];
[self->_history removeObjectAtIndex:index];
[self->_history insertObject:itemData atIndex:0];
[itemData release];
}
}
+ (VJDocumentHistory *)defaultHistory {
return VJDocumentHistoryDefaultHistory;
}
@end
@implementation VJDocumentHistoryItem
@synthesize address=_address, dates=_dates;
- (id)initWithDataDictionary:(NSDictionary *)dictionary {
self = [super init];
if (self != nil) {
self.address = [dictionary objectForKey:@"address"];
self.dates = [dictionary objectForKey:@"dates"];
}
return self;
}
+ (id)historyWithDataDictionary:(NSDictionary *)dictionary {
return [[[self alloc] initWithDataDictionary:dictionary] autorelease];
}
- (NSDictionary *)dataDictionary {
return [NSMutableDictionary dictionaryWithObjectsAndKeys:self.address, @"address", self.dates, @"dates", nil];
}
@end