Skip to content

Instantly share code, notes, and snippets.

Avatar

Dmytro Danylyk dmytrodanylyk

View GitHub Profile
@dmytrodanylyk
dmytrodanylyk / description.md
Last active Jan 30, 2020
Where this dependency comes from?
View description.md

Did you ever have android build failed​ issue because of dependency resolution?

… or you were curious where all these old rxjava dependencies come from?

You can pretty easy track the module causing issues via following gradle command.

gradlew :root-module:dependencyInsight \
--configuration debugRuntimeClasspath \ // or debugCompileClasspath
--dependency io.reactivex:rxjava:1.1.0 > dependencies.txt // saves result to 'dependencies.txt' file
View coroutines.gradle
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutineVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutineVersion"
View coroutines.gradle
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutineVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutineVersion"
View MainPresenterTest.kt
@Test
fun test() {
val dataProvider = Mockito.mock(DataProviderAPI::class.java)
val mockView = Mockito.mock(MainView::class.java)
val presenter = MainPresenter(mockView, dataProvider, Unconfined, Unconfined)
presenter.startPresenting()
...
}
View MainPresenter.kt
class MainPresenter(private val view: MainView,
private val dataProvider: DataProviderAPI
private val uiContext: CoroutineContext = UI,
private val ioContext: CoroutineContext = CommonPool) {
private fun loadData() = launch(uiContext) { // use the provided uiContext (UI)
view.showLoading()
// use the provided ioContext (CommonPool)
val task = async(bgContext) { dataProvider.loadData("Task") }
View MainPresenter.kt
class MainPresenter(private val view: MainView,
private val dataProvider: DataProviderAPI) {
private fun loadData() = launch(UI) { // UI - dispatches execution onto Android main UI thread
view.showLoading()
// CommonPool - represents common pool of shared threads as coroutine dispatcher
val task = async(CommonPool) { dataProvider.loadData("Task") }
val result = task.await()
View MainPresenter.kt
val exceptionHandler: CoroutineContext = CoroutineExceptionHandler { _, throwable -> throwable.printStackTrace() }
private fun loadData() = launch(uiContext + exceptionHandler) {
view.showLoading() // ui thread
val task = async(bgContext) { dataProvider.loadData("Task") }
val result = task.await() // non ui thread, suspend until the task is finished
view.showData(result) // ui thread
}
View MainPresenter.kt
var job: Job? = null
fun startPresenting() {
job = loadData()
job?.invokeOnCompletion { it: Throwable? ->
it?.printStackTrace() // (1)
// or
job?.getCompletionException()?.printStackTrace() // (2)
View MainPresenter.kt
private fun loadData() = async(uiContext) {
view.showLoading() // ui thread
val task = async(bgContext) { dataProvider.loadData("Task") }
val result = task.await() // non ui thread, suspend until the task is finished
view.showData(result) // ui thread
}
View MainPresenter.kt
data class Result<out T>(val success: T? = null, val error: Throwable? = null)
private fun loadData() = launch(uiContext) {
view.showLoading() // ui thread
val task = async(bgContext) { dataProvider.loadData("Task") }
val result: Result<String> = task.await() // non ui thread, suspend until the task is finished
if (result.success != null) {
view.showData(result.success) // ui thread