Android-CleanArchitecture-Kotlin icon indicating copy to clipboard operation
Android-CleanArchitecture-Kotlin copied to clipboard

Replace RecyclerView.Adapter to ListAdapter

Open mustafakuloglu opened this issue 7 years ago • 5 comments

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.

mustafakuloglu avatar Jul 25 '18 14:07 mustafakuloglu

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

kijka avatar Sep 18 '18 11:09 kijka

Will submit a PR for this

vincent-paing avatar Oct 01 '18 10:10 vincent-paing

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
}

Toffor avatar Oct 08 '18 07:10 Toffor

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

Zhuinden avatar Oct 08 '18 09:10 Zhuinden

@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.

Toffor avatar Oct 08 '18 12:10 Toffor