feat: Add support for removing categories

This commit is contained in:
Peter Vacho 2025-01-02 10:45:21 +01:00
parent 078a8e9239
commit 82de68633d
Signed by: school
GPG key ID: 8CFC3837052871B4
4 changed files with 87 additions and 13 deletions

View file

@ -1,31 +1,59 @@
package com.p_vacho.neat_calendar.adapters
import android.graphics.Color
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.p_vacho.neat_calendar.R
import androidx.core.content.ContextCompat
import com.p_vacho.neat_calendar.api.models.CategoryResponse
class CategoryChipAdapter(private val categories: List<CategoryResponse>) :
RecyclerView.Adapter<CategoryChipAdapter.CategoryViewHolder>() {
class CategoryChipAdapter(
private val categories: List<CategoryResponse>,
private val isRemovable: Boolean = false,
private val onRemoveCategory: ((CategoryResponse) -> Unit)? = null
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
inner class CategoryViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_category_chip, parent, false) as TextView
return CategoryViewHolder(view)
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 onBindViewHolder(holder: CategoryViewHolder, position: Int) {
val category = categories[position]
holder.textView.text = category.name
override fun getItemViewType(position: Int): Int {
return if (isRemovable) 1 else 0
}
holder.textView.backgroundTintList = android.content.res.ColorStateList.valueOf(category.color.toArgb())
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)
} else {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_category_chip, parent, false) as TextView
CategoryViewHolder(view)
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val category = categories[position]
if (holder is CategoryViewHolder) {
holder.textView.text = category.name
holder.textView.backgroundTintList = ColorStateList.valueOf(category.color.toArgb())
} else if (holder is RemovableCategoryViewHolder) {
holder.textView.text = category.name
holder.textView.backgroundTintList = ColorStateList.valueOf(category.color.toArgb())
holder.removeButton.setOnClickListener {
onRemoveCategory?.invoke(category)
}
}
}
override fun getItemCount(): Int = categories.size
}
}

View file

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960">
<path
android:pathData="m256,760 l-56,-56 224,-224 -224,-224 56,-56 224,224 224,-224 56,56 -224,224 224,224 -56,56 -224,-224 -224,224Z"
android:fillColor="#e8eaed"/>
</vector>

View file

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
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:paddingStart="8dp"
android:paddingEnd="4dp"
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:textSize="12sp"
android:textColor="?android:attr/textColorPrimary"
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>

View file

@ -41,4 +41,5 @@
<string name="end_time">End Time</string>
<string name="create_event">Create Event</string>
<string name="select_color">Select Color</string>
<string name="remove_category">Remove the category</string>
</resources>