mirror of
https://github.com/AdrianKuta/android-challange-adrian-kuta.git
synced 2025-07-02 00:08:01 +02:00
feat: Introduce Result type and update UI states
This commit introduces a generic `Result` sealed interface to represent success, error, and loading states for asynchronous operations. It also updates the home screen UI and ViewModel to utilize this `Result` type for displaying different states. Key changes: - Added `Result.kt` in `core:util` defining the `Result` sealed interface and an `asResult()` Flow extension. - Updated `HomeScreenViewModel` to use `asResult()` and map the `ObserveAirportsUseCase` output to `HomeUiState` (Loading, Success, Error). - Modified `HomeUiState` to be a sealed interface with `Loading`, `Success`, and `Error` subtypes. - Updated `HomeScreen` to handle different `HomeUiState` values and display appropriate UI (Loading text, Error text, or list of airports). - Added `core.util` dependency to `ui:home`. - Updated Moshi and Retrofit Moshi converter dependencies in `model/data/api/build.gradle.kts` and `gradle/libs.versions.toml`. - Added `moshi` and `moshiKotlinCodegen` versions to `gradle/libs.versions.toml`. - Removed `converter-moshi` and added `retrofit-converter-moshi` in `gradle/libs.versions.toml`. - Added `ksp(libs.moshi.kotlin.codegen)` to `model/data/api/build.gradle.kts`. - Added `INTERNET` permission to `app/src/main/AndroidManifest.xml`. - Updated `ObserveAirportsUseCaseImpl` to use `cacheKey` when setting airports info and to fetch airports for "pl" (lowercase). - Added `.editorconfig` file with Kotlin trailing comma settings.
This commit is contained in:
@ -0,0 +1,16 @@
|
||||
package dev.adriankuta.flights.core.util
|
||||
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onStart
|
||||
|
||||
sealed interface Result<out T> {
|
||||
data class Success<T>(val data: T) : Result<T>
|
||||
data class Error(val exception: Throwable) : Result<Nothing>
|
||||
data object Loading : Result<Nothing>
|
||||
}
|
||||
|
||||
fun <T> Flow<T>.asResult(): Flow<Result<T>> = map<T, Result<T>> { Result.Success(it) }
|
||||
.onStart { emit(Result.Loading) }
|
||||
.catch { emit(Result.Error(it)) }
|
Reference in New Issue
Block a user