From 574ce6afd73265dc0d0fc9575ef15323acbf0612 Mon Sep 17 00:00:00 2001 From: rain_home Date: Wed, 4 May 2016 19:50:27 +0900 Subject: [PATCH] fix. wind speed bug fix. wind speed bug add. dynamically change api --- SmileWeather/Classes/SmileWeatherData.m | 35 ++++----- SmileWeather/Classes/SmileWeatherDemoVC.h | 2 +- SmileWeather/Classes/SmileWeatherDemoVC.m | 5 ++ SmileWeather/Classes/SmileWeatherDownLoader.h | 5 +- SmileWeather/Classes/SmileWeatherDownLoader.m | 71 ++++++++++++------- SmileWeather/Classes/SmileWeatherOneDayData.h | 5 +- SmileWeather/Classes/SmileWeatherOneDayData.m | 13 +++- 7 files changed, 85 insertions(+), 51 deletions(-) diff --git a/SmileWeather/Classes/SmileWeatherData.m b/SmileWeather/Classes/SmileWeatherData.m index 64cc3fb..a166879 100644 --- a/SmileWeather/Classes/SmileWeatherData.m +++ b/SmileWeather/Classes/SmileWeatherData.m @@ -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]; @@ -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]; @@ -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 @@ -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"]]; @@ -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"]]; @@ -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"]]; @@ -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); @@ -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; } diff --git a/SmileWeather/Classes/SmileWeatherDemoVC.h b/SmileWeather/Classes/SmileWeatherDemoVC.h index 07a2604..f3dd9de 100644 --- a/SmileWeather/Classes/SmileWeatherDemoVC.h +++ b/SmileWeather/Classes/SmileWeatherDemoVC.h @@ -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) iddelegate; +@property (weak, nonatomic, nullable) iddelegate; //demo vc /*!Create a demo view for show the ability of the SmileWeather.*/ +(nonnull SmileWeatherDemoVC*)DemoVCToView:(nonnull UIView*)parentView; diff --git a/SmileWeather/Classes/SmileWeatherDemoVC.m b/SmileWeather/Classes/SmileWeatherDemoVC.m index ec80f44..a0d994a 100644 --- a/SmileWeather/Classes/SmileWeatherDemoVC.m +++ b/SmileWeather/Classes/SmileWeatherDemoVC.m @@ -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; @@ -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]; }); } } diff --git a/SmileWeather/Classes/SmileWeatherDownLoader.h b/SmileWeather/Classes/SmileWeatherDownLoader.h index f7cf802..8736cec 100644 --- a/SmileWeather/Classes/SmileWeatherDownLoader.h +++ b/SmileWeather/Classes/SmileWeatherDownLoader.h @@ -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; diff --git a/SmileWeather/Classes/SmileWeatherDownLoader.m b/SmileWeather/Classes/SmileWeatherDownLoader.m index d4aa6aa..e5a4bda 100644 --- a/SmileWeather/Classes/SmileWeatherDownLoader.m +++ b/SmileWeather/Classes/SmileWeatherDownLoader.m @@ -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"); @@ -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]]) { @@ -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]; @@ -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]; @@ -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) { diff --git a/SmileWeather/Classes/SmileWeatherOneDayData.h b/SmileWeather/Classes/SmileWeatherOneDayData.h index 1a00527..75c921f 100644 --- a/SmileWeather/Classes/SmileWeatherOneDayData.h +++ b/SmileWeather/Classes/SmileWeatherOneDayData.h @@ -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; @@ -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; diff --git a/SmileWeather/Classes/SmileWeatherOneDayData.m b/SmileWeather/Classes/SmileWeatherOneDayData.m index 977e1cc..71c7124 100644 --- a/SmileWeather/Classes/SmileWeatherOneDayData.m +++ b/SmileWeather/Classes/SmileWeatherOneDayData.m @@ -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; @@ -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