Developer Area

root/apps/iphone/my.tel/trunk/Classes/RecordEditController.m @ 149

Revision 149, 9.4 kB (checked in by henri, 5 years ago)

Inserted Telnic Ltd. in place of MyCompanyName

Line 
1//
2//  RecordEditController.m
3//  VIP.tel
4//
5//  Created by Henri Asseily on 11/21/08.
6//  Copyright 2008 Telnic Ltd.. All rights reserved.
7//
8
9#import "RecordEditController.h"
10
11#define kTransitionDuration    0.75
12
13@implementation RecordEditController
14
15@synthesize barHeader;
16@synthesize barButtonSave;
17@synthesize rPicker;
18@synthesize rValue;
19@synthesize rLabel;
20@synthesize rVisibility;
21@synthesize saveIndicator;
22@synthesize caller;
23
24#pragma mark ----- Calling methods
25
26- (void)presentViewForRecord:(NSDictionary *)aRec {
27        if (!self.view) {
28                NSLog(@"Fatal error: record edit view wasn't properly loaded");
29                return;
30                //[self loadView];
31                // TODO: rpicker doesn't have any data here.
32                // Either exit this method without showing the view, or add to the RecordEditDelegate
33                // protocol a method for a callback to fill the rpicker.
34        }
35        [theRecord release];
36        if (!aRec) { // new
37                theRecord = [[NSMutableDictionary dictionaryWithCapacity:8] retain];
38                barHeader.title = @"New Item";
39                rValue.text = @"";
40                rLabel.text = @"";
41                rVisibility.on = TRUE;
42                [theRecord setObject:@"0" forKey:@"apiId"];
43                [theRecord setObject:[VIPConnect sharedInstance].selectedDomain forKey:@"domainName"];
44                [rPicker selectRow:0 inComponent:0 animated:NO];        // trigger a UI change
45                [rPicker selectRow:0 inComponent:1 animated:NO];        // trigger a UI change
46                [self pickerView:rPicker didSelectRow:0 inComponent:0]; // trigger a data change
47                [self pickerView:rPicker didSelectRow:0 inComponent:1]; // trigger a data change
48        } else { // edit
49                if (!aRec) {
50                        NSLog(@"Missing record for RecordEditController");
51                        return;
52                }
53                theRecord = [[NSMutableDictionary dictionaryWithDictionary:aRec] retain];
54                barHeader.title = @"Edit Item";
55                [theRecord setObject:[VIPConnect sharedInstance].selectedDomain forKey:@"domainName"];
56                rValue.text = [theRecord objectForKey:@"value"];
57                rLabel.text = [theRecord objectForKey:@"label"];
58                if ([(NSArray *)[theRecord objectForKey:@"profiles"] containsObject:[VIPConnect sharedInstance].selectedProfileId]) {
59                        rVisibility.on = TRUE;
60                } else {
61                        rVisibility.on = FALSE;
62                }
63        }
64        // TODO: Handle multiple location indicators and service keys
65        // We currently only respect the first one
66        NSString *theService = [(NSArray *)[theRecord objectForKey:@"serviceKeys"] objectAtIndex:0];
67        NSString *theLi;
68        if ([(NSArray *)[theRecord objectForKey:@"locations"] count] == 0) {
69                theLi = @"";
70        } else {
71                theLi = [(NSArray *)[theRecord objectForKey:@"locations"] objectAtIndex:0];
72        }
73       
74        [rPicker selectRow:[sArrKey indexOfObject:theService] inComponent:0 animated:NO];
75        [rPicker selectRow:[liArrKey indexOfObject:theLi] inComponent:1 animated:NO];
76        [self viewWillAppear:YES];
77    [UIView setAnimationDelegate:self];
78    [UIView setAnimationDidStopSelector:@selector(animationDidStop:animationIDfinished:finished:context:)];
79    [UIView beginAnimations:nil context:nil];
80    [UIView setAnimationDuration:kTransitionDuration];
81   
82    [UIView setAnimationTransition:(UIViewAnimationTransitionFlipFromLeft)
83                                                   forView:[(UIViewController *)caller view] cache:YES];
84        [[(UIViewController *)caller view] addSubview:self.view];
85    [UIView commitAnimations];
86}
87
88- (void)setPickerCol1:(NSDictionary *)pickerData1 col2:(NSDictionary *)pickerData2 {
89        // Sets picker data and sorts the columns
90        NSUInteger i, count;
91
92        // Do first column
93        // First set the keys and related strings
94        [sArrKey setArray:[pickerData1 allKeys]];
95        [sArrVal removeAllObjects];
96        for (NSString *aK in sArrKey) {
97                [sArrVal addObject:[pickerData1 objectForKey:aK]];
98        }
99        // Create a reverse lookup to find the service keys from the strings
100        NSDictionary *pickerDict1 = [NSDictionary dictionaryWithObjects:sArrKey forKeys:sArrVal];
101        // Sort the string arrays as the picker strings
102        [sArrVal sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
103        // Resort the key arrays based on the strings arrays
104        count = [sArrVal count];
105        for (i = 0; i < count; i++) {
106                [sArrKey replaceObjectAtIndex:i
107                                                   withObject:[pickerDict1 objectForKey:[sArrVal objectAtIndex:i]]];
108        }
109        // and finally reload the picker component
110        [rPicker reloadComponent:0];
111       
112        // Same for second column, but also add a "None" entry
113        [liArrKey setArray:[pickerData2 allKeys]];
114        [liArrVal removeAllObjects];
115        for (NSString *aK in liArrKey) {
116                [liArrVal addObject:[pickerData2 objectForKey:aK]];
117        }
118        NSDictionary *pickerDict2 = [NSDictionary dictionaryWithObjects:liArrKey forKeys:liArrVal];
119        [liArrVal sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
120        count = [liArrVal count];
121        for (i = 0; i < count; i++) {
122                [liArrKey replaceObjectAtIndex:i
123                                                        withObject:[pickerDict2 objectForKey:[liArrVal objectAtIndex:i]]];
124        }
125        if ([(NSString *)[liArrKey objectAtIndex:0] length] != 0) {
126                // There isn't an "empty" choice, add it
127                [liArrKey insertObject:@"" atIndex:0];
128                [liArrVal insertObject:@"" atIndex:0];
129        }
130       
131        [rPicker reloadComponent:1];   
132}
133
134#pragma mark ------ Standard View Methods
135- (void)viewDidLoad {
136    [super viewDidLoad];
137
138        sArrKey = [[NSMutableArray arrayWithCapacity:30] retain];
139        sArrVal = [[NSMutableArray arrayWithCapacity:30] retain];
140        liArrKey = [[NSMutableArray arrayWithCapacity:10] retain];
141        liArrVal = [[NSMutableArray arrayWithCapacity:10] retain];
142}
143
144- (void)viewWillAppear:(BOOL)animated {
145        [rPicker reloadComponent:0];
146        [rPicker reloadComponent:1];
147}
148
149- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
150    // Return YES for supported orientations
151    return (interfaceOrientation == UIInterfaceOrientationPortrait);
152}
153
154
155- (void)didReceiveMemoryWarning {
156    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
157    // Release anything that's not essential, such as cached data
158}
159
160
161- (void)dealloc {
162        [sArrKey release];
163        [sArrVal release];
164        [liArrKey release];
165        [liArrVal release];     
166    [super dealloc];
167}
168
169#pragma mark ------ Actions
170
171- (IBAction)didClickCancel:(id)sender {
172        [self.view removeFromSuperview];
173}
174
175- (IBAction)didClickSave:(id)sender {
176        // store value, label, profiles.
177        // servicekey and LI are set when the user selects the pickerViews
178        // apiId and domainName are set at the start in presentView
179        [theRecord setObject:rValue.text forKey:@"value"];
180        [theRecord setObject:rLabel.text forKey:@"label"];
181
182        NSMutableArray *newProfilesArray = [NSMutableArray arrayWithArray:[theRecord objectForKey:@"profiles"]];
183        [newProfilesArray removeObjectIdenticalTo:[VIPConnect sharedInstance].selectedProfileId];
184        if (rVisibility.on) {
185                [newProfilesArray addObject:[VIPConnect sharedInstance].selectedProfileId];
186        }
187        [theRecord setObject:newProfilesArray forKey:@"profiles"];
188       
189        // Store the record
190        barButtonSave.enabled = NO;
191        [saveIndicator startAnimating];
192        Record *conn = [[[Record alloc] init] autorelease];
193        [conn setTheDelegate:self];
194        [conn setActionSel:@selector(afterSaveAction:)];
195        [conn setConnectionUrl:[conn urlFromAction:@"storerecord"]];
196        [conn setPayload:theRecord];
197        [conn performLookup:TRUE];
198}
199
200- (void)afterSaveAction:(NSDictionary *)parsedJson {
201        if ([[parsedJson valueForKey:@"success"] integerValue] == 1) {
202                [caller didStoreRecord:theRecord];
203                [self.view removeFromSuperview];
204        } else {
205                [JsonConnection throwJsonErrorAlert:parsedJson];
206        }
207        [saveIndicator stopAnimating];
208        barButtonSave.enabled = YES;   
209}
210
211#pragma mark ------ TextField delegate methods
212- (void)textFieldDidBeginEditing:(UITextField *)textField {
213        if (self.view.frame.origin.y >=0) {
214                CGRect newFrame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y -230,
215                                                                         self.view.frame.size.width, self.view.frame.size.height + 230);
216                self.view.frame = newFrame;
217        }
218        return;
219}
220- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
221        return YES;
222}
223- (BOOL)textFieldShouldReturn:(UITextField *)textField {
224        [textField resignFirstResponder];
225        if (self.view.frame.origin.y <0) {
226                CGRect newFrame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 230,
227                                                                         self.view.frame.size.width, self.view.frame.size.height - 230);
228               
229                self.view.frame = newFrame;
230        }
231        return YES;
232}
233
234#pragma mark ------ Picker data source methods
235
236- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
237        return 2;
238}
239
240- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
241        if (component == 0) {
242                return [sArrVal count];
243        }
244        if (component == 1) {
245                return [liArrVal count];
246        }
247        return 0;
248}
249
250#pragma mark ------ Picker delegate methods
251
252- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
253        // TODO: Handle multiple service keys and LIs
254        if (component == 0) {
255                [theRecord setObject:[NSArray arrayWithObject:[sArrKey objectAtIndex:row]]
256                                          forKey:@"serviceKeys"];
257        }
258        if (component == 1) {
259                [theRecord setObject:[NSArray arrayWithObject:[liArrKey objectAtIndex:row]]
260                                          forKey:@"locations"];
261        }
262}
263
264/*
265- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
266}
267 */
268
269- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
270        if (component == 0) {
271                return 200;
272        }
273        if (component == 1) {
274                return 100;
275        }
276        return 0;
277}
278
279
280- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
281        if (component == 0) {
282                return [sArrVal objectAtIndex:row];
283        }
284        if (component == 1) {
285                return [liArrVal objectAtIndex:row];
286        }
287        return @"";
288}
289
290/*
291- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
292                  forComponent:(NSInteger)component reusingView:(UIView *)view {
293}
294 */
295                               
296@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