Created
September 17, 2018 13:45
-
-
Save pavelaizen/249db7c366c9ad09dc66c2ee69512e25 to your computer and use it in GitHub Desktop.
Simple CoroutineScope implementation
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
import android.app.Application | |
import android.arch.lifecycle.AndroidViewModel | |
import android.arch.lifecycle.MutableLiveData | |
import kotlinx.coroutines.experimental.* | |
import kotlinx.coroutines.experimental.android.Main | |
import kotlin.coroutines.experimental.CoroutineContext | |
abstract class BaseFragmentViewModel(application: Application) : AndroidViewModel(application), CoroutineScope { | |
private val job = Job() | |
open val modelDispatcher: CoroutineDispatcher = Dispatchers.Default | |
override val coroutineContext: CoroutineContext | |
get() = modelDispatcher + job | |
override fun onCleared() { | |
super.onCleared() | |
job.cancel() | |
} | |
} | |
class SuccessScreenModel(application: Application) : BaseFragmentViewModel(application) { | |
val delayComplete = MutableLiveData<Unit>() | |
override val modelDispatcher: CoroutineDispatcher | |
get() = Dispatchers.Main | |
fun waitBitMore(delay: Int) { | |
launch { | |
delay(delay) | |
delayComplete.value = Unit | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment