Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
fix: RecyclerView id access from ListView (#1489)
Browse files Browse the repository at this point in the history
Signed-off-by: Matheus Ribeiro <[email protected]>
  • Loading branch information
matheusribeirozup authored Apr 15, 2021
1 parent fbac822 commit 12b533c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ constructor(
observeBindChanges(rootView, recyclerView, dataSource!!) { value ->
canScrollEnd = true
val adapter = recyclerView.adapter as ListAdapter
adapter.setList(value)
adapter.setList(value, this.id)
if (value?.isEmpty() == true) {
executeScrollEndActions()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import br.com.zup.beagle.android.context.AsyncActionData
import br.com.zup.beagle.android.context.normalizeContextValue
import br.com.zup.beagle.android.data.serializer.BeagleSerializer
import br.com.zup.beagle.android.utils.setIsAutoGenerateIdEnabled
import br.com.zup.beagle.android.utils.toAndroidId
import br.com.zup.beagle.android.view.ViewFactory
import br.com.zup.beagle.core.ServerDrivenComponent

Expand Down Expand Up @@ -155,11 +156,11 @@ internal class ListAdapter(
holder.onViewAttachedToWindow()
}

fun setList(list: List<Any>?) {
fun setList(list: List<Any>?, componentId: String? = null) {
list?.let {
if (list != listItems) {
clearAdapterContent()
notifyListViewIdViewModel(listItems.isEmpty())
notifyListViewIdViewModel(listItems.isEmpty(), componentId)
listItems = list
adapterItems = list.map { ListItem(data = it.normalizeContextValue()) }
notifyDataSetChanged()
Expand All @@ -172,10 +173,10 @@ internal class ListAdapter(
createdViewHolders.clear()
}

private fun notifyListViewIdViewModel(adapterPreviouslyEmpty: Boolean) {
private fun notifyListViewIdViewModel(adapterPreviouslyEmpty: Boolean, componentId: String?) {
listViewModels
.listViewIdViewModel
.createSingleManagerByListViewId(getRecyclerId(), adapterPreviouslyEmpty)
.createSingleManagerByListViewId(getRecyclerId(componentId), adapterPreviouslyEmpty)
}

private fun clearList() {
Expand All @@ -192,10 +193,11 @@ internal class ListAdapter(
}
}

private fun getRecyclerId(): Int {
return recyclerId.takeIf {
private fun getRecyclerId(componentId: String?): Int {
val id = recyclerId.takeIf {
it != View.NO_ID
} ?: createTempId()
} ?: componentId?.toAndroidId()
return id ?: createTempId()
}

private fun createTempId(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ internal class ListViewHolder(
position: Int,
recyclerId: Int
) {
val itemViewId = bindIdToViewModel(itemView, isRecycled, position, recyclerId)
val itemViewId = bindIdToViewModel(position, recyclerId)
setUpdatedIdToViewAndManagers(itemView, itemViewId, listItem, isRecycled)

viewsWithId.forEach { (id, view) ->
Expand All @@ -223,14 +223,14 @@ internal class ListViewHolder(

val viewsWithContextAndWithoutId = viewsWithContext.filterNot { viewsWithId.containsValue(it) }
viewsWithContextAndWithoutId.forEach { view ->
val subViewId = bindIdToViewModel(view, isRecycled, position, recyclerId)
val subViewId = bindIdToViewModel(position, recyclerId)
setUpdatedIdToViewAndManagers(view, subViewId, listItem, isRecycled)
}

val viewsWithOnInitAndWithoutIdAndContext =
viewsWithOnInit.filterNot { viewsWithId.containsValue(it) || viewsWithContext.contains(it) }
viewsWithOnInitAndWithoutIdAndContext.forEach { view ->
val subViewId = bindIdToViewModel(view, isRecycled, position, recyclerId)
val subViewId = bindIdToViewModel(position, recyclerId)
setUpdatedIdToViewAndManagers(view, subViewId, listItem, isRecycled)
}

Expand All @@ -241,12 +241,12 @@ internal class ListViewHolder(
viewsWithOnInit.contains(it)
}
.forEach { innerRecyclerWithoutId ->
val subViewId = bindIdToViewModel(innerRecyclerWithoutId, isRecycled, position, recyclerId)
val subViewId = bindIdToViewModel(position, recyclerId)
setUpdatedIdToViewAndManagers(innerRecyclerWithoutId, subViewId, listItem, isRecycled)
}
}

private fun bindIdToViewModel(view: View, isRecycled: Boolean, position: Int, recyclerId: Int): Int {
private fun bindIdToViewModel(position: Int, recyclerId: Int): Int {
return listViewModels.listViewIdViewModel.getViewId(recyclerId, position)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package br.com.zup.beagle.automatedtests.builders

import br.com.zup.beagle.automatedtests.model.Genre
import br.com.zup.beagle.core.Style
import br.com.zup.beagle.ext.applyFlex
import br.com.zup.beagle.ext.applyStyle
Expand Down Expand Up @@ -51,10 +52,6 @@ data class PageResponse(
var result: Bind<List<Any>>
)

data class GenreResponse(
var genres: Any? = null
)

data class CategoryResponse(
var category: Any? = null
)
Expand Down Expand Up @@ -209,20 +206,6 @@ object ListViewScreenBuilder {
)

private fun secondListView() = Container(
context = ContextData(id = "genreResponse", value = GenreResponse()),
onInit = listOf(
SendRequest(
url = "/book-database/categories",
onSuccess = listOf(
SetContext(
contextId = "genreResponse",
value = GenreResponse(
genres = "@{onSuccess.data}"
)
)
)
)
),
children = listOf(
Text("Categories List View (nested)")
.applyStyle(
Expand All @@ -240,7 +223,7 @@ object ListViewScreenBuilder {
return ListView(
key = "id",
direction = ListDirection.VERTICAL,
dataSource = expressionOf("@{genreResponse.genres}"),
dataSource = valueOf(Genre.createMock()),
template = Container(
context = ContextData(id = "categoryResponse", value = CategoryResponse()),
children = listOf(
Expand Down

0 comments on commit 12b533c

Please sign in to comment.