From cdc1889b44834fee2da6e87b4ae693facf31e351 Mon Sep 17 00:00:00 2001 From: TheScarastic Date: Thu, 11 Apr 2024 14:38:51 +0530 Subject: [PATCH] blissweather: Get city name through reverse geocode api --- .../e/blissweather/worker/WorkerUtils.kt | 25 +++++++++++++++++++ .../e/blissweather/models/WeatherCity.kt | 5 ++++ .../e/blissweather/models/WeatherData.kt | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/android/src/main/kotlin/foundation/e/blissweather/worker/WorkerUtils.kt b/android/src/main/kotlin/foundation/e/blissweather/worker/WorkerUtils.kt index c618101..3039f13 100644 --- a/android/src/main/kotlin/foundation/e/blissweather/worker/WorkerUtils.kt +++ b/android/src/main/kotlin/foundation/e/blissweather/worker/WorkerUtils.kt @@ -14,6 +14,7 @@ import foundation.e.blissweather.ApiSuccess import foundation.e.blissweather.api.OpenWeatherRepository import foundation.e.blissweather.models.CombinedWeatherResponse import foundation.e.blissweather.models.Coordinate +import java.util.Locale object WorkerUtils { private const val TAG = "WorkerUtils" @@ -35,6 +36,30 @@ object WorkerUtils { } } + val reverseGeocode = + when (val result = weatherRepository.getLocationNameByCoords(coords)) { + is ApiSuccess -> result.data + is ApiError -> { + Log.e(TAG, "[${result.code}] ReverseGeocode Error: ${result.message}") + null + } + is ApiException -> { + Log.e(TAG, "ReverseGeocode Error:", result.e) + null + } + } + + if (current != null && reverseGeocode != null) { + val name = + reverseGeocode[0] + .localNames + .getOrDefault(Locale.getDefault().language, reverseGeocode[0].name) + + if (name.isNotEmpty()) { + current.name = name + } + } + val forecasts = when (val result = weatherRepository.getDaysForecastByCoords(coords)) { is ApiSuccess -> result.data diff --git a/data/src/main/kotlin/foundation/e/blissweather/models/WeatherCity.kt b/data/src/main/kotlin/foundation/e/blissweather/models/WeatherCity.kt index cc4d4c3..0e8d6ae 100644 --- a/data/src/main/kotlin/foundation/e/blissweather/models/WeatherCity.kt +++ b/data/src/main/kotlin/foundation/e/blissweather/models/WeatherCity.kt @@ -9,7 +9,9 @@ package foundation.e.blissweather.models import android.os.Parcelable import androidx.annotation.Keep +import com.squareup.moshi.Json import kotlinx.parcelize.Parcelize +import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @Keep @@ -21,4 +23,7 @@ data class WeatherCity( val lon: Double, val country: String?, val state: String?, + @Json(name = "local_names") + @SerialName("local_names") + val localNames: Map = emptyMap() ) : Parcelable diff --git a/data/src/main/kotlin/foundation/e/blissweather/models/WeatherData.kt b/data/src/main/kotlin/foundation/e/blissweather/models/WeatherData.kt index cfb0e3f..2ce51f2 100644 --- a/data/src/main/kotlin/foundation/e/blissweather/models/WeatherData.kt +++ b/data/src/main/kotlin/foundation/e/blissweather/models/WeatherData.kt @@ -17,7 +17,7 @@ import kotlinx.serialization.Serializable data class WeatherData( @Json(name = "cod") @SerialName("cod") val code: Int, val id: Int, - val name: String, + var name: String, val main: Main, val sys: Sys, val dt: Long, -- GitLab