2020-06-17 19:07:47 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020 Conny Duck
|
|
|
|
*
|
|
|
|
* This file is part of Pixelcat.
|
|
|
|
*
|
|
|
|
* Pixelcat 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.
|
|
|
|
*
|
|
|
|
* Pixelcat 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 this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-06-12 15:44:45 +02:00
|
|
|
package at.connyduck.pixelcat.components.compose
|
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.ViewGroup
|
2020-06-17 14:48:18 +02:00
|
|
|
import androidx.core.view.setPadding
|
2020-06-12 15:44:45 +02:00
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
|
|
import androidx.recyclerview.widget.ListAdapter
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import at.connyduck.pixelcat.R
|
|
|
|
import at.connyduck.pixelcat.databinding.ItemComposeImageBinding
|
2020-06-17 14:48:18 +02:00
|
|
|
import at.connyduck.sparkbutton.helpers.Utils
|
2020-06-12 15:44:45 +02:00
|
|
|
import coil.api.load
|
|
|
|
import java.io.File
|
|
|
|
|
2020-06-17 14:48:18 +02:00
|
|
|
interface OnImageActionClickListener {
|
|
|
|
fun onAddImage()
|
|
|
|
}
|
|
|
|
|
|
|
|
class ComposeImageAdapter(
|
|
|
|
private val listener: OnImageActionClickListener
|
|
|
|
) : ListAdapter<String, ComposeImageViewHolder>(
|
2020-06-12 15:44:45 +02:00
|
|
|
object : DiffUtil.ItemCallback<String>() {
|
|
|
|
override fun areItemsTheSame(old: String, new: String): Boolean {
|
|
|
|
return old == new
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun areContentsTheSame(old: String, new: String): Boolean {
|
|
|
|
return old == new
|
|
|
|
}
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ComposeImageViewHolder {
|
|
|
|
val binding =
|
|
|
|
ItemComposeImageBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
|
|
|
return ComposeImageViewHolder(binding)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(holder: ComposeImageViewHolder, position: Int) {
|
|
|
|
|
|
|
|
getItem(position)?.let { uri ->
|
|
|
|
|
2020-06-17 14:48:18 +02:00
|
|
|
if (uri == ADD_ITEM) {
|
|
|
|
holder.binding.root.load(R.drawable.ic_plus_square_large)
|
|
|
|
holder.binding.root.setPadding(Utils.dpToPx(holder.binding.root.context, 40))
|
|
|
|
holder.binding.root.setOnClickListener {
|
|
|
|
listener.onAddImage()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
holder.binding.root.load(File(uri)) {
|
|
|
|
placeholder(R.drawable.ic_cat)
|
|
|
|
error(R.drawable.ic_message)
|
|
|
|
}
|
|
|
|
holder.binding.root.setOnClickListener {}
|
2020-06-12 15:44:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-17 14:48:18 +02:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val ADD_ITEM = "add_item"
|
|
|
|
}
|
2020-06-12 15:44:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ComposeImageViewHolder(val binding: ItemComposeImageBinding) :
|
2020-06-12 19:58:15 +02:00
|
|
|
RecyclerView.ViewHolder(binding.root)
|