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 bf53ade3..718d4d43 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 @@ -39,28 +39,33 @@ class LogViewHolder( popupMenu.setForceShowIcon(true) binding.root.setOnClickListener { - if (adapter().selectedItems.isNotEmpty()) + val adapter = adapter() ?: return@setOnClickListener + + if (adapter.selectedItems.isNotEmpty()) selectItem() else expandOrCollapseItem() } binding.root.setOnLongClickListener { - if (adapter().selectedItems.isNotEmpty()) + val adapter = adapter() ?: return@setOnLongClickListener true + + if (adapter.selectedItems.isNotEmpty()) expandOrCollapseItem() else popupMenu.show() + return@setOnLongClickListener true } } override fun bindTo(data: LogLine) { - adapter().textSize.also { + adapter()?.textSize?.also { binding.logText.textSize = it binding.levelView.textSize = it } binding.logText.text = buildString { - adapter().logsFormat.apply { + adapter()?.logsFormat?.apply { if (date) append(dateTimeFormatter.formatDate(data.dateAndTime) + " ") if (time) append(dateTimeFormatter.formatTime(data.dateAndTime) + " ") if (uid) append(data.uid + " ") @@ -81,21 +86,23 @@ class LogViewHolder( popupMenu.dismiss() } - private fun selectItem() = adapter().selectedItems.apply { + private fun selectItem() = adapter()?.selectedItems?.apply { currentItem?.also { selectedItem(it, !any { logLine -> it.id == logLine.id }) } } - private fun expandOrCollapseItem() = adapter().expandedStates.apply { - currentItem?.also { - put(it.id, !getOrElse(it.id) { adapter().logsExpanded }) - changeExpandedAndSelected(it) + private fun expandOrCollapseItem() = adapter()?.apply { + expandedStates.apply { + currentItem?.also { + put(it.id, !getOrElse(it.id) { logsExpanded }) + changeExpandedAndSelected(it) + } } } - private fun changeExpandedAndSelected(logLine: LogLine) { - binding.logText.maxLines = if (adapter().expandedStates.getOrElse(logLine.id) { adapter().logsExpanded }) Int.MAX_VALUE else 1 - binding.container.isSelected = adapter().selectedItems.contains(logLine) + private fun changeExpandedAndSelected(logLine: LogLine) = adapter()?.apply { + binding.logText.maxLines = if (expandedStates.getOrElse(logLine.id) { logsExpanded }) Int.MAX_VALUE else 1 + binding.container.isSelected = selectedItems.contains(logLine) } } \ No newline at end of file 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 8489dd42..a118ad87 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 @@ -6,13 +6,13 @@ import com.f0x1d.logfox.adapter.base.BaseListAdapter abstract class BaseViewHolder(protected val binding: D): RecyclerView.ViewHolder(binding.root) { - protected val baseAdapter get() = bindingAdapter as BaseListAdapter - protected val elements: List get() = baseAdapter.currentList + protected val baseAdapter get() = bindingAdapter as? BaseListAdapter + protected val elements: List get() = baseAdapter?.currentList ?: emptyList() protected val currentItem: T? get() = elements.getOrNull(bindingAdapterPosition) abstract fun bindTo(data: T) open fun recycle() {} open fun detach() {} - fun adapter() = bindingAdapter as R + fun adapter() = bindingAdapter as? R } \ No newline at end of file