feat: Actually use Chip for removable category chips

This commit is contained in:
Peter Vacho 2025-01-05 12:02:56 +01:00
parent cc38fe5484
commit 025234a93b
Signed by: school
GPG key ID: 8CFC3837052871B4
3 changed files with 38 additions and 63 deletions

View file

@ -2,11 +2,9 @@ package com.p_vacho.neat_calendar.adapters
import android.content.res.ColorStateList
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.chip.Chip
import com.p_vacho.neat_calendar.R
import com.p_vacho.neat_calendar.api.models.CategoryResponse
@ -14,47 +12,37 @@ class CategoryChipAdapter(
private val categories: List<CategoryResponse>,
private val isRemovable: Boolean = false,
private val onRemoveCategory: ((CategoryResponse, Int) -> Unit)? = null
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
) : RecyclerView.Adapter<CategoryChipAdapter.CategoryChipViewHolder>() {
inner class CategoryViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
inner class RemovableCategoryViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView = view.findViewById(R.id.chipText)
val removeButton: ImageButton = view.findViewById(R.id.removeButton)
}
inner class CategoryChipViewHolder(val chip: Chip) : RecyclerView.ViewHolder(chip)
override fun getItemViewType(position: Int): Int {
return if (isRemovable) 1 else 0
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return if (viewType == 1) {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_removable_category_chip, parent, false)
RemovableCategoryViewHolder(view)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryChipViewHolder {
val layoutResId = if (viewType == 1) {
R.layout.item_removable_category_chip
} else {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_category_chip, parent, false) as TextView
CategoryViewHolder(view)
}
R.layout.item_category_chip
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val chip = LayoutInflater.from(parent.context)
.inflate(layoutResId, parent, false) as Chip
return CategoryChipViewHolder(chip)
}
override fun onBindViewHolder(holder: CategoryChipViewHolder, position: Int) {
val category = categories[position]
val chip = holder.chip
if (holder is CategoryViewHolder) {
// Regular category chip
holder.textView.text = category.name
holder.textView.backgroundTintList = ColorStateList.valueOf(category.color.toArgb())
} else if (holder is RemovableCategoryViewHolder) {
// Removable category chip
holder.textView.text = category.name
// Set the chip's text and background color
chip.text = category.name
chip.chipStrokeColor = ColorStateList.valueOf(category.color.toArgb())
// Apply background tint to the LinearLayout (container)
holder.itemView.backgroundTintList = ColorStateList.valueOf(category.color.toArgb())
// Set up the remove button's click listener
holder.removeButton.setOnClickListener {
if (isRemovable) {
// Set close icon click listener for removable chips
chip.setOnCloseIconClickListener {
onRemoveCategory?.invoke(category, position)
}
}

View file

@ -80,7 +80,7 @@ class EventCardAdapter(
// Initialize empty state for categories
holder.rvCategories.layoutManager =
LinearLayoutManager(holder.itemView.context, LinearLayoutManager.HORIZONTAL, false)
holder.rvCategories.adapter = CategoryChipAdapter(emptyList())
holder.rvCategories.adapter = CategoryChipAdapter(emptyList(), isRemovable = false)
// Events list might contain events that are owned by others (invited events)
// we can't show the categories & edit / delete buttons for these, as we don't
@ -138,7 +138,7 @@ class EventCardAdapter(
}
// Update the RecyclerView adapter on the main thread
holder.rvCategories.adapter = CategoryChipAdapter(categories)
holder.rvCategories.adapter = CategoryChipAdapter(categories, isRemovable = false)
}
}
}

View file

@ -1,37 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
<com.google.android.material.chip.Chip
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/removableChip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginEnd="8dp"
android:background="@drawable/bg_category_chip"
android:backgroundTint="@android:color/holo_blue_dark"
android:paddingStart="8dp"
android:paddingEnd="4dp"
android:paddingEnd="8dp"
android:paddingTop="4dp"
android:paddingBottom="4dp">
<!-- Text for the category -->
<TextView
android:id="@+id/chipText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:textSize="12sp"
android:textColor="?android:attr/textColorPrimary"
app:chipIcon="@drawable/ic_tag"
app:chipIconTint="?android:attr/textColorSecondary"
app:chipBackgroundColor="@android:color/transparent"
app:chipSurfaceColor="@android:color/transparent"
app:chipStrokeColor="@android:color/holo_blue_dark"
app:chipStrokeWidth="1dp"
app:closeIconEnabled="true"
app:closeIcon="@drawable/ic_close"
app:closeIconTint="?android:attr/textColorSecondary"
tools:text="Work" />
<!-- X button to remove the category -->
<ImageButton
android:id="@+id/removeButton"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginStart="8dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/remove_category"
android:src="@drawable/ic_close"
app:tint="?android:attr/textColorPrimary" />
</LinearLayout>