-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor ZeWeatherService to use DI (#363)
- Loading branch information
Showing
6 changed files
with
74 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
zeapp/android/src/main/java/de/berlindroid/zeapp/zeservices/ZeWeatherApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package de.berlindroid.zeapp.zeservices | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import retrofit2.http.GET | ||
|
||
interface ZeWeatherApi { | ||
|
||
@Serializable | ||
data class Weather( | ||
@SerialName(value = "hourly") | ||
val hourly: Hourly, | ||
) | ||
|
||
@Serializable | ||
data class Hourly( | ||
@SerialName(value = "time") | ||
val time: List<String>, | ||
|
||
@SerialName(value = "temperature_2m") | ||
val temperature: List<Double>, | ||
) | ||
|
||
@GET("v1/forecast?latitude=52.5244&longitude=13.4105&hourly=temperature_2m&forecast_days=16") | ||
suspend fun getWeather(): Weather | ||
} |
89 changes: 29 additions & 60 deletions
89
zeapp/android/src/main/java/de/berlindroid/zeapp/zeservices/ZeWeatherService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,38 @@ | ||
package de.berlindroid.zeapp.zeservices | ||
|
||
import de.berlindroid.zeapp.zemodels.WeatherData | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import okhttp3.MediaType.Companion.toMediaType | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.kotlinx.serialization.asConverterFactory | ||
import retrofit2.http.GET | ||
|
||
internal suspend fun fetchWeather(date: String): WeatherData { | ||
try { | ||
val weather = weatherApiService.getWeather() | ||
|
||
val tempIndex = weather.hourly.time.indexOfFirst { | ||
it.contains("${date}T12:00") | ||
} | ||
if (tempIndex == -1) { | ||
import javax.inject.Inject | ||
|
||
|
||
/** | ||
* Service to fetch the weather data from the API by the provided date | ||
*/ | ||
class ZeWeatherService @Inject constructor(private val zeWeatherApi: ZeWeatherApi) { | ||
|
||
internal suspend fun fetchWeather(date: String): WeatherData { | ||
try { | ||
val weather = zeWeatherApi.getWeather() | ||
|
||
val tempIndex = weather.hourly.time.indexOfFirst { | ||
it.contains("${date}T12:00") | ||
} | ||
if (tempIndex == -1) { | ||
return WeatherData( | ||
day = null, | ||
temperature = -1.0, | ||
) | ||
} | ||
val temperature = weather.hourly.temperature[tempIndex] | ||
val day = weather.hourly.time[tempIndex] | ||
return WeatherData( | ||
day = day, | ||
temperature = temperature, | ||
) | ||
} catch (e: Exception) { | ||
return WeatherData( | ||
day = null, | ||
temperature = -1.0, | ||
temperature = -42.0, | ||
) | ||
} | ||
val temperature = weather.hourly.temperature[tempIndex] | ||
val day = weather.hourly.time[tempIndex] | ||
return WeatherData( | ||
day = day, | ||
temperature = temperature, | ||
) | ||
} catch (e: Exception) { | ||
return WeatherData( | ||
day = null, | ||
temperature = -42.0, | ||
) | ||
} | ||
} | ||
|
||
private val json = Json { | ||
ignoreUnknownKeys = true | ||
} | ||
|
||
private val retrofit = Retrofit.Builder() | ||
.baseUrl("https://api.open-meteo.com") | ||
.addConverterFactory(json.asConverterFactory("application/json".toMediaType())) | ||
.build() | ||
|
||
private val weatherApiService = retrofit.create(WeatherApi::class.java) | ||
|
||
private interface WeatherApi { | ||
|
||
@Serializable | ||
data class Weather( | ||
@SerialName(value = "hourly") | ||
val hourly: Hourly, | ||
) | ||
|
||
@Serializable | ||
data class Hourly( | ||
@SerialName(value = "time") | ||
val time: List<String>, | ||
|
||
@SerialName(value = "temperature_2m") | ||
val temperature: List<Double>, | ||
) | ||
|
||
@GET("v1/forecast?latitude=52.5244&longitude=13.4105&hourly=temperature_2m&forecast_days=16") | ||
suspend fun getWeather(): Weather | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters