Skip to content

Commit

Permalink
Show element comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 21, 2024
1 parent 4acc2e1 commit 0c500ec
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 0 deletions.
60 changes: 60 additions & 0 deletions app/src/androidMain/kotlin/element/CommentsAdapter.kt
Original file line number Diff line number Diff line change
@@ -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<ElementComment, CommentsAdapter.ItemViewHolder>(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<ElementComment>() {

override fun areItemsTheSame(
oldItem: ElementComment,
newItem: ElementComment,
): Boolean {
return newItem == oldItem
}

override fun areContentsTheSame(
oldItem: ElementComment,
newItem: ElementComment,
): Boolean {
return newItem == oldItem
}
}
}
15 changes: 15 additions & 0 deletions app/src/androidMain/kotlin/element/ElementFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!!

Expand All @@ -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())
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions app/src/androidMain/res/drawable/comment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:tint="?attr/colorControlNormal"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:fillColor="@android:color/white"
android:pathData="M240,560L720,560L720,480L240,480L240,560ZM240,440L720,440L720,360L240,360L240,440ZM240,320L720,320L720,240L240,240L240,320ZM880,880L720,720L160,720Q127,720 103.5,696.5Q80,673 80,640L80,160Q80,127 103.5,103.5Q127,80 160,80L800,80Q833,80 856.5,103.5Q880,127 880,160L880,880ZM160,640L754,640L800,685L800,160Q800,160 800,160Q800,160 800,160L160,160Q160,160 160,160Q160,160 160,160L160,640Q160,640 160,640Q160,640 160,640ZM160,640Q160,640 160,640Q160,640 160,640L160,160Q160,160 160,160Q160,160 160,160L160,160Q160,160 160,160Q160,160 160,160L160,640Z" />
</vector>
22 changes: 22 additions & 0 deletions app/src/androidMain/res/layout/fragment_element.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,28 @@
app:drawableStartCompat="@drawable/schedule"
app:drawableTint="?colorSecondary" />

<TextView
android:id="@+id/comments_title"
style="?attr/textAppearanceSubtitle1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="20dp"
android:paddingHorizontal="16dp"
android:paddingVertical="16dp"
android:singleLine="true"
android:text="@string/comments_d"
app:drawableStartCompat="@drawable/comment"
app:drawableTint="?colorSecondary" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/comments"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Space
android:layout_width="match_parent"
android:layout_height="60dp" />

</LinearLayout>

</androidx.core.widget.NestedScrollView>
Expand Down
45 changes: 45 additions & 0 deletions app/src/androidMain/res/layout/item_comment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingVertical="8dp">

<ImageView
android:id="@+id/icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:layout_marginHorizontal="18dp"
android:contentDescription="@string/icon"
android:src="@drawable/person_alt"
app:tint="?attr/colorOnSurfaceVariant" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:id="@+id/message"
style="?attr/textAppearanceSubtitle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="0dp"
android:paddingEnd="16dp"
tools:text="Not verified" />

<TextView
android:id="@+id/date"
style="?textAppearanceCaption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Severity: 5" />

</LinearLayout>

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/androidMain/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<string name="days_since_verified">Days since verified</string>
<string name="new_merchant_accepts_bitcoins">New merchant accepts bitcoins</string>
<string name="delivery">Delivery</string>
<string name="comments_d" translatable="false">Comments (%1$d)</string>
<plurals name="d_changes">
<item quantity="zero">%d changes</item>
<item quantity="one">%d change</item>
Expand Down

0 comments on commit 0c500ec

Please sign in to comment.