Skip to content

Instantly share code, notes, and snippets.

View dmytrodanylyk's full-sized avatar

Dmytro Danylyk dmytrodanylyk

View GitHub Profile
@dmytrodanylyk
dmytrodanylyk / description.md
Last active June 20, 2022 15:00
Where this dependency comes from?

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
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutineVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutineVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutineVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutineVersion"
@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()
...
}
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") }
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()
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
}
var job: Job? = null
fun startPresenting() {
job = loadData()
job?.invokeOnCompletion { it: Throwable? ->
it?.printStackTrace() // (1)
// or
job?.getCompletionException()?.printStackTrace() // (2)
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
}
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