root/apps/iphone/my.tel/trunk/Classes/CurrentAccount.m
@
795
| Revision 795, 7.6 kB (checked in by henri, 2 years ago) |
|---|
| Rev | Line | |
|---|---|---|
| [794] | 1 | // |
| 2 | // CurrentAccount.m | |
| 3 | // VIP.tel | |
| 4 | // | |
| 5 | // Created by Henri Asseily on 1/28/11. | |
| 6 | // Copyright 2011 Telnic Ltd. All rights reserved. | |
| 7 | // | |
| 8 | ||
| 9 | #import "CurrentAccount.h" | |
| 10 | #import "Record.h" | |
| 11 | #import "Keyword.h" | |
| 12 | ||
| 13 | // This is a singleton class, see below | |
| 14 | static CurrentAccount *currentAccount = nil; | |
| 15 | ||
| 16 | @interface CurrentAccount (PrivateMethods) | |
| 17 | ||
| 18 | - (void)getServices; | |
| 19 | - (void)getLocations; | |
| 20 | - (void)getKeywords; | |
| 21 | ||
| 22 | - (void)updateServicesWithJson:(NSDictionary *)parsedJson; | |
| 23 | - (void)updateLocationsWithJson:(NSDictionary *)parsedJson; | |
| 24 | - (void)updateKeywordsWithJson:(NSDictionary *)parsedJson; | |
| 25 | ||
| 26 | @end | |
| 27 | ||
| 28 | @implementation CurrentAccount | |
| 29 | ||
| 30 | @synthesize timestamp; | |
| 31 | @synthesize services; | |
| 32 | @synthesize locations; | |
| 33 | @synthesize keywords; | |
| 34 | ||
| 35 | #pragma mark - | |
| 36 | #pragma mark init and refresh | |
| 37 | ||
| 38 | - (CurrentAccount *)init { | |
| 39 | self = [super init]; | |
| 40 | // Fill default English versions from file | |
| [795] | 41 | [self willChangeValueForKey:@"services"]; |
| 42 | [self willChangeValueForKey:@"locations"]; | |
| 43 | [self willChangeValueForKey:@"keywords"]; | |
| [794] | 44 | _serviceKeys = [[NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] |
| 45 | pathForResource:@"ServiceTypes" | |
| 46 | ofType:@"plist"]] retain]; | |
| 47 | _locationKeys = [[NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] | |
| 48 | pathForResource:@"LocationIndicators" | |
| 49 | ofType:@"plist"]] retain]; | |
| 50 | _keywordKeys = [[NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] | |
| 51 | pathForResource:@"KeywordTypesLongNames" | |
| 52 | ofType:@"plist"]] retain]; | |
| 53 | [self didChangeValueForKey:@"services"]; | |
| 54 | [self didChangeValueForKey:@"locations"]; | |
| 55 | [self didChangeValueForKey:@"keywords"]; | |
| 56 | ||
| [795] | 57 | [self willChangeValueForKey:@"timestamp"]; |
| [794] | 58 | _timestamp = [[NSDate date] retain]; |
| 59 | [self didChangeValueForKey:@"timestamp"]; | |
| 60 | ||
| 61 | return self; | |
| 62 | } | |
| 63 | ||
| 64 | - (void)refreshData { | |
| [795] | 65 | // [self getServices]; |
| [794] | 66 | [self getLocations]; |
| 67 | [self getKeywords]; | |
| [795] | 68 | [self performSelector:@selector(getServices) withObject:nil afterDelay:30]; |
| [794] | 69 | } |
| 70 | ||
| 71 | #pragma mark - | |
| 72 | #pragma mark accessing localized info | |
| 73 | ||
| 74 | - (NSString *)localizedNameForService:(NSString *)aKey { | |
| 75 | NSString *val = (NSString *)[_serviceKeys objectForKey:aKey]; | |
| 76 | if (val == nil) | |
| 77 | return aKey; | |
| 78 | return val; | |
| 79 | } | |
| 80 | ||
| 81 | - (NSString *)localizedNameForLocation:(NSString *)aKey { | |
| 82 | NSString *val = (NSString *)[_locationKeys objectForKey:aKey]; | |
| 83 | if (val == nil) | |
| 84 | return aKey; | |
| 85 | return val; | |
| 86 | } | |
| 87 | ||
| 88 | - (NSString *)localizedNameForKeyword:(NSString *)aKey { | |
| 89 | NSString *val = (NSString *)[_keywordKeys objectForKey:aKey]; | |
| 90 | if (val == nil) | |
| 91 | return aKey; | |
| 92 | return val; | |
| 93 | } | |
| 94 | ||
| 95 | #pragma mark - | |
| 96 | #pragma mark data management | |
| 97 | ||
| 98 | - (void)getServices { | |
| 99 | // successResult = { | |
| 100 | // success: true, | |
| 101 | // serviceKeys: [{key: "voice", label: "Voice Call", id: -1}, | |
| 102 | // {key: "fax", label: "Fax", id: -1}, | |
| 103 | // {key: "x-c2c", label: "Click2Communicate", id: 21}], | |
| 104 | // actionMessages: "request successful", | |
| 105 | // "2nd message here", | |
| 106 | // "3rd message here"] | |
| 107 | // }; | |
| 108 | ||
| 109 | Record *conn = [[[Record alloc] init] autorelease]; | |
| 110 | [conn setTheDelegate:self]; | |
| 111 | [conn setActionSel:@selector(updateServicesWithJson:)]; | |
| 112 | [conn setConnectionUrl:[conn urlFromAction:@"getservicekeys"]]; | |
| 113 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:1] retain]; | |
| 114 | [conn setPayload:requestData]; | |
| 115 | [conn performLookup:TRUE]; | |
| 116 | [requestData release]; | |
| 117 | } | |
| 118 | ||
| 119 | - (void)updateServicesWithJson:(NSDictionary *)parsedJson { | |
| 120 | if (!parsedJson) return; | |
| 121 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { | |
| 122 | NSArray *serviceKeys = (NSArray *)[parsedJson valueForKey:@"serviceKeys"]; | |
| 123 | NSDictionary *aKey; | |
| [795] | 124 | [self willChangeValueForKey:@"services"]; |
| [794] | 125 | [_serviceKeys removeAllObjects]; |
| 126 | for (aKey in serviceKeys) { | |
| 127 | [_serviceKeys setObject:(NSString *)[aKey objectForKey:@"label"] | |
| 128 | forKey:(NSString *)[aKey objectForKey:@"key"]]; | |
| 129 | } | |
| 130 | [self didChangeValueForKey:@"services"]; | |
| [795] | 131 | [self willChangeValueForKey:@"timestamp"]; |
| [794] | 132 | [_timestamp release]; |
| 133 | _timestamp = [[NSDate date] retain]; | |
| 134 | [self didChangeValueForKey:@"timestamp"]; | |
| 135 | } | |
| 136 | } | |
| 137 | ||
| 138 | - (void)getLocations { | |
| 139 | // successResult = { | |
| 140 | // success: true, | |
| 141 | // locations: ["x-home", "Home", "x-work", "Work", "x-main", | |
| 142 | // "Main Contact"], | |
| 143 | // actionMessages: "request successful", | |
| 144 | // "2nd message here", | |
| 145 | // "3rd message here"] | |
| 146 | // }; | |
| 147 | ||
| 148 | Record *conn = [[[Record alloc] init] autorelease]; | |
| 149 | [conn setTheDelegate:self]; | |
| 150 | [conn setActionSel:@selector(updateLocationsWithJson:)]; | |
| 151 | [conn setConnectionUrl:[conn urlFromAction:@"getlocations"]]; | |
| 152 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:1] retain]; | |
| 153 | [conn setPayload:requestData]; | |
| 154 | [conn performLookup:TRUE]; | |
| 155 | [requestData release]; | |
| 156 | } | |
| 157 | ||
| 158 | - (void)updateLocationsWithJson:(NSDictionary *)parsedJson { | |
| 159 | if (!parsedJson) return; | |
| 160 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { | |
| 161 | NSArray *liArray = (NSArray *)[parsedJson valueForKey:@"locations"]; | |
| 162 | NSUInteger i, count = [liArray count]; | |
| [795] | 163 | [self willChangeValueForKey:@"services"]; |
| [794] | 164 | [_locationKeys removeAllObjects]; |
| 165 | for (i = 0; i < count; i = i + 2) { | |
| 166 | [_locationKeys setObject:(NSString *)[liArray objectAtIndex:(i+1)] | |
| 167 | forKey:(NSString *)[liArray objectAtIndex:i]]; | |
| 168 | } | |
| 169 | [self didChangeValueForKey:@"locations"]; | |
| [795] | 170 | [self willChangeValueForKey:@"timestamp"]; |
| [794] | 171 | [_timestamp release]; |
| 172 | _timestamp = [[NSDate date] retain]; | |
| 173 | [self didChangeValueForKey:@"timestamp"]; | |
| 174 | } | |
| 175 | } | |
| 176 | ||
| 177 | - (void)getKeywords { | |
| 178 | // successResult = { | |
| 179 | // success: true, | |
| 180 | // actionMessages: ["valid keywords retrieved", | |
| 181 | // "2nd message here"], | |
| 182 | // validKeywordList: [ {shortForm: "pa", displayText: "Postal address", | |
| 183 | // canHaveSecondaries: true, canBeSecondary: false}, | |
| 184 | // {shortForm: "a1", displayText: "Address line 1", | |
| 185 | // canHaveSecondaries: false, canBeSecondary: true} | |
| 186 | // ] | |
| 187 | // }; | |
| 188 | ||
| 189 | Keyword *conn = [[[Keyword alloc] init] autorelease]; | |
| 190 | [conn setTheDelegate:self]; | |
| 191 | [conn setActionSel:@selector(updateKeywordsWithJson:)]; | |
| 192 | [conn setConnectionUrl:[conn urlFromAction:@"getvalidkeywords"]]; | |
| 193 | NSMutableDictionary *requestData = [[NSMutableDictionary dictionaryWithCapacity:1] retain]; | |
| 194 | [conn setPayload:requestData]; | |
| 195 | [conn performLookup:TRUE]; | |
| 196 | [requestData release]; | |
| 197 | } | |
| 198 | ||
| 199 | - (void)updateKeywordsWithJson:(NSDictionary *)parsedJson { | |
| 200 | if (!parsedJson) return; | |
| 201 | if ([[parsedJson valueForKey:@"success"] integerValue] == 1) { | |
| 202 | NSArray *keyTypeList = (NSArray *)[parsedJson valueForKey:@"validKeywordList"]; | |
| 203 | NSDictionary *aKeyType; | |
| [795] | 204 | [self willChangeValueForKey:@"keywords"]; |
| [794] | 205 | [_keywordKeys removeAllObjects]; |
| 206 | for (aKeyType in keyTypeList) { | |
| 207 | [_keywordKeys setObject:(NSString *)[aKeyType objectForKey:@"displayText"] | |
| 208 | forKey:(NSString *)[aKeyType objectForKey:@"shortForm"]]; | |
| 209 | } | |
| 210 | [self didChangeValueForKey:@"keywords"]; | |
| [795] | 211 | [self willChangeValueForKey:@"timestamp"]; |
| [794] | 212 | [_timestamp release]; |
| 213 | _timestamp = [[NSDate date] retain]; | |
| 214 | [self didChangeValueForKey:@"timestamp"]; | |
| 215 | } | |
| 216 | } | |
| 217 | ||
| 218 | #pragma mark - | |
| 219 | #pragma mark singleton object methods | |
| 220 | ||
| 221 | // See "Creating a Singleton Instance" in the Cocoa Fundamentals Guide for more info | |
| 222 | ||
| 223 | + (CurrentAccount *)sharedInstance { | |
| 224 | @synchronized(self) { | |
| 225 | if (currentAccount == nil) { | |
| 226 | [[self alloc] init]; // assignment not done here | |
| 227 | } | |
| 228 | } | |
| 229 | return currentAccount; | |
| 230 | } | |
| 231 | ||
| 232 | + (id)allocWithZone:(NSZone *)zone { | |
| 233 | @synchronized(self) { | |
| 234 | if (currentAccount == nil) { | |
| 235 | currentAccount = [super allocWithZone:zone]; | |
| 236 | return currentAccount; // assignment and return on first allocation | |
| 237 | } | |
| 238 | } | |
| 239 | return nil; // on subsequent allocation attempts return nil | |
| 240 | } | |
| 241 | ||
| 242 | - (id)copyWithZone:(NSZone *)zone | |
| 243 | { | |
| 244 | return self; | |
| 245 | } | |
| 246 | ||
| 247 | - (id)retain { | |
| 248 | return self; | |
| 249 | } | |
| 250 | ||
| 251 | - (unsigned)retainCount { | |
| 252 | return UINT_MAX; // denotes an object that cannot be released | |
| 253 | } | |
| 254 | ||
| 255 | - (void)release { | |
| 256 | //do nothing | |
| 257 | } | |
| 258 | ||
| 259 | - (id)autorelease { | |
| 260 | return self; | |
| 261 | } | |
| 262 | ||
| 263 | @end |
Note: See TracBrowser
for help on using the browser.








