2 // EditAccountGroupsViewController.m
5 // Copyright 2011 GRNET S.A. All rights reserved.
7 // Redistribution and use in source and binary forms, with or
8 // without modification, are permitted provided that the following
11 // 1. Redistributions of source code must retain the above
12 // copyright notice, this list of conditions and the following
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.
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.
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.
38 #import "EditAccountGroupsViewController.h"
39 #import "UIViewController+Conveniences.h"
40 #import "AccountManager.h"
42 #define kGroupDetails 0
46 @implementation EditAccountGroupsViewController
49 @synthesize groupName, groupUsers, removeGroupEnabled;
50 @synthesize groups,metadata;
65 #pragma mark - View lifecycle
74 [super viewDidUnload];
77 - (void)viewWillAppear:(BOOL)animated
79 [super viewWillAppear:animated];
82 - (void)viewDidAppear:(BOOL)animated
84 [super viewDidAppear:animated];
87 - (void)viewWillDisappear:(BOOL)animated
89 [super viewWillDisappear:animated];
92 - (void)viewDidDisappear:(BOOL)animated
94 [super viewDidDisappear:animated];
97 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
99 // Return YES for supported orientations
100 return (interfaceOrientation == UIInterfaceOrientationPortrait);
103 #pragma mark - Table view data source
105 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
107 if (removeGroupEnabled)
113 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
115 if (section == kGroupDetails)
121 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
123 static NSString *CellIdentifier = @"Cell";
125 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
127 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
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];
143 if (indexPath.row == 0) {
144 textField.returnKeyType = UIReturnKeyNext;
145 textField.placeholder = @"Group name";
146 textField.text = self.groupName;
149 else if (indexPath.row == 1) {
150 textField.returnKeyType = UIReturnKeyDefault;
151 textField.placeholder = @"Group users";
152 textField.text = self.groupUsers;
155 cell.selectionStyle = UITableViewCellSelectionStyleNone;
156 [cell.contentView addSubview:textField];
158 else if (indexPath.section == kSave) {
159 cell.textLabel.text = @"Save";
161 else if (indexPath.section == kRemove) {
162 cell.textLabel.text = @"Remove";
169 #pragma mark - Table view delegate
171 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
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;
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"];
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;
195 activityMessage = @"Saving group";
197 activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage]text:activityMessage];
198 [activityIndicatorView addToView:self.view];
200 [groups removeObjectForKey:groupName];
201 [groups setObject:newGroupUsers forKey:newGroupName];
202 NSDictionary *accountInfo = [NSDictionary dictionaryWithObjectsAndKeys:groups, @"groups",
203 metadata, @"metadata",
206 [self.account.manager writeAccountMetadata:accountInfo];
207 successObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeAccountMetadataRequestSucceeded"
209 queue:[NSOperationQueue mainQueue]
210 usingBlock:^(NSNotification *notification)
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];
221 failureObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeAccountMetadataRequestFailed"
223 queue:[NSOperationQueue mainQueue]
224 usingBlock:^(NSNotification *notification)
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"]];
233 [[NSNotificationCenter defaultCenter] removeObserver:failureObserver];
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];
240 [groups removeObjectForKey:groupName];
241 NSDictionary *accountInfo = [NSDictionary dictionaryWithObjectsAndKeys:groups, @"groups",
242 metadata, @"metadata",
246 [self.account.manager writeAccountMetadata:accountInfo];
247 successObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeAccountMetadataRequestSucceeded"
249 queue:[NSOperationQueue mainQueue]
250 usingBlock:^(NSNotification *notification)
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];
260 failureObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeAccountMetadataRequestFailed"
262 queue:[NSOperationQueue mainQueue]
263 usingBlock:^(NSNotification *notification)
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"]];
270 [[NSNotificationCenter defaultCenter] removeObserver:failureObserver];
276 #pragma mark - Textfield delegate
278 - (void)textFieldDidEndEditing:(UITextField *)textField
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"];
290 - (BOOL)textFieldShouldReturn:(UITextField *)textField
293 if ([textField returnKeyType] == UIReturnKeyNext)
295 NSInteger nextTag = [textField tag] + 1;
296 UIView *nextTextField = [self.tableView viewWithTag:nextTag];
297 [nextTextField becomeFirstResponder];
301 [textField resignFirstResponder];