From 9cf4882f41164d0c9fdd520e0a5feb588d4142f9 Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Fri, 13 Jan 2023 19:26:24 +0100 Subject: [PATCH] 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 --- .../com/keylesspalace/tusky/MainActivity.kt | 41 ++++++++++- .../tusky/adapter/PlaceholderViewHolder.kt | 48 +++++++++--- .../preference/PreferencesFragment.kt | 30 ++++++++ .../components/timeline/TimelineFragment.kt | 73 +++++++++++++++++++ .../timeline/TimelineTypeMappers.kt | 6 +- .../viewmodel/CachedTimelineRemoteMediator.kt | 9 ++- .../viewmodel/CachedTimelineViewModel.kt | 34 ++++++--- .../viewmodel/NetworkTimelineViewModel.kt | 2 +- .../timeline/viewmodel/TimelineViewModel.kt | 9 ++- .../com/keylesspalace/tusky/db/TimelineDao.kt | 15 ++++ .../tusky/db/TimelineStatusEntity.kt | 8 +- .../tusky/network/MastodonApi.kt | 1 + .../tusky/settings/SettingsConstants.kt | 1 + .../tusky/usecase/DeveloperToolsUseCase.kt | 46 ++++++++++++ .../res/layout/item_status_placeholder.xml | 57 +++++++++++---- app/src/main/res/values/donottranslate.xml | 10 +++ app/src/main/res/values/strings.xml | 5 ++ app/src/main/res/values/styles.xml | 6 ++ .../CachedTimelineRemoteMediatorTest.kt | 8 +- 19 files changed, 360 insertions(+), 49 deletions(-) create mode 100644 app/src/main/java/com/keylesspalace/tusky/usecase/DeveloperToolsUseCase.kt diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt b/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt index c7530282..08edcb6c 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.kt @@ -20,7 +20,6 @@ import android.content.Context import android.content.DialogInterface import android.content.Intent import android.content.pm.PackageManager -import android.content.res.ColorStateList import android.graphics.Bitmap import android.graphics.Color import android.graphics.drawable.Animatable @@ -84,6 +83,7 @@ import com.keylesspalace.tusky.interfaces.ActionButtonActivity import com.keylesspalace.tusky.interfaces.ReselectableFragment import com.keylesspalace.tusky.pager.MainPagerAdapter import com.keylesspalace.tusky.settings.PrefKeys +import com.keylesspalace.tusky.usecase.DeveloperToolsUseCase import com.keylesspalace.tusky.usecase.LogoutUsecase import com.keylesspalace.tusky.util.deleteStaleCachedMedia import com.keylesspalace.tusky.util.emojify @@ -141,6 +141,9 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje @Inject lateinit var logoutUsecase: LogoutUsecase + @Inject + lateinit var developerToolsUseCase: DeveloperToolsUseCase + private val binding by viewBinding(ActivityMainBinding::inflate) private lateinit var header: AccountHeaderView @@ -559,16 +562,46 @@ class MainActivity : BottomSheetActivity(), ActionButtonActivity, HasAndroidInje } if (BuildConfig.DEBUG) { + // Add a "Developer tools" entry. Code that makes it easier to + // set the app state at runtime belongs here, it will never + // be exposed to users. binding.mainDrawer.addItems( + DividerDrawerItem(), secondaryDrawerItem { - nameText = "debug" - isEnabled = false - textColor = ColorStateList.valueOf(Color.GREEN) + nameText = "Developer tools" + isEnabled = true + iconicsIcon = GoogleMaterial.Icon.gmd_developer_mode + onClick = { + buildDeveloperToolsDialog().show() + } } ) } } + private fun buildDeveloperToolsDialog(): AlertDialog { + return AlertDialog.Builder(this) + .setTitle("Developer Tools") + .setItems( + arrayOf("Create \"Load more\" gap") + ) { _, which -> + Log.d(TAG, "Developer tools: $which") + when (which) { + 0 -> { + Log.d(TAG, "Creating \"Load more\" gap") + lifecycleScope.launch { + accountManager.activeAccount?.let { + developerToolsUseCase.createLoadMoreGap( + it.id + ) + } + } + } + } + } + .create() + } + override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(binding.mainDrawer.saveInstanceState(outState)) } diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/PlaceholderViewHolder.kt b/app/src/main/java/com/keylesspalace/tusky/adapter/PlaceholderViewHolder.kt index e80e3746..c277ea38 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/PlaceholderViewHolder.kt +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/PlaceholderViewHolder.kt @@ -15,26 +15,52 @@ package com.keylesspalace.tusky.adapter import android.view.View -import android.widget.Button -import android.widget.ProgressBar import androidx.recyclerview.widget.RecyclerView +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. - * Either displays "load more" button or a progress indicator. - **/ + * + * 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) { - private val loadMoreButton: Button = itemView.findViewById(R.id.button_load_more) - private val progressBar: ProgressBar = itemView.findViewById(R.id.progressBar) + 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, progress: Boolean) { - loadMoreButton.visibility = if (progress) View.GONE else View.VISIBLE - progressBar.visibility = if (progress) View.VISIBLE else View.GONE - loadMoreButton.isEnabled = true - loadMoreButton.setOnClickListener { v: View? -> + 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 + + // 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 + loadMoreButton.icon = drawable + loadMoreButton.text = "" listener.onLoadMore(bindingAdapterPosition) } } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt index ddc8d372..807b0429 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/preference/PreferencesFragment.kt @@ -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) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/timeline/TimelineFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/timeline/TimelineFragment.kt index 7c6f3367..f8395c56 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/timeline/TimelineFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/timeline/TimelineFragment.kt @@ -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) + ) + } } } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/timeline/TimelineTypeMappers.kt b/app/src/main/java/com/keylesspalace/tusky/components/timeline/TimelineTypeMappers.kt index 735f0492..dc220b3f 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/timeline/TimelineTypeMappers.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/timeline/TimelineTypeMappers.kt @@ -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) } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/CachedTimelineRemoteMediator.kt b/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/CachedTimelineRemoteMediator.kt index b29ab0cf..a1557764 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/CachedTimelineRemoteMediator.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/CachedTimelineRemoteMediator.kt @@ -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 diff --git a/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/CachedTimelineViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/CachedTimelineViewModel.kt index 779fe38d..a8eaaf32 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/CachedTimelineViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/CachedTimelineViewModel.kt @@ -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) ) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/NetworkTimelineViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/NetworkTimelineViewModel.kt index 7335e8f3..f569b57f 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/NetworkTimelineViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/NetworkTimelineViewModel.kt @@ -259,7 +259,7 @@ class NetworkTimelineViewModel @Inject constructor( limit: Int ): Response> { 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 -> { diff --git a/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/TimelineViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/TimelineViewModel.kt index b5ed9c4e..ea2b687f 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/TimelineViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/timeline/viewmodel/TimelineViewModel.kt @@ -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)) + } } } diff --git a/app/src/main/java/com/keylesspalace/tusky/db/TimelineDao.kt b/app/src/main/java/com/keylesspalace/tusky/db/TimelineDao.kt index 3387c8a5..dfd15326 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/TimelineDao.kt +++ b/app/src/main/java/com/keylesspalace/tusky/db/TimelineDao.kt @@ -214,6 +214,13 @@ AND timelineUserId = :accountId @Query("SELECT serverId FROM TimelineStatusEntity WHERE timelineUserId = :accountId AND (LENGTH(:serverId) < LENGTH(serverId) OR (LENGTH(:serverId) = LENGTH(serverId) AND :serverId < serverId)) ORDER BY LENGTH(serverId) ASC, serverId ASC LIMIT 1") abstract suspend fun getIdAbove(accountId: Long, serverId: String): String? + /** + * Returns the ID directly below [serverId], or null if [serverId] is the ID of the bottom + * status + */ + @Query("SELECT serverId FROM TimelineStatusEntity WHERE timelineUserId = :accountId AND (LENGTH(:serverId) > LENGTH(serverId) OR (LENGTH(:serverId) = LENGTH(serverId) AND :serverId > serverId)) ORDER BY LENGTH(serverId) DESC, serverId DESC LIMIT 1") + abstract suspend fun getIdBelow(accountId: Long, serverId: String): String? + /** * Returns the id of the next placeholder after [serverId] */ @@ -222,4 +229,12 @@ AND timelineUserId = :accountId @Query("SELECT COUNT(*) FROM TimelineStatusEntity WHERE timelineUserId = :accountId") abstract suspend fun getStatusCount(accountId: Long): Int + + /** Developer tools: Find N most recent status IDs */ + @Query("SELECT serverId FROM TimelineStatusEntity WHERE timelineUserId = :accountId ORDER BY LENGTH(serverId) DESC, serverId DESC LIMIT :count") + abstract suspend fun getMostRecentNStatusIds(accountId: Long, count: Int): List + + /** Developer tools: Convert a status to a placeholder */ + @Query("UPDATE TimelineStatusEntity SET authorServerId = NULL WHERE serverId = :serverId") + abstract suspend fun convertStatustoPlaceholder(serverId: String) } diff --git a/app/src/main/java/com/keylesspalace/tusky/db/TimelineStatusEntity.kt b/app/src/main/java/com/keylesspalace/tusky/db/TimelineStatusEntity.kt index e65c5f4a..3bc4bc7d 100644 --- a/app/src/main/java/com/keylesspalace/tusky/db/TimelineStatusEntity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/db/TimelineStatusEntity.kt @@ -77,13 +77,17 @@ data class TimelineStatusEntity( val reblogAccountId: String?, val poll: String?, val muted: Boolean?, - val expanded: Boolean, // used as the "loading" attribute when this TimelineStatusEntity is a placeholder + /** Also used as the "loading" attribute when this TimelineStatusEntity is a placeholder */ + val expanded: Boolean, val contentCollapsed: Boolean, val contentShowing: Boolean, val pinned: Boolean, val card: String?, val language: String?, -) +) { + val isPlaceholder: Boolean + get() = this.authorServerId == null +} @Entity( primaryKeys = ["serverId", "timelineUserId"] diff --git a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt index d21d3a86..5633ad3d 100644 --- a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt +++ b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt @@ -90,6 +90,7 @@ interface MastodonApi { @Throws(Exception::class) suspend fun homeTimeline( @Query("max_id") maxId: String? = null, + @Query("min_id") minId: String? = null, @Query("since_id") sinceId: String? = null, @Query("limit") limit: Int? = null ): Response> diff --git a/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt b/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt index 2eaca64e..6df811d6 100644 --- a/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt +++ b/app/src/main/java/com/keylesspalace/tusky/settings/SettingsConstants.kt @@ -21,6 +21,7 @@ object PrefKeys { const val FAB_HIDE = "fabHide" const val LANGUAGE = "language" const val STATUS_TEXT_SIZE = "statusTextSize" + const val READING_ORDER = "readingOrder" const val MAIN_NAV_POSITION = "mainNavPosition" const val HIDE_TOP_TOOLBAR = "hideTopToolbar" const val ABSOLUTE_TIME_VIEW = "absoluteTimeView" diff --git a/app/src/main/java/com/keylesspalace/tusky/usecase/DeveloperToolsUseCase.kt b/app/src/main/java/com/keylesspalace/tusky/usecase/DeveloperToolsUseCase.kt new file mode 100644 index 00000000..8724718a --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/usecase/DeveloperToolsUseCase.kt @@ -0,0 +1,46 @@ +package com.keylesspalace.tusky.usecase + +import android.util.Log +import androidx.room.withTransaction +import com.keylesspalace.tusky.db.AppDatabase +import com.keylesspalace.tusky.db.TimelineDao +import javax.inject.Inject + +/** + * Functionality that is only intended to be used by the "Developer Tools" menu when built + * in debug mode. + */ +class DeveloperToolsUseCase @Inject constructor( + private val db: AppDatabase +) { + + private var timelineDao: TimelineDao = db.timelineDao() + + /** + * Create a gap in the home timeline to make it easier to interactively experiment with + * different "Load more" behaviours. + * + * Do this by taking the 10 most recent statuses, keeping the first 2, deleting the next 7, + * and replacing the last one with a placeholder. + */ + suspend fun createLoadMoreGap(accountId: Long) { + db.withTransaction { + val ids = timelineDao.getMostRecentNStatusIds(accountId, 10) + val maxId = ids[2] + val minId = ids[8] + val placeHolderId = ids[9] + + Log.d( + "TAG", + "createLoadMoreGap: creating gap between $minId .. $maxId (new placeholder: $placeHolderId" + ) + + timelineDao.deleteRange(accountId, minId, maxId) + timelineDao.convertStatustoPlaceholder(placeHolderId) + } + } + + companion object { + const val TAG = "DeveloperToolsUseCase" + } +} diff --git a/app/src/main/res/layout/item_status_placeholder.xml b/app/src/main/res/layout/item_status_placeholder.xml index fdaeb9b3..daa45fd2 100644 --- a/app/src/main/res/layout/item_status_placeholder.xml +++ b/app/src/main/res/layout/item_status_placeholder.xml @@ -1,23 +1,50 @@ - -