Skip to content

Commit 2fa239a

Browse files
committed
Improved scroll performance
1 parent e748473 commit 2fa239a

File tree

3 files changed

+44
-33
lines changed

3 files changed

+44
-33
lines changed

app/src/main/java/com/nononsenseapps/feeder/FeederApplication.kt

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class FeederApplication : Application(), DIAware, ImageLoaderFactory {
161161
.build()
162162

163163
ImageLoader.Builder(instance())
164+
.dispatcher(Dispatchers.Default)
164165
.okHttpClient(okHttpClient = okHttpClient)
165166
.diskCache(
166167
DiskCache.Builder()

app/src/main/java/com/nononsenseapps/feeder/archmodel/FeedItemStore.kt

+4-6
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ class FeedItemStore(override val di: DI) : DIAware {
6969
Pager(
7070
config =
7171
PagingConfig(
72-
pageSize = PAGE_SIZE,
73-
enablePlaceholders = false,
72+
pageSize = 10,
73+
initialLoadSize = 100,
74+
prefetchDistance = 100,
75+
jumpThreshold = 100,
7476
),
7577
) {
7678
val queryString = StringBuilder()
@@ -311,10 +313,6 @@ class FeedItemStore(override val di: DI) : DIAware {
311313
) {
312314
dao.updateWordCountFull(id, wordCount)
313315
}
314-
315-
companion object {
316-
private const val PAGE_SIZE = 100
317-
}
318316
}
319317

320318
val mediumDateTimeFormat: DateTimeFormatter =

app/src/main/java/com/nononsenseapps/feeder/ui/compose/feed/FeedScreen.kt

+39-27
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
9595
import androidx.navigation.NavController
9696
import androidx.paging.compose.LazyPagingItems
9797
import androidx.paging.compose.collectAsLazyPagingItems
98+
import androidx.paging.compose.itemContentType
99+
import androidx.paging.compose.itemKey
98100
import com.nononsenseapps.feeder.ApplicationCoroutineScope
99101
import com.nononsenseapps.feeder.R
100102
import com.nononsenseapps.feeder.archmodel.FeedItemStyle
@@ -140,8 +142,6 @@ import com.nononsenseapps.feeder.ui.compose.utils.rememberIsItemVisible
140142
import com.nononsenseapps.feeder.util.ActivityLauncher
141143
import com.nononsenseapps.feeder.util.ToastMaker
142144
import com.nononsenseapps.feeder.util.emailBugReportIntent
143-
import com.nononsenseapps.feeder.util.logDebug
144-
import kotlinx.coroutines.CoroutineScope
145145
import kotlinx.coroutines.Dispatchers
146146
import kotlinx.coroutines.delay
147147
import kotlinx.coroutines.launch
@@ -788,7 +788,7 @@ fun FeedScreen(
788788
gridState = feedGridState,
789789
pagedFeedItems = pagedFeedItems,
790790
modifier = innerModifier,
791-
).also { logDebug(LOG_TAG, "Showing GRID") }
791+
)
792792

793793
FeedScreenType.FeedList ->
794794
FeedListContent(
@@ -812,7 +812,7 @@ fun FeedScreen(
812812
listState = feedListState,
813813
pagedFeedItems = pagedFeedItems,
814814
modifier = innerModifier,
815-
).also { logDebug(LOG_TAG, "Showing LIST") }
815+
)
816816
}
817817
}
818818
}
@@ -1109,21 +1109,18 @@ fun FeedListContent(
11091109
This is a trick to make the list stay at item 0 when updates come in IF it is
11101110
scrolled to the top.
11111111
*/
1112-
item {
1112+
item(
1113+
key = "SpacerScrollTrick",
1114+
contentType = "SpacerScrollTrick",
1115+
) {
11131116
Spacer(modifier = Modifier.fillMaxWidth())
11141117
}
11151118
items(
1116-
pagedFeedItems.itemCount,
1117-
key = { itemIndex ->
1118-
pagedFeedItems.itemSnapshotList.items[itemIndex].id
1119-
},
1120-
contentType = { itemIndex ->
1121-
pagedFeedItems.itemSnapshotList.items[itemIndex].contentType(viewState.feedItemStyle)
1122-
},
1119+
count = pagedFeedItems.itemCount,
1120+
key = pagedFeedItems.itemKey { it.id },
1121+
contentType = pagedFeedItems.itemContentType { it.contentType(viewState.feedItemStyle) },
11231122
) { itemIndex ->
1124-
val previewItem =
1125-
pagedFeedItems[itemIndex]
1126-
?: return@items
1123+
val previewItem = pagedFeedItems[itemIndex] ?: PLACEHOLDER_ITEM
11271124

11281125
if (viewState.markAsReadOnScroll && previewItem.unread) {
11291126
val visible: Boolean by listState.rememberIsItemVisible(
@@ -1209,7 +1206,10 @@ fun FeedListContent(
12091206
This item is provide padding for the FAB
12101207
*/
12111208
if (viewState.showFab && !viewState.isBottomBarVisible) {
1212-
item {
1209+
item(
1210+
key = "SpacerForFab",
1211+
contentType = "SpacerForFab",
1212+
) {
12131213
Spacer(
12141214
modifier =
12151215
Modifier
@@ -1303,20 +1303,15 @@ fun FeedGridContent(
13031303
modifier = Modifier.fillMaxSize(),
13041304
) {
13051305
items(
1306-
pagedFeedItems.itemCount,
1307-
key = { itemIndex ->
1308-
pagedFeedItems.itemSnapshotList.items[itemIndex].id
1309-
},
1310-
contentType = { itemIndex ->
1311-
pagedFeedItems.itemSnapshotList.items[itemIndex].contentType(feedItemStyle)
1312-
},
1306+
count = pagedFeedItems.itemCount,
1307+
key = pagedFeedItems.itemKey { it.id },
1308+
contentType = pagedFeedItems.itemContentType { it.contentType(feedItemStyle) },
13131309
) { itemIndex ->
1314-
val previewItem =
1315-
pagedFeedItems[itemIndex]
1316-
?: return@items
1310+
val previewItem = pagedFeedItems[itemIndex] ?: PLACEHOLDER_ITEM
13171311

13181312
// Very important that items don't change size or disappear when scrolling
1319-
if (viewState.markAsReadOnScroll && previewItem.unread) {
1313+
// Placeholder will have no id
1314+
if (previewItem.id > ID_UNSET && viewState.markAsReadOnScroll && previewItem.unread) {
13201315
val visible: Boolean by gridState.rememberIsItemVisible(
13211316
key = previewItem.id,
13221317
)
@@ -1478,3 +1473,20 @@ fun MarkItemAsReadOnScroll(
14781473
}
14791474
}
14801475
}
1476+
1477+
private val PLACEHOLDER_ITEM =
1478+
FeedListItem(
1479+
id = ID_UNSET,
1480+
title = "",
1481+
snippet = "",
1482+
feedTitle = "",
1483+
unread = true,
1484+
pubDate = "",
1485+
image = null,
1486+
link = null,
1487+
bookmarked = false,
1488+
feedImageUrl = null,
1489+
primarySortTime = Instant.EPOCH,
1490+
rawPubDate = null,
1491+
wordCount = 0,
1492+
)

0 commit comments

Comments
 (0)