From 0c500ec4a19ce0db9f42f5b5c5655b5d62bbb0bd Mon Sep 17 00:00:00 2001 From: Igor Bubelov Date: Mon, 21 Oct 2024 15:51:12 +0700 Subject: [PATCH] Show element comments --- .../kotlin/element/CommentsAdapter.kt | 60 +++++++++++++++++++ .../kotlin/element/ElementFragment.kt | 15 +++++ .../element_comment/ElementCommentQueries.kt | 1 + app/src/androidMain/res/drawable/comment.xml | 11 ++++ .../res/layout/fragment_element.xml | 22 +++++++ .../androidMain/res/layout/item_comment.xml | 45 ++++++++++++++ app/src/androidMain/res/values/strings.xml | 1 + 7 files changed, 155 insertions(+) create mode 100644 app/src/androidMain/kotlin/element/CommentsAdapter.kt create mode 100644 app/src/androidMain/res/drawable/comment.xml create mode 100644 app/src/androidMain/res/layout/item_comment.xml diff --git a/app/src/androidMain/kotlin/element/CommentsAdapter.kt b/app/src/androidMain/kotlin/element/CommentsAdapter.kt new file mode 100644 index 00000000..411e7bcd --- /dev/null +++ b/app/src/androidMain/kotlin/element/CommentsAdapter.kt @@ -0,0 +1,60 @@ +package element + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.DiffUtil +import androidx.recyclerview.widget.ListAdapter +import androidx.recyclerview.widget.RecyclerView.ViewHolder +import element_comment.ElementComment +import org.btcmap.databinding.ItemCommentBinding +import java.time.OffsetDateTime +import java.time.format.DateTimeFormatter +import java.time.format.FormatStyle + +class CommentsAdapter : + ListAdapter(DiffCallback()) { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder { + val binding = ItemCommentBinding.inflate( + LayoutInflater.from(parent.context), + parent, + false, + ) + return ItemViewHolder(binding) + } + + override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { + holder.bind(getItem(position)) + } + + class ItemViewHolder( + private val binding: ItemCommentBinding, + ) : ViewHolder( + binding.root, + ) { + fun bind(item: ElementComment) { + binding.apply { + message.text = item.comment + val dateFormat = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT) + date.text = dateFormat.format(OffsetDateTime.parse(item.createdAt)) + } + } + } + + class DiffCallback : DiffUtil.ItemCallback() { + + override fun areItemsTheSame( + oldItem: ElementComment, + newItem: ElementComment, + ): Boolean { + return newItem == oldItem + } + + override fun areContentsTheSame( + oldItem: ElementComment, + newItem: ElementComment, + ): Boolean { + return newItem == oldItem + } + } +} \ No newline at end of file diff --git a/app/src/androidMain/kotlin/element/ElementFragment.kt b/app/src/androidMain/kotlin/element/ElementFragment.kt index 4f22c8c8..33564932 100644 --- a/app/src/androidMain/kotlin/element/ElementFragment.kt +++ b/app/src/androidMain/kotlin/element/ElementFragment.kt @@ -20,7 +20,9 @@ import androidx.core.view.isVisible import androidx.core.view.updateLayoutParams import androidx.fragment.app.Fragment import androidx.navigation.fragment.findNavController +import androidx.recyclerview.widget.LinearLayoutManager import coil.load +import element_comment.ElementCommentRepo import icons.iconTypeface import kotlinx.coroutines.flow.update import kotlinx.coroutines.runBlocking @@ -41,10 +43,14 @@ class ElementFragment : Fragment() { private val elementsRepo: ElementsRepo by inject() + private val elementCommentRepo: ElementCommentRepo by inject() + private val resultModel: SearchResultModel by activityViewModel() private var elementId = -1L + val commentsAdapter = CommentsAdapter() + private var _binding: FragmentElementBinding? = null private val binding get() = _binding!! @@ -56,6 +62,10 @@ class ElementFragment : Fragment() { } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + binding.comments.layoutManager = LinearLayoutManager(requireContext()) + binding.comments.isNestedScrollingEnabled = false + binding.comments.adapter = commentsAdapter + if (arguments != null) { ViewCompat.setOnApplyWindowInsetsListener(binding.toolbar) { appBar, windowInsets -> val insets = windowInsets.getInsets(WindowInsetsCompat.Type.statusBars()) @@ -338,6 +348,11 @@ class ElementFragment : Fragment() { } else { binding.image.isVisible = false } + + val comments = runBlocking { elementCommentRepo.selectByElementId(element.id) } + binding.commentsTitle.text = getString(R.string.comments_d, comments.size) + binding.commentsTitle.isVisible = comments.isNotEmpty() + commentsAdapter.submitList(comments) } private fun TextView.styleAsLink() { diff --git a/app/src/androidMain/kotlin/element_comment/ElementCommentQueries.kt b/app/src/androidMain/kotlin/element_comment/ElementCommentQueries.kt index f5201a25..e41d5013 100644 --- a/app/src/androidMain/kotlin/element_comment/ElementCommentQueries.kt +++ b/app/src/androidMain/kotlin/element_comment/ElementCommentQueries.kt @@ -89,6 +89,7 @@ class ElementCommentQueries(private val conn: SQLiteConnection) { updated_at FROM element_comment WHERE element_id = ?1 + ORDER BY created_at DESC """ ).use { it.bindLong(1, elementId) diff --git a/app/src/androidMain/res/drawable/comment.xml b/app/src/androidMain/res/drawable/comment.xml new file mode 100644 index 00000000..7038cd16 --- /dev/null +++ b/app/src/androidMain/res/drawable/comment.xml @@ -0,0 +1,11 @@ + + + diff --git a/app/src/androidMain/res/layout/fragment_element.xml b/app/src/androidMain/res/layout/fragment_element.xml index ae56b5f1..58950d97 100644 --- a/app/src/androidMain/res/layout/fragment_element.xml +++ b/app/src/androidMain/res/layout/fragment_element.xml @@ -202,6 +202,28 @@ app:drawableStartCompat="@drawable/schedule" app:drawableTint="?colorSecondary" /> + + + + + + diff --git a/app/src/androidMain/res/layout/item_comment.xml b/app/src/androidMain/res/layout/item_comment.xml new file mode 100644 index 00000000..436b448b --- /dev/null +++ b/app/src/androidMain/res/layout/item_comment.xml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/androidMain/res/values/strings.xml b/app/src/androidMain/res/values/strings.xml index a55eb638..7fa6c346 100644 --- a/app/src/androidMain/res/values/strings.xml +++ b/app/src/androidMain/res/values/strings.xml @@ -67,6 +67,7 @@ Days since verified New merchant accepts bitcoins Delivery + Comments (%1$d) %d changes %d change