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

Added possibility to get view types from the adapter to add support for GridLayoutManager.SpanSizeLookup #16

Closed meruiden closed 4 years ago

meruiden commented 4 years ago

Example usage:

private val loadingIndicatorType = adapter.getItemViewTypeFromClass(LoadingIndicator::class.java)

override fun getSpanSize(position: Int): Int {
    val itemType = adapter.getItemViewType(position)

    return if(itemType == loadingIndicatorType) {
        spanCount
    } else {
        1
    }
}

See #15

idanatz commented 4 years ago

Looks good.

a couple of final thins:

  1. Before addressing the next bullets, pull from develop branch, I've made local changes before your PR and pushed them a few hours ago, after that address the issues below.

  2. I know I wrote it but on seconds thought, change class UnsupportedClassException(msg: String = "Class must implement Diffable interface") : Throwable(msg) to class UnsupportedClassException(msg: String = "") : Throwable(msg) and move the msg "Class must implement Diffable interface" to the throw site. it's more understandable when reading the code to see the actual message.

  3. Why is fun getItemViewTypeFromClass(clazz: Class<*>): Int? returning Int? ? getCreatorUniqueIndex returns Int, it will never be nullable.

  4. add this documentation below /**

    • Retrieves the view type of an item with a given class.
    • Note that this class must implement the Diffable interface and the adapter must contain items of that class.
    • @throws UnsupportedClassException if the class does not implement the Diffable interface. / fun getItemViewTypeFromClass(clazz: Class<>): Int?

Note that there are some validations that need to be implemented now that the getItemViewType and getCreatorUniqueIndex are publicly accessible, but I will add them after this PR will be merged.

meruiden commented 4 years ago

Why is fun getItemViewTypeFromClass(clazz: Class<*>): Int? returning Int? ? getCreatorUniqueIndex returns Int, it will never be nullable.

Whoops that shouldn't be nullable indeed. I'll remove this

meruiden commented 4 years ago

Looks good.

a couple of final thins:

  1. Before addressing the next bullets, pull from develop branch, I've made local changes before your PR and pushed them a few hours ago, after that address the issues below.
  2. I know I wrote it but on seconds thought, change class UnsupportedClassException(msg: String = "Class must implement Diffable interface") : Throwable(msg) to class UnsupportedClassException(msg: String = "") : Throwable(msg) and move the msg "Class must implement Diffable interface" to the throw site. it's more understandable when reading the code to see the actual message.
  3. Why is fun getItemViewTypeFromClass(clazz: Class<*>): Int? returning Int? ? getCreatorUniqueIndex returns Int, it will never be nullable.
  4. add this documentation below /**

    • Retrieves the view type of an item with a given class.
    • Note that this class must implement the Diffable interface and the adapter must contain items of that class.
    • @throws UnsupportedClassException if the class does not implement the Diffable interface. / fun getItemViewTypeFromClass(clazz: Class<>): Int?

Note that there are some validations that need to be implemented now that the getItemViewType and getCreatorUniqueIndex are publicly accessible, but I will add them after this PR will be merged.

I pulled the latest changes from develop and changed the stuff you requested, merged all in to master and pushed.

idanatz commented 4 years ago

merged your PR, thanks! will be released as a minor version in a few days after il check everything is good.