2022-08-15 11:00:18 +02: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>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.components.viewthread
|
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import androidx.recyclerview.widget.DiffUtil
|
|
|
|
import androidx.recyclerview.widget.ListAdapter
|
|
|
|
import com.keylesspalace.tusky.R
|
|
|
|
import com.keylesspalace.tusky.adapter.StatusBaseViewHolder
|
|
|
|
import com.keylesspalace.tusky.adapter.StatusDetailedViewHolder
|
|
|
|
import com.keylesspalace.tusky.adapter.StatusViewHolder
|
|
|
|
import com.keylesspalace.tusky.interfaces.StatusActionListener
|
|
|
|
import com.keylesspalace.tusky.util.StatusDisplayOptions
|
|
|
|
import com.keylesspalace.tusky.viewdata.StatusViewData
|
|
|
|
|
|
|
|
class ThreadAdapter(
|
|
|
|
private val statusDisplayOptions: StatusDisplayOptions,
|
|
|
|
private val statusActionListener: StatusActionListener
|
|
|
|
) : ListAdapter<StatusViewData.Concrete, StatusBaseViewHolder>(ThreadDifferCallback) {
|
|
|
|
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StatusBaseViewHolder {
|
|
|
|
return when (viewType) {
|
|
|
|
VIEW_TYPE_STATUS -> {
|
|
|
|
val view = LayoutInflater.from(parent.context)
|
|
|
|
.inflate(R.layout.item_status, parent, false)
|
|
|
|
StatusViewHolder(view)
|
|
|
|
}
|
|
|
|
VIEW_TYPE_STATUS_DETAILED -> {
|
|
|
|
val view = LayoutInflater.from(parent.context)
|
|
|
|
.inflate(R.layout.item_status_detailed, parent, false)
|
|
|
|
StatusDetailedViewHolder(view)
|
|
|
|
}
|
|
|
|
else -> error("Unknown item type: $viewType")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBindViewHolder(viewHolder: StatusBaseViewHolder, position: Int) {
|
|
|
|
val status = getItem(position)
|
|
|
|
viewHolder.setupWithStatus(status, statusActionListener, statusDisplayOptions)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getItemViewType(position: Int): Int {
|
|
|
|
return if (getItem(position).isDetailed) {
|
|
|
|
VIEW_TYPE_STATUS_DETAILED
|
|
|
|
} else {
|
|
|
|
VIEW_TYPE_STATUS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
Improve the actual and perceived speed of thread loading (#3118)
* Improve the actual and perceived speed of thread loading
To improve the actual speed, note that if the user has opened a thread from
their home timeline then the initial status is cached in the database. Other
statuses in the same thread may be cached as well.
So try and load the initial status from the database, falling back to the
network if it's not present (e.g., the user has opened a thread from the
local or federated timelines, or a search).
Introduce a new loading state to deal with this case.
In typical cases this allows the UI to display the initial status immediately
with no need to show a progress indicator.
To improve the perceived speed, delay showing the initial loading circular
progress indicators by 500ms. If loading the initial status completes within
that time no spinner is shown and the user will perceive the action as
close-to-immediate
(https://www.nngroup.com/articles/response-times-3-important-limits/).
Additionally, introduce an extra indeterminate progress indicator.
The new indicator is linear, anchored to the bottom of the screen, and shows
progress loading ancestor/descendant statuses. Like the other indicator is
also delayed 500ms from when ancestor/descendant status information is
fetched, and if the fetch completes in that time it will not be shown.
* Mark `getStatus` as suspend so it doesn't run on the main thread
* Save an allocation, use an isDetailed parameter to TimelineStatusWithAccount.toViewData
Rename Status.toViewData's "detailed" parameter to "isDetailed" for
consistency with other uses.
* Ensure suspend functions run to completion when testing
* Delay-load the status from the network even if it's cached
This speeds up the UI while ensuring it will eventually contain accurate data
from the remote.
* Load the network status before updating the list
Avoids excess animations if the network copy has changes
* Fix UI flicker when loading reblogged statuses
* Lint
* Fixup tests
2023-01-09 21:31:31 +01:00
|
|
|
private const val TAG = "ThreadAdapter"
|
2022-08-15 11:00:18 +02:00
|
|
|
private const val VIEW_TYPE_STATUS = 0
|
|
|
|
private const val VIEW_TYPE_STATUS_DETAILED = 1
|
|
|
|
|
|
|
|
val ThreadDifferCallback = object : DiffUtil.ItemCallback<StatusViewData.Concrete>() {
|
|
|
|
override fun areItemsTheSame(
|
|
|
|
oldItem: StatusViewData.Concrete,
|
|
|
|
newItem: StatusViewData.Concrete
|
|
|
|
): Boolean {
|
|
|
|
return oldItem.id == newItem.id
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun areContentsTheSame(
|
|
|
|
oldItem: StatusViewData.Concrete,
|
|
|
|
newItem: StatusViewData.Concrete
|
|
|
|
): Boolean {
|
|
|
|
return false // Items are different always. It allows to refresh timestamp on every view holder update
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getChangePayload(
|
|
|
|
oldItem: StatusViewData.Concrete,
|
|
|
|
newItem: StatusViewData.Concrete
|
|
|
|
): 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|