From 025234a93b100ccf8126e5029f5c58593d8d498e Mon Sep 17 00:00:00 2001 From: Peter Vacho Date: Sun, 5 Jan 2025 12:02:56 +0100 Subject: [PATCH] feat: Actually use Chip for removable category chips --- .../adapters/CategoryChipAdapter.kt | 50 +++++++------------ .../adapters/EventCardAdapter.kt | 4 +- .../layout/item_removable_category_chip.xml | 47 +++++++---------- 3 files changed, 38 insertions(+), 63 deletions(-) diff --git a/app/src/main/java/com/p_vacho/neat_calendar/adapters/CategoryChipAdapter.kt b/app/src/main/java/com/p_vacho/neat_calendar/adapters/CategoryChipAdapter.kt index 77fa310..972e99c 100644 --- a/app/src/main/java/com/p_vacho/neat_calendar/adapters/CategoryChipAdapter.kt +++ b/app/src/main/java/com/p_vacho/neat_calendar/adapters/CategoryChipAdapter.kt @@ -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, private val isRemovable: Boolean = false, private val onRemoveCategory: ((CategoryResponse, Int) -> Unit)? = null -) : RecyclerView.Adapter() { +) : RecyclerView.Adapter() { - 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 } + + val chip = LayoutInflater.from(parent.context) + .inflate(layoutResId, parent, false) as Chip + return CategoryChipViewHolder(chip) } - override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + 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) } } diff --git a/app/src/main/java/com/p_vacho/neat_calendar/adapters/EventCardAdapter.kt b/app/src/main/java/com/p_vacho/neat_calendar/adapters/EventCardAdapter.kt index ae3a05a..0b3f201 100644 --- a/app/src/main/java/com/p_vacho/neat_calendar/adapters/EventCardAdapter.kt +++ b/app/src/main/java/com/p_vacho/neat_calendar/adapters/EventCardAdapter.kt @@ -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) } } } diff --git a/app/src/main/res/layout/item_removable_category_chip.xml b/app/src/main/res/layout/item_removable_category_chip.xml index 195c39d..215531a 100644 --- a/app/src/main/res/layout/item_removable_category_chip.xml +++ b/app/src/main/res/layout/item_removable_category_chip.xml @@ -1,37 +1,24 @@ - - - - - - - - - + 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" />