From 69f51c3a60ed8a9a72b69154c09a5de2ecd55e5e Mon Sep 17 00:00:00 2001 From: F0x1d Date: Fri, 13 Oct 2023 00:20:44 +0300 Subject: [PATCH] [fix]: possible crash of getting current item in not visible view holder --- .../f0x1d/logfox/ui/fragment/base/BaseViewModelFragment.kt | 4 +++- .../java/com/f0x1d/logfox/ui/viewholder/AppViewHolder.kt | 2 +- .../java/com/f0x1d/logfox/ui/viewholder/CrashViewHolder.kt | 4 ++-- .../java/com/f0x1d/logfox/ui/viewholder/FilterViewHolder.kt | 6 +++--- .../java/com/f0x1d/logfox/ui/viewholder/LogViewHolder.kt | 6 +++--- .../com/f0x1d/logfox/ui/viewholder/RecordingViewHolder.kt | 4 ++-- .../com/f0x1d/logfox/ui/viewholder/base/BaseViewHolder.kt | 2 +- 7 files changed, 15 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/f0x1d/logfox/ui/fragment/base/BaseViewModelFragment.kt b/app/src/main/java/com/f0x1d/logfox/ui/fragment/base/BaseViewModelFragment.kt index 4bce3df6..5a654990 100644 --- a/app/src/main/java/com/f0x1d/logfox/ui/fragment/base/BaseViewModelFragment.kt +++ b/app/src/main/java/com/f0x1d/logfox/ui/fragment/base/BaseViewModelFragment.kt @@ -22,7 +22,9 @@ abstract class BaseViewModelFragment: BaseFr viewModel.snackbarEventsData.observe(viewLifecycleOwner) { if (it.isConsumed) return@observe - snackbar(it.consume()!!) + it.consume()?.also { message -> + snackbar(message) + } } } diff --git a/app/src/main/java/com/f0x1d/logfox/ui/viewholder/AppViewHolder.kt b/app/src/main/java/com/f0x1d/logfox/ui/viewholder/AppViewHolder.kt index 5afd2787..b4318f7e 100644 --- a/app/src/main/java/com/f0x1d/logfox/ui/viewholder/AppViewHolder.kt +++ b/app/src/main/java/com/f0x1d/logfox/ui/viewholder/AppViewHolder.kt @@ -13,7 +13,7 @@ class AppViewHolder( init { binding.root.setOnClickListener { - click(currentItem) + click(currentItem ?: return@setOnClickListener) } } diff --git a/app/src/main/java/com/f0x1d/logfox/ui/viewholder/CrashViewHolder.kt b/app/src/main/java/com/f0x1d/logfox/ui/viewholder/CrashViewHolder.kt index 8d748ee6..233e102d 100644 --- a/app/src/main/java/com/f0x1d/logfox/ui/viewholder/CrashViewHolder.kt +++ b/app/src/main/java/com/f0x1d/logfox/ui/viewholder/CrashViewHolder.kt @@ -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) } } diff --git a/app/src/main/java/com/f0x1d/logfox/ui/viewholder/FilterViewHolder.kt b/app/src/main/java/com/f0x1d/logfox/ui/viewholder/FilterViewHolder.kt index 5495dc61..9e87f81f 100644 --- a/app/src/main/java/com/f0x1d/logfox/ui/viewholder/FilterViewHolder.kt +++ b/app/src/main/java/com/f0x1d/logfox/ui/viewholder/FilterViewHolder.kt @@ -17,15 +17,15 @@ class FilterViewHolder( ): BaseViewHolder(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) } diff --git a/app/src/main/java/com/f0x1d/logfox/ui/viewholder/LogViewHolder.kt b/app/src/main/java/com/f0x1d/logfox/ui/viewholder/LogViewHolder.kt index 360071c4..08eff904 100644 --- a/app/src/main/java/com/f0x1d/logfox/ui/viewholder/LogViewHolder.kt +++ b/app/src/main/java/com/f0x1d/logfox/ui/viewholder/LogViewHolder.kt @@ -31,7 +31,7 @@ class LogViewHolder( true } R.id.copy_item -> { - copyLog.invoke(currentItem) + copyLog.invoke(currentItem ?: return@setOnMenuItemClickListener false) true } @@ -86,7 +86,7 @@ class LogViewHolder( } private fun selectItem() = adapter().selectedItems.apply { - currentItem.also { + currentItem?.also { if (any { logLine -> it.id == logLine.id }) remove(it) else @@ -97,7 +97,7 @@ class LogViewHolder( } private fun expandOrCollapseItem() = adapter().expandedStates.apply { - currentItem.also { + currentItem?.also { put(it.id, !getOrElse(it.id) { adapter().logsExpanded }) changeExpandedAndSelected(it) } diff --git a/app/src/main/java/com/f0x1d/logfox/ui/viewholder/RecordingViewHolder.kt b/app/src/main/java/com/f0x1d/logfox/ui/viewholder/RecordingViewHolder.kt index 554fb70c..a8ec894f 100644 --- a/app/src/main/java/com/f0x1d/logfox/ui/viewholder/RecordingViewHolder.kt +++ b/app/src/main/java/com/f0x1d/logfox/ui/viewholder/RecordingViewHolder.kt @@ -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) } } diff --git a/app/src/main/java/com/f0x1d/logfox/ui/viewholder/base/BaseViewHolder.kt b/app/src/main/java/com/f0x1d/logfox/ui/viewholder/base/BaseViewHolder.kt index fea62ce6..8489dd42 100644 --- a/app/src/main/java/com/f0x1d/logfox/ui/viewholder/base/BaseViewHolder.kt +++ b/app/src/main/java/com/f0x1d/logfox/ui/viewholder/base/BaseViewHolder.kt @@ -8,7 +8,7 @@ abstract class BaseViewHolder(protected val binding: D): Rec protected val baseAdapter get() = bindingAdapter as BaseListAdapter protected val elements: List 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() {}