Keep scroll position when loading missing statuses (#3000)
* Change "Load more" to load oldest statuses first in home timeline Previous behaviour loaded missing statuses by using "since_id" and "max_id". This loads the most recent N statuses, looking backwards from "max_id". Change to load the oldest statuses first, assuming the user is scrolling up through the timeline and will want to load statuses in reverse chronological order. * Scroll to the bottom of new entries added by "Load more" - Remember the position of the "Load more" placeholder - Check the position of inserted entries - If they match, scroll to the bottom * Change "Load more" to load oldest statuses first in home timeline Previous behaviour loaded missing statuses by using "since_id" and "max_id". This loads the most recent N statuses, looking backwards from "max_id". Change to load the oldest statuses first, assuming the user is scrolling up through the timeline and will want to load statuses in reverse chronological order. * Scroll to the bottom of new entries added by "Load more" - Remember the position of the "Load more" placeholder - Check the position of inserted entries - If they match, scroll to the bottom * Ensure the user can't have two simultaneous "Load more" coroutines Having two simultanous coroutines would break the calculation used to figure out which item in the list to scroll to after a "Load more" in the timeline. Do this by: - Creating a TimelineUiState and associated flow that tracks the "Load more" state - Updating this in the (Cached|Network)TimelineViewModel - Listening for changes to it in TimelineFragment, and notifying the adapter - The adapter will disable any placeholder views while "Load more" is active * Revert changes that loaded the oldest statuses instead of the newest * Be more robust about locating the status to scroll to Weirdness with the PagingData library meant that positionStart could still be wrong after "Load more" was clicked. Instead, remember the position of the "Load more" item and the ID of the status immediately after it. When new items are added, search for the remembered status at the position of the "Load more" item. This is quick, testing at most LOAD_AT_ONCE items in the adapter. If the remembered status is not visible on screen then scroll to it. * Lint * Add a preference to specify the reading order Default behaviour (oldest first) is for "load more" to load statuses and stay at the oldest of the new statuses. Alternative behaviour (if the user is reading from top to bottom) is to stay at the newest of the new statuses. * Move ReadingOrder enum construction logic in to the enum * Jump to top if swipe/refresh while preferring newest-first order * Show a circular progress spinner during "Load more" operations Remove a dedicated view, and use an icon on the button instead. Adjust the placeholder attributes and styles accordingly. * Remove the "loadMoreActive" property Complicates the code and doesn't really achieve the desired effect. If the user wants to tap multiple "Load more" buttons they can. * Update comments in TimelineFragment * Respect the user's reading order preference if it changes * Add developer tools This is for functionality that makes it easier for developers to interact with the app, or get it in to a known-state. These features are for use by users, so are only visible in debug builds. * Adjust how content is loaded based on preferred reading order - Add the readingOrder to TimelineViewModel so derived classes can use it. - Update the homeTimeline API to support the `minId` parameter and update calls in NetworkTimelineViewModel In CachedTimelineViewModel: - Set the bounds of the load to be the status IDs on either side of the placeholder ID (update TimelineDao with a new query for this) - Load statuses using either minId or sinceId depending on the reading order - Is there was no overlap then insert the new placeholder at the start/end of the list depending on reading order * Lint * Rename unused dialog parameter to _ * Update API arguments in tests * Simplify ReadingOrder preference handling * Fix bug with Placeholder and the "expanded" property If a status is a Placeholder the "expanded" propery is used to indicate whether or not it is loading. replaceStatusRange() set this property based on the old value, and the user's alwaysOpenSpoiler preference setting. This shouldn't have been used if the status is a Placeholder, as it can lead to incorrect loading states. Fix this. While I'm here, introduce an explicit computed property for whether a TimelineStatusEntity is a placeholder, and use that for code clarity. * Set the "Load more" button background to transparent * Fix typo. * Inline spec, update comment * Revert 1480c6aa3ac5c0c2d362fb271f47ea2259ab14e2 Turns out the behaviour is not desired. * Remove unnecessary Log call * Extract function * Change default to newest first
This commit is contained in:
parent
c4d569314f
commit
9cf4882f41
19 changed files with 360 additions and 49 deletions
|
@ -51,6 +51,26 @@ class PreferencesFragment : PreferenceFragmentCompat(), Injectable {
|
|||
private val iconSize by lazy { resources.getDimensionPixelSize(R.dimen.preference_icon_size) }
|
||||
private var httpProxyPref: Preference? = null
|
||||
|
||||
enum class ReadingOrder {
|
||||
/** User scrolls up, reading statuses oldest to newest */
|
||||
OLDEST_FIRST,
|
||||
|
||||
/** User scrolls down, reading statuses newest to oldest. Default behaviour. */
|
||||
NEWEST_FIRST;
|
||||
|
||||
companion object {
|
||||
fun from(s: String?): ReadingOrder {
|
||||
s ?: return NEWEST_FIRST
|
||||
|
||||
return try {
|
||||
valueOf(s.uppercase())
|
||||
} catch (_: Throwable) {
|
||||
NEWEST_FIRST
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
makePreferenceScreen {
|
||||
preferenceCategory(R.string.pref_title_appearance_settings) {
|
||||
|
@ -90,6 +110,16 @@ class PreferencesFragment : PreferenceFragmentCompat(), Injectable {
|
|||
icon = makeIcon(GoogleMaterial.Icon.gmd_format_size)
|
||||
}
|
||||
|
||||
listPreference {
|
||||
setDefaultValue(ReadingOrder.NEWEST_FIRST.name)
|
||||
setEntries(R.array.reading_order_names)
|
||||
setEntryValues(R.array.reading_order_values)
|
||||
key = PrefKeys.READING_ORDER
|
||||
setSummaryProvider { entry }
|
||||
setTitle(R.string.pref_title_reading_order)
|
||||
icon = makeIcon(GoogleMaterial.Icon.gmd_sort)
|
||||
}
|
||||
|
||||
listPreference {
|
||||
setDefaultValue("top")
|
||||
setEntries(R.array.pref_main_nav_position_options)
|
||||
|
|
|
@ -42,6 +42,7 @@ import com.keylesspalace.tusky.adapter.StatusBaseViewHolder
|
|||
import com.keylesspalace.tusky.appstore.EventHub
|
||||
import com.keylesspalace.tusky.appstore.PreferenceChangedEvent
|
||||
import com.keylesspalace.tusky.appstore.StatusComposedEvent
|
||||
import com.keylesspalace.tusky.components.preference.PreferencesFragment.ReadingOrder
|
||||
import com.keylesspalace.tusky.components.timeline.viewmodel.CachedTimelineViewModel
|
||||
import com.keylesspalace.tusky.components.timeline.viewmodel.NetworkTimelineViewModel
|
||||
import com.keylesspalace.tusky.components.timeline.viewmodel.TimelineViewModel
|
||||
|
@ -62,6 +63,7 @@ import com.keylesspalace.tusky.util.hide
|
|||
import com.keylesspalace.tusky.util.show
|
||||
import com.keylesspalace.tusky.util.viewBinding
|
||||
import com.keylesspalace.tusky.viewdata.AttachmentViewData
|
||||
import com.keylesspalace.tusky.viewdata.StatusViewData
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.rxjava3.core.Observable
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
|
@ -101,6 +103,38 @@ class TimelineFragment :
|
|||
private var isSwipeToRefreshEnabled = true
|
||||
private var hideFab = false
|
||||
|
||||
/**
|
||||
* Adapter position of the placeholder that was most recently clicked to "Load more". If null
|
||||
* then there is no active "Load more" operation
|
||||
*/
|
||||
private var loadMorePosition: Int? = null
|
||||
|
||||
/** ID of the status immediately below the most recent "Load more" placeholder click */
|
||||
// The Paging library assumes that the user will be scrolling down a list of items,
|
||||
// and if new items are loaded but not visible then it's reasonable to scroll to the top
|
||||
// of the inserted items. It does not seem to be possible to disable that behaviour.
|
||||
//
|
||||
// That behaviour should depend on the user's preferred reading order. If they prefer to
|
||||
// read oldest first then the list should be scrolled to the bottom of the freshly
|
||||
// inserted statuses.
|
||||
//
|
||||
// To do this:
|
||||
//
|
||||
// 1. When "Load more" is clicked (onLoadMore()):
|
||||
// a. Remember the adapter position of the "Load more" item in loadMorePosition
|
||||
// b. Remember the ID of the status immediately below the "Load more" item in
|
||||
// statusIdBelowLoadMore
|
||||
// 2. After the new items have been inserted, search the adapter for the position of the
|
||||
// status with id == statusIdBelowLoadMore.
|
||||
// 3. If this position is still visible on screen then do nothing, otherwise, scroll the view
|
||||
// so that the status is visible.
|
||||
//
|
||||
// The user can then scroll up to read the new statuses.
|
||||
private var statusIdBelowLoadMore: String? = null
|
||||
|
||||
/** The user's preferred reading order */
|
||||
private lateinit var readingOrder: ReadingOrder
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
|
@ -130,6 +164,8 @@ class TimelineFragment :
|
|||
isSwipeToRefreshEnabled = arguments.getBoolean(ARG_ENABLE_SWIPE_TO_REFRESH, true)
|
||||
|
||||
val preferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
||||
readingOrder = ReadingOrder.from(preferences.getString(PrefKeys.READING_ORDER, null))
|
||||
|
||||
val statusDisplayOptions = StatusDisplayOptions(
|
||||
animateAvatars = preferences.getBoolean(PrefKeys.ANIMATE_GIF_AVATARS, false),
|
||||
mediaPreviewEnabled = accountManager.activeAccount!!.mediaPreviewEnabled,
|
||||
|
@ -207,6 +243,9 @@ class TimelineFragment :
|
|||
}
|
||||
}
|
||||
}
|
||||
if (readingOrder == ReadingOrder.OLDEST_FIRST) {
|
||||
updateReadingPositionForOldestFirst()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -253,6 +292,33 @@ class TimelineFragment :
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the correct reading position in the timeline after the user clicked "Load more",
|
||||
* assuming the reading position should be below the freshly-loaded statuses.
|
||||
*/
|
||||
// Note: The positionStart parameter to onItemRangeInserted() does not always
|
||||
// match the adapter position where data was inserted (which is why loadMorePosition
|
||||
// is tracked manually, see this bug report for another example:
|
||||
// https://github.com/android/architecture-components-samples/issues/726).
|
||||
private fun updateReadingPositionForOldestFirst() {
|
||||
var position = loadMorePosition ?: return
|
||||
val statusIdBelowLoadMore = statusIdBelowLoadMore ?: return
|
||||
|
||||
var status: StatusViewData?
|
||||
while (adapter.peek(position).let { status = it; it != null }) {
|
||||
if (status?.id == statusIdBelowLoadMore) {
|
||||
val lastVisiblePosition =
|
||||
(binding.recyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition()
|
||||
if (position > lastVisiblePosition) {
|
||||
binding.recyclerView.scrollToPosition(position)
|
||||
}
|
||||
break
|
||||
}
|
||||
position++
|
||||
}
|
||||
loadMorePosition = null
|
||||
}
|
||||
|
||||
private fun setupSwipeRefreshLayout() {
|
||||
binding.swipeRefreshLayout.isEnabled = isSwipeToRefreshEnabled
|
||||
binding.swipeRefreshLayout.setOnRefreshListener(this)
|
||||
|
@ -344,6 +410,8 @@ class TimelineFragment :
|
|||
|
||||
override fun onLoadMore(position: Int) {
|
||||
val placeholder = adapter.peek(position)?.asPlaceholderOrNull() ?: return
|
||||
loadMorePosition = position
|
||||
statusIdBelowLoadMore = adapter.peek(position + 1)?.id
|
||||
viewModel.loadMore(placeholder.id)
|
||||
}
|
||||
|
||||
|
@ -404,6 +472,11 @@ class TimelineFragment :
|
|||
adapter.notifyItemRangeChanged(0, adapter.itemCount)
|
||||
}
|
||||
}
|
||||
PrefKeys.READING_ORDER -> {
|
||||
readingOrder = ReadingOrder.from(
|
||||
sharedPreferences.getString(PrefKeys.READING_ORDER, null)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
|
||||
package com.keylesspalace.tusky.components.timeline
|
||||
|
||||
import android.util.Log
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import com.keylesspalace.tusky.db.TimelineAccountEntity
|
||||
|
@ -30,6 +31,8 @@ import com.keylesspalace.tusky.entity.TimelineAccount
|
|||
import com.keylesspalace.tusky.viewdata.StatusViewData
|
||||
import java.util.Date
|
||||
|
||||
private const val TAG = "TimelineTypeMappers"
|
||||
|
||||
data class Placeholder(
|
||||
val id: String,
|
||||
val loading: Boolean
|
||||
|
@ -150,7 +153,8 @@ fun Status.toEntity(
|
|||
}
|
||||
|
||||
fun TimelineStatusWithAccount.toViewData(gson: Gson, isDetailed: Boolean = false): StatusViewData {
|
||||
if (this.status.authorServerId == null) {
|
||||
if (this.status.isPlaceholder) {
|
||||
Log.d(TAG, "Constructing Placeholder(${this.status.serverId}, ${this.status.expanded})")
|
||||
return StatusViewData.Placeholder(this.status.serverId, this.status.expanded)
|
||||
}
|
||||
|
||||
|
|
|
@ -153,7 +153,14 @@ class CachedTimelineRemoteMediator(
|
|||
if (oldStatus != null) break
|
||||
}
|
||||
|
||||
val expanded = oldStatus?.expanded ?: activeAccount.alwaysOpenSpoiler
|
||||
// The "expanded" property for Placeholders determines whether or not they are
|
||||
// in the "loading" state, and should not be affected by the account's
|
||||
// "alwaysOpenSpoiler" preference
|
||||
val expanded = if (oldStatus?.isPlaceholder == true) {
|
||||
oldStatus.expanded
|
||||
} else {
|
||||
oldStatus?.expanded ?: activeAccount.alwaysOpenSpoiler
|
||||
}
|
||||
val contentShowing = oldStatus?.contentShowing ?: activeAccount.alwaysShowSensitiveMedia || !status.actionableStatus.sensitive
|
||||
val contentCollapsed = oldStatus?.contentCollapsed ?: true
|
||||
|
||||
|
|
|
@ -32,6 +32,8 @@ import com.keylesspalace.tusky.appstore.EventHub
|
|||
import com.keylesspalace.tusky.appstore.FavoriteEvent
|
||||
import com.keylesspalace.tusky.appstore.PinEvent
|
||||
import com.keylesspalace.tusky.appstore.ReblogEvent
|
||||
import com.keylesspalace.tusky.components.preference.PreferencesFragment.ReadingOrder.NEWEST_FIRST
|
||||
import com.keylesspalace.tusky.components.preference.PreferencesFragment.ReadingOrder.OLDEST_FIRST
|
||||
import com.keylesspalace.tusky.components.timeline.Placeholder
|
||||
import com.keylesspalace.tusky.components.timeline.toEntity
|
||||
import com.keylesspalace.tusky.components.timeline.toViewData
|
||||
|
@ -169,13 +171,23 @@ class CachedTimelineViewModel @Inject constructor(
|
|||
|
||||
val response = db.withTransaction {
|
||||
val idAbovePlaceholder = timelineDao.getIdAbove(activeAccount.id, placeholderId)
|
||||
val nextPlaceholderId =
|
||||
timelineDao.getNextPlaceholderIdAfter(activeAccount.id, placeholderId)
|
||||
api.homeTimeline(
|
||||
maxId = idAbovePlaceholder,
|
||||
sinceId = nextPlaceholderId,
|
||||
limit = LOAD_AT_ONCE
|
||||
)
|
||||
val idBelowPlaceholder = timelineDao.getIdBelow(activeAccount.id, placeholderId)
|
||||
when (readingOrder) {
|
||||
// Using minId, loads up to LOAD_AT_ONCE statuses with IDs immediately
|
||||
// after minId and no larger than maxId
|
||||
OLDEST_FIRST -> api.homeTimeline(
|
||||
maxId = idAbovePlaceholder,
|
||||
minId = idBelowPlaceholder,
|
||||
limit = LOAD_AT_ONCE
|
||||
)
|
||||
// Using sinceId, loads up to LOAD_AT_ONCE statuses immediately before
|
||||
// maxId, and no smaller than minId.
|
||||
NEWEST_FIRST -> api.homeTimeline(
|
||||
maxId = idAbovePlaceholder,
|
||||
sinceId = idBelowPlaceholder,
|
||||
limit = LOAD_AT_ONCE
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
val statuses = response.body()
|
||||
|
@ -218,12 +230,16 @@ class CachedTimelineViewModel @Inject constructor(
|
|||
/* In case we loaded a whole page and there was no overlap with existing statuses,
|
||||
we insert a placeholder because there might be even more unknown statuses */
|
||||
if (overlappedStatuses == 0 && statuses.size == LOAD_AT_ONCE) {
|
||||
/* This overrides the last of the newly loaded statuses with a placeholder
|
||||
/* This overrides the first/last of the newly loaded statuses with a placeholder
|
||||
to guarantee the placeholder has an id that exists on the server as not all
|
||||
servers handle client generated ids as expected */
|
||||
val idToConvert = when (readingOrder) {
|
||||
OLDEST_FIRST -> statuses.first().id
|
||||
NEWEST_FIRST -> statuses.last().id
|
||||
}
|
||||
timelineDao.insertStatus(
|
||||
Placeholder(
|
||||
statuses.last().id,
|
||||
idToConvert,
|
||||
loading = false
|
||||
).toEntity(activeAccount.id)
|
||||
)
|
||||
|
|
|
@ -259,7 +259,7 @@ class NetworkTimelineViewModel @Inject constructor(
|
|||
limit: Int
|
||||
): Response<List<Status>> {
|
||||
return when (kind) {
|
||||
Kind.HOME -> api.homeTimeline(fromId, uptoId, limit)
|
||||
Kind.HOME -> api.homeTimeline(maxId = fromId, sinceId = uptoId, limit = limit)
|
||||
Kind.PUBLIC_FEDERATED -> api.publicTimeline(null, fromId, uptoId, limit)
|
||||
Kind.PUBLIC_LOCAL -> api.publicTimeline(true, fromId, uptoId, limit)
|
||||
Kind.TAG -> {
|
||||
|
|
|
@ -34,6 +34,7 @@ import com.keylesspalace.tusky.appstore.PreferenceChangedEvent
|
|||
import com.keylesspalace.tusky.appstore.ReblogEvent
|
||||
import com.keylesspalace.tusky.appstore.StatusDeletedEvent
|
||||
import com.keylesspalace.tusky.appstore.UnfollowEvent
|
||||
import com.keylesspalace.tusky.components.preference.PreferencesFragment.ReadingOrder
|
||||
import com.keylesspalace.tusky.components.timeline.util.ifExpected
|
||||
import com.keylesspalace.tusky.db.AccountManager
|
||||
import com.keylesspalace.tusky.entity.Filter
|
||||
|
@ -54,7 +55,7 @@ abstract class TimelineViewModel(
|
|||
private val api: MastodonApi,
|
||||
private val eventHub: EventHub,
|
||||
protected val accountManager: AccountManager,
|
||||
private val sharedPreferences: SharedPreferences,
|
||||
protected val sharedPreferences: SharedPreferences,
|
||||
private val filterModel: FilterModel
|
||||
) : ViewModel() {
|
||||
|
||||
|
@ -71,6 +72,7 @@ abstract class TimelineViewModel(
|
|||
protected var alwaysOpenSpoilers = false
|
||||
private var filterRemoveReplies = false
|
||||
private var filterRemoveReblogs = false
|
||||
protected var readingOrder: ReadingOrder = ReadingOrder.OLDEST_FIRST
|
||||
|
||||
fun init(
|
||||
kind: Kind,
|
||||
|
@ -88,6 +90,8 @@ abstract class TimelineViewModel(
|
|||
filterRemoveReblogs =
|
||||
!sharedPreferences.getBoolean(PrefKeys.TAB_FILTER_HOME_BOOSTS, true)
|
||||
}
|
||||
readingOrder = ReadingOrder.from(sharedPreferences.getString(PrefKeys.READING_ORDER, null))
|
||||
|
||||
this.alwaysShowSensitiveMedia = accountManager.activeAccount!!.alwaysShowSensitiveMedia
|
||||
this.alwaysOpenSpoilers = accountManager.activeAccount!!.alwaysOpenSpoiler
|
||||
|
||||
|
@ -212,6 +216,9 @@ abstract class TimelineViewModel(
|
|||
alwaysShowSensitiveMedia =
|
||||
accountManager.activeAccount!!.alwaysShowSensitiveMedia
|
||||
}
|
||||
PrefKeys.READING_ORDER -> {
|
||||
readingOrder = ReadingOrder.from(sharedPreferences.getString(PrefKeys.READING_ORDER, null))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue