This file contains 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 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 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 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 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 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 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 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" | |
} |
NewerOlder