2021-03-07 19:24:01 +01:00
|
|
|
/* Copyright 2021 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-02-12 19:22:37 +01:00
|
|
|
package com.keylesspalace.tusky.components.conversation
|
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.ViewGroup
|
2021-06-17 18:54:56 +02:00
|
|
|
import androidx.paging.PagingDataAdapter
|
2019-02-12 19:22:37 +01:00
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
|
|
import com.keylesspalace.tusky.R
|
2022-05-30 19:06:14 +02:00
|
|
|
import com.keylesspalace.tusky.adapter.StatusBaseViewHolder
|
2019-02-12 19:22:37 +01:00
|
|
|
import com.keylesspalace.tusky.interfaces.StatusActionListener
|
2019-12-30 21:37:20 +01:00
|
|
|
import com.keylesspalace.tusky.util.StatusDisplayOptions
|
2019-02-12 19:22:37 +01:00
|
|
|
|
2019-12-30 21:37:20 +01:00
|
|
|
class ConversationAdapter(
|
2022-05-30 19:06:14 +02:00
|
|
|
private var statusDisplayOptions: StatusDisplayOptions,
|
2021-06-17 18:54:56 +02:00
|
|
|
private val listener: StatusActionListener
|
2022-04-15 13:20:27 +02:00
|
|
|
) : PagingDataAdapter<ConversationViewData, ConversationViewHolder>(CONVERSATION_COMPARATOR) {
|
2019-02-12 19:22:37 +01:00
|
|
|
|
2022-05-30 19:06:14 +02:00
|
|
|
var mediaPreviewEnabled: Boolean
|
|
|
|
get() = statusDisplayOptions.mediaPreviewEnabled
|
|
|
|
set(mediaPreviewEnabled) {
|
|
|
|
statusDisplayOptions = statusDisplayOptions.copy(
|
|
|
|
mediaPreviewEnabled = mediaPreviewEnabled
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ConversationViewHolder {
|
|
|
|
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_conversation, parent, false)
|
|
|
|
return ConversationViewHolder(view, statusDisplayOptions, listener)
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 18:54:56 +02:00
|
|
|
override fun onBindViewHolder(holder: ConversationViewHolder, position: Int) {
|
2022-05-30 19:06:14 +02:00
|
|
|
onBindViewHolder(holder, position, emptyList())
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(
|
|
|
|
holder: ConversationViewHolder,
|
|
|
|
position: Int,
|
|
|
|
payloads: List<Any>
|
|
|
|
) {
|
|
|
|
getItem(position)?.let { conversationViewData ->
|
|
|
|
holder.setupWithConversation(conversationViewData, payloads.firstOrNull())
|
|
|
|
}
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
2022-04-15 13:20:27 +02:00
|
|
|
val CONVERSATION_COMPARATOR = object : DiffUtil.ItemCallback<ConversationViewData>() {
|
|
|
|
override fun areItemsTheSame(oldItem: ConversationViewData, newItem: ConversationViewData): Boolean {
|
2021-06-17 18:54:56 +02:00
|
|
|
return oldItem.id == newItem.id
|
|
|
|
}
|
2019-02-12 19:22:37 +01:00
|
|
|
|
2022-04-15 13:20:27 +02:00
|
|
|
override fun areContentsTheSame(oldItem: ConversationViewData, newItem: ConversationViewData): Boolean {
|
2022-05-30 19:06:14 +02:00
|
|
|
return false // Items are different always. It allows to refresh timestamp on every view holder update
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getChangePayload(oldItem: ConversationViewData, newItem: ConversationViewData): Any? {
|
|
|
|
return if (oldItem == newItem) {
|
|
|
|
// If items are equal - update timestamp only
|
|
|
|
listOf(StatusBaseViewHolder.Key.KEY_CREATED)
|
|
|
|
} else {
|
|
|
|
// If items are different - update the whole view holder
|
|
|
|
null
|
|
|
|
}
|
2021-06-17 18:54:56 +02:00
|
|
|
}
|
2019-02-12 19:22:37 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-17 18:54:56 +02:00
|
|
|
}
|