Added account groups functionality.
[pithos-ios] / Classes / EditAccountGroupsViewController.m
1 //
2 //  EditAccountGroupsViewController.m
3 //  pithos-ios
4 //
5 // Copyright 2011 GRNET S.A. All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
9 // conditions are met:
10 // 
11 //   1. Redistributions of source code must retain the above
12 //      copyright notice, this list of conditions and the following
13 //      disclaimer.
14 // 
15 //   2. Redistributions in binary form must reproduce the above
16 //      copyright notice, this list of conditions and the following
17 //      disclaimer in the documentation and/or other materials
18 //      provided with the distribution.
19 // 
20 // THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
21 // OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
24 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 // POSSIBILITY OF SUCH DAMAGE.
32 // 
33 // The views and conclusions contained in the software and
34 // documentation are those of the authors and should not be
35 // interpreted as representing official policies, either expressed
36 // or implied, of GRNET S.A.
37
38 #import "EditAccountGroupsViewController.h"
39 #import "UIViewController+Conveniences.h"
40 #import "AccountManager.h"
41
42 #define kGroupDetails 0 
43 #define kSave 1
44 #define kRemove 2
45
46 @implementation EditAccountGroupsViewController
47
48 @synthesize account;
49 @synthesize groupName, groupUsers, removeGroupEnabled;
50 @synthesize groups,metadata;
51
52
53 - (void)dealloc
54 {
55     [groupName release];
56     [groupUsers release];
57     [account release];
58     [metadata release];
59     [groups release];
60     
61     [super dealloc];
62 }
63
64
65 #pragma mark - View lifecycle
66
67 - (void)viewDidLoad
68 {
69     [super viewDidLoad];
70 }
71
72 - (void)viewDidUnload
73 {
74     [super viewDidUnload];
75 }
76
77 - (void)viewWillAppear:(BOOL)animated
78 {
79     [super viewWillAppear:animated];
80 }
81
82 - (void)viewDidAppear:(BOOL)animated
83 {
84     [super viewDidAppear:animated];
85 }
86
87 - (void)viewWillDisappear:(BOOL)animated
88 {
89     [super viewWillDisappear:animated];
90 }
91
92 - (void)viewDidDisappear:(BOOL)animated
93 {
94     [super viewDidDisappear:animated];
95 }
96
97 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
98 {
99     // Return YES for supported orientations
100     return (interfaceOrientation == UIInterfaceOrientationPortrait);
101 }
102
103 #pragma mark - Table view data source
104
105 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
106 {
107     if (removeGroupEnabled)
108         return 3;
109     else
110         return 2;
111 }
112
113 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
114 {
115     if (section == kGroupDetails)
116         return 2;
117     else
118         return 1;
119 }
120
121 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
122 {
123     static NSString *CellIdentifier = @"Cell";
124     
125     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
126     if (cell == nil) {
127         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
128     }
129     
130     cell.accessoryType = UITableViewCellAccessoryNone;
131     if (indexPath.section == kGroupDetails) {
132         CGRect bounds = [cell.contentView bounds];
133         CGRect rect = CGRectInset(bounds, 20.0, 10.0);                        
134         UITextField *textField = [[UITextField alloc] initWithFrame:rect];
135         [textField setFrame:rect];
136         [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
137         [textField setBackgroundColor:[UIColor whiteColor]];
138         [textField setOpaque:YES];
139         [textField setAutocorrectionType:UITextAutocorrectionTypeNo];
140         [textField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
141         [textField setDelegate:self];
142         
143         if (indexPath.row == 0) {
144             textField.returnKeyType = UIReturnKeyNext;
145             textField.placeholder = @"Group name";
146             textField.text = self.groupName;
147             textField.tag = 0;
148         }
149         else if (indexPath.row == 1) {
150             textField.returnKeyType = UIReturnKeyDefault;
151             textField.placeholder = @"Group users";
152             textField.text = self.groupUsers;
153             textField.tag = 1;
154         }
155         cell.selectionStyle = UITableViewCellSelectionStyleNone;
156         [cell.contentView addSubview:textField];
157     }
158     else if (indexPath.section == kSave) { 
159         cell.textLabel.text = @"Save";
160     }
161     else if (indexPath.section == kRemove) {
162         cell.textLabel.text = @"Remove";
163     }
164     
165     
166     return cell;
167 }
168
169 #pragma mark - Table view delegate
170
171 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
172 {
173     NSString *activityMessage;
174     NSIndexPath *keyCellIndexPath;
175     NSIndexPath *valueCellIndexPath;
176     UITableViewCell *cell;
177     if (indexPath.section == kSave) {
178         keyCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:kGroupDetails];
179         cell = [self.tableView cellForRowAtIndexPath:keyCellIndexPath];
180         UITextField *textField = [[cell.contentView subviews] objectAtIndex:0];
181         NSString *newGroupName = textField.text;
182         
183         if ([newGroupName length] == 0
184             || [[newGroupName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
185             [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
186             [self alert:@"Invalid input" message:@"Group name cannot be empty"];
187             return;
188         }
189         
190         valueCellIndexPath = [NSIndexPath indexPathForRow:1 inSection:kGroupDetails];
191         cell = [self.tableView cellForRowAtIndexPath:valueCellIndexPath];
192         textField = [[cell.contentView subviews] objectAtIndex:0];
193         NSString *newGroupUsers = textField.text;
194                     
195         activityMessage = @"Saving group";
196         
197         activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage]text:activityMessage];
198         [activityIndicatorView addToView:self.view];
199
200         [groups removeObjectForKey:groupName];
201         [groups setObject:newGroupUsers forKey:newGroupName];
202         NSDictionary *accountInfo = [NSDictionary dictionaryWithObjectsAndKeys:groups, @"groups",
203                                         metadata, @"metadata",
204                                         nil
205                                         ];
206         [self.account.manager writeAccountMetadata:accountInfo];
207         successObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeAccountMetadataRequestSucceeded"
208                                                                             object:self.account
209                                                                              queue:[NSOperationQueue mainQueue]
210                                                                         usingBlock:^(NSNotification *notification)
211                             {                                   
212                                 [activityIndicatorView removeFromSuperviewAndRelease];
213                                 self.groupName = newGroupName;
214                                 self.groupUsers = newGroupUsers;
215                                 self.removeGroupEnabled = YES;
216                                 self.navigationItem.title = @"Edit Group";
217                                 [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
218                                 [[NSNotificationCenter defaultCenter] removeObserver:successObserver];
219                             }];
220         
221         failureObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeAccountMetadataRequestFailed" 
222                                                                             object:self.account 
223                                                                              queue:[NSOperationQueue mainQueue] 
224                                                                         usingBlock:^(NSNotification *notification)
225                             {
226                                 [activityIndicatorView removeFromSuperviewAndRelease];
227                                 [groups removeObjectForKey:newGroupName];
228                                 [groups setObject:self.groupUsers forKey:self.groupName];
229                                 [self.tableView reloadData];
230                                 [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
231                                 [self alert:@"There was a problem saving the group information." request:[notification.userInfo objectForKey:@"request"]];
232                                    
233                                 [[NSNotificationCenter defaultCenter] removeObserver:failureObserver];
234                             }];
235     } else if (indexPath.section == kRemove) {
236         activityMessage = @"Removing group";
237         activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage];
238         [activityIndicatorView addToView:self.view];
239         
240         [groups removeObjectForKey:groupName];
241         NSDictionary *accountInfo = [NSDictionary dictionaryWithObjectsAndKeys:groups, @"groups",
242                                         metadata, @"metadata",
243                                         nil
244                                         ];
245
246         [self.account.manager writeAccountMetadata:accountInfo];
247         successObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeAccountMetadataRequestSucceeded"
248                                                                             object:self.account
249                                                                              queue:[NSOperationQueue mainQueue]
250                                                                         usingBlock:^(NSNotification *notification)
251                             {
252                                 [activityIndicatorView removeFromSuperviewAndRelease];
253                                 self.groupName = @"";
254                                 self.groupUsers = @"";
255                                 [self.tableView reloadData];
256                                 [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
257                                 [[NSNotificationCenter defaultCenter] removeObserver:successObserver];
258                             }];
259         
260         failureObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeAccountMetadataRequestFailed" 
261                                                                             object:self.account 
262                                                                              queue:[NSOperationQueue mainQueue] 
263                                                                         usingBlock:^(NSNotification *notification)
264                             {
265                                 [activityIndicatorView removeFromSuperviewAndRelease];
266                                 [groups setObject:groupUsers forKey:groupName];
267                                 [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
268                                 [self alert:@"There was a problem removing the group information." request:[notification.userInfo objectForKey:@"request"]];
269                                    
270                                 [[NSNotificationCenter defaultCenter] removeObserver:failureObserver];
271                             }];
272     }
273
274 }
275
276 #pragma mark - Textfield delegate
277
278 - (void)textFieldDidEndEditing:(UITextField *)textField
279 {
280     if ([textField tag] == 0) {
281         if ([textField.text length] == 0
282             || [[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
283             [self alert:@"Invalid input" message:@"Group name cannot be empty"];
284         }
285     }
286 }
287
288
289
290 - (BOOL)textFieldShouldReturn:(UITextField *)textField
291 {
292     
293     if ([textField returnKeyType] == UIReturnKeyNext)
294     {
295         NSInteger nextTag = [textField tag] + 1;
296         UIView *nextTextField = [self.tableView viewWithTag:nextTag];
297         [nextTextField becomeFirstResponder];
298     }
299     else
300     {
301         [textField resignFirstResponder];
302     }
303     
304     return YES;
305 }
306
307
308 @end