Skip to content

Commit

Permalink
Change ViewState from generic class to sealed class
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaKordic committed Sep 2, 2019
1 parent 5074bb0 commit 069de6f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
25 changes: 4 additions & 21 deletions app/src/main/java/com/cobeisfresh/template/ui/base/ViewState.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
package com.cobeisfresh.template.ui.base

import com.cobeisfresh.template.ui.base.ViewState.Status.*
sealed class ViewState<out T : Any>
class Success<out T : Any>(val data: T) : ViewState<T>()
class Error<out T : Any>(val error: Throwable) : ViewState<T>()
class Loading<out T : Any> : ViewState<T>()

data class ViewState<T>(val status: Status, val data: T?, val error: Throwable?) {

enum class Status {
SUCCESS, ERROR, LOADING
}

companion object {
fun <T> success(data: T): ViewState<T> {
return ViewState(SUCCESS, data, null)
}

fun <T> error(error: Throwable): ViewState<T> {
return ViewState(ERROR, null, error)
}

fun <T> loading(): ViewState<T> {
return ViewState(LOADING, null, null)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.cobeisfresh.template.common.DEFAULT_CITY_NAME
import com.cobeisfresh.template.common.extensions.launch
import com.cobeisfresh.template.ui.base.Error
import com.cobeisfresh.template.ui.base.Loading
import com.cobeisfresh.template.ui.base.Success
import com.cobeisfresh.template.ui.base.ViewState
import com.example.domain.interaction.weather.GetWeatherUseCase
import com.example.domain.model.WeatherInfo
Expand All @@ -20,9 +23,9 @@ class WeatherViewModel(private val getWeather: GetWeatherUseCase) : ViewModel()

fun getWeatherForLocation(location: String = DEFAULT_CITY_NAME) =
launch {
_weatherLiveData.value = ViewState.loading()
_weatherLiveData.value = Loading()
getWeather(location)
.onSuccess { _weatherLiveData.postValue(ViewState.success(it)) }
.onFailure { _weatherLiveData.postValue(ViewState.error(it.throwable)) }
.onSuccess { _weatherLiveData.postValue(Success(it)) }
.onFailure { _weatherLiveData.postValue(Error(it.throwable)) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import com.cobeisfresh.template.common.convertKelvinToCelsius
import com.cobeisfresh.template.common.extensions.hideKeyboard
import com.cobeisfresh.template.common.extensions.onClick
import com.cobeisfresh.template.common.extensions.subscribe
import com.cobeisfresh.template.ui.base.BaseFragment
import com.cobeisfresh.template.ui.base.ViewState
import com.cobeisfresh.template.ui.base.ViewState.Status.*
import com.cobeisfresh.template.ui.base.*
import com.cobeisfresh.template.ui.weather.presentation.WeatherViewModel
import com.example.domain.model.WeatherInfo
import kotlinx.android.synthetic.main.fragment_weather.*
Expand Down Expand Up @@ -36,14 +34,12 @@ class WeatherFragment : BaseFragment() {
}

private fun handleViewState(viewState: ViewState<WeatherInfo>) {
with(viewState) {
when (status) {
LOADING -> showLoading(weatherLoadingProgress)
SUCCESS -> data?.run(::showWeatherData)
ERROR -> {
hideLoading(weatherLoadingProgress)
showError(error?.message, weatherActivityContainer)
}
when (viewState) {
is Loading -> showLoading(weatherLoadingProgress)
is Success -> showWeatherData(viewState.data)
is Error -> {
hideLoading(weatherLoadingProgress)
showError(viewState.error.message, weatherActivityContainer)
}
}
}
Expand Down

0 comments on commit 069de6f

Please sign in to comment.