Developer Area

root/apps/iphone/my.tel/trunk/Classes/GroupsViewController.m @ 623

Revision 623, 11.1 kB (checked in by henri, 4 years ago)

Changed groups management UI to conform to new standard

Line 
1//
2//  GroupsViewController.m
3//  My.tel
4//
5//  Created by Henri Asseily on 2/6/09.
6//  Copyright 2009 __MyCompanyName__. All rights reserved.
7//
8
9#import "GroupsViewController.h"
10
11@interface GroupsViewController (PrivateMethods)
12- (void)setDidPreload:(BOOL)preload;
13@end;
14
15
16@implementation GroupsViewController
17
18@synthesize groupsArray;
19@synthesize delegate;
20
21#define kAlertAddId 1
22
23#pragma mark -
24#pragma mark Designated initializer
25
26+ (GroupsViewController *)controllerWithDelegate:(id <TelControllerDelegate>)aDelegate preload:(BOOL)preload {
27        GroupsViewController *theC = [[[GroupsViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
28        theC.delegate = aDelegate;
29        theC.groupsArray = [NSMutableArray arrayWithCapacity:10];
30        if (preload) {
31                [theC listGroups];
32        }
33        [theC setDidPreload:preload];
34        return theC;   
35}
36
37- (void)setDidPreload:(BOOL)preload {
38        didPreload = preload;
39}
40
41
42#pragma mark ------ Default Methods
43- (void)viewDidLoad {
44    [super viewDidLoad];
45        self.navigationItem.rightBarButtonItem = [self editButtonItem];
46        // disable editing until we've loaded the data
47        self.navigationItem.rightBarButtonItem.enabled = NO;
48        self.title = @"Privacy Groups";
49}
50
51
52- (void)viewWillAppear:(BOOL)animated {
53        [super viewWillAppear:animated];
54        // Don't get the data if we preloaded
55        if (didPreload) {
56                didPreload = NO;
57        } else {
58                [self listGroups];
59        }
60}
61
62- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
63    // Return YES for supported orientations
64    return (interfaceOrientation == UIInterfaceOrientationPortrait);
65}
66
67- (void)didReceiveMemoryWarning {
68    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
69    // Release anything that's not essential, such as cached data
70}
71
72
73- (void)dealloc {
74    [super dealloc];
75}
76
77#pragma mark ------ TableView methods
78
79- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
80    return 1;
81}
82
83- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
84    return [groupsArray count];
85}
86
87- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
88                   editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
89        NSDictionary *sGroup = [groupsArray objectAtIndex:indexPath.row];
90        if ([[sGroup valueForKey:@"allFriends"] integerValue] == 1) {
91                return UITableViewCellEditingStyleNone;
92        }
93        return UITableViewCellEditingStyleDelete;
94}
95
96- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
97        // Disable swipe-to-delete: don't allow row edit if table not in edit mode
98        return (tableView.editing);
99}
100
101- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
102forRowAtIndexPath:(NSIndexPath *)indexPath {
103        if (editingStyle == UITableViewCellEditingStyleDelete) {
104                [self deleteGroupAtIndex:indexPath.row];
105        }               
106}
107
108- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
109   
110    static NSString *CellIdentifier = @"GroupListCell";
111   
112    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
113    if (cell == nil) {
114        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
115                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
116    }
117    // Configure the cell
118        NSDictionary *sGroup = [groupsArray objectAtIndex:indexPath.row];
119        if ([[sGroup valueForKey:@"allFriends"] integerValue] == 1) {
120                NSString *gName = [sGroup valueForKey:@"name"];
121                cell.textLabel.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:cell.textLabel.font.pointSize];
122                if ([gName isEqualToString:@"All friends"]) {
123                        cell.textLabel.text = gName;
124                } else {
125                        cell.textLabel.text = [gName stringByAppendingString:@" (All friends)"];
126                }
127        } else {
128                cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:cell.textLabel.font.pointSize];
129                cell.textLabel.text = [sGroup valueForKey:@"name"];
130        }
131        [cell.textLabel setNeedsDisplay];
132    return cell;
133}
134
135- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
136        [tableView deselectRowAtIndexPath:indexPath animated:YES];
137        currentGroupIndex = indexPath.row;
138        GroupFriendsController *newController = [GroupFriendsController
139                                                                                         controllerForGroup:[groupsArray objectAtIndex:indexPath.row] delegate:self];
140        [[self navigationController] pushViewController:newController animated:YES];
141}
142
143- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
144        [super setEditing:editing animated:animated];
145        if (editing) {
146                self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
147                                                                                                                                                                                           target:self
148                                                                                                                                                                                           action:@selector(didClickAdd:)] autorelease];
149        } else {
150                self.navigationItem.leftBarButtonItem = nil;
151        }
152}
153
154
155#pragma mark ------ UI Methods and JSON delegates
156
157- (IBAction)didClickAdd:(id)sender {
158        AlertRenameView *alert = [AlertRenameView alloc];
159        [[alert initWithTitle:@"New Group Name"
160                                  message:@"\n\n"
161                                 delegate:self
162                cancelButtonTitle:@"Cancel"
163                otherButtonTitles:@"OK", nil] autorelease];
164        alert.alertId = [NSNumber numberWithInt:kAlertAddId];
165        alert.uiStringField.text = @"";
166        [alert show];
167}
168
169- (void)deleteGroupAtIndex:(NSUInteger)index {
170        NSDictionary *aGroup = [groupsArray objectAtIndex:index];
171        if (!aGroup) {
172                return;
173        }
174        [aGroup retain];
175        Group *conn = [[[Group alloc] init] autorelease];
176        [conn setTheDelegate:self];
177        [conn setActionSel:@selector(groupDeletedWithJson:)];
178        [conn setConnectionUrl:[conn urlFromAction:@"deletegroup"]];
179       
180        NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:2] retain];
181        [requestData setObject:[self.delegate domain] forKey:@"domainName"];
182        [requestData setObject:[aGroup objectForKey:@"id"] forKey:@"groupId"];
183       
184        [conn setPayload:requestData];
185        NSDictionary *parsedJson = [conn performLookup:FALSE];
186        [requestData release];
187        if ([[parsedJson valueForKey:@"success"] integerValue] == 0) {
188                [Group throwJsonErrorAlert:parsedJson];
189        } else {
190                [self.groupsArray removeObjectAtIndex:index];
191                NSIndexPath *deleteIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
192        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:deleteIndexPath] withRowAnimation:UITableViewRowAnimationFade];
193        }
194}
195
196- (void)updateUITableWithJson:(NSDictionary *)parsedJson {
197#ifdef DEBUG
198        NSLog(@"%@", [parsedJson descriptionInStringsFileFormat]);
199#endif
200       
201        [groupsArray removeAllObjects];
202        if ([[parsedJson valueForKey:@"success"] integerValue] == 1) {
203                // Deep copy the JSON object, we'll be using it as the main data source for the group work
204                // including the group details modifications. Therefore we need each group (a dictionary)
205                // to be mutable
206                // Also set (cache) the allFriendsGroup for the GroupFriendsController to use
207                NSDictionary *aGroup;
208                NSMutableDictionary *mGroup;
209                if ([parsedJson objectForKey:@"groupList"] != [NSNull null]) {
210                        for (aGroup in (NSArray *)[parsedJson valueForKey:@"groupList"]) {
211                                mGroup = [NSMutableDictionary dictionaryWithDictionary:aGroup];
212                                if ([[mGroup objectForKey:@"allFriends"] integerValue] == 1) {
213                                        [MyTelConnect sharedInstance].allFriendsGroupId = [mGroup objectForKey:@"id"];
214                                }
215                                [groupsArray addObject:mGroup];
216                        }
217                } else {
218                        // no groups
219                }
220                [self.delegate dataDidChangeInController:self];
221                [self.tableView reloadData];
222                self.navigationItem.rightBarButtonItem.enabled = YES;
223        } else {
224                [self.tableView reloadData];
225                [Group throwJsonErrorAlert:parsedJson];
226        }
227}
228
229#pragma mark ------ TelControllerDelegate methods
230
231- (void)dataDidChangeInController:(UIViewController *)controller {
232        if ([controller isMemberOfClass:[GroupFriendsController class]]) {
233                [self listGroups];
234        }
235}
236
237- (NSString *)domain {
238        // request the domain from the calling controller
239        return [self.delegate domain];
240}
241
242- (void)objectWillGetDeletedInController:(UIViewController *)controller {
243        if ([controller isMemberOfClass:[GroupFriendsController class]]) {
244                [self deleteGroupAtIndex:currentGroupIndex];
245        }
246}
247
248#pragma mark ------ AlertRenameView delegate methods
249
250- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
251        AlertRenameView *aV = (AlertRenameView *)alertView;
252        if (buttonIndex == aV.cancelButtonIndex) {
253                // Cancel
254                return;
255        }
256        if ([aV.uiStringField.text length] == 0) {
257                return;
258        }
259        if ([aV.alertId integerValue] == kAlertAddId) {
260                [self createGroup:aV.uiStringField.text];
261                [self listGroups];
262                return;
263        }
264}
265
266#pragma mark ------ Data Management
267
268- (void)listGroups {
269//      inputGroup = {
270//      domainName: "cartman.tel"
271//      };
272//     
273//      successResult = {
274//      success: true,
275//      groupList: [{id: 12, name: "friends", allFriends: true},
276//              {id: 23, name: "family", allFriends: false}],
277//      actionMessages: "request successful",
278//              "2nd message here",
279//              "3rd message here"]
280//      };
281       
282        Group *conn = [[[Group alloc] init] autorelease];
283        [conn setTheDelegate:self];
284        [conn setActionSel:@selector(updateUITableWithJson:)];
285        [conn setConnectionUrl:[conn urlFromAction:@"getgrouplist"]];
286       
287        NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:2] retain];
288        [requestData setObject:[self.delegate domain] forKey:@"domainName"];
289       
290        [conn setPayload:requestData];
291        [conn performLookup:TRUE];
292        [requestData release];
293}
294
295- (void)createGroup:(NSString *)aGroupName {
296//      inputGroup = {
297//      domainName: "cartman.tel",
298//      groupId: 0,
299//      groupName: "friends",
300//      readerIds: [12, 1, 2]
301//      };
302//     
303//      successResult = {
304//      success: true,
305//      groupId: 24,
306//      actionMessages: "group stored",
307//              "2nd message here",
308//              "3rd message here"]
309//      };
310        [aGroupName retain];
311        Group *conn = [[[Group alloc] init] autorelease];
312        [conn setTheDelegate:self];
313        [conn setActionSel:@selector(updateUITableWithJson:)];
314        [conn setConnectionUrl:[conn urlFromAction:@"storegroup"]];
315       
316        // create empty group
317        NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:4] retain];
318        [requestData setObject:[self.delegate domain] forKey:@"domainName"];
319        [requestData setObject:@"0" forKey:@"groupId"];
320        [requestData setObject:aGroupName forKey:@"groupName"];
321        [requestData setObject:[NSArray array] forKey:@"readerIds"];
322       
323        [conn setPayload:requestData];
324        NSDictionary *parsedJson = [conn performLookup:FALSE];
325        [requestData release]; 
326        if ([[parsedJson valueForKey:@"success"] integerValue] == 0) {
327                [Group throwJsonErrorAlert:parsedJson];
328        } else {
329                NSDictionary *newGroup = [NSDictionary dictionaryWithObjectsAndKeys:[parsedJson valueForKey:@"groupId"], @"id",
330                                                                aGroupName, @"name",
331                                                                @"false", @"allFriends",
332                                                                nil];
333                [groupsArray addObject:newGroup];
334                NSIndexPath *lastPath = [NSIndexPath indexPathForRow:[self.tableView numberOfRowsInSection:0] inSection:0];
335                [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:lastPath] withRowAnimation:UITableViewRowAnimationFade];
336        }
337        [aGroupName release];
338       
339}
340
341#pragma mark ------ KVO handling
342
343- (void)observeValueForKeyPath:(NSString *)keyPath
344                                          ofObject:(id)object
345                        change:(NSDictionary *)change
346                       context:(void *)context {
347       
348        if ([keyPath isEqual:@"selectedDomain"]) {
349                [self listGroups];             
350                return;
351        }       
352}
353
354
355@end
Note: See TracBrowser for help on using the browser.
Telnic
Search This Site
Partners
Neustar
ICANN
Main site | WHOIS | Sell .tel | FAQ | Archived Site | About Telnic | Contact Us