diff --git a/CHANGELOG.md b/CHANGELOG.md index ad25ed8..f00b1d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/RELEASING.md b/RELEASING.md index e2787dd..d5cdaaf 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -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. \ No newline at end of file diff --git a/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/api/position/WhenCallingGetItemPosition_ThenItemPosition_ShouldBeCorrect.kt b/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/api/position/WhenCallingGetItemPosition_ThenItemPosition_ShouldBeCorrect.kt new file mode 100644 index 0000000..51a48f7 --- /dev/null +++ b/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/api/position/WhenCallingGetItemPosition_ThenItemPosition_ShouldBeCorrect.kt @@ -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() { + init { + config = modulesGenerator.generateValidItemModuleConfig(R.layout.test_model_large) + onBind { model, _, _ -> + model.onBindCalls++ + } + } + } +} \ No newline at end of file diff --git a/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/api/position/WhenPassingMissingItem_ThenGetItemPosition_ShouldReturnProperValue.kt b/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/api/position/WhenPassingMissingItem_ThenGetItemPosition_ShouldReturnProperValue.kt new file mode 100644 index 0000000..5b0cd6c --- /dev/null +++ b/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/api/position/WhenPassingMissingItem_ThenGetItemPosition_ShouldReturnProperValue.kt @@ -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 + } + } + } +} \ No newline at end of file diff --git a/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/api/view_type/WhenCallingGetItemViewTypeFromPosition_ThenItemViewTypes_ShouldBeCorrect.kt b/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/api/view_type/WhenCallingGetItemViewTypeFromPosition_ThenItemViewTypes_ShouldBeCorrect.kt index 2091382..55352cd 100644 --- a/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/api/view_type/WhenCallingGetItemViewTypeFromPosition_ThenItemViewTypes_ShouldBeCorrect.kt +++ b/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/api/view_type/WhenCallingGetItemViewTypeFromPosition_ThenItemViewTypes_ShouldBeCorrect.kt @@ -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 diff --git a/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/api/view_type/WhenPassingNotDiffableClass_ThenGetItemViewTypeFromClass_ShouldThrowException.kt b/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/api/view_type/WhenPassingNotDiffableClass_ThenGetItemViewTypeFromClass_ShouldThrowException.kt deleted file mode 100644 index ccb7174..0000000 --- a/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/api/view_type/WhenPassingNotDiffableClass_ThenGetItemViewTypeFromClass_ShouldThrowException.kt +++ /dev/null @@ -1,22 +0,0 @@ -@file:Suppress("ClassName") - -package com.idanatz.oneadapter.tests.api.view_type - -import androidx.test.ext.junit.runners.AndroidJUnit4 -import com.idanatz.oneadapter.external.UnsupportedClassException -import com.idanatz.oneadapter.helpers.BaseTest -import org.junit.Test -import org.junit.runner.RunWith - -@RunWith(AndroidJUnit4::class) -class WhenPassingNotDiffableClass_ThenGetItemViewTypeFromClass_ShouldThrowException : BaseTest() { - - @Test(expected = UnsupportedClassException::class) - fun test() { - configure { - act { - oneAdapter.getItemViewTypeFromClass(String::class.java) - } - } - } -} \ No newline at end of file diff --git a/oneadapter/src/main/java/com/idanatz/oneadapter/OneAdapter.kt b/oneadapter/src/main/java/com/idanatz/oneadapter/OneAdapter.kt index fb59b62..d6f78e7 100644 --- a/oneadapter/src/main/java/com/idanatz/oneadapter/OneAdapter.kt +++ b/oneadapter/src/main/java/com/idanatz/oneadapter/OneAdapter.kt @@ -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 getItemViewTypeFromClass(ofClass: Class): 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) } diff --git a/oneadapter/src/main/java/com/idanatz/oneadapter/internal/InternalAdapter.kt b/oneadapter/src/main/java/com/idanatz/oneadapter/internal/InternalAdapter.kt index 9a36991..e02b3de 100644 --- a/oneadapter/src/main/java/com/idanatz/oneadapter/internal/InternalAdapter.kt +++ b/oneadapter/src/main/java/com/idanatz/oneadapter/internal/InternalAdapter.kt @@ -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) - } - override fun onViewRecycled(holder: OneViewHolder) { super.onViewRecycled(holder) holder.onUnbind(holder.model) @@ -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 getItemViewTypeFromClass(clazz: Class): Int = viewHolderCreatorsStore.getCreatorUniqueIndex(clazz as Class) + + fun getItemPosition(item: Diffable) = data.indexOf(item) } \ No newline at end of file diff --git a/versions.gradle b/versions.gradle index da4b5b5..364685d 100755 --- a/versions.gradle +++ b/versions.gradle @@ -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