Skip to content

Commit

Permalink
Added: API to get position of adapter item
Browse files Browse the repository at this point in the history
  • Loading branch information
idanatz committed Mar 13, 2023
1 parent b553171 commit d5a0198
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog
=========

Version 2.1.5
-------------
* Added: API to get position of adapter item

Version 2.1.4
-------------
* Fixed: metadata position was not correct sometimes
Expand Down
2 changes: 1 addition & 1 deletion RELEASING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Release Steps
2. Update CHANGELOG with changes.
3. Update README (features, latest dep version, etc...)
4. Clean project.
5. Tag release commit (x.x.x).
5. Commit & tag release commit (x.x.x).
6. Create release branch (vx.x.x).
7. Merge to master.
8. Create Git Release from the version tag.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@file:Suppress("ClassName")

package com.idanatz.oneadapter.tests.api.position

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.idanatz.oneadapter.external.modules.ItemModule
import com.idanatz.oneadapter.helpers.BaseTest
import com.idanatz.oneadapter.models.TestModel
import com.idanatz.oneadapter.test.R
import org.amshove.kluent.shouldEqualTo
import org.junit.Test
import org.junit.runner.RunWith

private const val POSITION_TO_SCROLL_TO = 10

@RunWith(AndroidJUnit4::class)
class WhenCallingGetItemPosition_ThenItemPosition_ShouldBeCorrect : BaseTest() {

@Test
fun test() {
configure {
val models = modelGenerator.generateModels(15)
var positionToScrollTo = -1

prepareOnActivity {
oneAdapter.attachItemModule(TestItemModule())
}
actOnActivity {
oneAdapter.add(models)
runWithDelay { // run with delay to let the items settle
positionToScrollTo = oneAdapter.getItemPosition(models[POSITION_TO_SCROLL_TO])
recyclerView.smoothScrollToPosition(positionToScrollTo)
}
}
untilAsserted {
oneAdapter.getItemPosition(models[0]) shouldEqualTo 0
positionToScrollTo shouldEqualTo POSITION_TO_SCROLL_TO
models[POSITION_TO_SCROLL_TO].onBindCalls shouldEqualTo 1
}
}
}

inner class TestItemModule : ItemModule<TestModel>() {
init {
config = modulesGenerator.generateValidItemModuleConfig(R.layout.test_model_large)
onBind { model, _, _ ->
model.onBindCalls++
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@file:Suppress("ClassName")

package com.idanatz.oneadapter.tests.api.position

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.idanatz.oneadapter.helpers.BaseTest
import org.amshove.kluent.shouldEqualTo
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class WhenPassingMissingItem_ThenGetItemPosition_ShouldReturnProperValue : BaseTest() {

@Test
fun test() {
configure {
var position = 0
act {
position = oneAdapter.getItemPosition(modelGenerator.generateModel())
}
assert {
position shouldEqualTo -1
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class WhenCallingGetItemViewTypeFromPosition_ThenItemViewTypes_ShouldBeCorrect :
}
}
actOnActivity {
runWithDelay(250) { oneAdapter.add(modelGenerator.generateDifferentModels(2)) }
oneAdapter.add(modelGenerator.generateDifferentModels(2))
}
untilAsserted {
oneAdapter.getItemViewType(0) shouldEqualTo 0
Expand Down

This file was deleted.

14 changes: 9 additions & 5 deletions oneadapter/src/main/java/com/idanatz/oneadapter/OneAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,18 @@ class OneAdapter(recyclerView: RecyclerView) {

/**
* Retrieves the view type associated with the items of the 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
* or is not registered as an Module data type.
* @throws MissingModuleDefinitionException if the class is not registered as an Module data type.
*/
fun getItemViewTypeFromClass(clazz: Class<*>): Int {
return internalAdapter.getItemViewTypeFromClass(clazz)
fun <M : Diffable> getItemViewTypeFromClass(ofClass: Class<M>): Int {
return internalAdapter.getItemViewTypeFromClass(ofClass)
}

/**
* Returns the index of the first occurrence of the specified item in the adapter, or -1 if the specified
* item is not contained in the adapter.
*/
fun getItemPosition(item: Diffable): Int = internalAdapter.getItemPosition(item)

fun registerAdapterDataObserver(observer: RecyclerView.AdapterDataObserver) {
internalAdapter.registerAdapterDataObserver(observer)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,6 @@ internal class InternalAdapter(val recyclerView: RecyclerView) : RecyclerView.Ad

override fun getItemViewType(position: Int) = viewHolderCreatorsStore.getCreatorUniqueIndex(data[position].javaClass)

fun getItemViewTypeFromClass(clazz: Class<*>): Int {
Validator.validateModelClassIsDiffable(clazz)
return viewHolderCreatorsStore.getCreatorUniqueIndex(clazz as Class<Diffable>)
}

override fun onViewRecycled(holder: OneViewHolder<Diffable>) {
super.onViewRecycled(holder)
holder.onUnbind(holder.model)
Expand Down Expand Up @@ -374,5 +369,9 @@ internal class InternalAdapter(val recyclerView: RecyclerView) : RecyclerView.Ad
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
oneScrollListener?.let { recyclerView.removeOnScrollListener(it) }
}
//endregion
//endregion

fun <M : Diffable> getItemViewTypeFromClass(clazz: Class<M>): Int = viewHolderCreatorsStore.getCreatorUniqueIndex(clazz as Class<Diffable>)

fun getItemPosition(item: Diffable) = data.indexOf(item)
}
2 changes: 1 addition & 1 deletion versions.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {
MAJOR_VERSION = 2
MINOR_VERSION = 1
HOTFIX_VERSION = 4
HOTFIX_VERSION = 5

VERSION_NAME = "${MAJOR_VERSION}.${MINOR_VERSION}.${HOTFIX_VERSION}"
VERSION_CODE = MAJOR_VERSION * 10000 + MINOR_VERSION * 100 + HOTFIX_VERSION
Expand Down

0 comments on commit d5a0198

Please sign in to comment.