From 8e48c396e86a38945b01be5855d39bbf0ec3ce31 Mon Sep 17 00:00:00 2001 From: Idan Atsmon Date: Mon, 24 Jul 2023 11:07:39 +0300 Subject: [PATCH] Added: Metadata to query if an item is first or last --- CHANGELOG.md | 4 ++ .../idanatz/oneadapter/helpers/BaseTest.kt | 2 - ...IsFirstMetadata_ShouldBeTrueOnFirstItem.kt | 46 +++++++++++++++++++ ...emIsLastMetadata_ShouldBeTrueOnLastItem.kt | 45 ++++++++++++++++++ .../oneadapter/internal/InternalAdapter.kt | 7 ++- .../oneadapter/internal/holders/Metadata.kt | 2 + versions.gradle | 2 +- 7 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/modules/item/metadata/WhenOnBindInvoked_ThenItemIsFirstMetadata_ShouldBeTrueOnFirstItem.kt create mode 100644 oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/modules/item/metadata/WhenOnBindInvoked_ThenItemIsLastMetadata_ShouldBeTrueOnLastItem.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index f00b1d3..3a35aef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ Changelog ========= +Version 2.1.6 +------------- +* Added: isFirst and isLast to Metadata + Version 2.1.5 ------------- * Added: API to get position of adapter item diff --git a/oneadapter/src/androidTest/java/com/idanatz/oneadapter/helpers/BaseTest.kt b/oneadapter/src/androidTest/java/com/idanatz/oneadapter/helpers/BaseTest.kt index 317d403..c1701b3 100644 --- a/oneadapter/src/androidTest/java/com/idanatz/oneadapter/helpers/BaseTest.kt +++ b/oneadapter/src/androidTest/java/com/idanatz/oneadapter/helpers/BaseTest.kt @@ -6,10 +6,8 @@ import androidx.lifecycle.Lifecycle import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.idanatz.oneadapter.generators.ModelGenerator -import org.junit.runner.RunWith import org.junit.Before import androidx.test.ext.junit.rules.activityScenarioRule -import androidx.test.ext.junit.runners.AndroidJUnit4 import com.idanatz.oneadapter.OneAdapter import com.idanatz.oneadapter.dsl.TestBuilder import com.idanatz.oneadapter.generators.ModulesGenerator diff --git a/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/modules/item/metadata/WhenOnBindInvoked_ThenItemIsFirstMetadata_ShouldBeTrueOnFirstItem.kt b/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/modules/item/metadata/WhenOnBindInvoked_ThenItemIsFirstMetadata_ShouldBeTrueOnFirstItem.kt new file mode 100644 index 0000000..4d51053 --- /dev/null +++ b/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/modules/item/metadata/WhenOnBindInvoked_ThenItemIsFirstMetadata_ShouldBeTrueOnFirstItem.kt @@ -0,0 +1,46 @@ +@file:Suppress("ClassName") + +package com.idanatz.oneadapter.tests.modules.item.metadata + +import android.util.Log +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.models.TestModel1 +import com.idanatz.oneadapter.test.R +import org.amshove.kluent.shouldBe +import org.amshove.kluent.shouldContainSame +import org.amshove.kluent.shouldEqualTo +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class WhenOnBindInvoked_ThenItemIsFirstMetadata_ShouldBeTrueOnFirstItem : BaseTest() { + + private var firstCondition = booleanArrayOf(false, false, false, false, false) + + @Test + fun test() { + configure { + prepareOnActivity { + oneAdapter.run { + attachItemModule(TestItemModule()) + setItems(modelGenerator.generateModels(5).toMutableList()) + } + } + untilAsserted { + firstCondition shouldContainSame booleanArrayOf(true, false, false, false, false) + } + } + } + + inner class TestItemModule : ItemModule() { + init { + config = modulesGenerator.generateValidItemModuleConfig(R.layout.test_model_small) + onBind { model, _, metadata -> + firstCondition[metadata.position] = metadata.isFirst + } + } + } +} \ No newline at end of file diff --git a/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/modules/item/metadata/WhenOnBindInvoked_ThenItemIsLastMetadata_ShouldBeTrueOnLastItem.kt b/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/modules/item/metadata/WhenOnBindInvoked_ThenItemIsLastMetadata_ShouldBeTrueOnLastItem.kt new file mode 100644 index 0000000..f6775c9 --- /dev/null +++ b/oneadapter/src/androidTest/java/com/idanatz/oneadapter/tests/modules/item/metadata/WhenOnBindInvoked_ThenItemIsLastMetadata_ShouldBeTrueOnLastItem.kt @@ -0,0 +1,45 @@ +@file:Suppress("ClassName") + +package com.idanatz.oneadapter.tests.modules.item.metadata + +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.models.TestModel1 +import com.idanatz.oneadapter.test.R +import org.amshove.kluent.shouldBe +import org.amshove.kluent.shouldContainSame +import org.amshove.kluent.shouldEqualTo +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class WhenOnBindInvoked_ThenItemIsLastMetadata_ShouldBeTrueOnLastItem : BaseTest() { + + private var lastCondition = booleanArrayOf(false, false, false, false, false) + + @Test + fun test() { + configure { + prepareOnActivity { + oneAdapter.run { + attachItemModule(TestItemModule()) + setItems(modelGenerator.generateModels(5).toMutableList()) + } + } + untilAsserted { + lastCondition shouldContainSame booleanArrayOf(false, false, false, false, true) + } + } + } + + inner class TestItemModule : ItemModule() { + init { + config = modulesGenerator.generateValidItemModuleConfig(R.layout.test_model_small) + onBind { model, _, metadata -> + lastCondition[metadata.position] = metadata.isLast + } + } + } +} \ No newline at end of file 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 e02b3de..ef7097e 100644 --- a/oneadapter/src/main/java/com/idanatz/oneadapter/internal/InternalAdapter.kt +++ b/oneadapter/src/main/java/com/idanatz/oneadapter/internal/InternalAdapter.kt @@ -12,6 +12,7 @@ import com.idanatz.oneadapter.external.interfaces.Diffable import com.idanatz.oneadapter.external.modules.* import com.idanatz.oneadapter.internal.diffing.OneDiffUtil import com.idanatz.oneadapter.internal.holders.* +import com.idanatz.oneadapter.internal.holders.Metadata as HolderMetadata import com.idanatz.oneadapter.internal.holders.OneViewHolder import com.idanatz.oneadapter.internal.holders_creators.ViewHolderCreator import com.idanatz.oneadapter.internal.holders_creators.ViewHolderCreatorsStore @@ -109,9 +110,11 @@ internal class InternalAdapter(val recyclerView: RecyclerView) : RecyclerView.Ad override fun onBindViewHolder(holder: OneViewHolder, position: Int) { val model = data[position] val isFirstBind = holderPositionHandler.isFirstBind(holder.itemViewType, position) - val metadata = Metadata( - position = holder.adapterPosition, // don't use position variable, caused bugs with swiping + val metadata = HolderMetadata( + position = holder.adapterPosition, // don't use position variable for future use, caused bugs with swiping isRebinding = !isFirstBind && !recyclerView.isScrolling, + isFirst = position == 0, + isLast = position == itemCount - 1, animationMetadata = object : AnimationMetadata { override val isAnimatingFirstBind: Boolean = if (holder.firstBindAnimation != null) isFirstBind else false }, diff --git a/oneadapter/src/main/java/com/idanatz/oneadapter/internal/holders/Metadata.kt b/oneadapter/src/main/java/com/idanatz/oneadapter/internal/holders/Metadata.kt index 3986d65..66e388a 100644 --- a/oneadapter/src/main/java/com/idanatz/oneadapter/internal/holders/Metadata.kt +++ b/oneadapter/src/main/java/com/idanatz/oneadapter/internal/holders/Metadata.kt @@ -5,6 +5,8 @@ import com.idanatz.oneadapter.external.event_hooks.SwipeEventHook data class Metadata( val position: Int = -1, val isRebinding: Boolean, + val isFirst: Boolean, + val isLast: Boolean, private val animationMetadata: AnimationMetadata? = null, private val selectionMetadata: SelectionMetadata? = null, private val swipeMetadata: SwipeMetadata? = null diff --git a/versions.gradle b/versions.gradle index 364685d..ead058c 100755 --- a/versions.gradle +++ b/versions.gradle @@ -1,7 +1,7 @@ ext { MAJOR_VERSION = 2 MINOR_VERSION = 1 - HOTFIX_VERSION = 5 + HOTFIX_VERSION = 6 VERSION_NAME = "${MAJOR_VERSION}.${MINOR_VERSION}.${HOTFIX_VERSION}" VERSION_CODE = MAJOR_VERSION * 10000 + MINOR_VERSION * 100 + HOTFIX_VERSION