Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e9b4ceed authored by Jonathan Klee's avatar Jonathan Klee
Browse files

Implement fallback with default city

when city in the right language cannot be found
in the json.
parent 4c42eacb
Loading
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -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);
+16 −10
Original line number Diff line number Diff line
@@ -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 (locales == null) {
            Log.e(TAG, "Could not get locales");
        if (defaultCityName == null) {
            Log.e(TAG, "Could not get default city name");
            return;
        }

        String countryCode = Locale.getDefault().getCountry().toLowerCase(Locale.ROOT);
        if (!locales.has(countryCode)) {
            final JsonElement jsonElement = locales.get("en");
            if (jsonElement == null) {
        if (locales == null) {
            Log.e(TAG, "Could not get locales");
            notifyUi(defaultCityName);
            return;
        }

            countryCode = jsonElement.getAsString();
        }

        String countryCode = Locale.getDefault().getCountry().toLowerCase(Locale.ROOT);
        final JsonElement jsonElement = locales.get(countryCode);
        if (jsonElement == null) {
            Log.e(TAG, "Could not get city name in country code: " + countryCode);
            notifyUi(defaultCityName);
            return;
        }