|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Brayan Oliveira <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify it under |
| 5 | + * the terms of the GNU General Public License as published by the Free Software |
| 6 | + * Foundation; either version 3 of the License, or (at your option) any later |
| 7 | + * version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY |
| 10 | + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 11 | + * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the GNU General Public License along with |
| 14 | + * this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | + */ |
| 16 | +package com.ichi2.anki.ui.windows.reviewer.whiteboard |
| 17 | + |
| 18 | +import android.graphics.drawable.GradientDrawable |
| 19 | +import android.graphics.drawable.LayerDrawable |
| 20 | +import android.view.LayoutInflater |
| 21 | +import android.view.View |
| 22 | +import android.view.ViewGroup |
| 23 | +import androidx.recyclerview.widget.RecyclerView |
| 24 | +import com.google.android.material.button.MaterialButton |
| 25 | +import com.ichi2.anki.R |
| 26 | +import kotlin.math.roundToInt |
| 27 | + |
| 28 | +/** |
| 29 | + * Adapter for displaying a list of brushes in a RecyclerView. |
| 30 | + * |
| 31 | + * @param onBrushClick Callback when a brush color is clicked. |
| 32 | + * @param onBrushLongClick Callback when a brush color is long-clicked. |
| 33 | + */ |
| 34 | +class BrushAdapter( |
| 35 | + private val onBrushClick: (View, Int) -> Unit, |
| 36 | + private val onBrushLongClick: (Int) -> Unit, |
| 37 | +) : RecyclerView.Adapter<BrushAdapter.BrushViewHolder>() { |
| 38 | + private var brushes: List<BrushInfo> = emptyList() |
| 39 | + private var activeIndex: Int = -1 |
| 40 | + private var isEraserActive: Boolean = false |
| 41 | + |
| 42 | + fun updateData( |
| 43 | + newBrushes: List<BrushInfo>, |
| 44 | + newActiveIndex: Int, |
| 45 | + eraserActive: Boolean, |
| 46 | + ) { |
| 47 | + brushes = newBrushes |
| 48 | + activeIndex = newActiveIndex |
| 49 | + isEraserActive = eraserActive |
| 50 | + notifyDataSetChanged() |
| 51 | + } |
| 52 | + |
| 53 | + fun updateSelection( |
| 54 | + newActiveIndex: Int, |
| 55 | + eraserActive: Boolean, |
| 56 | + ) { |
| 57 | + val oldIndex = activeIndex |
| 58 | + activeIndex = newActiveIndex |
| 59 | + isEraserActive = eraserActive |
| 60 | + |
| 61 | + if (oldIndex in brushes.indices) notifyItemChanged(oldIndex) |
| 62 | + if (newActiveIndex in brushes.indices) notifyItemChanged(newActiveIndex) |
| 63 | + } |
| 64 | + |
| 65 | + override fun getItemCount(): Int = brushes.size |
| 66 | + |
| 67 | + override fun onCreateViewHolder( |
| 68 | + parent: ViewGroup, |
| 69 | + viewType: Int, |
| 70 | + ): BrushViewHolder { |
| 71 | + val inflater = LayoutInflater.from(parent.context) |
| 72 | + val itemView = inflater.inflate(R.layout.button_color_brush, parent, false) |
| 73 | + return BrushViewHolder(itemView) |
| 74 | + } |
| 75 | + |
| 76 | + override fun onBindViewHolder( |
| 77 | + holder: BrushViewHolder, |
| 78 | + position: Int, |
| 79 | + ) { |
| 80 | + val brush = brushes[position] |
| 81 | + holder.bind(brush, position == activeIndex && !isEraserActive) |
| 82 | + } |
| 83 | + |
| 84 | + inner class BrushViewHolder( |
| 85 | + itemView: View, |
| 86 | + ) : RecyclerView.ViewHolder(itemView) { |
| 87 | + private val button: MaterialButton = itemView as MaterialButton |
| 88 | + |
| 89 | + fun bind( |
| 90 | + brush: BrushInfo, |
| 91 | + isSelected: Boolean, |
| 92 | + ) = button.apply { |
| 93 | + isCheckable = true |
| 94 | + isChecked = isSelected |
| 95 | + text = brush.width.roundToInt().toString() |
| 96 | + iconTint = null |
| 97 | + |
| 98 | + val layer = icon?.mutate() as? LayerDrawable |
| 99 | + val fill = layer?.findDrawableByLayerId(R.id.brush_preview_fill) as? GradientDrawable |
| 100 | + fill?.setColor(brush.color) |
| 101 | + |
| 102 | + setOnClickListener { onBrushClick(it, bindingAdapterPosition) } |
| 103 | + setOnLongClickListener { |
| 104 | + onBrushLongClick(bindingAdapterPosition) |
| 105 | + true |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments