android10 / Android-CleanArchitecture-Kotlin

This is a movies sample app in Kotlin, which is part of a serie of blog posts I have written about architecting android application using different approaches.
https://fernandocejas.com/2018/05/07/architecting-android-reloaded/
4.68k stars 929 forks source link

Replace RecyclerView.Adapter to ListAdapter #51

Open mustafakuloglu opened 6 years ago

mustafakuloglu commented 6 years ago

Here is some explanation about why we must use ListAdapter.
This project is so good. Thanks for this project and explanations. But if you use diffutil in adapter, you can get some good skills. Recycler.Adapter and diffutil is little bit complex and have some boilerplate code. ListAdapter have some good solutions about this. Details in link. Please read that article.

kijka commented 5 years ago

I made a fork of this project and there is an example of using ListAdapter: https://github.com/vladsonkin/bikeindex/blob/master/app/src/main/kotlin/com/sonkins/bikeindex/features/bikes/filter/colors/ColorsAdapter.kt

vincent-paing commented 5 years ago

Will submit a PR for this

Toffor commented 5 years ago

You can do something like below i think.

abstract class UseCase<out Type, in Params> : CoroutineScope where Type : Any {

    private val job = Job()

    abstract suspend fun run(params: Params): Either<Failure, Type>

    fun execute(onResult: (Either<Failure, Type>) -> Unit, params: Params) {
        val job = async(CommonPool) { run(params) }
        launch { onResult.invoke(job.await()) }
    }

    fun cancel() = job.cancel()

    override val coroutineContext: CoroutineContext
        get() = job + Dispatchers.Main
}
Zhuinden commented 5 years ago

@Toffor is it possible to use launch instead of async?

Toffor commented 5 years ago

@Zhuinden I think its possible but in this example if you don't use async it will throw NetworkOnMainThreadException. If you use https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter or a solution like this, then you can use just launch because network request already done an other context. Also I think suspend modifier is redundant in this example.