Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
data class ExampleItem1(
val icon: String,
val text: String,
override val uniqueProperty: Any = text
) : ListItem
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
data class ExampleItem2(
val icon: String,
val text1: String,
val text2: String,
override val uniqueProperty: Any = text1
) : ListItem
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
data class ExampleItem3(
val icon1: String,
val text: String,
val icon2: String,
override val uniqueProperty: Any = text
) : ListItem
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>?,
var emails: List<String>?) : ListItem {

override fun getUniqueProperty() = id
}
data class ContactItem(
val id: Long,
val displayName: String?,
val isFavorite: Boolean,
val photo: Uri?,
var phones: List<String>?,
var emails: List<String>?,
override val uniqueProperty: Any = id
) : ListItem
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
data class HeaderItem(
val title: String,
override val uniqueProperty: Any = title
) : ListItem
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class ParentAdapterDelegate(

override fun onViewDetachedFromWindow(holder: RecyclerView.ViewHolder) {
holder.withAdapterPosition<ListItem> { item, _ ->
disposables[item.getUniqueProperty()]?.clear()
disposables[item.uniqueProperty]?.clear()
}
}

Expand All @@ -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 }
}
12 changes: 7 additions & 5 deletions core-adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand All @@ -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()
}
}
12 changes: 8 additions & 4 deletions core-adapter/src/test/java/com/nullgr/core/adapter/TestItems.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
open class BaseTestItem : ListItem {
override val uniqueProperty: Any = this::class.toString()
}

class TestItem1 : BaseTestItem()
class TestItem2 : BaseTestItem()
class TestItem : BaseTestItem()
class ItemTest : BaseTestItem()