2019-12-22 11:42:13 +01:00
|
|
|
/* Copyright 2019 Tusky Contributors
|
|
|
|
*
|
|
|
|
* 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>. */
|
|
|
|
|
2019-12-22 11:47:34 +01:00
|
|
|
package com.keylesspalace.tusky.components.scheduled
|
2019-12-22 11:42:13 +01:00
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2021-06-24 21:24:04 +02:00
|
|
|
import androidx.paging.PagingDataAdapter
|
2019-12-30 20:40:27 +01:00
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
2022-03-20 20:21:42 +01:00
|
|
|
import com.keylesspalace.tusky.databinding.ItemScheduledStatusBinding
|
2019-12-22 11:42:13 +01:00
|
|
|
import com.keylesspalace.tusky.entity.ScheduledStatus
|
2021-03-07 19:24:01 +01:00
|
|
|
import com.keylesspalace.tusky.util.BindingHolder
|
2019-12-22 11:42:13 +01:00
|
|
|
|
2022-03-20 20:21:42 +01:00
|
|
|
interface ScheduledStatusActionListener {
|
2019-12-30 20:48:01 +01:00
|
|
|
fun edit(item: ScheduledStatus)
|
|
|
|
fun delete(item: ScheduledStatus)
|
2019-12-22 11:42:13 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 20:21:42 +01:00
|
|
|
class ScheduledStatusAdapter(
|
|
|
|
val listener: ScheduledStatusActionListener
|
|
|
|
) : PagingDataAdapter<ScheduledStatus, BindingHolder<ItemScheduledStatusBinding>>(
|
2021-06-28 22:04:34 +02:00
|
|
|
object : DiffUtil.ItemCallback<ScheduledStatus>() {
|
|
|
|
override fun areItemsTheSame(oldItem: ScheduledStatus, newItem: ScheduledStatus): Boolean {
|
|
|
|
return oldItem.id == newItem.id
|
|
|
|
}
|
2019-12-30 20:40:27 +01:00
|
|
|
|
2021-06-28 22:04:34 +02:00
|
|
|
override fun areContentsTheSame(oldItem: ScheduledStatus, newItem: ScheduledStatus): Boolean {
|
|
|
|
return oldItem == newItem
|
2019-12-30 20:40:27 +01:00
|
|
|
}
|
2021-06-28 22:04:34 +02:00
|
|
|
}
|
2019-12-30 20:40:27 +01:00
|
|
|
) {
|
2019-12-22 11:42:13 +01:00
|
|
|
|
2022-03-20 20:21:42 +01:00
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder<ItemScheduledStatusBinding> {
|
|
|
|
val binding = ItemScheduledStatusBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
2021-03-07 19:24:01 +01:00
|
|
|
return BindingHolder(binding)
|
2019-12-22 11:42:13 +01:00
|
|
|
}
|
|
|
|
|
2022-03-20 20:21:42 +01:00
|
|
|
override fun onBindViewHolder(holder: BindingHolder<ItemScheduledStatusBinding>, position: Int) {
|
2021-06-28 22:04:34 +02:00
|
|
|
getItem(position)?.let { item ->
|
2021-03-07 19:24:01 +01:00
|
|
|
holder.binding.edit.isEnabled = true
|
|
|
|
holder.binding.delete.isEnabled = true
|
|
|
|
holder.binding.text.text = item.params.text
|
|
|
|
holder.binding.edit.setOnClickListener { v: View ->
|
2019-12-30 20:40:27 +01:00
|
|
|
v.isEnabled = false
|
2019-12-30 20:48:01 +01:00
|
|
|
listener.edit(item)
|
2019-12-30 20:40:27 +01:00
|
|
|
}
|
2021-03-07 19:24:01 +01:00
|
|
|
holder.binding.delete.setOnClickListener { v: View ->
|
2019-12-30 20:40:27 +01:00
|
|
|
v.isEnabled = false
|
2019-12-30 20:48:01 +01:00
|
|
|
listener.delete(item)
|
2019-12-30 20:40:27 +01:00
|
|
|
}
|
2019-12-22 11:42:13 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-07 19:24:01 +01:00
|
|
|
}
|