From 5fd532d69bb27be7c65178c17416ae7ef5dfc8fd Mon Sep 17 00:00:00 2001 From: Nik Clayton Date: Sun, 11 Jun 2023 16:23:52 +0200 Subject: [PATCH] Notification tab cleanups (#3692) - Use NO_POSITION instead of hardcoding 0. - Don't set a state restoration policy, PagingDataAdapter already does that - Return the closest item, not just the closest page, in getRefreshKey --- .../tusky/components/notifications/NotificationsFragment.kt | 3 ++- .../components/notifications/NotificationsPagingAdapter.kt | 4 ---- .../components/notifications/NotificationsPagingSource.kt | 5 +++-- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationsFragment.kt b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationsFragment.kt index 19d6edf9..6191f5f6 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationsFragment.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationsFragment.kt @@ -41,6 +41,7 @@ import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.RecyclerView.NO_POSITION import androidx.recyclerview.widget.RecyclerView.SCROLL_STATE_IDLE import androidx.recyclerview.widget.SimpleItemAnimator import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener @@ -201,7 +202,7 @@ class NotificationsFragment : // Save the ID of the first notification visible in the list, so the user's // reading position is always restorable. - layoutManager.findFirstVisibleItemPosition().takeIf { it >= 0 }?.let { position -> + layoutManager.findFirstVisibleItemPosition().takeIf { it != NO_POSITION }?.let { position -> adapter.snapshot().getOrNull(position)?.id?.let { id -> viewModel.accept(InfallibleUiAction.SaveVisibleId(visibleId = id)) } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationsPagingAdapter.kt b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationsPagingAdapter.kt index d97a6957..faa1aefc 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationsPagingAdapter.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationsPagingAdapter.kt @@ -117,10 +117,6 @@ class NotificationsPagingAdapter( ) } - init { - stateRestorationPolicy = StateRestorationPolicy.PREVENT_WHEN_EMPTY - } - override fun getItemViewType(position: Int): Int { return NotificationViewKind.from(getItem(position)?.type).ordinal } diff --git a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationsPagingSource.kt b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationsPagingSource.kt index 480d5d68..b754989d 100644 --- a/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationsPagingSource.kt +++ b/app/src/main/java/com/keylesspalace/tusky/components/notifications/NotificationsPagingSource.kt @@ -204,8 +204,9 @@ class NotificationsPagingSource @Inject constructor( override fun getRefreshKey(state: PagingState): String? { return state.anchorPosition?.let { anchorPosition -> - val anchorPage = state.closestPageToPosition(anchorPosition) - anchorPage?.prevKey ?: anchorPage?.nextKey + val id = state.closestItemToPosition(anchorPosition)?.id + Log.d(TAG, " getRefreshKey returning $id") + return id } }