|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2017-2025 NewPipe contributors <https://newpipe.net> |
| 3 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +package org.schabi.newpipe.fragments.list.search |
| 7 | + |
| 8 | +import android.view.LayoutInflater |
| 9 | +import android.view.ViewGroup |
| 10 | +import androidx.recyclerview.widget.DiffUtil |
| 11 | +import androidx.recyclerview.widget.ListAdapter |
| 12 | +import androidx.recyclerview.widget.RecyclerView |
| 13 | +import org.schabi.newpipe.R |
| 14 | +import org.schabi.newpipe.databinding.ItemSearchSuggestionBinding |
| 15 | +import org.schabi.newpipe.fragments.list.search.SuggestionListAdapter.SuggestionItemHolder |
| 16 | + |
| 17 | +class SuggestionListAdapter : |
| 18 | + ListAdapter<SuggestionItem, SuggestionItemHolder>(SuggestionItemCallback()) { |
| 19 | + |
| 20 | + var listener: OnSuggestionItemSelected? = null |
| 21 | + |
| 22 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SuggestionItemHolder { |
| 23 | + return SuggestionItemHolder( |
| 24 | + ItemSearchSuggestionBinding.inflate(LayoutInflater.from(parent.context), parent, false) |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + override fun onBindViewHolder(holder: SuggestionItemHolder, position: Int) { |
| 29 | + val currentItem = getItem(position) |
| 30 | + holder.updateFrom(currentItem) |
| 31 | + holder.binding.suggestionSearch.setOnClickListener { |
| 32 | + listener?.onSuggestionItemSelected(currentItem) |
| 33 | + } |
| 34 | + holder.binding.suggestionSearch.setOnLongClickListener { |
| 35 | + listener?.onSuggestionItemLongClick(currentItem) |
| 36 | + true |
| 37 | + } |
| 38 | + holder.binding.suggestionInsert.setOnClickListener { |
| 39 | + listener?.onSuggestionItemInserted(currentItem) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + interface OnSuggestionItemSelected { |
| 44 | + fun onSuggestionItemSelected(item: SuggestionItem) |
| 45 | + |
| 46 | + fun onSuggestionItemInserted(item: SuggestionItem) |
| 47 | + |
| 48 | + fun onSuggestionItemLongClick(item: SuggestionItem) |
| 49 | + } |
| 50 | + |
| 51 | + class SuggestionItemHolder(val binding: ItemSearchSuggestionBinding) : |
| 52 | + RecyclerView.ViewHolder(binding.getRoot()) { |
| 53 | + fun updateFrom(item: SuggestionItem) { |
| 54 | + binding.itemSuggestionIcon.setImageResource( |
| 55 | + if (item.fromHistory) { |
| 56 | + R.drawable.ic_history |
| 57 | + } else { |
| 58 | + R.drawable.ic_search |
| 59 | + } |
| 60 | + ) |
| 61 | + binding.itemSuggestionQuery.text = item.query |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private class SuggestionItemCallback : DiffUtil.ItemCallback<SuggestionItem>() { |
| 66 | + override fun areItemsTheSame(oldItem: SuggestionItem, newItem: SuggestionItem): Boolean { |
| 67 | + return oldItem.fromHistory == newItem.fromHistory && oldItem.query == newItem.query |
| 68 | + } |
| 69 | + |
| 70 | + override fun areContentsTheSame(oldItem: SuggestionItem, newItem: SuggestionItem): Boolean { |
| 71 | + return true // items' contents never change; the list of items themselves does |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments