-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added possibility to get view types from the adapter to add support for GridLayoutManager.SpanSizeLookup #16
Conversation
…or GridLayoutManager.SpanSizeLookup
…e used in GridLayoutManager.SpanSizeLookup
…om classes rather than just instances
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
Please see my comments
@@ -11,7 +11,7 @@ internal class ViewHolderCreatorsStore { | |||
holderCreators[clazz] = creator | |||
} | |||
|
|||
fun getCreatorUniqueIndex(clazz: Class<Diffable>): Int { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for this change?
Each item type the adapter holds must implement Diffable, it doesn't matter if its an internal holder type or external class from the user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I changed this is to allow calling the function like this:
getCreatorUniqueIndex(LoadingIndicator::class.java)
When the type is < Diffable > rather then <*> the compiler complains that LoadingIndicator is not a Diffable even though it is. This does work with instances but not when using LoadingIndicator::class.java
. You could do a runtime check maybe and throw if it doesn't inherit from Diffable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ViewHolderCreatorsStore is an internal class of the library, it should not be used with classes other than DIffable.
The way i see it, the function on OneAdapter.kt can be called with type < * >, but inside InternalAdapter, the function getItemViewTypeFromClass will validate the class if it is of type DIffable (using the Validator class) and throw a new exception (that will be written in the Exceptions.kt)
class UnsupportedClassException(msg: String = "Class must implement Diffable interface") : Throwable(msg)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pushed the changes, let me know what u think :)
@@ -352,6 +352,10 @@ internal class InternalAdapter(val recyclerView: RecyclerView) : RecyclerView.Ad | |||
} | |||
//endregion | |||
|
|||
fun getItemViewTypeFromClass(clazz: Class<*>): Int? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change to Class < Diffable > for the reason written in
ViewHolderCreatorsStore.kt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and place it below
fun getItemViewType
oneadapter/src/main/java/com/idanatz/oneadapter/external/holders/ExternalHolders.kt
Outdated
Show resolved
Hide resolved
@@ -79,6 +79,14 @@ class OneAdapter(recyclerView: RecyclerView) { | |||
} | |||
} | |||
|
|||
fun getItemViewType(position: Int): Int { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change to Class < Diffable > for the reason written in
ViewHolderCreatorsStore.kt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and place these functions at the end of the file
changed some leftover Class<*> to Class<Diffable> added some extension functions to item list
… getItemViewTypeFromClass
Looks good. a couple of final thins:
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. |
Whoops that shouldn't be nullable indeed. I'll remove this |
…e changes (some small refactoring) requested from the maintainer
I pulled the latest changes from develop and changed the stuff you requested, merged all in to master and pushed. |
merged your PR, thanks! |
Example usage:
See #15