Skip to content

Commit

Permalink
[fix]: some crash idk
Browse files Browse the repository at this point in the history
  • Loading branch information
F0x1d committed Dec 5, 2023
1 parent 31da92b commit 50382d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
31 changes: 19 additions & 12 deletions app/src/main/java/com/f0x1d/logfox/ui/viewholder/LogViewHolder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,28 +39,33 @@ class LogViewHolder(
popupMenu.setForceShowIcon(true)

binding.root.setOnClickListener {
if (adapter<LogsAdapter>().selectedItems.isNotEmpty())
val adapter = adapter<LogsAdapter>() ?: return@setOnClickListener

if (adapter.selectedItems.isNotEmpty())
selectItem()
else
expandOrCollapseItem()
}
binding.root.setOnLongClickListener {
if (adapter<LogsAdapter>().selectedItems.isNotEmpty())
val adapter = adapter<LogsAdapter>() ?: return@setOnLongClickListener true

if (adapter.selectedItems.isNotEmpty())
expandOrCollapseItem()
else
popupMenu.show()

return@setOnLongClickListener true
}
}

override fun bindTo(data: LogLine) {
adapter<LogsAdapter>().textSize.also {
adapter<LogsAdapter>()?.textSize?.also {
binding.logText.textSize = it
binding.levelView.textSize = it
}

binding.logText.text = buildString {
adapter<LogsAdapter>().logsFormat.apply {
adapter<LogsAdapter>()?.logsFormat?.apply {
if (date) append(dateTimeFormatter.formatDate(data.dateAndTime) + " ")
if (time) append(dateTimeFormatter.formatTime(data.dateAndTime) + " ")
if (uid) append(data.uid + " ")
Expand All @@ -81,21 +86,23 @@ class LogViewHolder(
popupMenu.dismiss()
}

private fun selectItem() = adapter<LogsAdapter>().selectedItems.apply {
private fun selectItem() = adapter<LogsAdapter>()?.selectedItems?.apply {
currentItem?.also {
selectedItem(it, !any { logLine -> it.id == logLine.id })
}
}

private fun expandOrCollapseItem() = adapter<LogsAdapter>().expandedStates.apply {
currentItem?.also {
put(it.id, !getOrElse(it.id) { adapter<LogsAdapter>().logsExpanded })
changeExpandedAndSelected(it)
private fun expandOrCollapseItem() = adapter<LogsAdapter>()?.apply {
expandedStates.apply {
currentItem?.also {
put(it.id, !getOrElse(it.id) { logsExpanded })
changeExpandedAndSelected(it)
}
}
}

private fun changeExpandedAndSelected(logLine: LogLine) {
binding.logText.maxLines = if (adapter<LogsAdapter>().expandedStates.getOrElse(logLine.id) { adapter<LogsAdapter>().logsExpanded }) Int.MAX_VALUE else 1
binding.container.isSelected = adapter<LogsAdapter>().selectedItems.contains(logLine)
private fun changeExpandedAndSelected(logLine: LogLine) = adapter<LogsAdapter>()?.apply {
binding.logText.maxLines = if (expandedStates.getOrElse(logLine.id) { logsExpanded }) Int.MAX_VALUE else 1
binding.container.isSelected = selectedItems.contains(logLine)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import com.f0x1d.logfox.adapter.base.BaseListAdapter

abstract class BaseViewHolder<T, D : ViewBinding>(protected val binding: D): RecyclerView.ViewHolder(binding.root) {

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

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

fun <R> adapter() = bindingAdapter as R
fun <R> adapter() = bindingAdapter as? R
}

0 comments on commit 50382d3

Please sign in to comment.