From 644269d6110d011a83edf47ed6d1ed5fd2965f01 Mon Sep 17 00:00:00 2001 From: 0xd9a <0xd9a@noreply.codeberg.org> Date: Thu, 26 May 2022 13:54:15 +0530 Subject: [PATCH] update thread lines --- .../android/helper/RecyclerViewThreadLines.kt | 62 ++++++++----------- .../timeline/FragmentMastodonContext.java | 2 +- 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/app/fedilab/android/helper/RecyclerViewThreadLines.kt b/app/src/main/java/app/fedilab/android/helper/RecyclerViewThreadLines.kt index ca675b086..f8e258e58 100644 --- a/app/src/main/java/app/fedilab/android/helper/RecyclerViewThreadLines.kt +++ b/app/src/main/java/app/fedilab/android/helper/RecyclerViewThreadLines.kt @@ -43,6 +43,7 @@ class RecyclerViewThreadLines(context: Context, private val lineInfoList: List 0) lineColors[j - 1] else Color.GRAY // draw lines for below statuses - if (j != level && j >= lineInfo.fullLinesStart && j <= lineInfo.fullLinesEnd) + if (j != level && lineInfo.lines.contains(j)) c.drawLine(lineStart, lineTop, lineStart, view.bottom.toFloat(), paint) // draw vertical line for current statuses - if (j == level && i != 0) { + if (j == level && position != 0) { // top the line starts at the middle of the above status if (i > 0) lineTop -= parent.getChildAt(i - 1).height / 2 - 1 // '- 1' is to prevent overlapping with above horizontal line @@ -76,12 +78,8 @@ class RecyclerViewThreadLines(context: Context, private val lineInfoList: List= nextLineInfo.fullLinesStart && level <= nextLineInfo.fullLinesEnd) { - lineBottom = view.bottom.toFloat() - } - } + if (position < lineInfoList.lastIndex && lineInfoList[position + 1].lines.contains(level) && j != maxLevel) + lineBottom = view.bottom.toFloat() // if level is max, use a dashed line if (j == maxLevel) paint.pathEffect = dashPathEffect @@ -99,7 +97,7 @@ class RecyclerViewThreadLines(context: Context, private val lineInfoList: List) private val Int.dpToPx: Float get() = this * Resources.getSystem().displayMetrics.density @@ -112,43 +110,33 @@ class RecyclerViewThreadLines(context: Context, private val lineInfoList: List { +fun getThreadDecorationInfo(statusContext: StatusContext): MutableList { val lineInfoList = mutableListOf() - repeat(fediContext.ancestors.size) { lineInfoList.add(LineInfo(0, true, 0, 0)) } - lineInfoList.add(LineInfo(0, fediContext.descendants.isNotEmpty(), 0, 0)) - val descendantsLineInfoList = List(fediContext.descendants.size) { LineInfo(0, false, 0, 0) } - for (i in fediContext.descendants.indices) { - fediContext.descendants[i].let { status -> + repeat(statusContext.ancestors.size) { lineInfoList.add(LineInfo(0, listOf(0))) } + lineInfoList.add(LineInfo(0, listOf(0))) + val descendantsLineInfoList = List(statusContext.descendants.size) { LineInfo(0, listOf()) } + for (i in statusContext.descendants.indices) { + statusContext.descendants[i].let { status -> var level = 0 if (status.in_reply_to_id != null) { - if (status.in_reply_to_id == selectedStatusId) - level = 1 - else { - var replyToId: String? = status.in_reply_to_id - while (replyToId != null && level < RecyclerViewThreadLines.threadLineColors.size) { - level += 1 - replyToId = fediContext.descendants.firstOrNull { it.id == replyToId }?.in_reply_to_id - } + var replyToId: String? = status.in_reply_to_id + while (replyToId != null && level < RecyclerViewThreadLines.threadLineColors.size) { + level += 1 + replyToId = statusContext.descendants.firstOrNull { it.id == replyToId }?.in_reply_to_id } } descendantsLineInfoList[i].level = level - val firstReply = fediContext.descendants.firstOrNull { it.in_reply_to_id == status.id } - if (firstReply == null) descendantsLineInfoList[i].end = true } } - for (i in descendantsLineInfoList.indices) { - var fullLinesStart = descendantsLineInfoList[i].level - var fullLinesEnd = descendantsLineInfoList[i].level - var fullLinesEndSet = false - for (j in i + 1 until descendantsLineInfoList.lastIndex) { - if (!fullLinesEndSet && descendantsLineInfoList[j].level < descendantsLineInfoList[i].level) { - fullLinesEnd = descendantsLineInfoList[j].level - fullLinesEndSet = true - } - fullLinesStart = descendantsLineInfoList[j].level.coerceAtMost(fullLinesStart) + for (i in descendantsLineInfoList.indices.reversed()) { + val lines: MutableList = mutableListOf() + val lineInfo = descendantsLineInfoList[i] + lines.add(lineInfo.level) + if (i < descendantsLineInfoList.lastIndex) { + val belowLineInfo = descendantsLineInfoList[i + 1] + lines.addAll(belowLineInfo.lines.filter { it < lineInfo.level }) } - descendantsLineInfoList[i].fullLinesStart = fullLinesStart - descendantsLineInfoList[i].fullLinesEnd = fullLinesEnd + descendantsLineInfoList[i].lines = lines } lineInfoList.addAll(descendantsLineInfoList) diff --git a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonContext.java b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonContext.java index c33c1fbb1..13d9c8ad4 100644 --- a/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonContext.java +++ b/app/src/main/java/app/fedilab/android/ui/fragment/timeline/FragmentMastodonContext.java @@ -251,7 +251,7 @@ public class FragmentMastodonContext extends Fragment { binding.recyclerView.removeItemDecorationAt(i); } } - List threadDecorationInfo = getThreadDecorationInfo(context, focusedStatus.id); + List threadDecorationInfo = getThreadDecorationInfo(context); binding.recyclerView.addItemDecoration(new RecyclerViewThreadLines(requireContext(), threadDecorationInfo)); binding.swipeContainer.setRefreshing(false); binding.recyclerView.scrollToPosition(statusPosition);