From e9b4ceed0e4a0cda8e8af8162d1354ca0e02415f Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Mon, 16 Jan 2023 21:03:16 +0100 Subject: [PATCH] Implement fallback with default city when city in the right language cannot be found in the json. --- .../features/weather/ForecastBuilder.java | 10 ++++++- .../features/weather/WeatherUpdater.java | 26 ++++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/foundation/e/blisslauncher/features/weather/ForecastBuilder.java b/app/src/main/java/foundation/e/blisslauncher/features/weather/ForecastBuilder.java index 7b927e29f9..a5108c6496 100644 --- a/app/src/main/java/foundation/e/blisslauncher/features/weather/ForecastBuilder.java +++ b/app/src/main/java/foundation/e/blisslauncher/features/weather/ForecastBuilder.java @@ -73,7 +73,15 @@ public class ForecastBuilder { // City TextView textCity = weatherPanel.findViewById(R.id.weather_city); - textCity.setText(Preferences.getCachedCity(context, w.getCity())); + String city; + + if (Preferences.useCustomWeatherLocation(context)) { + city = w.getCity(); + } else { + city = Preferences.getCachedCity(context, w.getCity()); + } + + textCity.setText(city); // Weather Condition TextView weatherCondition = weatherPanel.findViewById(R.id.weather_condition); diff --git a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java index 9a10a3a2d8..8801e2d81a 100644 --- a/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java +++ b/app/src/main/java/foundation/e/blisslauncher/features/weather/WeatherUpdater.java @@ -176,6 +176,8 @@ public class WeatherUpdater { if (!Preferences.useCustomWeatherLocation(mWeakContext.get())) { reverseGeocodeLocation(getMostRecentLocation()); + } else { + Log.w(TAG, "Do not reverse geocode location. User is using a custom location."); } } @@ -233,32 +235,36 @@ public class WeatherUpdater { } JsonObject locales; + JsonObject root; + String defaultCityName; try { final String json = body.string(); final JsonArray array = new JsonParser().parse(json).getAsJsonArray(); - locales = array.get(0).getAsJsonObject().getAsJsonObject("local_names"); + + root = array.get(0).getAsJsonObject(); + locales = root.getAsJsonObject("local_names"); + defaultCityName = root.get("name").getAsString(); } catch (IOException | IllegalStateException | JsonSyntaxException exception) { Log.e(TAG, "Exception caught", exception); return; } + if (defaultCityName == null) { + Log.e(TAG, "Could not get default city name"); + return; + } + if (locales == null) { Log.e(TAG, "Could not get locales"); + notifyUi(defaultCityName); return; } String countryCode = Locale.getDefault().getCountry().toLowerCase(Locale.ROOT); - if (!locales.has(countryCode)) { - final JsonElement jsonElement = locales.get("en"); - if (jsonElement == null) { - return; - } - - countryCode = jsonElement.getAsString(); - } - final JsonElement jsonElement = locales.get(countryCode); if (jsonElement == null) { + Log.e(TAG, "Could not get city name in country code: " + countryCode); + notifyUi(defaultCityName); return; } -- GitLab