Skip to content

Commit

Permalink
[fix]: possible crash of getting current item in not visible view holder
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Oct 12, 2023
1 parent 0d310f9 commit 69f51c3
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ abstract class BaseViewModelFragment<T : BaseViewModel, D : ViewBinding>: BaseFr
viewModel.snackbarEventsData.observe(viewLifecycleOwner) {
if (it.isConsumed) return@observe

snackbar(it.consume<String>()!!)
it.consume<String>()?.also { message ->
snackbar(message)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AppViewHolder(

init {
binding.root.setOnClickListener {
click(currentItem)
click(currentItem ?: return@setOnClickListener)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class CrashViewHolder(

init {
binding.root.setOnClickListener {
click.invoke(currentItem)
click.invoke(currentItem ?: return@setOnClickListener)
}
binding.deleteButton.setOnClickListener {
delete.invoke(currentItem)
delete.invoke(currentItem ?: return@setOnClickListener)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class FilterViewHolder(
): BaseViewHolder<UserFilter, ItemFilterBinding>(binding) {

private val checkedListener = OnlyUserCheckedChangeListener(binding.enabledBox) { button, isChecked ->
checked.invoke(currentItem, isChecked)
checked.invoke(currentItem ?: return@OnlyUserCheckedChangeListener, isChecked)
}

init {
binding.root.setOnClickListener {
click.invoke(currentItem)
click.invoke(currentItem ?: return@setOnClickListener)
}
binding.deleteButton.setOnClickListener {
delete.invoke(currentItem)
delete.invoke(currentItem ?: return@setOnClickListener)
}
binding.enabledBox.setOnCheckedChangeListener(checkedListener)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class LogViewHolder(
true
}
R.id.copy_item -> {
copyLog.invoke(currentItem)
copyLog.invoke(currentItem ?: return@setOnMenuItemClickListener false)
true
}

Expand Down Expand Up @@ -86,7 +86,7 @@ class LogViewHolder(
}

private fun selectItem() = adapter<LogsAdapter>().selectedItems.apply {
currentItem.also {
currentItem?.also {
if (any { logLine -> it.id == logLine.id })
remove(it)
else
Expand All @@ -97,7 +97,7 @@ class LogViewHolder(
}

private fun expandOrCollapseItem() = adapter<LogsAdapter>().expandedStates.apply {
currentItem.also {
currentItem?.also {
put(it.id, !getOrElse(it.id) { adapter<LogsAdapter>().logsExpanded })
changeExpandedAndSelected(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class RecordingViewHolder(

init {
binding.root.setOnClickListener {
click.invoke(currentItem)
click.invoke(currentItem ?: return@setOnClickListener)
}
binding.deleteButton.setOnClickListener {
delete.invoke(currentItem)
delete.invoke(currentItem ?: return@setOnClickListener)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ abstract class BaseViewHolder<T, D : ViewBinding>(protected val binding: D): Rec

protected val baseAdapter get() = bindingAdapter as BaseListAdapter<T, D>
protected val elements: List<T> get() = baseAdapter.currentList
protected val currentItem: T get() = elements[bindingAdapterPosition]
protected val currentItem: T? get() = elements.getOrNull(bindingAdapterPosition)

abstract fun bindTo(data: T)
open fun recycle() {}
Expand Down

0 comments on commit 69f51c3

Please sign in to comment.