root / Classes / EditAccountGroupsViewController.m @ 3a8071d4
History | View | Annotate | Download (11 kB)
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 |
#import "APICallback.h" |
43 |
|
44 |
#define kGroupDetails 0 |
45 |
#define kSave 1 |
46 |
#define kRemove 2 |
47 |
|
48 |
@implementation EditAccountGroupsViewController |
49 |
|
50 |
@synthesize account; |
51 |
@synthesize groupName, groupUsers, removeGroupEnabled; |
52 |
@synthesize groups,metadata; |
53 |
|
54 |
|
55 |
- (void)dealloc |
56 |
{ |
57 |
[groupName release]; |
58 |
[groupUsers release]; |
59 |
[account release]; |
60 |
[metadata release]; |
61 |
[groups release]; |
62 |
[activityIndicatorView release]; |
63 |
[super dealloc]; |
64 |
} |
65 |
|
66 |
|
67 |
#pragma mark - View lifecycle |
68 |
|
69 |
- (void)viewDidLoad |
70 |
{ |
71 |
[super viewDidLoad]; |
72 |
} |
73 |
|
74 |
- (void)viewDidUnload |
75 |
{ |
76 |
[super viewDidUnload]; |
77 |
} |
78 |
|
79 |
- (void)viewWillAppear:(BOOL)animated |
80 |
{ |
81 |
[super viewWillAppear:animated]; |
82 |
} |
83 |
|
84 |
- (void)viewDidAppear:(BOOL)animated |
85 |
{ |
86 |
[super viewDidAppear:animated]; |
87 |
} |
88 |
|
89 |
- (void)viewWillDisappear:(BOOL)animated |
90 |
{ |
91 |
[super viewWillDisappear:animated]; |
92 |
} |
93 |
|
94 |
- (void)viewDidDisappear:(BOOL)animated |
95 |
{ |
96 |
[super viewDidDisappear:animated]; |
97 |
} |
98 |
|
99 |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { |
100 |
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || (toInterfaceOrientation == 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.textLabel.text = @""; |
131 |
cell.accessoryType = UITableViewCellAccessoryNone; |
132 |
if (indexPath.section == kGroupDetails) { |
133 |
UITextField *textField = nil; |
134 |
for (id subView in cell.contentView.subviews) { |
135 |
if ([subView isKindOfClass:[UITextField class]]) { |
136 |
textField = (UITextField *)subView; |
137 |
} |
138 |
} |
139 |
|
140 |
if (textField == nil) { |
141 |
CGRect bounds = [cell.contentView bounds]; |
142 |
CGRect rect = CGRectInset(bounds, 10.0, 10.0); |
143 |
textField = [[UITextField alloc] initWithFrame:rect]; |
144 |
[textField setFrame:rect]; |
145 |
} |
146 |
|
147 |
[textField setClearButtonMode:UITextFieldViewModeWhileEditing]; |
148 |
[textField setBackgroundColor:[UIColor clearColor]]; |
149 |
[textField setOpaque:YES]; |
150 |
[textField setAutocorrectionType:UITextAutocorrectionTypeNo]; |
151 |
[textField setAutocapitalizationType:UITextAutocapitalizationTypeNone]; |
152 |
[textField setDelegate:self]; |
153 |
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |
154 |
|
155 |
if (indexPath.row == 0) { |
156 |
textField.returnKeyType = UIReturnKeyNext; |
157 |
textField.placeholder = @"Group name"; |
158 |
textField.text = self.groupName; |
159 |
textField.tag = 0; |
160 |
} |
161 |
else if (indexPath.row == 1) { |
162 |
textField.returnKeyType = UIReturnKeyDefault; |
163 |
textField.placeholder = @"Group users"; |
164 |
textField.text = self.groupUsers; |
165 |
textField.tag = 1; |
166 |
} |
167 |
cell.selectionStyle = UITableViewCellSelectionStyleNone; |
168 |
[cell.contentView addSubview:textField]; |
169 |
} |
170 |
else if (indexPath.section == kSave) { |
171 |
cell.textLabel.text = @"Save"; |
172 |
} |
173 |
else if (indexPath.section == kRemove) { |
174 |
cell.textLabel.text = @"Remove"; |
175 |
} |
176 |
|
177 |
|
178 |
return cell; |
179 |
} |
180 |
|
181 |
#pragma mark - Table view delegate |
182 |
|
183 |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
184 |
{ |
185 |
NSString *activityMessage; |
186 |
NSIndexPath *keyCellIndexPath; |
187 |
NSIndexPath *valueCellIndexPath; |
188 |
UITableViewCell *cell; |
189 |
|
190 |
if (indexPath.section != kGroupDetails) |
191 |
[self.view endEditing:YES]; |
192 |
|
193 |
if (indexPath.section == kSave) { |
194 |
keyCellIndexPath = [NSIndexPath indexPathForRow:0 inSection:kGroupDetails]; |
195 |
cell = [self.tableView cellForRowAtIndexPath:keyCellIndexPath]; |
196 |
UITextField *textField = [[cell.contentView subviews] objectAtIndex:0]; |
197 |
NSString *newGroupName = textField.text; |
198 |
|
199 |
if (![self userInputIsValid:newGroupName fieldName:@"Group name"]) { |
200 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
201 |
return; |
202 |
} |
203 |
|
204 |
valueCellIndexPath = [NSIndexPath indexPathForRow:1 inSection:kGroupDetails]; |
205 |
cell = [self.tableView cellForRowAtIndexPath:valueCellIndexPath]; |
206 |
textField = [[cell.contentView subviews] objectAtIndex:0]; |
207 |
NSString *newGroupUsers = textField.text; |
208 |
|
209 |
if (![self userInputIsValid:newGroupUsers fieldName:@"Group users"]) { |
210 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
211 |
return; |
212 |
} |
213 |
|
214 |
activityMessage = @"Saving group"; |
215 |
[activityIndicatorView release]; |
216 |
activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage]text:activityMessage]; |
217 |
[activityIndicatorView addToView:self.view]; |
218 |
|
219 |
[groups removeObjectForKey:groupName]; |
220 |
[groups setObject:newGroupUsers forKey:newGroupName]; |
221 |
NSDictionary *accountInfo = [NSDictionary dictionaryWithObjectsAndKeys:groups, @"groups", |
222 |
metadata, @"metadata", |
223 |
nil |
224 |
]; |
225 |
[[self.account.manager writeAccountMetadata:accountInfo] |
226 |
success:^(OpenStackRequest *request) { |
227 |
[activityIndicatorView removeFromSuperview]; |
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 |
[self.tableView reloadData]; |
234 |
} |
235 |
failure:^(OpenStackRequest *request) { |
236 |
[activityIndicatorView removeFromSuperview]; |
237 |
[groups removeObjectForKey:newGroupName]; |
238 |
[groups setObject:self.groupUsers forKey:self.groupName]; |
239 |
[self.tableView reloadData]; |
240 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
241 |
[self alert:@"There was a problem saving the group information." request:request]; |
242 |
}]; |
243 |
} else if (indexPath.section == kRemove) { |
244 |
activityMessage = @"Removing group"; |
245 |
[activityIndicatorView release]; |
246 |
activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:activityMessage] text:activityMessage]; |
247 |
[activityIndicatorView addToView:self.view]; |
248 |
|
249 |
[groups removeObjectForKey:groupName]; |
250 |
NSDictionary *accountInfo = [NSDictionary dictionaryWithObjectsAndKeys:groups, @"groups", |
251 |
metadata, @"metadata", |
252 |
nil |
253 |
]; |
254 |
|
255 |
[[self.account.manager writeAccountMetadata:accountInfo] |
256 |
success:^(OpenStackRequest *request) { |
257 |
[activityIndicatorView removeFromSuperview]; |
258 |
self.groupName = @""; |
259 |
self.groupUsers = @""; |
260 |
self.removeGroupEnabled = NO; |
261 |
[self.tableView reloadData]; |
262 |
[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; |
263 |
} |
264 |
failure:^(OpenStackRequest *request) { |
265 |
[activityIndicatorView removeFromSuperview]; |
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:request]; |
269 |
}]; |
270 |
} |
271 |
} |
272 |
|
273 |
#pragma mark - Textfield delegate |
274 |
|
275 |
- (BOOL)textFieldShouldReturn:(UITextField *)textField |
276 |
{ |
277 |
|
278 |
if ([textField returnKeyType] == UIReturnKeyNext) |
279 |
{ |
280 |
[self userInputIsValid:textField.text fieldName:@"Group name"]; |
281 |
|
282 |
NSInteger nextTag = [textField tag] + 1; |
283 |
UIView *nextTextField = [self.tableView viewWithTag:nextTag]; |
284 |
[nextTextField becomeFirstResponder]; |
285 |
} |
286 |
else |
287 |
{ |
288 |
[self userInputIsValid:textField.text fieldName:@"Group users"]; |
289 |
[textField resignFirstResponder]; |
290 |
} |
291 |
|
292 |
return YES; |
293 |
} |
294 |
|
295 |
#pragma mark - Helper methods |
296 |
|
297 |
- (BOOL)userInputIsValid:(NSString *)input fieldName:(NSString *)fieldName { |
298 |
if (![input length] || ![[input stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]) { |
299 |
[self alert:@"Invalid input" message:[NSString stringWithFormat:@"%@ field cannot be empty", fieldName]]; |
300 |
return NO; |
301 |
} |
302 |
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"= "]; |
303 |
if ([input rangeOfCharacterFromSet:set].location != NSNotFound) { |
304 |
[self alert:@"Invalid input" message:[NSString stringWithFormat:@"%@ field cannot contain '=' or whitespace characters", fieldName]]; |
305 |
return NO; |
306 |
} |
307 |
|
308 |
return YES; |
309 |
} |
310 |
|
311 |
|
312 |
|
313 |
@end |