idanatz / OneAdapter

A Viewholderless Adapter for RecyclerView, who supports builtin diffing, states (paging, empty...), events (clicking, swiping...), and more.
MIT License
470 stars 45 forks source link

Get Emptiness module programmatically #18

Closed francescogatto closed 4 years ago

francescogatto commented 4 years ago

Hi! very nice library I'm trying to change programmatically text and lottieview of the emptiness module. This is my flow: 1) show emptiness module 2) loading some data in recycler view 3) swipe to refresh and data now is empty 4) emptiness module is not showing

or

1) show emptiness module 2) loading some data in recycler view 3) search for some data 4) data is empty 4) show emptiness module with a text like "Nothing found during the research"

Thanks!

francescogatto commented 4 years ago

i also tried:

emptinessModule.currentText = "text" prodAdapter.clear()

clear doesn't call the binding method of emptiness module :(

idanatz commented 4 years ago

The Emptiness Module is very simple, you will get an onBind call what the adapter is empty. If you call clear() you will get the onBind call.

If the behavior is different in your project perhaps you are doing something wrong? Have you looked at the sample project? https://github.com/ironSource/OneAdapter/blob/master/sample/app/src/main/java/com/idanatz/sample/examples/features/EmptinessModuleActivity.kt I'm using the module in several projects and it works as expected.

Please add full code examples

francescogatto commented 4 years ago

Hey @idanatz , the problem is this Scenario: 1) init of adapter
prodAdapter = OneAdapter(prodRecyclerView).attachItemModule(ProdModule()) emptinessModule = EmptinessModuleImpl("Effettua una ricerca per trovare i prodotti vicino a te", R.raw.radar) prodAdapter.attachEmptinessModule(emptinessModule) 2) call the service and get the result: imageSearchButton.setSafeOnClickListener { viewModel.getProducts(lng, lat, radius).observeKt { when (it) { is Result.Success -> { if (it.data.isEmpty()) { prodAdapter.add(emptyList()) emptinessModule.currentText = "text" prodAdapter.clear() } else { prodAdapter.add(it.data) } } else -> { } } } } 3) if the result is always empty, clear never call the unbind method of the emptiness module. Here the code of the module `class EmptinessModuleImpl(val text: String, val lottieResourse: Int = R.raw.empty_list) : EmptinessModule() {

var currentText = ""
init {
    currentText = text
}
override fun provideModuleConfig(): EmptinessModuleConfig = object : EmptinessModuleConfig() {
    override fun withLayoutResource() = R.layout.empty_state
}

override fun onBind(viewBinder: ViewBinder) {
    val animation = viewBinder.findViewById<LottieAnimationView>(R.id.animation_view)
    animation.setAnimation(lottieResourse)
    animation.playAnimation()
    viewBinder.findViewById<TextView>(R.id.textView).text = currentText
}

override fun onUnbind(viewBinder: ViewBinder) {
    val animation = viewBinder.findViewById<LottieAnimationView>(R.id.animation_view)
    animation.pauseAnimation()
}

}`

idanatz commented 4 years ago

why are you adding an empty list into the adapter? prodAdapter.add(emptyList())

because of this line, the adapter is not really empty (it has an empty list inside as an item) so the emptiness module is not being used.

remove this line and just call: emptinessModule.currentText = "text" prodAdapter.clear()

francescogatto commented 4 years ago

I did it, but didn't work. I think the problem is in updateData, because the diffutils doesn't see any change(?)

idanatz commented 4 years ago

Oh, I think i see your problem. The Adapter was already empty and you want it to call onBind again when you call clear() ?

francescogatto commented 4 years ago

Exactly, in this way, I can use Emptiness module to show the user errors from the server or just advice that the result is empty :)

idanatz commented 4 years ago

I got it, did not think to use the module in this way. Will be fixed in the next version 👍

idanatz commented 4 years ago

Version 1.5.0 is out with the fix. Please notice that there are breaking changes in the modules API due to other requests. The sample project is updated as well for reference.