@@ -95,6 +95,8 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
95
95
import androidx.navigation.NavController
96
96
import androidx.paging.compose.LazyPagingItems
97
97
import androidx.paging.compose.collectAsLazyPagingItems
98
+ import androidx.paging.compose.itemContentType
99
+ import androidx.paging.compose.itemKey
98
100
import com.nononsenseapps.feeder.ApplicationCoroutineScope
99
101
import com.nononsenseapps.feeder.R
100
102
import com.nononsenseapps.feeder.archmodel.FeedItemStyle
@@ -140,8 +142,6 @@ import com.nononsenseapps.feeder.ui.compose.utils.rememberIsItemVisible
140
142
import com.nononsenseapps.feeder.util.ActivityLauncher
141
143
import com.nononsenseapps.feeder.util.ToastMaker
142
144
import com.nononsenseapps.feeder.util.emailBugReportIntent
143
- import com.nononsenseapps.feeder.util.logDebug
144
- import kotlinx.coroutines.CoroutineScope
145
145
import kotlinx.coroutines.Dispatchers
146
146
import kotlinx.coroutines.delay
147
147
import kotlinx.coroutines.launch
@@ -788,7 +788,7 @@ fun FeedScreen(
788
788
gridState = feedGridState,
789
789
pagedFeedItems = pagedFeedItems,
790
790
modifier = innerModifier,
791
- ). also { logDebug( LOG_TAG , " Showing GRID " ) }
791
+ )
792
792
793
793
FeedScreenType .FeedList ->
794
794
FeedListContent (
@@ -812,7 +812,7 @@ fun FeedScreen(
812
812
listState = feedListState,
813
813
pagedFeedItems = pagedFeedItems,
814
814
modifier = innerModifier,
815
- ). also { logDebug( LOG_TAG , " Showing LIST " ) }
815
+ )
816
816
}
817
817
}
818
818
}
@@ -1109,21 +1109,18 @@ fun FeedListContent(
1109
1109
This is a trick to make the list stay at item 0 when updates come in IF it is
1110
1110
scrolled to the top.
1111
1111
*/
1112
- item {
1112
+ item(
1113
+ key = " SpacerScrollTrick" ,
1114
+ contentType = " SpacerScrollTrick" ,
1115
+ ) {
1113
1116
Spacer (modifier = Modifier .fillMaxWidth())
1114
1117
}
1115
1118
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) },
1123
1122
) { itemIndex ->
1124
- val previewItem =
1125
- pagedFeedItems[itemIndex]
1126
- ? : return @items
1123
+ val previewItem = pagedFeedItems[itemIndex] ? : PLACEHOLDER_ITEM
1127
1124
1128
1125
if (viewState.markAsReadOnScroll && previewItem.unread) {
1129
1126
val visible: Boolean by listState.rememberIsItemVisible(
@@ -1209,7 +1206,10 @@ fun FeedListContent(
1209
1206
This item is provide padding for the FAB
1210
1207
*/
1211
1208
if (viewState.showFab && ! viewState.isBottomBarVisible) {
1212
- item {
1209
+ item(
1210
+ key = " SpacerForFab" ,
1211
+ contentType = " SpacerForFab" ,
1212
+ ) {
1213
1213
Spacer (
1214
1214
modifier =
1215
1215
Modifier
@@ -1303,20 +1303,15 @@ fun FeedGridContent(
1303
1303
modifier = Modifier .fillMaxSize(),
1304
1304
) {
1305
1305
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) },
1313
1309
) { itemIndex ->
1314
- val previewItem =
1315
- pagedFeedItems[itemIndex]
1316
- ? : return @items
1310
+ val previewItem = pagedFeedItems[itemIndex] ? : PLACEHOLDER_ITEM
1317
1311
1318
1312
// 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) {
1320
1315
val visible: Boolean by gridState.rememberIsItemVisible(
1321
1316
key = previewItem.id,
1322
1317
)
@@ -1478,3 +1473,20 @@ fun MarkItemAsReadOnScroll(
1478
1473
}
1479
1474
}
1480
1475
}
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