Yuito-app-android/app/src/main/java/com/keylesspalace/tusky/adapter/PlaceholderViewHolder.kt

68 lines
2.7 KiB
Kotlin
Raw Normal View History

/* 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.adapter
import android.view.View
import androidx.recyclerview.widget.RecyclerView
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
2023-01-13 19:26:24 +01:00
import com.google.android.material.button.MaterialButton
import com.google.android.material.progressindicator.CircularProgressIndicatorSpec
import com.google.android.material.progressindicator.IndeterminateDrawable
import com.keylesspalace.tusky.R
import com.keylesspalace.tusky.interfaces.StatusActionListener
/**
* Placeholder for different timelines.
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
2023-01-13 19:26:24 +01:00
*
* Displays a "Load more" button for a particular status ID, or a
* circular progress wheel if the status' page is being loaded.
*
* The user can only have one "Load more" operation in progress at
* a time (determined by the adapter), so the contents of the view
* and the enabled state is driven by that.
*/
class PlaceholderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
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
2023-01-13 19:26:24 +01:00
private val loadMoreButton: MaterialButton = itemView.findViewById(R.id.button_load_more)
private val drawable = IndeterminateDrawable.createCircularDrawable(
itemView.context,
CircularProgressIndicatorSpec(itemView.context, null)
)
fun setup(listener: StatusActionListener, loading: Boolean) {
itemView.isEnabled = !loading
loadMoreButton.isEnabled = !loading
if (loading) {
loadMoreButton.text = ""
loadMoreButton.icon = drawable
return
}
loadMoreButton.text = itemView.context.getString(R.string.load_more_placeholder_text)
loadMoreButton.icon = null
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
2023-01-13 19:26:24 +01:00
// To allow the user to click anywhere in the layout to load more content set the click
// listener on the parent layout instead of loadMoreButton.
//
// See the comments in item_status_placeholder.xml for more details.
itemView.setOnClickListener {
itemView.isEnabled = false
loadMoreButton.isEnabled = false
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
2023-01-13 19:26:24 +01:00
loadMoreButton.icon = drawable
loadMoreButton.text = ""
listener.onLoadMore(bindingAdapterPosition)
}
}
}