feat: Actually use Chip for removable category chips
This commit is contained in:
parent
cc38fe5484
commit
025234a93b
|
@ -2,11 +2,9 @@ package com.p_vacho.neat_calendar.adapters
|
||||||
|
|
||||||
import android.content.res.ColorStateList
|
import android.content.res.ColorStateList
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.ImageButton
|
|
||||||
import android.widget.TextView
|
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
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.R
|
||||||
import com.p_vacho.neat_calendar.api.models.CategoryResponse
|
import com.p_vacho.neat_calendar.api.models.CategoryResponse
|
||||||
|
|
||||||
|
@ -14,47 +12,37 @@ class CategoryChipAdapter(
|
||||||
private val categories: List<CategoryResponse>,
|
private val categories: List<CategoryResponse>,
|
||||||
private val isRemovable: Boolean = false,
|
private val isRemovable: Boolean = false,
|
||||||
private val onRemoveCategory: ((CategoryResponse, Int) -> Unit)? = null
|
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 CategoryChipViewHolder(val chip: Chip) : RecyclerView.ViewHolder(chip)
|
||||||
|
|
||||||
inner class RemovableCategoryViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
|
||||||
val textView: TextView = view.findViewById(R.id.chipText)
|
|
||||||
val removeButton: ImageButton = view.findViewById(R.id.removeButton)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getItemViewType(position: Int): Int {
|
override fun getItemViewType(position: Int): Int {
|
||||||
return if (isRemovable) 1 else 0
|
return if (isRemovable) 1 else 0
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryChipViewHolder {
|
||||||
return if (viewType == 1) {
|
val layoutResId = if (viewType == 1) {
|
||||||
val view = LayoutInflater.from(parent.context)
|
R.layout.item_removable_category_chip
|
||||||
.inflate(R.layout.item_removable_category_chip, parent, false)
|
|
||||||
RemovableCategoryViewHolder(view)
|
|
||||||
} else {
|
} else {
|
||||||
val view = LayoutInflater.from(parent.context)
|
R.layout.item_category_chip
|
||||||
.inflate(R.layout.item_category_chip, parent, false) as TextView
|
|
||||||
CategoryViewHolder(view)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 category = categories[position]
|
||||||
|
val chip = holder.chip
|
||||||
|
|
||||||
if (holder is CategoryViewHolder) {
|
// Set the chip's text and background color
|
||||||
// Regular category chip
|
chip.text = category.name
|
||||||
holder.textView.text = category.name
|
chip.chipStrokeColor = ColorStateList.valueOf(category.color.toArgb())
|
||||||
holder.textView.backgroundTintList = ColorStateList.valueOf(category.color.toArgb())
|
|
||||||
} else if (holder is RemovableCategoryViewHolder) {
|
|
||||||
// Removable category chip
|
|
||||||
holder.textView.text = category.name
|
|
||||||
|
|
||||||
// Apply background tint to the LinearLayout (container)
|
if (isRemovable) {
|
||||||
holder.itemView.backgroundTintList = ColorStateList.valueOf(category.color.toArgb())
|
// Set close icon click listener for removable chips
|
||||||
|
chip.setOnCloseIconClickListener {
|
||||||
// Set up the remove button's click listener
|
|
||||||
holder.removeButton.setOnClickListener {
|
|
||||||
onRemoveCategory?.invoke(category, position)
|
onRemoveCategory?.invoke(category, position)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,7 +80,7 @@ class EventCardAdapter(
|
||||||
// Initialize empty state for categories
|
// Initialize empty state for categories
|
||||||
holder.rvCategories.layoutManager =
|
holder.rvCategories.layoutManager =
|
||||||
LinearLayoutManager(holder.itemView.context, LinearLayoutManager.HORIZONTAL, false)
|
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)
|
// 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
|
// 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
|
// Update the RecyclerView adapter on the main thread
|
||||||
holder.rvCategories.adapter = CategoryChipAdapter(categories)
|
holder.rvCategories.adapter = CategoryChipAdapter(categories, isRemovable = false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,37 +1,24 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<com.google.android.material.chip.Chip
|
||||||
<LinearLayout
|
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
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: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_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="horizontal"
|
|
||||||
android:gravity="center_vertical"
|
|
||||||
android:layout_marginEnd="8dp"
|
android:layout_marginEnd="8dp"
|
||||||
android:background="@drawable/bg_category_chip"
|
|
||||||
android:backgroundTint="@android:color/holo_blue_dark"
|
|
||||||
android:paddingStart="8dp"
|
android:paddingStart="8dp"
|
||||||
android:paddingEnd="4dp"
|
android:paddingEnd="8dp"
|
||||||
android:paddingTop="4dp"
|
android:paddingTop="4dp"
|
||||||
android:paddingBottom="4dp">
|
android:paddingBottom="4dp"
|
||||||
|
android:textSize="12sp"
|
||||||
<!-- Text for the category -->
|
android:textColor="?android:attr/textColorPrimary"
|
||||||
<TextView
|
app:chipIcon="@drawable/ic_tag"
|
||||||
android:id="@+id/chipText"
|
app:chipIconTint="?android:attr/textColorSecondary"
|
||||||
android:layout_width="wrap_content"
|
app:chipBackgroundColor="@android:color/transparent"
|
||||||
android:layout_height="wrap_content"
|
app:chipSurfaceColor="@android:color/transparent"
|
||||||
android:textSize="12sp"
|
app:chipStrokeColor="@android:color/holo_blue_dark"
|
||||||
android:textColor="?android:attr/textColorPrimary"
|
app:chipStrokeWidth="1dp"
|
||||||
tools:text="Work" />
|
app:closeIconEnabled="true"
|
||||||
|
app:closeIcon="@drawable/ic_close"
|
||||||
<!-- X button to remove the category -->
|
app:closeIconTint="?android:attr/textColorSecondary"
|
||||||
<ImageButton
|
tools:text="Work" />
|
||||||
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>
|
|
||||||
|
|
Loading…
Reference in a new issue