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

Commit fb458bf7 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 10ce3dad
Loading
Loading
Loading
Loading
Loading
+14 −10
Original line number Diff line number Diff line
@@ -235,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;
        }