UI changes.
[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 #import "OpenStackRequest.h"
42
43 #define kGroupDetails 0 
44 #define kSave 1
45 #define kRemove 2
46
47 @implementation EditAccountGroupsViewController
48
49 @synthesize account;
50 @synthesize groupName, groupUsers, removeGroupEnabled;
51 @synthesize groups,metadata;
52
53
54 - (void)dealloc
55 {
56     [groupName release];
57     [groupUsers release];
58     [account release];
59     [metadata release];
60     [groups release];
61     
62     [super dealloc];
63 }
64
65
66 #pragma mark - View lifecycle
67
68 - (void)viewDidLoad
69 {
70     [super viewDidLoad];
71 }
72
73 - (void)viewDidUnload
74 {
75     [super viewDidUnload];
76 }
77
78 - (void)viewWillAppear:(BOOL)animated
79 {
80     [super viewWillAppear:animated];
81 }
82
83 - (void)viewDidAppear:(BOOL)animated
84 {
85     [super viewDidAppear:animated];
86 }
87
88 - (void)viewWillDisappear:(BOOL)animated
89 {
90     [super viewWillDisappear:animated];
91 }
92
93 - (void)viewDidDisappear:(BOOL)animated
94 {
95     [super viewDidDisappear:animated];
96 }
97
98 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
99     return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == UIInterfaceOrientationPortrait);
100 }
101
102 #pragma mark - Table view data source
103
104 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
105 {
106     if (removeGroupEnabled)
107         return 3;
108     else
109         return 2;
110 }
111
112 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
113 {
114     if (section == kGroupDetails)
115         return 2;
116     else
117         return 1;
118 }
119
120 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
121 {
122     static NSString *CellIdentifier = @"Cell";
123     
124     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
125     if (cell == nil) {
126         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
127     }
128     
129     cell.textLabel.text = @"";
130     cell.accessoryType = UITableViewCellAccessoryNone;
131     if (indexPath.section == kGroupDetails) {
132         UITextField *textField = nil;
133         for (id subView in cell.contentView.subviews) {
134             if ([subView isKindOfClass:[UITextField class]]) {
135                 textField = (UITextField *)subView;
136             }
137         }
138         
139         if (textField == nil) {
140             CGRect bounds = [cell.contentView bounds];
141             CGRect rect = CGRectInset(bounds, 20.0, 10.0);                        
142             textField = [[UITextField alloc] initWithFrame:rect];
143             [textField setFrame:rect];
144         }
145         
146         [textField setClearButtonMode:UITextFieldViewModeWhileEditing];
147         [textField setBackgroundColor:[UIColor clearColor]];
148         [textField setOpaque:YES];
149         [textField setAutocorrectionType:UITextAutocorrectionTypeNo];
150         [textField setAutocapitalizationType:UITextAutocapitalizationTypeNone];
151         [textField setDelegate:self];
152         textField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
153
154         if (indexPath.row == 0) {
155             textField.returnKeyType = UIReturnKeyNext;
156             textField.placeholder = @"Group name";
157             textField.text = self.groupName;
158             textField.tag = 0;
159         }
160         else if (indexPath.row == 1) {
161             textField.returnKeyType = UIReturnKeyDefault;
162             textField.placeholder = @"Group users";
163             textField.text = self.groupUsers;
164             textField.tag = 1;
165         }
166         cell.selectionStyle = UITableViewCellSelectionStyleNone;
167         [cell.contentView addSubview:textField];
168     }
169     else if (indexPath.section == kSave) { 
170         cell.textLabel.text = @"Save";
171     }
172     else if (indexPath.section == kRemove) {
173         cell.textLabel.text = @"Remove";
174     }
175     
176     
177     return cell;
178 }
179
180 #pragma mark - Table view delegate
181
182 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
183 {
184     NSString *activityMessage;
185     NSIndexPath *keyCellIndexPath;
186     NSIndexPath *valueCellIndexPath;
187     UITableViewCell *cell;
188
189     if (indexPath.section != kGroupDetails)
190         [self.view endEditing:YES];
191     
192     if (indexPath.section == kSave) {
193         keyCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:kGroupDetails];
194         cell = [self.tableView cellForRowAtIndexPath:keyCellIndexPath];
195         UITextField *textField = [[cell.contentView subviews] objectAtIndex:0];
196         NSString *newGroupName = textField.text;
197         
198         if ([newGroupName length] == 0
199             || [[newGroupName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
200             [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
201             [self alert:@"Invalid input" message:@"Group name cannot be empty"];
202             return;
203         }
204         
205         valueCellIndexPath = [NSIndexPath indexPathForRow:1 inSection:kGroupDetails];
206         cell = [self.tableView cellForRowAtIndexPath:valueCellIndexPath];
207         textField = [[cell.contentView subviews] objectAtIndex:0];
208         NSString *newGroupUsers = textField.text;
209                     
210         activityMessage = @"Saving group";
211         
212         activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage]text:activityMessage];
213         [activityIndicatorView addToView:self.view];
214
215         [groups removeObjectForKey:groupName];
216         [groups setObject:newGroupUsers forKey:newGroupName];
217         NSDictionary *accountInfo = [NSDictionary dictionaryWithObjectsAndKeys:groups, @"groups",
218                                         metadata, @"metadata",
219                                         nil
220                                         ];
221         [self.account.manager writeAccountMetadata:accountInfo];
222         successObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeAccountMetadataRequestSucceeded"
223                                                                             object:self.account
224                                                                              queue:[NSOperationQueue mainQueue]
225                                                                         usingBlock:^(NSNotification *notification)
226                             {                                   
227                                 [activityIndicatorView removeFromSuperviewAndRelease];
228                                 self.groupName = newGroupName;
229                                 self.groupUsers = newGroupUsers;
230                                 self.removeGroupEnabled = YES;
231                                 self.navigationItem.title = @"Edit Group";
232                                 [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
233                                 [[NSNotificationCenter defaultCenter] removeObserver:successObserver];
234                             }];
235         
236         failureObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeAccountMetadataRequestFailed" 
237                                                                             object:self.account 
238                                                                              queue:[NSOperationQueue mainQueue] 
239                                                                         usingBlock:^(NSNotification *notification)
240                             {
241                                 [activityIndicatorView removeFromSuperviewAndRelease];
242                                 [groups removeObjectForKey:newGroupName];
243                                 [groups setObject:self.groupUsers forKey:self.groupName];
244                                 [self.tableView reloadData];
245                                 [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
246                                 [self alert:@"There was a problem saving the group information." request:[notification.userInfo objectForKey:@"request"]];
247                                    
248                                 [[NSNotificationCenter defaultCenter] removeObserver:failureObserver];
249                             }];
250     } else if (indexPath.section == kRemove) {
251         activityMessage = @"Removing group";
252         activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage];
253         [activityIndicatorView addToView:self.view];
254         
255         [groups removeObjectForKey:groupName];
256         NSDictionary *accountInfo = [NSDictionary dictionaryWithObjectsAndKeys:groups, @"groups",
257                                         metadata, @"metadata",
258                                         nil
259                                         ];
260
261         [self.account.manager writeAccountMetadata:accountInfo];
262         successObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeAccountMetadataRequestSucceeded"
263                                                                             object:self.account
264                                                                              queue:[NSOperationQueue mainQueue]
265                                                                         usingBlock:^(NSNotification *notification)
266                             {
267                                 [activityIndicatorView removeFromSuperviewAndRelease];
268                                 self.groupName = @"";
269                                 self.groupUsers = @"";
270                                 [self.tableView reloadData];
271                                 [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
272                                 [[NSNotificationCenter defaultCenter] removeObserver:successObserver];
273                             }];
274         
275         failureObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeAccountMetadataRequestFailed" 
276                                                                             object:self.account 
277                                                                              queue:[NSOperationQueue mainQueue] 
278                                                                         usingBlock:^(NSNotification *notification)
279                             {
280                                 [activityIndicatorView removeFromSuperviewAndRelease];
281                                 [groups setObject:groupUsers forKey:groupName];
282                                 [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
283                                 [self alert:@"There was a problem removing the group information." request:[notification.userInfo objectForKey:@"request"]];
284                                    
285                                 [[NSNotificationCenter defaultCenter] removeObserver:failureObserver];
286                             }];
287     }
288
289 }
290
291 #pragma mark - Textfield delegate
292
293 //- (void)textFieldDidEndEditing:(UITextField *)textField {
294 //    if ([textField tag] == 0) {
295 //        if ([textField.text length] == 0
296 //            || [[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
297 //            [self alert:@"Invalid input" message:@"Group name cannot be empty"];
298 //        }
299 //    }
300 //}
301
302 - (BOOL)textFieldShouldReturn:(UITextField *)textField
303 {
304     
305     if ([textField returnKeyType] == UIReturnKeyNext)
306     {
307         if ([textField.text length] == 0
308             || [[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] == 0) {
309             [self alert:@"Invalid input" message:@"Group name cannot be empty"];
310         }
311
312         NSInteger nextTag = [textField tag] + 1;
313         UIView *nextTextField = [self.tableView viewWithTag:nextTag];
314         [nextTextField becomeFirstResponder];
315     }
316     else
317     {
318         [textField resignFirstResponder];
319     }
320     
321     return YES;
322 }
323
324
325 @end