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
| dependencies { | |
| //Other dependencies... | |
| implementation "android.arch.lifecycle:extensions:1.1.0" | |
| annotationProcessor "android.arch.lifecycle:compiler:1.1.0" | |
| testImplementation "android.arch.core:core-testing:1.1.0" | |
| } |
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 { | |
| object LOADING : ResourceState() | |
| object SUCCESS : ResourceState() | |
| object ERROR : 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
| data class Resource<out T> constructor( | |
| val state: ResourceState, | |
| val data: T? = null, | |
| val message: String? = 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
| class UserListViewModel : ViewModel() { | |
| val users = MutableLiveData<Resource<List<User>>>() | |
| private fun loading() = users.setLoading() | |
| private fun success(userList: List<User>) = users.setSuccess(userList) | |
| private fun error(throwable: Throwable) = users.setError(throwable.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
| class CounterViewModel : ViewModel() { | |
| val counter = MutableLiveData<Int>() | |
| var number = 0 | |
| fun onButtonClick() = counter.setValue(++number) | |
| } |
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 MainActivity : AppCompatActivity() { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| val vm = ViewModelProviders.of(this)[CounterViewModel::class.java] | |
| vm.counter.observe(this, Observer({ textView.text = it.toString() })) | |
| button.setOnClickListener { vm.onButtonClick() } | |
| } |
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 updateUsers(resource: Resource<List<Users>>?) { | |
| resource?.let { | |
| when (it.state) { | |
| ResourceState.LOADING -> loading.visible() | |
| ResourceState.SUCCESS -> loading.gone() | |
| ResourceState.ERROR -> loading.gone() | |
| } | |
| it.data?.let { adapter.addItems(it) } | |
| it.message?.let { snackBar.show() } | |
| } |
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 CounterViewModelTest { | |
| private lateinit var viewModel: CounterViewModel | |
| @Rule | |
| @JvmField | |
| val instantTaskExecutorRule: TestRule = InstantTaskExecutorRule() | |
| @Before | |
| fun setUp() { |
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
| @Singleton | |
| class ViewModelFactory @Inject constructor(private val viewModels: MutableMap<Class<out ViewModel>, Provider<ViewModel>>) : ViewModelProvider.Factory { | |
| override fun <T : ViewModel> create(modelClass: Class<T>): T = viewModels[modelClass]?.get() as T | |
| } | |
| @Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER) | |
| @kotlin.annotation.Retention(AnnotationRetention.RUNTIME) | |
| @MapKey | |
| internal annotation class ViewModelKey(val value: KClass<out ViewModel>) |
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 PostListViewModel @Inject constructor(private val usersPostsUseCase: UsersPostsUseCase) : | |
| ViewModel() { | |
| val posts = MutableLiveData<Resource<List<PostItem>>>() | |
| private val compositeDisposable = CompositeDisposable() | |
| fun get(refresh: Boolean = false) = | |
| compositeDisposable.add(usersPostsUseCase.get(refresh) | |
| .doOnSubscribe { posts.setLoading() } | |
| .subscribeOn(Schedulers.io()) |
OlderNewer