Skip to content

Instantly share code, notes, and snippets.

View prokash-sarkar's full-sized avatar
🎯
Focusing

Prokash Sarkar prokash-sarkar

🎯
Focusing
View GitHub Profile
package library;
/**
* Created by Prokash Sarkar on March, 2021
* Copyright https://prokashsarkar.com
*/
public class CommandLibrary {
public static final String KEY_FUNNY = "funny";
public static final String KEY_JOKE = "joke";
@prokash-sarkar
prokash-sarkar / LiveDataTestUtil.kt
Created February 25, 2021 05:22
If you need to observe LiveData changes, then you can use an extension function called LiveDataTestUtil. It will help you observe LiveData changes without any extra effort.
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun <T> LiveData<T>.getOrAwaitValue(
time: Long = 2,
timeUnit: TimeUnit = TimeUnit.SECONDS,
afterObserve: () -> Unit = {}
): T {
var data: T? = null
val latch = CountDownLatch(1)
val observer = object : Observer<T> {
override fun onChanged(o: T?) {
class MainViewModel(
private val dispatcher: CoroutineDispatcher
) : ViewModel() {
private var _userData: MutableLiveData<Any> = MutableLiveData<Any>()
val userData: LiveData<Any> = _userData
suspend fun saveSessionData() {
viewModelScope.launch(dispatcher) {
_userData.value = "some_user_data"
class MainViewModel(
private val dispatcher: CoroutineDispatcher
) : ViewModel() {
private var _userData: MutableLiveData<Any> = MutableLiveData<Any>()
val userData: LiveData<Any> = _userData
suspend fun saveSessionData() {
viewModelScope.launch(dispatcher) {
_userData.value = "some_user_data"
class MainViewModel (
private val dispatcher: CoroutineDispatcher
) : ViewModel() {
private var userData: Any? = null
fun getUserData(): Any? = userData
suspend fun saveSessionData() {
viewModelScope.launch(dispatcher) {
userData = "some_user_data"
class MainViewModel : ViewModel() {
private var userData: Any? = null
fun getUserData(): Any? = userData
suspend fun saveSessionData() {
viewModelScope.launch(Dispatchers.IO) {
userData = "some_user_data"
}
}
class MainViewModel : ViewModel() {
private var userData: Any? = null
fun getUserData(): Any? = userData
suspend fun saveSessionData() {
viewModelScope.launch(Dispatchers.IO) {
userData = "some_user_data"
}
}
@ExperimentalCoroutinesApi
class MainCoroutineRule : TestWatcher(), TestCoroutineScope by TestCoroutineScope() {
override fun starting(description: Description) {
super.starting(description)
Dispatchers.setMain(this.coroutineContext[ContinuationInterceptor] as CoroutineDispatcher)
}
override fun finished(description: Description) {
super.finished(description)
class MainViewModel : ViewModel() {
private var userData: Any? = null
fun getUserData(): Any? = userData
suspend fun saveSessionData() {
viewModelScope.launch {
userData = "some_user_data"
}
}
class MainViewModel : ViewModel() {
private var userData: Any? = null
fun getUserData(): Any? = userData
suspend fun saveSessionData() {
viewModelScope.launch {
userData = "some_user_data"
}
}