Skip to content

Commit

Permalink
add "update single component" api to ComponentAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
flopshot committed May 6, 2020
1 parent 68c5d84 commit 2f2bfad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions example/src/main/java/com/seannajera/dkouple/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.seannajera.dkouple

import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

private val tag = "Dkouple_MainActivity"

private val componentFactory: ComponentFactory = MainComponentFactory()

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -22,5 +27,20 @@ class MainActivity : AppCompatActivity() {
ItemComponent("1", "First Item")
)
)

Handler(Looper.getMainLooper()).postDelayed({

val firstItemUpdated = componentAdapter.updateComponent(
ItemComponent("1", "Still First Item")
)

val nonExistentItemUpdated = componentAdapter.updateComponent(
ItemComponent("2", "I am not in the components")
)

Log.i(tag, "First item was updated: $firstItemUpdated") // "First item was updated: true"
Log.i(tag, "Non existent item was updated: $nonExistentItemUpdated") // "Non existent item was updated: false"

}, 5000)
}
}
17 changes: 17 additions & 0 deletions library/src/main/java/com/seannajera/dkouple/ComponentAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ class ComponentAdapter(private val componentFactory: ComponentFactory) :

fun applyComponents(components: List<Component>) = submitList(components)

fun updateComponent(component: Component): Boolean {
var isComponentInList = false

val updatedComponentList = currentList.map {
if (it.id == component.id) {
isComponentInList = true
return@map component
} else {
return@map it
}
}

if (isComponentInList) submitList(updatedComponentList)

return isComponentInList
}

companion object {
val componentDiffer = object : DiffUtil.ItemCallback<Component>() {
override fun areItemsTheSame(
Expand Down

0 comments on commit 2f2bfad

Please sign in to comment.