mikepenz / FastAdapter

The bullet proof, fast and easy to use adapter library, which minimizes developing time to a fraction...
https://mikepenz.dev
Apache License 2.0
3.83k stars 492 forks source link

Suggestion - Extend ISwipeable #1008

Closed MFlisar closed 2 years ago

MFlisar commented 3 years ago

About this issue

I want to distinguish between items the do allow swipe left / swipe right / both. Currently I can only distinguish can swipe / can not swipe.

My suggestion is to extend the ISwipeable interface:

interface ISwipeable {
    /** @return true if swipeable */
    val isSwipeable: Boolean

    /** @return true if the provided direction is supported */
    fun isDirectionSupported(direction: Int) : Boolean = true
}

What do you think about this? It's a very small and backwards compatible change with the default implementation inside the interface. isDirectionSupported will only be called if isSwipeable return true.

Details

mikepenz commented 3 years ago

@MFlisar being able to identify the swipeable direction makes sense.

How would the real implementation for isDirectionSupported look like? Would a different interface not also fullfil this requirement? (As the implementation itself would also fall in the target apps responsibility it looks like?)

MFlisar commented 3 years ago

How would the real implementation for isDirectionSupported look like?

I do have items that do support "starting" them and sections, that do not support this. But both items do support delete. so I would use it like following:

In my case like following:

class ItemSection(val data: Data) : IItem, ISwipeable  {

    val isSwipeable: Boolean = true

    fun isDirectionSupported(direction: Int) : Boolean {
        return direction = ItemTouchHelper.RIGHT; // only right swipe is supported, sections can only be deleted and can't be started
    }

}

class Item(val data: Data) : IItem, ISwipeable  {

    val isSwipeable: Boolean = true

    fun isDirectionSupported(direction: Int) : Boolean {
        return true; // items can be started and deleted, so both swipe directions are supported
    }

}

Would a different interface not also fullfil this requirement?

Sure, but personally I think an interface with default implementation is more discoverable (especially if the features are so closely)

MFlisar commented 3 years ago

By the way, if backwards compatibility is not required, I would even replace the interface with following for the sake of simplicity:

interface ISwipeable {
    fun isSwipeable(direction: Int) : Boolean
}

Migration guide would simply look like following:

Replace

  override val isSwipeable = true|false

With

 override fun isSwipeable(direction: Int) = true|false
mikepenz commented 3 years ago

I'd opt for the backwards compatible form. Looked over it, and your proposal makes sense. It will also need to be integrated SimpleSwipeDrawerCallback and SimpleSwipeCallback

Would you like to open a PR for this?

MFlisar commented 3 years ago

Would you like to open a PR for this?

Done

mikepenz commented 2 years ago

Thank you so much. Will be in the next update