This file contains hidden or 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
class MovieListViewModel @Inject constructor(private val dataProvider: DataProvider) : ViewModel() { | |
val movieDiscover = MutableLiveData<List<Movie>>() | |
fun fetchMovieDiscover() { | |
dataProvider.getMovieDiscover().subscribeBy( | |
onSuccess = { result -> movieDiscover.postValue(result) | |
}, | |
onError = { error -> movieDiscover.postValue(null) } | |
) |
This file contains hidden or 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
sealed class ResourceState | |
class LoadingState : ResourceState() | |
class EmptyState : ResourceState() | |
data class PopulatedState<T>(val data: T) : ResourceState() | |
data class ErrorState(val message: String?) : ResourceState() |
This file contains hidden or 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
class MovieListViewModel @Inject constructor(private val dataProvider: DataProvider) : ViewModel() { | |
val movieDiscover = MutableLiveData<ResourceState>() | |
fun fetchMovieDiscover() { | |
movieDiscover.postValue(LoadingState()) | |
dataProvider.getMovieDiscover().subscribeBy( | |
onSuccess = { result -> | |
if (result.isEmpty()) movieDiscover.postValue(EmptyState()) | |
else movieDiscover.postValue(PopulatedState(result)) |
This file contains hidden or 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
movieDiscover.observe(activity, Observer { it?.apply { updateView(this) }}) | |
private fun updateView(resourceState: ResourceState) = when(resourceState) { | |
is LoadingState -> showLoading(true) | |
is PopulatedState<*> -> populate(resourceState.data as List<Movie>) | |
is EmptyState -> showEmptyMessage() | |
is ErrorState -> showError(resourceState.message) | |
} |
This file contains hidden or 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
sealed class ResourceState<T> | |
class LoadingState<T> : ResourceState<T>() | |
class EmptyState<T> : ResourceState<T>() | |
data class PopulatedState<T>(val data: T) : ResourceState<T>() | |
data class ErrorState<T>(val message: String?) : ResourceState<T>() |
This file contains hidden or 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
//MovieListViewModel | |
val movieDiscover = MutableLiveData<ResourceState<List<Movie>>>() | |
//View | |
private fun updateView(resourceState: ResourceState<List<Movie>>) = when(resourceState) { | |
is LoadingState -> showLoading(true) | |
is PopulatedState -> populate(resourceState.data) | |
is EmptyState -> showEmptyMessage() | |
is ErrorState -> showError(resourceState.message) | |
} |
This file contains hidden or 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
@Rule | |
@JvmField | |
val instantExecutorRule = InstantTaskExecutorRule() //Very very important |
This file contains hidden or 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
@Mock | |
private lateinit var dataProvider: DataProvider | |
@Mock lateinit var observer: Observer<ResourceState<List<Movie>>> | |
private lateinit var viewModel: MovieListViewModel | |
@Before | |
fun init() { | |
MockitoAnnotations.initMocks(this) | |
viewModel = MovieListViewModel(dataProvider) | |
.apply { movieDiscover.observeForever(observer) } |
This file contains hidden or 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
private fun mockFetchMovieDiscover(block: SingleEmitter<MovieDiscoveryResponse>.() -> Unit) { | |
`when`(dataProvider.getMovieDiscover()).thenReturn(Single.create { e -> e.block() }) | |
} | |
@Test | |
fun fetchMovieDiscover_test_success_with_empty() { | |
mockFetchMovieDiscover { onSuccess(emptyList()) } | |
viewModel.fetchMovieDiscover() |
This file contains hidden or 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
sealed class ResourceState<T> | |
class LoadingState<T> : ResourceState<T>() { | |
override fun hashCode(): Int = javaClass.hashCode() | |
override fun equals(other: Any?) = equalz(other) | |
} | |
class EmptyState<T> : ResourceState<T>() { | |
override fun hashCode(): Int = javaClass.hashCode() | |
override fun equals(other: Any?) = equalz(other) | |
} |
OlderNewer