Added the original comment as first item in replies.

This commit is contained in:
faisalcodes 2023-01-30 12:03:10 +05:30
parent c5e21b5f18
commit 042cf26c1c
6 changed files with 45 additions and 16 deletions

View File

@ -18,5 +18,5 @@ object IntentData {
const val downloading = "downloading" const val downloading = "downloading"
const val openAudioPlayer = "openAudioPlayer" const val openAudioPlayer = "openAudioPlayer"
const val fragmentToOpen = "fragmentToOpen" const val fragmentToOpen = "fragmentToOpen"
const val replyPage = "replyPage" const val comment = "comment"
} }

View File

@ -5,8 +5,11 @@ import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import android.widget.Toast import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.text.parseAsHtml import androidx.core.text.parseAsHtml
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R import com.github.libretube.R
@ -20,6 +23,7 @@ import com.github.libretube.util.ClipboardHelper
import com.github.libretube.util.ImageHelper import com.github.libretube.util.ImageHelper
import com.github.libretube.util.NavigationHelper import com.github.libretube.util.NavigationHelper
import com.github.libretube.util.TextUtils import com.github.libretube.util.TextUtils
import kotlinx.serialization.json.Json
class CommentsAdapter( class CommentsAdapter(
private val fragment: Fragment?, private val fragment: Fragment?,
@ -69,11 +73,26 @@ class CommentsAdapter(
dismiss.invoke() dismiss.invoke()
} }
if (isRepliesAdapter) {
repliesCount.visibility = View.GONE
repliesAvailable.visibility = View.GONE
if (position == 0) {
root.setBackgroundColor(ContextCompat.getColor(root.context, R.color.replies_original_comment_background))
root.updateLayoutParams<MarginLayoutParams> { bottomMargin = 40 }
divider.visibility = View.VISIBLE
} else {
root.background = null
root.updateLayoutParams<MarginLayoutParams> { bottomMargin = 0 }
divider.visibility = View.GONE
}
}
if (!isRepliesAdapter && comment.repliesPage != null) { if (!isRepliesAdapter && comment.repliesPage != null) {
val repliesFragment = CommentsRepliesFragment().apply { val repliesFragment = CommentsRepliesFragment().apply {
arguments = Bundle().apply { arguments = Bundle().apply {
putString(IntentData.videoId, videoId) putString(IntentData.videoId, videoId)
putString(IntentData.replyPage, comment.repliesPage) putString(IntentData.comment, Json.encodeToString(Comment.serializer(), comment))
} }
} }
root.setOnClickListener { root.setOnClickListener {

View File

@ -5,10 +5,12 @@ import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.api.RetrofitInstance import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.Comment
import com.github.libretube.api.obj.CommentsPage import com.github.libretube.api.obj.CommentsPage
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.FragmentCommentsBinding import com.github.libretube.databinding.FragmentCommentsBinding
@ -19,6 +21,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
class CommentsRepliesFragment : Fragment() { class CommentsRepliesFragment : Fragment() {
private lateinit var binding: FragmentCommentsBinding private lateinit var binding: FragmentCommentsBinding
@ -40,12 +43,16 @@ class CommentsRepliesFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
val videoId = arguments?.getString(IntentData.videoId) ?: "" val videoId = arguments?.getString(IntentData.videoId) ?: ""
val nextPage = arguments?.getString(IntentData.replyPage) ?: "" val comment = Json.decodeFromString(
Comment.serializer(),
arguments?.getString(IntentData.comment) ?: """{}""",
)
repliesAdapter = CommentsAdapter(null, videoId, mutableListOf(), true) { repliesAdapter = CommentsAdapter(null, videoId, mutableListOf(comment), true) {
viewModel.commentsSheetDismiss?.invoke() viewModel.commentsSheetDismiss?.invoke()
} }
binding.commentsRV.updatePadding(top = 0)
binding.commentsRV.layoutManager = LinearLayoutManager(view.context) binding.commentsRV.layoutManager = LinearLayoutManager(view.context)
binding.commentsRV.adapter = repliesAdapter binding.commentsRV.adapter = repliesAdapter
@ -61,7 +68,7 @@ class CommentsRepliesFragment : Fragment() {
} }
} }
loadInitialReplies(videoId, nextPage, repliesAdapter) loadInitialReplies(videoId, comment.repliesPage ?: "", repliesAdapter)
} }
private fun loadInitialReplies( private fun loadInitialReplies(
@ -69,17 +76,10 @@ class CommentsRepliesFragment : Fragment() {
nextPage: String, nextPage: String,
repliesAdapter: CommentsAdapter repliesAdapter: CommentsAdapter
) { ) {
when (repliesAdapter.itemCount) { binding.progress.visibility = View.VISIBLE
0 -> { fetchReplies(videoId, nextPage) {
binding.progress.visibility = View.VISIBLE repliesAdapter.updateItems(it.comments)
fetchReplies(videoId, nextPage) { binding.progress.visibility = View.GONE
repliesAdapter.updateItems(it.comments)
binding.progress.visibility = View.GONE
}
}
else -> {
repliesAdapter.clear()
}
} }
} }

View File

@ -14,6 +14,7 @@
android:orientation="vertical" android:orientation="vertical"
android:paddingStart="15dp" android:paddingStart="15dp"
android:paddingEnd="15dp" android:paddingEnd="15dp"
app:layout_constraintBottom_toTopOf="@id/divider"
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<LinearLayout <LinearLayout
@ -132,4 +133,11 @@
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>
<com.google.android.material.divider.MaterialDivider
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -2,4 +2,5 @@
<resources> <resources>
<color name="text_color_secondary">#BFBFBF</color> <color name="text_color_secondary">#BFBFBF</color>
<color name="drag_handle_color">#3A3A3A</color> <color name="drag_handle_color">#3A3A3A</color>
<color name="replies_original_comment_background">#222222</color>
</resources> </resources>

View File

@ -5,6 +5,7 @@
<color name="shortcut_color">#0061A6</color> <color name="shortcut_color">#0061A6</color>
<color name="text_color_secondary">#505050</color> <color name="text_color_secondary">#505050</color>
<color name="drag_handle_color">#CCCCCC</color> <color name="drag_handle_color">#CCCCCC</color>
<color name="replies_original_comment_background">#E8E8E8</color>
<color name="blue_md_theme_light_primary">#0058CB</color> <color name="blue_md_theme_light_primary">#0058CB</color>
<color name="blue_md_theme_light_onPrimary">#FFFFFF</color> <color name="blue_md_theme_light_onPrimary">#FFFFFF</color>