-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableViewUserDefaultsHelper.m
167 lines (124 loc) · 4.15 KB
/
TableViewUserDefaultsHelper.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// TableViewUserDefaultsHelper.cs
// BPTracker
//
// Created by Robert Saccone on 6/10/12.
// Copyright (c) 2017 Robert Saccone. All rights reserved.
//
#import "TableViewUserDefaultsHelper.h"
@interface TableViewUserDefaultsHelper ()
@property(nonatomic, strong) NSArray *keyNames;
@end
@implementation TableViewUserDefaultsHelper
{
@private
NSArray *keyNames_;
}
@synthesize keyNames = keyNames_;
- (id)initWithKeyNames:(NSArray *)keyNames
{
if (!keyNames)
{
NSLog(@"TableViewUserDefaultsHelper init: keyNames is nil!");
NSAssert(NO, @"keyNames is nil!");
return nil;
}
if (keyNames.count < numTableViewKeys)
{
NSLog(@"TableViewUserDefaultsHelper init: incorrect number of keys (%lu) in keyNames", (unsigned long)keyNames.count);
NSAssert2(NO, @"keyNames count is %lu, should be %d", (unsigned long)keyNames.count, numTableViewKeys);
return nil;
}
for (NSString *key in keyNames)
{
if ((key == nil) && (key.length == 0))
{
NSLog(@"TableViewUserDefaultsHelper init: one or more keys is nil or empty!");
return nil;
}
}
self = [super init];
if (self != nil)
{
keyNames_ = keyNames;
}
return self;
}
- (id)init
{
return [self initWithKeyNames:nil];
}
- (NSIndexPath *)getSavedSelection
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *keyNames = self.keyNames;
NSNumber *value = [userDefaults objectForKey:[keyNames objectAtIndex:sectionKeyIndex]];
if (!value)
{
return nil;
}
NSInteger section = [value integerValue];
value = [userDefaults objectForKey:[self.keyNames objectAtIndex:rowKeyIndex]];
if (!value)
{
return nil;
}
NSInteger row = [value integerValue];
return [NSIndexPath indexPathForRow:row inSection:section];
}
- (BOOL)getSavedEditingFlag
{
BOOL editing = NO;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSNumber *value = [userDefaults objectForKey:[self.keyNames objectAtIndex:editFlagKeyIndex]];
if (value)
{
editing = [value boolValue];
}
return editing;
}
- (void)saveEditingFlag:(BOOL)editingFlag
{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:editingFlag]
forKey:[self.keyNames objectAtIndex:editFlagKeyIndex]];
}
- (void)saveSelectedIndex:(NSIndexPath *)indexPath withEditFlag:(BOOL)editing
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (!indexPath)
{
[userDefaults setObject:nil
forKey:[self.keyNames objectAtIndex:sectionKeyIndex]];
[userDefaults setObject:nil
forKey:[self.keyNames objectAtIndex:rowKeyIndex]];
[userDefaults setObject:nil
forKey:[self.keyNames objectAtIndex:editFlagKeyIndex]];
}
else
{
[userDefaults setObject:[NSNumber numberWithInteger:indexPath.section]
forKey:[self.keyNames objectAtIndex:sectionKeyIndex]];
[userDefaults setObject:[NSNumber numberWithInteger:indexPath.row]
forKey:[self.keyNames objectAtIndex:rowKeyIndex]];
[userDefaults setObject:[NSNumber numberWithBool:editing]
forKey:[self.keyNames objectAtIndex:editFlagKeyIndex]];
}
[userDefaults synchronize];
}
- (BOOL)getSortAscendingFlag
{
BOOL sortAscending = YES;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSNumber *value = [userDefaults objectForKey:[self.keyNames objectAtIndex:sortAscendingIndex]];
if (value)
{
sortAscending = [value boolValue];
}
return sortAscending;
}
- (void)saveSortAscendingFlag:(BOOL)sortAscendingFlag
{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:sortAscendingFlag]
forKey:[self.keyNames objectAtIndex:sortAscendingIndex]];
}
@end