Skip to content

Commit

Permalink
fix. wind speed bug
Browse files Browse the repository at this point in the history
fix.  wind speed bug
add. dynamically change api
  • Loading branch information
liu044100 committed May 4, 2016
1 parent 99be88b commit 574ce6a
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 51 deletions.
35 changes: 14 additions & 21 deletions SmileWeather/Classes/SmileWeatherData.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ -(NSArray*)createOneDayDataForForecast:(NSArray*)forecastData{
forecast.humidity = [self createHumidityStringFromObject:[forecastday valueForKey:@"avehumidity"]];;

//wind speed
forecast.windSpeed = [self createWindSpeedStringFromObject:[[forecastday valueForKey:@"avewind"] valueForKey:@"kph"]];
forecast.windSpeedRaw = [self createWindSpeedRawFromObject:[[forecastday valueForKey:@"avewind"] valueForKey:@"kph"]];
forecast.windDirection = [[forecastday valueForKey:@"avewind"] valueForKey:@"dir"];

[results addObject:forecast];
Expand Down Expand Up @@ -176,7 +176,7 @@ -(NSArray*)createOneDayDataForHourly:(NSArray*)hourlyData{
forecast.humidity = [NSString stringWithFormat:@"%@%%", [hourlyData valueForKey:@"humidity"]];

//wind
forecast.windSpeed = [self createWindSpeedStringFromObject:[[hourlyData valueForKey:@"wspd"] valueForKey:@"metric"]];
forecast.windSpeedRaw = [self createWindSpeedRawFromObject:[[hourlyData valueForKey:@"wspd"] valueForKey:@"metric"]];
forecast.windDirection = [[hourlyData valueForKey:@"wdir"] valueForKey:@"dir"];

[results addObject:forecast];
Expand Down Expand Up @@ -233,7 +233,7 @@ -(void)configureJSON_wunderground:(NSDictionary*)jsonData{
self.currentData.humidity = [self createHumidityStringFromObject:[currentObservation valueForKey:@"relative_humidity"]];

//wind speed
self.currentData.windSpeed = [self createWindSpeedStringFromObject:[currentObservation valueForKey:@"wind_kph"]];
self.currentData.windSpeedRaw = [self createWindSpeedRawFromObject:[currentObservation valueForKey:@"wind_kph"]];
self.currentData.windDirection = [currentObservation valueForKey:@"wind_dir"];

//today only property
Expand Down Expand Up @@ -282,7 +282,7 @@ -(void)configureJSON_openweathermap:(NSDictionary*)jsonData{
self.currentData.humidity = [self createHumidityStringFromObject:[mainDataDic valueForKey:@"humidity"]];

//wind speed
self.currentData.windSpeed = [self createWindSpeedStringFromObject:[windDic valueForKey:@"speed"]];
self.currentData.windSpeedRaw = [self createWindSpeedRawFromObject:[windDic valueForKey:@"speed"]];

//today only property
self.currentData.pressureRaw = [self createPressureStringFromObject:[mainDataDic valueForKey:@"pressure"]];
Expand Down Expand Up @@ -339,7 +339,7 @@ -(void)configureForecastDaysAndHourly_openweathermap:(NSArray*)object{
forecast.humidity = [self createHumidityStringFromObject:[mainDataDic valueForKey:@"humidity"]];

//wind speed
forecast.windSpeed = [self createWindSpeedStringFromObject:[windDic valueForKey:@"speed"]];
forecast.windSpeedRaw = [self createWindSpeedRawFromObject:[windDic valueForKey:@"speed"]];

//precipitation
forecast.precipitationRaw = [self createAmountOfRainFromObject_openweathermap:[obj objectForKey:@"rain"]];
Expand Down Expand Up @@ -371,7 +371,7 @@ -(void)configureForecastDaysAndHourly_openweathermap:(NSArray*)object{
forecast.humidity = [self createHumidityStringFromObject:[mainDataDic valueForKey:@"humidity"]];

//wind speed
forecast.windSpeed = [self createWindSpeedStringFromObject:[windDic valueForKey:@"speed"]];
forecast.windSpeedRaw = [self createWindSpeedRawFromObject:[windDic valueForKey:@"speed"]];

//precipitation
forecast.precipitationRaw = [self createAmountOfRainFromObject_openweathermap:[obj objectForKey:@"rain"]];
Expand Down Expand Up @@ -429,7 +429,7 @@ -(NSString*)createSunStringFromObject_openweathermap:(id)object{


-(SmileTemperature)createTemperatureFromObject_openweathermap:(id)object{
SmileTemperature result;
SmileTemperature result = SmileTemperatureMake(0, 0, false);
if ([object isKindOfClass:[NSNumber class]]) {
NSNumber *tempNum = (NSNumber*)object;
result = SmileTemperatureMake([SmileWeatherData tempToFahrenheit:tempNum], [SmileWeatherData tempToCelcius:tempNum], YES);
Expand Down Expand Up @@ -574,26 +574,19 @@ -(NSString*)createPressureStringFromObject:(id)object {
return result;
}

-(NSString*)createWindSpeedStringFromObject:(id)object {
NSString *result;

if (self.weatherAPI == API_wunderground) {
if ([object isKindOfClass:[NSNumber class]]) {
-(NSString*)createWindSpeedRawFromObject:(id)object {
NSString *result = @"";
if (self.weatherAPI == API_openweathermap) {
if ([object isKindOfClass:[NSNumber class]] || [object isKindOfClass:[NSString class]]) {
NSNumber *value = (NSNumber*)object;
result = [NSString stringWithFormat:@"%.0f M/S", value.floatValue];
} else {
result = @"-- M/S";
result = [NSString stringWithFormat:@"%.0f", value.floatValue];
}

} else if (self.weatherAPI == API_openweathermap){
} else if (self.weatherAPI == API_wunderground){
if ([object isKindOfClass:[NSNumber class]] || [object isKindOfClass:[NSString class]]) {
CGFloat value = [object floatValue]* 1000 / (60 * 60);
result = [NSString stringWithFormat:@"%.0f M/S", value];
} else {
result = @"-- M/S";
result = [NSString stringWithFormat:@"%.0f", value];
}
}

return result;
}

Expand Down
2 changes: 1 addition & 1 deletion SmileWeather/Classes/SmileWeatherDemoVC.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@property (nonatomic, getter= isFahrenheit) BOOL fahrenheit;
/*!Night mode will change background color to black color.*/
@property (nonatomic) BOOL nightMode;
@property (weak, nonatomic) id<SmileDemoChangeTempUnitsDelegate>delegate;
@property (weak, nonatomic, nullable) id<SmileDemoChangeTempUnitsDelegate>delegate;
//demo vc
/*!Create a demo view for show the ability of the SmileWeather.*/
+(nonnull SmileWeatherDemoVC*)DemoVCToView:(nonnull UIView*)parentView;
Expand Down
5 changes: 5 additions & 0 deletions SmileWeather/Classes/SmileWeatherDemoVC.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ -(void)viewDidLoad {
self.activityView.backgroundColor = [UIColor redColor];
self.activityView.layer.cornerRadius = CGRectGetMidX(self.activityView.bounds);

[self handleAPILogo];
}

-(void)handleAPILogo {
if ([SmileWeatherDownLoader sharedDownloader].weatherAPI == API_wunderground) {
self.logo_openweather.hidden = YES;
self.logo_wunderground.hidden = NO;
Expand Down Expand Up @@ -228,6 +232,7 @@ -(void)setData:(SmileWeatherData *)data{
[self.collectionView_hourly scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
}
[self updateUI];
[self handleAPILogo];
});
}
}
Expand Down
5 changes: 4 additions & 1 deletion SmileWeather/Classes/SmileWeatherDownLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ typedef NS_ENUM(int, SmileWeatherAPI) {
/*!The weather api used for your project, please set it in your info.plist in advance.*/
@property (nonatomic, readonly) SmileWeatherAPI weatherAPI;

/*!Dynamically change api.*/
-(void)changeAPI:(SmileWeatherAPI)newAPI;

+(nonnull SmileWeatherDownLoader*)sharedDownloader;

//raw data
//Raw data
-(void)getWeatherRawDataFromURL:(nonnull NSURL*)url completion:(nonnull SmileWeatherRawDataCompletion)completion;

-(void)getWeatherRawDicFromURL:(nonnull NSURL*)url completion:(nonnull SmileWeatherRawDicCompletion)completion;
Expand Down
71 changes: 45 additions & 26 deletions SmileWeather/Classes/SmileWeatherDownLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ @interface SmileWeatherDownLoader()

@implementation SmileWeatherDownLoader

#pragma mark - Info Dic

+(NSDictionary*)smileWeatherInfoDic{
NSDictionary *dic = [[SmileWeatherDownLoader appInfoPlist] objectForKey:INFO_DIC];
NSAssert(dic != nil, @"Please add SmileWeather key to your Info.plist");
Expand All @@ -43,6 +45,18 @@ +(NSDictionary*)appInfoPlist{
return _sharedInstance;
}

#pragma mark - Init

+(SmileWeatherDownLoader*)sharedDownloader {
static SmileWeatherDownLoader *sharedDownloader = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSDictionary *smileInfo = [SmileWeatherDownLoader smileWeatherInfoDic];
sharedDownloader = [[SmileWeatherDownLoader alloc] initFromInfoKey:smileInfo];
});
return sharedDownloader;
}

-(instancetype)initFromInfoKey:(NSDictionary*)smileInfo{
SmileWeatherDownLoader *sharedDownloader;
if ([smileInfo[API_NOW] isKindOfClass:[NSNumber class]]) {
Expand All @@ -56,27 +70,34 @@ -(instancetype)initFromInfoKey:(NSDictionary*)smileInfo{
} else if (!apikey_wunderground && apikey_openweathermap){
sharedDownloader = [[SmileWeatherDownLoader alloc] initWithOpenweathermapAPIKey:apikey_openweathermap];
} else {
NSAssert( apikey_wunderground != nil && apikey_openweathermap !=nil, @"No Wunderground or Openweathermap key to your Info.plist");

NSAssert( apikey_wunderground == nil && apikey_openweathermap ==nil, @"Both of Wunderground and Openweathermap key in your Info.plist, please add API_NOW key to select which one is used.");
NSAssert( apikey_wunderground != nil && apikey_openweathermap !=nil, @"No Wunderground or Openweathermap key in your Info.plist");
NSAssert( apikey_wunderground == nil && apikey_openweathermap ==nil, @"Both of Wunderground and Openweathermap key in your Info.plist, please add API_NOW key that show which api should be used.");
}
}

return sharedDownloader;
}

+(SmileWeatherDownLoader*)sharedDownloader {
static SmileWeatherDownLoader *sharedDownloader = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSDictionary *smileInfo = [SmileWeatherDownLoader smileWeatherInfoDic];
sharedDownloader = [[SmileWeatherDownLoader alloc] initFromInfoKey:smileInfo];
});
return sharedDownloader;
- (instancetype)initWithAPIType:(SmileWeatherAPI)type
{
self.weatherAPI = type;
NSDictionary *smileInfo = [SmileWeatherDownLoader smileWeatherInfoDic];
NSString *apikey;
if (self.weatherAPI == API_wunderground) {
apikey = smileInfo[API_KEY_wunderground];
NSAssert(apikey != nil, @"Please add Wunderground key to your Info.plist");
self = [[SmileWeatherDownLoader alloc] initWithWundergroundAPIKey:apikey];
} else if (self.weatherAPI == API_openweathermap){
apikey = smileInfo[API_KEY_openweathermap];
NSAssert(apikey != nil, @"Please add openweathermap key to your Info.plist");
self = [[SmileWeatherDownLoader alloc] initWithOpenweathermapAPIKey:apikey];
}
return self;
}

- (instancetype)initWithWundergroundAPIKey:(NSString*)apikey{
if(self = [super init]) {
NSAssert( apikey != nil && apikey.length > 0, @"No Wunderground key in your Info.plist");
self.weatherAPI = API_wunderground;
self.key = apikey;
self.geocoder = [[CLGeocoder alloc]init];
Expand All @@ -87,6 +108,7 @@ - (instancetype)initWithWundergroundAPIKey:(NSString*)apikey{

- (instancetype)initWithOpenweathermapAPIKey:(NSString*)apikey{
if(self = [super init]) {
NSAssert( apikey != nil && apikey.length > 0, @"No Openweathermap key in your Info.plist");
self.weatherAPI = API_openweathermap;
self.key = apikey;
self.geocoder = [[CLGeocoder alloc]init];
Expand All @@ -95,27 +117,24 @@ - (instancetype)initWithOpenweathermapAPIKey:(NSString*)apikey{
return self;
}

#pragma mark - Dynamically change api

- (instancetype)initWithAPIType:(SmileWeatherAPI)type
{
if(self = [super init]) {
self.weatherAPI = type;
-(void)changeAPI:(SmileWeatherAPI)newAPI {
if (self.weatherAPI != newAPI) {
self.weatherAPI = newAPI;
NSDictionary *smileInfo = [SmileWeatherDownLoader smileWeatherInfoDic];
NSString *apikey;
if (self.weatherAPI == API_wunderground) {
apikey = smileInfo[API_KEY_wunderground];
NSAssert(apikey != nil, @"Please add Wunderground key to your Info.plist");
self = [[SmileWeatherDownLoader alloc] initWithWundergroundAPIKey:apikey];
} else if (self.weatherAPI == API_openweathermap){
apikey = smileInfo[API_KEY_openweathermap];
NSAssert(apikey != nil, @"Please add openweathermap key to your Info.plist");
self = [[SmileWeatherDownLoader alloc] initWithOpenweathermapAPIKey:apikey];
switch (newAPI) {
case API_openweathermap:
self.key = smileInfo[API_KEY_openweathermap];
case API_wunderground:
self.key = smileInfo[API_KEY_wunderground];
default:
break;
}
}
return self;
}

#pragma mark - download weather data
#pragma mark - Download weather data

-(void)getWeatherRawDicFromURL:(NSURL *)url completion:(SmileWeatherRawDicCompletion)completion{
if (!url | !completion) {
Expand Down
5 changes: 4 additions & 1 deletion SmileWeather/Classes/SmileWeatherOneDayData.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ static inline SmileTemperature SmileTemperatureMake(CGFloat fahrenheit, CGFloat
*/

//pop = 90;
///Openweathermap unit: mm, Wunderground unit: %
@property (copy, nonatomic) NSString *precipitationRaw;
@property (readonly, nonatomic) NSString *precipitation;

Expand All @@ -61,7 +62,9 @@ avewind = {
mph = 15;
};
*/
@property (copy, nonatomic) NSString *windSpeed;
///unit is M/S
@property (copy, nonatomic) NSString *windSpeedRaw;
@property (readonly, nonatomic) NSString *windSpeed;
@property (copy, nonatomic) NSString *windDirection;


Expand Down
13 changes: 12 additions & 1 deletion SmileWeather/Classes/SmileWeatherOneDayData.m
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ - (instancetype)initWithCoder:(NSCoder *)decoder
_condition = [decoder decodeObjectForKey: SmileCoder_condition];
_precipitationRaw = [decoder decodeObjectForKey: SmileCoder_precipitationRaw];
_humidity = [decoder decodeObjectForKey: SmileCoder_humidity];
_windSpeed = [decoder decodeObjectForKey: SmileCoder_windSpeed];
_windSpeedRaw = [decoder decodeObjectForKey: SmileCoder_windSpeed];
_windDirection = [decoder decodeObjectForKey: SmileCoder_windDirection];
}
return self;
Expand All @@ -65,4 +65,15 @@ -(NSString *)precipitation{
return result;
}

-(NSString *)windSpeed {
NSString *unit = @"M/S";
NSString *result;
if (_windSpeedRaw && _windSpeedRaw.length > 0) {
result = [NSString stringWithFormat:@"%@ %@", _windSpeedRaw, unit];
} else {
result = [NSString stringWithFormat:@"-- %@", unit];
}
return result;
}

@end

0 comments on commit 574ce6a

Please sign in to comment.