Skip to content

Commit 698e5c5

Browse files
committed
Move loading state change to BaseViewModel, set it whenever UseCase is invoked
1 parent d6e8eca commit 698e5c5

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

app/src/main/java/com/cobeisfresh/template/ui/base/BaseViewModel.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ abstract class BaseViewModel<T : Any, E> : ViewModel(), KoinComponent {
2525
get() = _viewEffects
2626

2727
protected fun executeUseCase(action: suspend () -> Unit, noInternetAction: () -> Unit) {
28+
_viewState.value = Loading()
2829
if (connectivity.hasNetworkAccess()) {
2930
launch { action() }
3031
} else {
@@ -33,6 +34,7 @@ abstract class BaseViewModel<T : Any, E> : ViewModel(), KoinComponent {
3334
}
3435

3536
protected fun executeUseCase(action: suspend () -> Unit) {
37+
_viewState.value = Loading()
3638
launch { action() }
3739
}
3840
}

app/src/main/java/com/cobeisfresh/template/ui/weather/presentation/WeatherViewModel.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ import com.example.domain.model.onSuccess
1313

1414
class WeatherViewModel(private val getWeather: GetWeatherUseCase) : BaseViewModel<WeatherInfo, WeatherViewEffects>() {
1515

16-
fun getWeatherForLocation(location: String = DEFAULT_CITY_NAME) {
17-
executeUseCase {
18-
_viewState.value = Loading()
19-
getWeather(location)
20-
.onSuccess { _viewState.value = Success(it) }
21-
.onFailure { _viewState.value = Error(it.throwable) }
22-
}
23-
}
16+
fun getWeatherForLocation(location: String = DEFAULT_CITY_NAME) = executeUseCase {
17+
getWeather(location)
18+
.onSuccess { _viewState.value = Success(it) }
19+
.onFailure { _viewState.value = Error(it.throwable) }
20+
}
2421
}

data/src/main/java/com/example/data/repository/weather/BaseRepository.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ abstract class BaseRepository<T : Any, R : DomainMapper<T>> : KoinComponent {
1717
private val connectivity: Connectivity by inject()
1818
private val contextProvider: CoroutineContextProvider by inject()
1919

20+
/**
21+
* Use this if you need to cache data after fetching it from the api, or retrieve something from cache
22+
*/
2023
protected suspend fun fetchData(
2124
apiDataProvider: suspend () -> Result<T>,
2225
dbDataProvider: suspend () -> R
@@ -33,6 +36,9 @@ abstract class BaseRepository<T : Any, R : DomainMapper<T>> : KoinComponent {
3336
}
3437
}
3538

39+
/**
40+
* Use this when communicating only with the api service
41+
*/
3642
protected suspend fun fetchData(dataProvider: () -> Result<T>): Result<T> {
3743
return if (connectivity.hasNetworkAccess()) {
3844
withContext(contextProvider.io) {

data/src/main/java/com/example/data/repository/weather/WeatherRepositoryImpl.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class WeatherRepositoryImpl(private val weatherApi: WeatherApi,
1717
fetchFromCacheAction = { weatherDao.getWeatherInfoForCity(location) },
1818
cacheAction = { weatherDao.saveWeatherInfo(it) })
1919
},
20-
dbDataProvider = { weatherDao.getWeatherInfoForCity(location) })
20+
dbDataProvider = { weatherDao.getWeatherInfoForCity(location) }
21+
)
2122
}
2223
}

0 commit comments

Comments
 (0)