-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenuWeatherController.m
More file actions
313 lines (278 loc) · 11.8 KB
/
MainMenuWeatherController.m
File metadata and controls
313 lines (278 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//
// MainMenuWeatherController.m
// MMWeather
//
// Created by Thomas Cool on 10/22/10.
// Copyright 2010 tomcool.org. All rights reserved.
//
#import "MainMenuWeatherController.h"
#import "MainMenuWeatherLocationController.h"
#import "MainMenuWeather.h"
#import "APXML/APXML.h"
#define WeatherFile [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences/com.nito.nitoTV.weather.plist"]
#define YAHOO_API @"dj0yJmk9ZXZQb3hzT3hkUUI5JmQ9WVdrOVRrb3dUV1oyTkdrbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD01Zg--"
#define NEWWeatherFile [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences/org.tomcool.mainmenu.weather.plist"]
#define OLDWeatherFile [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences/com.nito.nitoTV.weather.plist"]
#define plitFile [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences/org.tomcool.weather.plist"]
#define WDomain (CFStringRef)@"Library/Preferences/org.tomcool.weather.plist"
static NSString * const kEnabledBool = @"enabled";
static NSString * const kMainMenuLocation = @"mainmenuweather";
@implementation MainMenuWeatherController
+(SMFPreferences *)preferences {
static SMFPreferences *_weatherPreferences = nil;
if(!_weatherPreferences)
{
_weatherPreferences = [[SMFPreferences alloc] initWithPersistentDomainName:@"org.tomcool.MainMenuWeather.New"];
[_weatherPreferences registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
@"2400737",kMainMenuLocation,
[NSNumber numberWithBool:YES],kEnabledBool,
nil]];
}
return _weatherPreferences;
}
+(BOOL)enabled
{
CFPreferencesSynchronize(WDomain, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
BOOL r = (BOOL)CFPreferencesGetAppBooleanValue((CFStringRef)@"enabled", WDomain, nil);
}
+(void)setEnabled:(BOOL)e
{
CFPreferencesSetValue((CFStringRef)@"testVal", [NSNumber numberWithBool:e], WDomain, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
CFPreferencesSynchronize(WDomain, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
}
- (id)previewControlForItem:(long)row
{
NSString * f = [[NSBundle bundleForClass:[self class]] pathForResource:@"yahoo_logo" ofType:@"png"];
BRImage * image = [BRImage imageWithPath:f];
BRImageAndSyncingPreviewController *preview = [[BRImageAndSyncingPreviewController alloc] init];
[preview setImage:image];
return [preview autorelease];
}
-(void)convert
{
if (![[NSFileManager defaultManager] fileExistsAtPath:NEWWeatherFile] &&
[[NSFileManager defaultManager] fileExistsAtPath:OLDWeatherFile])
{
NSDictionary * dict = [NSDictionary dictionaryWithContentsOfFile:OLDWeatherFile];
NSArray *keys = [dict allKeys];
NSMutableDictionary *locations = [[NSMutableDictionary alloc]init];
for(NSString *key in keys)
{
[locations setObject:[dict objectForKey:key] forKey:[[dict objectForKey:key] objectForKey:@"location"]];
}
[locations writeToFile:NEWWeatherFile atomically:YES];
}
}
-(void)reloadLocations
{
[[self list] removeDividers];
[_locations removeAllObjects];
[_management removeAllObjects];
if ([[NSFileManager defaultManager] fileExistsAtPath:NEWWeatherFile])
{
NSDictionary * dict = [NSDictionary dictionaryWithContentsOfFile:NEWWeatherFile];
for(NSString *key in [dict allKeys])
{
[_locations addObject:[dict objectForKey:key]];
}
}
else
_locations = [[NSMutableArray alloc] init];
BRMenuItem *item = [[BRMenuItem alloc] init];
[item setText:@"Add by WOEID" withAttributes:[[BRThemeInfo sharedTheme] menuItemTextAttributes]];
[_management addObject:item];
[item release];
item = [[BRMenuItem alloc] init];
[item setText:@"Search WOEID" withAttributes:[[BRThemeInfo sharedTheme] menuItemTextAttributes]];
[_management addObject:item];
[item release];
item = [SMFMenuItem menuItem];
[item setTitle:@"Select Display Color"];
[_management addObject:item];
[[self list] addDividerAtIndex:[_locations count] withLabel:@"Management"];
}
-(id)init
{
self=[super init];
if (self==nil)
return nil;
[self setListTitle:@"Weather"];
[[MainMenuWeatherController preferences] setObject:@"Hello" forKey:@"Location"];
// if (![[NSFileManager defaultManager] fileExistsAtPath:NEWWeatherFile] &&
// [[NSFileManager defaultManager] fileExistsAtPath:OLDWeatherFile])
// {
// [self convert];
// }
[[self list] setDatasource:self];
_management = [[NSMutableArray alloc]init];
_locations = [[NSMutableArray alloc]init];
searchType=0;
[self reloadLocations];
return self;
}
- (float)heightForRow:(long)row { return 0.0f;}
- (BOOL)rowSelectable:(long)row { return YES;}
- (long)itemCount { return ((long)[_locations count] + (long)[_management count]);}
-(void)leftActionForRow:(long)row
{
}
-(void)rightActionForRow:(long)row
{
}
- (id)itemForRow:(long)row
{
if (row<[_locations count])
{
BRMenuItem *menuItem = [[BRMenuItem alloc] init];
NSDictionary *cityInfo = [_locations objectAtIndex:row];
NSString *city = [cityInfo objectForKey:@"name"];
NSString *units = [cityInfo objectForKey:@"units"];
[menuItem addAccessoryOfType:1];
[menuItem setText:(city?city:@"No Name")
withAttributes:[[BRThemeInfo sharedTheme] menuItemTextAttributes]];
[menuItem setRightJustifiedText:(units?units:@"c")
withAttributes:[[BRThemeInfo sharedTheme] menuItemSmallTextAttributes]];
return [menuItem autorelease];
}
else
{
row=row-[_locations count];
return [_management objectAtIndex:row];
}
return nil;
}
- (id)titleForRow:(long)row
{
if (row<[_locations count])
{
NSDictionary *cityInfo = [_locations objectAtIndex:row];
NSString *city = [cityInfo objectForKey:@"name"];
return (city?city:@"No Name");
}
else if(row<([_locations count]+[_management count]))
return [[_management objectAtIndex:(row-[_locations count])] text];
// else if(row == [_locations count])
// {
// return @"Add by WOEID";
// }
// else if(row==([_locations count]+1)){
// return @"Search WOEID";
// }
// else if(row==([_locations count]+2))
// {
// return @"Enabled";
// }
// else if(row==([_locations count]+3))
// {
// return @"Hello";
// }
return nil;
}
-(void)itemSelected:(long)selected
{
if (selected<[_locations count])
{
NSString *code = [[_locations objectAtIndex:selected] objectForKey:@"location"];
MainMenuWeatherLocationController *a = [[MainMenuWeatherLocationController alloc]initWithLocationCode:code];
[[self stack]pushController:[a autorelease]];
}
if (selected == [_locations count]) {
BRTextEntryController *controller = [[BRTextEntryController alloc] init];
[controller setTitle:@"Enter Yahoo Weather Location Code"];
[controller setTextEntryTextFieldLabel:@"ID"];
[controller setListIcon:[[BRThemeInfo sharedTheme]appleTVIcon]];
[controller setTextFieldDelegate:self];
//[controller setTextEntryStyle:2];
searchType=1;
//[controller setInitialTextEntryText:rightString];
[[self stack] pushController: controller];
}
if (selected == ([_locations count]+1)) {
BRTextEntryController *controller = [[BRTextEntryController alloc] init];
[controller setTitle:@"Input search string to use"];
[controller setTextEntryTextFieldLabel:@"Search"];
//[controller setListIcon:[[BRThemeInfo sharedTheme]appleTVIcon]];
[controller setTextFieldDelegate:self];
//[controller setTextEntryStyle:2];
searchType=2;
//[controller setInitialTextEntryText:rightString];
[[self stack] pushController: controller];
}
}
- (void) textDidChange: (id) sender
{
// do nothing for now
}
- (void) textDidEndEditing: (id) sender
{
if (searchType==1)
{
[self addLocation:[sender stringValue]];
[[self stack] popController];
}
else if(searchType==2)
{
NSString *url = [NSString stringWithFormat:@"http://where.yahooapis.com/v1/places.q('%@');start=0;count=5?appid=%@",[[sender stringValue] stringByReplacingOccurrencesOfString:@" " withString:@"\%20"],YAHOO_API,nil];
NSStringEncoding s;
NSString *str = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] usedEncoding:&s error:nil];
SMFDocument *doc = [SMFDocument documentWithXMLString:str];
SMFElement *elt = [doc rootElement];
NSArray *val2=[elt childrenNamed:@"place"];
NSMutableArray *cities = [[NSMutableArray alloc] init];
NSMutableArray *invocations=[[NSMutableArray alloc]init];
for (SMFElement *el in val2)
{
NSString *v = [el firstChildElementNamed:@"name"].value;
NSString *c = [el firstChildElementNamed:@"country"].value;
NSString *st = [el firstChildElementNamed:@"admin1"].value;
NSString *w = [el firstChildElementNamed:@"woeid"].value;
NSInvocation *invocation =[SMFInvocationCenteredMenuController invocationsForObject:self
withSelectorVal:@"addLocation:"
withArguments:[NSArray arrayWithObject:w]];
[cities addObject:[NSString stringWithFormat:@"%@, %@ %@",v,c,(st?st:@""),nil]];
[invocations addObject:invocation];
}
SMFInvocationCenteredMenuController *ctrl = [[SMFInvocationCenteredMenuController alloc] initWithTitles:cities
withInvocations:invocations
withTitle:@"Select Location"
withDescription:@"Please select the appropriate city"];
[cities release];
[invocations release];
[[self stack]swapController:ctrl];
}
}
-(void)addLocation:(NSString *)cc
{
NSDictionary *d = [MainMenuWeatherControl loadDictionaryForCode:cc usUnits:NO];
if (d!=nil) {
NSString *city = [d objectForKey:@"city"];
// NSString *code = [d objectForKey:@"code"];
NSString *units= @"c";
if (units && cc && city) {
NSString *filter = [city stringByReplacingOccurrencesOfString:@" " withString:@"_"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@",filter,nil];
NSArray *filteredArray = [[NSTimeZone knownTimeZoneNames] filteredArrayUsingPredicate:predicate];
NSString *timeZone=@"America/Chicago";
if ([filteredArray count]==1) {
timeZone=[filteredArray objectAtIndex:0];
}
NSDictionary *dd = [NSDictionary dictionaryWithObjectsAndKeys:
city,@"name",
cc,@"location",
units,@"units",
timeZone,@"timeZone",nil];
NSMutableDictionary *ddd = [NSMutableDictionary dictionaryWithContentsOfFile:NEWWeatherFile];
if (ddd==nil) {
ddd=[NSMutableDictionary dictionary];
}
[ddd setObject:dd forKey: cc];
[ddd writeToFile:NEWWeatherFile atomically:YES];
}
}
}
-(void)wasExhumed
{
[self reloadLocations];
[super wasExhumed];
}
@end