Skip to content

Commit

Permalink
Added: Metadata to query if an item is first or last
Browse files Browse the repository at this point in the history
  • Loading branch information
idanatz committed Jul 24, 2023
1 parent d5a0198 commit 8e48c39
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 5 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.6
-------------
* Added: isFirst and isLast to Metadata

Version 2.1.5
-------------
* Added: API to get position of adapter item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<TestModel>() {
init {
config = modulesGenerator.generateValidItemModuleConfig(R.layout.test_model_small)
onBind { model, _, metadata ->
firstCondition[metadata.position] = metadata.isFirst
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<TestModel>() {
init {
config = modulesGenerator.generateValidItemModuleConfig(R.layout.test_model_small)
onBind { model, _, metadata ->
lastCondition[metadata.position] = metadata.isLast
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -109,9 +110,11 @@ internal class InternalAdapter(val recyclerView: RecyclerView) : RecyclerView.Ad
override fun onBindViewHolder(holder: OneViewHolder<Diffable>, 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
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 = 5
HOTFIX_VERSION = 6

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

0 comments on commit 8e48c39

Please sign in to comment.