/
WxDataModel.m
165 lines (139 loc) · 4.65 KB
/
WxDataModel.m
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
//
// WxDataModel.m
// WxHere
//
// Created by Ayal Spitz on 7/31/09.
// Copyright (C) 2009 Ayal Spitz
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
#import "WxDataModel.h"
#import "GPSDataModel.h"
#import "LocationDataModel.h"
#import "CurrentConditionsDataModel.h"
#import "NOAADataModel.h"
#import "HourlyForecastDataModel.h"
@implementation WxDataModel
@synthesize gpsDataModel;
@synthesize locationDataModel;
@synthesize currentConditionsDataModel;
@synthesize noaaDataModel;
//@synthesize hourlyForecastDataModel;
#pragma mark Singelton
static WxDataModel *wxDataModel = nil;
+ (WxDataModel *)dataModel{
@synchronized(self) {
if (wxDataModel == nil) {
wxDataModel = [[self alloc] init]; // assignment not done here
}
}
return wxDataModel;
}
#pragma mark -
- (id) init{
self = [super init];
if (self != nil) {
gpsDataModel = [[GPSDataModel alloc]init];
locationDataModel = [[LocationDataModel alloc]init];
currentConditionsDataModel = [[CurrentConditionsDataModel alloc]init];
noaaDataModel = [[NOAADataModel alloc]init];
//hourlyForecastDataModel = [[HourlyForecastDataModel alloc]init];
done = 0;
[self addObservers];
}
return self;
}
- (void) dealloc{
[gpsDataModel release];
[locationDataModel release];
[currentConditionsDataModel release];
[noaaDataModel release];
//[hourlyForecastDataModel release];
[super dealloc];
}
#pragma mark -
#pragma mark Notification handeling
- (void)addObservers{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleErrorNotification:)
name:@"error"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleErrorNotification:)
name:@"exception"
object:nil];
[self.gpsDataModel addObserver:self forKeyPath:@"state" options:0 context:NULL];
[self.locationDataModel addObserver:self forKeyPath:@"state" options:0 context:NULL];
[self.currentConditionsDataModel addObserver:self forKeyPath:@"state" options:0 context:NULL];
[self.noaaDataModel addObserver:self forKeyPath:@"state" options:0 context:NULL];
//[self.hourlyForecastDataModel addObserver:self forKeyPath:@"state" options:0 context:NULL];
}
- (void)handleErrorNotification:(NSNotification *)notification{
self.state = Error;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([keyPath isEqual:@"state"]){
if ([object state] == Error){
// If the state of one of the sub data models returns an error then
// this data model is in error state too
self.state = Error;
} else {
if ([object class] == [GPSDataModel class]){
if (self.gpsDataModel.state == Updated){
[self.locationDataModel update:gpsDataModel];
[self.currentConditionsDataModel update:gpsDataModel];
[self.noaaDataModel update:gpsDataModel];
//[self.hourlyForecastDataModel update:gpsDataModel];
}
} else if ([object class] == [LocationDataModel class]){
if (self.locationDataModel.state == Updated){
done += 1;
}
} else if ([object class] == [CurrentConditionsDataModel class]){
if (self.currentConditionsDataModel.state == Updated){
done += 2;
}
} else if ([object class] == [NOAADataModel class]){
if (self.noaaDataModel.state == Updated){
done += 4;
}
} else if ([object class] == [NOAADataModel class]){
if (self.noaaDataModel.state == Updated){
done += 8;
}
}
if (done == 7){
self.state = Updated;
self.elapsedTime = [NSNumber numberWithDouble:([self.startTime timeIntervalSinceNow] * -1.0)];
}
}
}
}
#pragma mark -
- (void)update:(id)dataModel{
[super update:dataModel];
[self.gpsDataModel update:nil];
}
- (void)refresh{
[super refresh];
done = 0;
[gpsDataModel refresh];
[locationDataModel refresh];
[currentConditionsDataModel refresh];
[noaaDataModel refresh];
//[hourlyForecastDataModel refresh];
[self update:nil];
}
@end