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

Ability to use Unique Identifier of String Type #20

Closed mrhammadasif closed 4 years ago

mrhammadasif commented 4 years ago

The ability to use the Unique Identifier of Long Type can't satisfy every situation.

For example, the data which is Synced with Firebase (Realtime Database) can't have IDs Long values. Support Dynamic Type Support in Diffable

Diffable<Long> or Diffable<String>

idanatz commented 4 years ago

This is a little problematic due to the face that the adapter is using this unique identifier also to generate a holder item id (which has to be Long). The item id has to be unique in order for the RecyclerView holder selection to work properly.

If the user will supply a String as an identifier the adapter will have to convert it to Long somehow. Strings are infinite and Long are finite so collisions will happen in every form of transformation. Therefore, I think that this transformation needs to be performed on the user code and not as a feature as its not 100% safe.

You can use the hashcode and function followed by toLong(), the chances that the adapter holds 2 items with the same hashcode Firebase ID is pretty low. Of course, you can use a better way than hashcode to minizine the collision chance, it's up to you.