Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor ZeWeatherService to use DI #363

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions zeapp/android/src/main/java/de/berlindroid/zeapp/zedi/ApiModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import de.berlindroid.zeapp.zeservices.ZePassApi
import de.berlindroid.zeapp.zeservices.ZePassService
import de.berlindroid.zeapp.zeservices.ZeUserApi
import de.berlindroid.zeapp.zeservices.ZeUserService
import de.berlindroid.zeapp.zeservices.ZeWeatherApi
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
Expand All @@ -19,6 +20,7 @@ data class ZeServerBaseUrl(
)

private val BASE_URL = ZeServerBaseUrl("https://zebadge.app/api/")
private val METEO_URL = ZeServerBaseUrl("https://api.open-meteo.com")

@InstallIn(ViewModelComponent::class)
@Module
Expand Down Expand Up @@ -75,4 +77,14 @@ object ApiModule {
.create(
ZeUserService::class.java,
)

@Provides
fun provideZeWeatherApi(
json: Json,
): ZeWeatherApi =
Retrofit.Builder()
.baseUrl(METEO_URL.value)
.addConverterFactory(json.asConverterFactory("application/json".toMediaType()))
.build()
.create(ZeWeatherApi::class.java)
}
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
}
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import de.berlindroid.zeapp.zebits.composableToBitmap
import de.berlindroid.zeapp.zebits.isBinary
import de.berlindroid.zeapp.zemodels.WeatherData
import de.berlindroid.zeapp.zemodels.ZeConfiguration
import de.berlindroid.zeapp.zeservices.fetchWeather
import de.berlindroid.zeapp.zeui.zepages.WeatherPage
import de.berlindroid.zeapp.zeui.zetheme.ZeBlack
import de.berlindroid.zeapp.zeui.zetheme.ZeWhite
Expand All @@ -59,6 +58,7 @@ fun WeatherEditorDialog(
dismissed: () -> Unit = {},
accepted: (config: ZeConfiguration.Weather) -> Unit,
updateMessage: (String) -> Unit,
onFetchWeatherClick: suspend (String) -> WeatherData,
) {
val activity = LocalContext.current as Activity

Expand Down Expand Up @@ -200,9 +200,8 @@ fun WeatherEditorDialog(
item {
Button(
onClick = {
// Fix this please :)
scope.launch {
weatherData = fetchWeather(date)
weatherData = onFetchWeatherClick(date)
redrawComposableImage()
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ internal fun SelectedEditor(
dismissed = { vm.slotConfigured(null, null) },
accepted = { newConfig -> vm.slotConfigured(editor.slot, newConfig) },
updateMessage = vm::showMessage,
onFetchWeatherClick = vm::fetchWeather,
)

is ZeConfiguration.Quote -> RandomQuotesEditorDialog(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import de.berlindroid.zeapp.zeservices.ZeBadgeManager
import de.berlindroid.zeapp.zeservices.ZeClipboardService
import de.berlindroid.zeapp.zeservices.ZeImageProviderService
import de.berlindroid.zeapp.zeservices.ZePreferencesService
import de.berlindroid.zeapp.zeservices.ZeWeatherService
import de.berlindroid.zeapp.zeui.pixelManipulation
import de.berlindroid.zeapp.zeui.simulator.ZeSimulatorButtonAction
import de.berlindroid.zekompanion.ditherFloydSteinberg
Expand Down Expand Up @@ -47,6 +48,7 @@ class ZeBadgeViewModel @Inject constructor(
private val badgeManager: ZeBadgeManager,
private val preferencesService: ZePreferencesService,
private val clipboardService: ZeClipboardService,
private val weatherService: ZeWeatherService,
private val getTemplateConfigurations: GetTemplateConfigurations,
) : ViewModel() {

Expand Down Expand Up @@ -533,6 +535,8 @@ class ZeBadgeViewModel @Inject constructor(
}
}

suspend fun fetchWeather(date: String) = weatherService.fetchWeather(date)

private fun getInitialUIState(): ZeBadgeUiState =
ZeBadgeUiState(
message = "",
Expand Down
Loading