-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFriendmojiListController.m
More file actions
executable file
·292 lines (225 loc) · 9.78 KB
/
Copy pathFriendmojiListController.m
File metadata and controls
executable file
·292 lines (225 loc) · 9.78 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "SNAPI.h"
#include "FriendmojiListController.h"
#include <objc/runtime.h>
@interface Friend
@property (strong,nonatomic) NSString *display;
-(NSString*)name;
-(NSString*)getFriendmojiForViewType:(NSInteger)type;
-(long long)snapStreakCount;
@end
@interface Friends
-(NSArray*)getAllFriends;
@end
@interface User
-(Friends*)friends;
@end
@interface Manager
+(Manager*)shared;
-(User*)user;
@end
@interface FriendmojiCell : UITableViewCell {
}
@end
@implementation FriendmojiCell
@end
@interface FriendmojiListController ()
@property (strong,nonatomic) NSDictionary *settings;
@property (strong,nonatomic) NSArray *friendsWithStreaksNames;
@property (strong,nonatomic) NSArray *friendmojisWithStreaks;
@property (strong,nonatomic) NSArray *friendsWithoutStreaksNames;
@property (strong,nonatomic) NSArray *friendmojisWithoutStreaks;
@end
@implementation FriendmojiListController
-(id)init
{
self = [super init];
if (self) {
NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[documents objectAtIndex:0] stringByAppendingPathComponent:@"com.YungRaj.friendmojilist.plist"];
self.settings = [NSMutableDictionary dictionaryWithContentsOfFile:path];
NSMutableDictionary *friendsWithStreaks = [[NSMutableDictionary alloc] init];
NSMutableDictionary *friendsWithoutStreaks = [[NSMutableDictionary alloc] init];
Manager *manager = [objc_getClass("Manager") shared];
User *user = [manager user];
Friends *friends = [user friends];
for(Friend *f in [friends getAllFriends]){
NSString *displayName = [f display];
NSString *friendmoji = [f getFriendmojiForViewType:0];
if(displayName && ![displayName isEqual:@""]){
if([f snapStreakCount] > 2){
[friendsWithStreaks setObject:friendmoji forKey:displayName];
}else {
[friendsWithoutStreaks setObject:friendmoji forKey:displayName];
}
}else{
NSString *username = [f name];
if(username && ![username isEqual:@""]){
if([f snapStreakCount] > 2){
[friendsWithStreaks setObject:friendmoji forKey:username];
}else {
[friendsWithoutStreaks setObject:friendmoji forKey:username];
}
}
}
}
NSArray *friendsWithStreaksNames = [friendsWithStreaks allKeys];
NSMutableArray *friendmojisWithStreaks = [[NSMutableArray alloc] init];
NSArray *friendsWithoutStreaksNames = [friendsWithoutStreaks allKeys];
NSMutableArray *friendmojisWithoutStreaks = [[NSMutableArray alloc] init];
friendsWithStreaksNames = [friendsWithStreaksNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
friendsWithoutStreaksNames = [friendsWithoutStreaksNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
for(NSString *name in friendsWithStreaksNames){
NSString *friendmoji = [friendsWithStreaks objectForKey:name];
[friendmojisWithStreaks addObject:friendmoji];
}
for(NSString *name in friendsWithoutStreaksNames){
NSString *friendmoji = [friendsWithoutStreaks objectForKey:name];
[friendmojisWithoutStreaks addObject:friendmoji];
}
SNLog(@"friendmojilist:: Friends with streaks %@ %@",friendsWithStreaksNames,friendmojisWithStreaks);
SNLog(@"friendmojilist:: Friends without streaks %@ %@",friendsWithoutStreaksNames,friendmojisWithoutStreaks);
self.friendsWithStreaksNames = friendsWithStreaksNames;
self.friendmojisWithStreaks = friendmojisWithStreaks;
self.friendsWithoutStreaksNames = friendsWithoutStreaksNames;
self.friendmojisWithoutStreaks = friendmojisWithoutStreaks;
/* Create settings if they don't exist */
if(!self.settings){
NSMutableDictionary *settings = [[NSMutableDictionary alloc] init];
for(NSString *name in self.friendsWithStreaksNames){
[settings setObject:@NO forKey:name];
}
for(NSString *name in self.friendsWithoutStreaksNames){
[settings setObject:@NO forKey:name];
}
self.settings = settings;
SNLog(@"friendmojilist:: %@",self.settings);
}
/* Add the data source as an observer to find out when the
* FriendmojiListController will exit so that we can save the dictionary to file */
}
return self;
}
-(void)viewDidLoad{
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
-(CGSize)contentSize
{
return [self.tableView frame].size;
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
if([self isMovingFromParentViewController]){
SNLog(@"friendmojilist::Exiting friendmoji prefs, saving settings");
[self savePreferences];
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionName;
switch (section)
{
case 0:
sectionName = @"Friends with Streaks";
break;
case 1:
sectionName = @"Other Friends";
break;
// ...
default:
sectionName = @"";
break;
}
return sectionName;
}
-(NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{
switch (section)
{
case 0:
return [self.friendsWithStreaksNames count];
case 1:
return [self.friendsWithoutStreaksNames count];
}
return 0;
}
-(UITableViewCell*)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *identifier = @"friendmojiCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(!cell){
cell = [[[FriendmojiCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:identifier] autorelease];
}
if(indexPath.section == 0){
if([cell isKindOfClass:[FriendmojiCell class]]){
FriendmojiCell *friendmojiCell = (FriendmojiCell*)cell;
NSString *name = [self.friendsWithStreaksNames objectAtIndex:indexPath.row];
NSString *friendmoji = [self.friendmojisWithStreaks objectAtIndex:indexPath.row];
friendmojiCell.textLabel.text = [NSString stringWithFormat:@"%@ %@",name,friendmoji];
SNLog(@"friendmojilist::Cell for index %ld name %@ %@",(long)indexPath.row,name,friendmoji);
if([self.settings[name] boolValue]){
friendmojiCell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
friendmojiCell.accessoryType = UITableViewCellAccessoryNone;
}
}
} else if(indexPath.section == 1){
if([cell isKindOfClass:[FriendmojiCell class]]){
FriendmojiCell *friendmojiCell = (FriendmojiCell*)cell;
NSString *name = [self.friendsWithoutStreaksNames objectAtIndex:indexPath.row];
NSString *friendmoji = [self.friendmojisWithoutStreaks objectAtIndex:indexPath.row];
friendmojiCell.textLabel.text = [NSString stringWithFormat:@"%@ %@",name,friendmoji];
SNLog(@"friendmojilist:: Cell for index %ld name %@ %@",(long)indexPath.row,name,friendmoji);
if([self.settings[name] boolValue]){
friendmojiCell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
friendmojiCell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
return cell;
}
-(void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
FriendmojiCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *name = [NSString string];
if(indexPath.section == 0){
name = [self.friendsWithStreaksNames objectAtIndex:indexPath.row];
} else if(indexPath.section == 1){
name = [self.friendsWithoutStreaksNames objectAtIndex:indexPath.row];
}
[self.settings setValue:[NSNumber numberWithBool:![self.settings[name] boolValue]]
forKey:name];
if(cell.accessoryType==UITableViewCellAccessoryCheckmark){
cell.accessoryType = UITableViewCellAccessoryNone;
}else if(cell.accessoryType==UITableViewCellAccessoryNone){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
-(void)savePreferences{
SNLog(@"friendmojilist::Saved settings %@",self.settings);
NSDictionary *settings = self.settings;
NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[documents objectAtIndex:0] stringByAppendingPathComponent:@"com.YungRaj.friendmojilist.plist"];
[settings writeToFile:path
atomically:YES];
}
-(void)dealloc{
[super dealloc];
[self.settings release];
[self.friendsWithStreaksNames release];
[self.friendsWithoutStreaksNames release];
[self.friendmojisWithStreaks release];
[self.friendmojisWithoutStreaks release];
_settings = nil;
_friendsWithStreaksNames = nil;
_friendsWithoutStreaksNames = nil;
_friendmojisWithStreaks = nil;
_friendmojisWithoutStreaks = nil;
}
@end