Skip to content

Commit bf9fc87

Browse files
committed
LocationSearchFragment: update weather source/query after we get full location data
* HERE Maps needs an additional step to get full location data * Update location model after we get this data
1 parent c45109b commit bf9fc87

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

app/src/main/java/com/thewizrd/simpleweather/fragments/LocationSearchFragment.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -172,30 +172,12 @@ public LocationData call() throws CustomException, InterruptedException, Weather
172172
throw new CustomException(R.string.error_retrieve_location);
173173
}
174174

175-
final boolean isUS = LocationUtils.isUS(queryResult.getLocationCountry());
176-
177-
if (!Settings.isWeatherLoaded()) {
178-
// Default US provider to NWS
179-
if (isUS) {
180-
Settings.setAPI(WeatherAPI.NWS);
181-
queryResult.updateWeatherSource(WeatherAPI.NWS);
182-
} else {
183-
Settings.setAPI(WeatherAPI.HERE);
184-
queryResult.updateWeatherSource(WeatherAPI.HERE);
185-
}
186-
wm.updateAPI();
187-
}
188-
189175
if (Settings.usePersonalKey() && StringUtils.isNullOrWhitespace(Settings.getAPIKEY()) && wm.isKeyRequired()) {
190176
throw new CustomException(R.string.werror_invalidkey);
191177
}
192178

193179
TaskUtils.throwIfCancellationRequested(token);
194180

195-
if (WeatherAPI.NWS.equals(Settings.getAPI()) && !LocationUtils.isUS(queryResult.getLocationCountry())) {
196-
throw new CustomException(R.string.error_message_weather_us_only);
197-
}
198-
199181
// Need to get FULL location data for HERE API
200182
// Data provided is incomplete
201183
if (WeatherAPI.HERE.equals(queryResult.getLocationSource())
@@ -214,6 +196,24 @@ public LocationQueryViewModel call() throws WeatherException {
214196
throw new InterruptedException();
215197
}
216198

199+
final boolean isUS = LocationUtils.isUS(queryResult.getLocationCountry());
200+
201+
if (!Settings.isWeatherLoaded()) {
202+
// Default US provider to NWS
203+
if (isUS) {
204+
Settings.setAPI(WeatherAPI.NWS);
205+
queryResult.updateWeatherSource(WeatherAPI.NWS);
206+
} else {
207+
Settings.setAPI(WeatherAPI.HERE);
208+
queryResult.updateWeatherSource(WeatherAPI.HERE);
209+
}
210+
wm.updateAPI();
211+
}
212+
213+
if (WeatherAPI.NWS.equals(Settings.getAPI()) && !isUS) {
214+
throw new CustomException(R.string.error_message_weather_us_only);
215+
}
216+
217217
// Check if location already exists
218218
List<LocationData> locData = Settings.getLocationData();
219219
final LocationQueryViewModel finalQueryResult = queryResult;

wearapp/src/main/java/com/thewizrd/simpleweather/fragments/LocationSearchFragment.java

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -143,30 +143,12 @@ public LocationData call() throws CustomException, WeatherException, Interrupted
143143
throw new CustomException(R.string.error_retrieve_location);
144144
}
145145

146-
final boolean isUS = LocationUtils.isUS(queryResult.getLocationCountry());
147-
148-
if (!Settings.isWeatherLoaded()) {
149-
// Default US provider to NWS
150-
if (isUS) {
151-
Settings.setAPI(WeatherAPI.NWS);
152-
queryResult.updateWeatherSource(WeatherAPI.NWS);
153-
} else {
154-
Settings.setAPI(WeatherAPI.HERE);
155-
queryResult.updateWeatherSource(WeatherAPI.HERE);
156-
}
157-
wm.updateAPI();
158-
}
159-
160146
if (Settings.usePersonalKey() && StringUtils.isNullOrWhitespace(Settings.getAPIKEY()) && wm.isKeyRequired()) {
161147
throw new WeatherException(WeatherUtils.ErrorStatus.INVALIDAPIKEY);
162148
}
163149

164150
TaskUtils.throwIfCancellationRequested(token);
165151

166-
if (WeatherAPI.NWS.equals(Settings.getAPI()) && !isUS) {
167-
throw new CustomException(R.string.error_message_weather_us_only);
168-
}
169-
170152
// Need to get FULL location data for HERE API
171153
// Data provided is incomplete
172154
if (WeatherAPI.HERE.equals(queryResult.getLocationSource())
@@ -185,7 +167,23 @@ public LocationQueryViewModel call() throws WeatherException {
185167
throw new InterruptedException();
186168
}
187169

188-
TaskUtils.throwIfCancellationRequested(token);
170+
final boolean isUS = LocationUtils.isUS(queryResult.getLocationCountry());
171+
172+
if (!Settings.isWeatherLoaded()) {
173+
// Default US provider to NWS
174+
if (isUS) {
175+
Settings.setAPI(WeatherAPI.NWS);
176+
queryResult.updateWeatherSource(WeatherAPI.NWS);
177+
} else {
178+
Settings.setAPI(WeatherAPI.HERE);
179+
queryResult.updateWeatherSource(WeatherAPI.HERE);
180+
}
181+
wm.updateAPI();
182+
}
183+
184+
if (WeatherAPI.NWS.equals(Settings.getAPI()) && !isUS) {
185+
throw new CustomException(R.string.error_message_weather_us_only);
186+
}
189187

190188
// Get weather data
191189
LocationData location = new LocationData(queryResult);

0 commit comments

Comments
 (0)