diff --git a/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItem1.kt b/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItem1.kt index c1cbcbf..75e9b19 100644 --- a/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItem1.kt +++ b/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItem1.kt @@ -5,6 +5,8 @@ import com.nullgr.core.adapter.items.ListItem /** * @author vchernyshov. */ -data class ExampleItem1(val icon: String, val text: String): ListItem { - override fun getUniqueProperty(): Any = text -} \ No newline at end of file +data class ExampleItem1( + val icon: String, + val text: String, + override val uniqueProperty: Any = text +) : ListItem \ No newline at end of file diff --git a/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItem2.kt b/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItem2.kt index 766e1b1..a7420ee 100644 --- a/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItem2.kt +++ b/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItem2.kt @@ -5,6 +5,9 @@ import com.nullgr.core.adapter.items.ListItem /** * @author vchernyshov. */ -data class ExampleItem2(val icon: String, val text1: String, val text2: String) : ListItem { - override fun getUniqueProperty(): Any = text1 -} \ No newline at end of file +data class ExampleItem2( + val icon: String, + val text1: String, + val text2: String, + override val uniqueProperty: Any = text1 +) : ListItem \ No newline at end of file diff --git a/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItem3.kt b/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItem3.kt index 7280c8c..f3e4de5 100644 --- a/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItem3.kt +++ b/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItem3.kt @@ -5,6 +5,9 @@ import com.nullgr.core.adapter.items.ListItem /** * @author vchernyshov. */ -data class ExampleItem3(val icon1: String, val text: String, val icon2: String) : ListItem { - override fun getUniqueProperty(): Any = text -} \ No newline at end of file +data class ExampleItem3( + val icon1: String, + val text: String, + val icon2: String, + override val uniqueProperty: Any = text +) : ListItem \ No newline at end of file diff --git a/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItemWithPayloads.kt b/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItemWithPayloads.kt index 25f047c..3379898 100644 --- a/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItemWithPayloads.kt +++ b/app/src/main/java/com/nullgr/androidcore/adapter/items/ExampleItemWithPayloads.kt @@ -6,7 +6,12 @@ import com.nullgr.core.adapter.items.ListItem /** * @author chernyshov. */ -data class ExampleItemWithPayloads(val title: String, val subTitle: String, @ColorInt val color: Int) : ListItem { +data class ExampleItemWithPayloads( + val title: String, + val subTitle: String, + @ColorInt val color: Int, + override val uniqueProperty: Any = title +) : ListItem { override fun getChangePayload(other: ListItem): Any { if (this::class == other::class) { diff --git a/app/src/main/java/com/nullgr/androidcore/interactor/presentation/adapter/items/InteractorItem.kt b/app/src/main/java/com/nullgr/androidcore/interactor/presentation/adapter/items/InteractorItem.kt index f4002e4..a9e1ba1 100644 --- a/app/src/main/java/com/nullgr/androidcore/interactor/presentation/adapter/items/InteractorItem.kt +++ b/app/src/main/java/com/nullgr/androidcore/interactor/presentation/adapter/items/InteractorItem.kt @@ -5,7 +5,11 @@ import com.nullgr.core.adapter.items.ListItem /** * @author chernyshov. */ -data class InteractorItem(val type: Type, val title: String) : ListItem { +data class InteractorItem( + val type: Type, + val title: String, + override val uniqueProperty: Any = type +) : ListItem { enum class Type { OBSERVABLE, OBSERVABLE_LIST, diff --git a/app/src/main/java/com/nullgr/androidcore/rxcontacts/items/ContactItem.kt b/app/src/main/java/com/nullgr/androidcore/rxcontacts/items/ContactItem.kt index 707b029..7b026ea 100644 --- a/app/src/main/java/com/nullgr/androidcore/rxcontacts/items/ContactItem.kt +++ b/app/src/main/java/com/nullgr/androidcore/rxcontacts/items/ContactItem.kt @@ -6,12 +6,12 @@ import com.nullgr.core.adapter.items.ListItem /** * Created by Grishko Nikita on 01.02.18. */ -data class ContactItem(val id: Long, - val displayName: String?, - val isFavorite: Boolean, - val photo: Uri?, - var phones: List?, - var emails: List?) : ListItem { - - override fun getUniqueProperty() = id -} \ No newline at end of file +data class ContactItem( + val id: Long, + val displayName: String?, + val isFavorite: Boolean, + val photo: Uri?, + var phones: List?, + var emails: List?, + override val uniqueProperty: Any = id +) : ListItem \ No newline at end of file diff --git a/app/src/main/java/com/nullgr/androidcore/rxcontacts/items/HeaderItem.kt b/app/src/main/java/com/nullgr/androidcore/rxcontacts/items/HeaderItem.kt index 21f8997..86bf751 100644 --- a/app/src/main/java/com/nullgr/androidcore/rxcontacts/items/HeaderItem.kt +++ b/app/src/main/java/com/nullgr/androidcore/rxcontacts/items/HeaderItem.kt @@ -5,6 +5,7 @@ import com.nullgr.core.adapter.items.ListItem /** * Created by Grishko Nikita on 01.02.18. */ -data class HeaderItem(val title: String) : ListItem { - override fun getUniqueProperty() = title -} \ No newline at end of file +data class HeaderItem( + val title: String, + override val uniqueProperty: Any = title +) : ListItem \ No newline at end of file diff --git a/core-adapter-ktx/src/main/java/com/nullgr/core/adapter/ktx/ParentAdapterDelegate.kt b/core-adapter-ktx/src/main/java/com/nullgr/core/adapter/ktx/ParentAdapterDelegate.kt index 7ef845f..cea0aff 100644 --- a/core-adapter-ktx/src/main/java/com/nullgr/core/adapter/ktx/ParentAdapterDelegate.kt +++ b/core-adapter-ktx/src/main/java/com/nullgr/core/adapter/ktx/ParentAdapterDelegate.kt @@ -19,7 +19,7 @@ abstract class ParentAdapterDelegate( override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) { holder.withAdapterPosition { item, _ -> - disposables[item.getUniqueProperty()]?.clear() + disposables[item.uniqueProperty]?.clear() } } @@ -36,10 +36,10 @@ abstract class ParentAdapterDelegate( } } - protected open fun createOrGetAdapter(item: ListItem): DynamicAdapter = adapters[item.getUniqueProperty()] - ?: DynamicAdapter(factory).also { adapters[item.getUniqueProperty()] = it } + protected open fun createOrGetAdapter(item: ListItem): DynamicAdapter = adapters[item.uniqueProperty] + ?: DynamicAdapter(factory).also { adapters[item.uniqueProperty] = it } protected open fun createOrGetCompositeDisposable(item: ListItem): CompositeDisposable = - disposables[item.getUniqueProperty()] - ?: CompositeDisposable().also { disposables[item.getUniqueProperty()] = it } + disposables[item.uniqueProperty] + ?: CompositeDisposable().also { disposables[item.uniqueProperty] = it } } \ No newline at end of file diff --git a/core-adapter/README.md b/core-adapter/README.md index 8eba817..b92aba9 100644 --- a/core-adapter/README.md +++ b/core-adapter/README.md @@ -31,18 +31,20 @@ class SomeDelegatesFactory : AdapterDelegatesFactory { ``` For ```ListItem``` subclasses we recommend to use Kotlin data classes. -You need to override ```ListItem.getUniqueProperty``` if your list could contains more that one -element of same ```ListItem```. If you need to updated some view that represents one of item property using DiffUtils - override ```ListItem.getChangePayload```. Example of ```ListItem``` with payloads: ```kotlin -data class SomeItemWithPayloads(val id: String, val title: String, val subTitle: String, @ColorInt val color: Int) : ListItem { +data class SomeItemWithPayloads( + val id: String, + val title: String, + val subTitle: String, + @ColorInt val color: Int, + override val uniqueProperty: Any = id +) : ListItem { - override fun getUniqueProperty(): Any = id - override fun getChangePayload(other: ListItem): Any { if (this::class == other::class) { other as ExampleItemWithPayloads diff --git a/core-adapter/src/main/java/com/nullgr/core/adapter/items/ListItem.kt b/core-adapter/src/main/java/com/nullgr/core/adapter/items/ListItem.kt index 5a99e42..516f961 100644 --- a/core-adapter/src/main/java/com/nullgr/core/adapter/items/ListItem.kt +++ b/core-adapter/src/main/java/com/nullgr/core/adapter/items/ListItem.kt @@ -9,13 +9,19 @@ package com.nullgr.core.adapter.items */ interface ListItem { + /** + * Used in [areItemsTheSame] as an extra check for item uniqueness. + * You should return some unique property (id that won't change) for this item. + */ + val uniqueProperty: Any + /** * Called by the [com.nullgr.core.adapter.Callback] to decide whether two object represent the same item. - * Method makes check by two parameters: class of item and unique property returned by [getUniqueProperty]. + * Method makes check by two parameters: class of item and [uniqueProperty]. * In most cases you should not override this method and use default implementation. */ fun areItemsTheSame(other: ListItem): Boolean { - return this::class == other::class && this.getUniqueProperty() == other.getUniqueProperty() + return this::class == other::class && this.uniqueProperty == other.uniqueProperty } /** @@ -35,13 +41,4 @@ interface ListItem { fun getChangePayload(other: ListItem): Any { return Unit } - - /** - * Called in [areItemsTheSame] as a extra check for item uniquely. - * By default returns string representation of item class. - * If you list contains multiple items of same class you should override this method and return some unique property. - */ - fun getUniqueProperty(): Any { - return this::class.toString() - } } \ No newline at end of file diff --git a/core-adapter/src/test/java/com/nullgr/core/adapter/TestItems.kt b/core-adapter/src/test/java/com/nullgr/core/adapter/TestItems.kt index 4bfebe5..8b398f1 100644 --- a/core-adapter/src/test/java/com/nullgr/core/adapter/TestItems.kt +++ b/core-adapter/src/test/java/com/nullgr/core/adapter/TestItems.kt @@ -2,7 +2,11 @@ package com.nullgr.core.adapter import com.nullgr.core.adapter.items.ListItem -class TestItem1: ListItem -class TestItem2: ListItem -class TestItem: ListItem -class ItemTest: ListItem \ No newline at end of file +open class BaseTestItem : ListItem { + override val uniqueProperty: Any = this::class.toString() +} + +class TestItem1 : BaseTestItem() +class TestItem2 : BaseTestItem() +class TestItem : BaseTestItem() +class ItemTest : BaseTestItem() \ No newline at end of file