2019-02-12 19:22:37 +01:00
|
|
|
/* Copyright 2019 Conny Duck
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.adapter
|
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.MotionEvent
|
|
|
|
import android.view.ViewGroup
|
2020-05-15 22:10:29 +02:00
|
|
|
import androidx.core.view.size
|
2019-02-12 19:22:37 +01:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2021-03-07 19:24:01 +01:00
|
|
|
import androidx.viewbinding.ViewBinding
|
2020-05-15 22:10:29 +02:00
|
|
|
import com.google.android.material.chip.Chip
|
2019-03-24 08:59:55 +01:00
|
|
|
import com.keylesspalace.tusky.HASHTAG
|
2019-12-02 16:53:24 +01:00
|
|
|
import com.keylesspalace.tusky.LIST
|
2019-02-12 19:22:37 +01:00
|
|
|
import com.keylesspalace.tusky.R
|
|
|
|
import com.keylesspalace.tusky.TabData
|
2021-03-07 19:24:01 +01:00
|
|
|
import com.keylesspalace.tusky.databinding.ItemTabPreferenceBinding
|
|
|
|
import com.keylesspalace.tusky.databinding.ItemTabPreferenceSmallBinding
|
|
|
|
import com.keylesspalace.tusky.util.BindingHolder
|
2019-03-24 08:59:55 +01:00
|
|
|
import com.keylesspalace.tusky.util.hide
|
2022-12-31 13:01:35 +01:00
|
|
|
import com.keylesspalace.tusky.util.setDrawableTint
|
2019-03-24 08:59:55 +01:00
|
|
|
import com.keylesspalace.tusky.util.show
|
2019-02-12 19:22:37 +01:00
|
|
|
|
|
|
|
interface ItemInteractionListener {
|
|
|
|
fun onTabAdded(tab: TabData)
|
2019-12-19 18:35:55 +01:00
|
|
|
fun onTabRemoved(position: Int)
|
2019-02-12 19:22:37 +01:00
|
|
|
fun onStartDelete(viewHolder: RecyclerView.ViewHolder)
|
|
|
|
fun onStartDrag(viewHolder: RecyclerView.ViewHolder)
|
2020-09-02 12:28:13 +02:00
|
|
|
fun onActionChipClicked(tab: TabData, tabPosition: Int)
|
|
|
|
fun onChipClicked(tab: TabData, tabPosition: Int, chipPosition: Int)
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:13:24 +02:00
|
|
|
class TabAdapter(
|
|
|
|
private var data: List<TabData>,
|
|
|
|
private val small: Boolean,
|
|
|
|
private val listener: ItemInteractionListener,
|
|
|
|
private var removeButtonEnabled: Boolean = false
|
2021-03-07 19:24:01 +01:00
|
|
|
) : RecyclerView.Adapter<BindingHolder<ViewBinding>>() {
|
2019-02-12 19:22:37 +01:00
|
|
|
|
|
|
|
fun updateData(newData: List<TabData>) {
|
|
|
|
this.data = newData
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
2021-03-07 19:24:01 +01:00
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder<ViewBinding> {
|
|
|
|
val binding = if (small) {
|
|
|
|
ItemTabPreferenceSmallBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
2019-02-12 19:22:37 +01:00
|
|
|
} else {
|
2021-03-07 19:24:01 +01:00
|
|
|
ItemTabPreferenceBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
2021-03-07 19:24:01 +01:00
|
|
|
return BindingHolder(binding)
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
|
2021-03-07 19:24:01 +01:00
|
|
|
override fun onBindViewHolder(holder: BindingHolder<ViewBinding>, position: Int) {
|
2019-03-24 08:59:55 +01:00
|
|
|
val context = holder.itemView.context
|
2020-09-02 12:28:13 +02:00
|
|
|
val tab = data[position]
|
2021-03-07 19:24:01 +01:00
|
|
|
|
2019-03-24 08:59:55 +01:00
|
|
|
if (small) {
|
2021-03-07 19:24:01 +01:00
|
|
|
val binding = holder.binding as ItemTabPreferenceSmallBinding
|
|
|
|
|
|
|
|
binding.textView.setText(tab.text)
|
|
|
|
|
|
|
|
binding.textView.setCompoundDrawablesRelativeWithIntrinsicBounds(tab.icon, 0, 0, 0)
|
|
|
|
|
|
|
|
binding.textView.setOnClickListener {
|
2020-09-02 12:28:13 +02:00
|
|
|
listener.onTabAdded(tab)
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
2021-03-07 19:24:01 +01:00
|
|
|
} else {
|
|
|
|
val binding = holder.binding as ItemTabPreferenceBinding
|
|
|
|
|
|
|
|
if (tab.id == LIST) {
|
|
|
|
binding.textView.text = tab.arguments.getOrNull(1).orEmpty()
|
2019-02-12 19:22:37 +01:00
|
|
|
} else {
|
2021-03-07 19:24:01 +01:00
|
|
|
binding.textView.setText(tab.text)
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
2021-03-07 19:24:01 +01:00
|
|
|
|
|
|
|
binding.textView.setCompoundDrawablesRelativeWithIntrinsicBounds(tab.icon, 0, 0, 0)
|
|
|
|
|
|
|
|
binding.imageView.setOnTouchListener { _, event ->
|
|
|
|
if (event.action == MotionEvent.ACTION_DOWN) {
|
|
|
|
listener.onStartDrag(holder)
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
binding.removeButton.setOnClickListener {
|
2021-04-10 20:30:44 +02:00
|
|
|
listener.onTabRemoved(holder.bindingAdapterPosition)
|
2021-03-07 19:24:01 +01:00
|
|
|
}
|
|
|
|
binding.removeButton.isEnabled = removeButtonEnabled
|
2022-12-31 13:01:35 +01:00
|
|
|
setDrawableTint(
|
2021-06-28 21:13:24 +02:00
|
|
|
holder.itemView.context,
|
|
|
|
binding.removeButton.drawable,
|
|
|
|
(if (removeButtonEnabled) android.R.attr.textColorTertiary else R.attr.textColorDisabled)
|
2019-12-19 18:35:55 +01:00
|
|
|
)
|
2019-03-24 08:59:55 +01:00
|
|
|
|
2020-09-02 12:28:13 +02:00
|
|
|
if (tab.id == HASHTAG) {
|
2021-03-07 19:24:01 +01:00
|
|
|
binding.chipGroup.show()
|
2019-03-24 08:59:55 +01:00
|
|
|
|
2020-05-15 22:10:29 +02:00
|
|
|
/*
|
|
|
|
* The chip group will always contain the actionChip (it is defined in the xml layout).
|
|
|
|
* The other dynamic chips are inserted in front of the actionChip.
|
|
|
|
* This code tries to reuse already added chips to reduce the number of Views created.
|
|
|
|
*/
|
2020-09-02 12:28:13 +02:00
|
|
|
tab.arguments.forEachIndexed { i, arg ->
|
2020-05-15 22:10:29 +02:00
|
|
|
|
2021-03-07 19:24:01 +01:00
|
|
|
val chip = binding.chipGroup.getChildAt(i).takeUnless { it.id == R.id.actionChip } as Chip?
|
2021-06-28 21:13:24 +02:00
|
|
|
?: Chip(context).apply {
|
2023-01-09 21:24:37 +01:00
|
|
|
setCloseIconResource(R.drawable.ic_cancel_24dp)
|
|
|
|
isCheckable = false
|
2021-06-28 21:13:24 +02:00
|
|
|
binding.chipGroup.addView(this, binding.chipGroup.size - 1)
|
|
|
|
}
|
2020-05-15 22:10:29 +02:00
|
|
|
|
|
|
|
chip.text = arg
|
|
|
|
|
2021-06-28 21:13:24 +02:00
|
|
|
if (tab.arguments.size <= 1) {
|
2023-01-09 21:24:37 +01:00
|
|
|
chip.isCloseIconVisible = false
|
2020-05-15 22:10:29 +02:00
|
|
|
chip.setOnClickListener(null)
|
|
|
|
} else {
|
2023-01-09 21:24:37 +01:00
|
|
|
chip.isCloseIconVisible = true
|
2020-05-15 22:10:29 +02:00
|
|
|
chip.setOnClickListener {
|
2021-04-10 20:30:44 +02:00
|
|
|
listener.onChipClicked(tab, holder.bindingAdapterPosition, i)
|
2020-05-15 22:10:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:13:24 +02:00
|
|
|
while (binding.chipGroup.size - 1 > tab.arguments.size) {
|
2021-03-07 19:24:01 +01:00
|
|
|
binding.chipGroup.removeViewAt(tab.arguments.size)
|
2020-05-15 22:10:29 +02:00
|
|
|
}
|
2019-02-12 19:22:37 +01:00
|
|
|
|
2021-03-07 19:24:01 +01:00
|
|
|
binding.actionChip.setOnClickListener {
|
2021-04-10 20:30:44 +02:00
|
|
|
listener.onActionChipClicked(tab, holder.bindingAdapterPosition)
|
2019-03-24 08:59:55 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-07 19:24:01 +01:00
|
|
|
binding.chipGroup.hide()
|
2019-03-24 08:59:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 19:22:37 +01:00
|
|
|
|
2020-09-02 12:28:13 +02:00
|
|
|
override fun getItemCount() = data.size
|
2019-02-12 19:22:37 +01:00
|
|
|
|
2019-12-19 18:35:55 +01:00
|
|
|
fun setRemoveButtonVisible(enabled: Boolean) {
|
|
|
|
if (removeButtonEnabled != enabled) {
|
|
|
|
removeButtonEnabled = enabled
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
}
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|