Skip to content

Commit d71fdfd

Browse files
committed
fix(ui/paging): 优化分页键逻辑提高标识唯一性
- 新增`itemIndexKey`方法替换多个页面中使用的`itemKey`,以支持索引与内容结合的唯一标识逻辑。 - 调整`TrendingPage`、`FollowingScreen`、`SearchResultScreen`等页面的分页项键定义,保证键值的唯一性及一致性。 - 提取`Pager.kt`工具文件封装分页相关逻辑,提高代码的可复用性与维护性。 - 移除过时的`itemKey`直接引用,统一升级至改进版本。
1 parent 3415d48 commit d71fdfd

8 files changed

Lines changed: 34 additions & 19 deletions

File tree

common/ui/src/commonMain/kotlin/com/mrl/pixiv/common/compose/ui/illust/IllustGrid.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import androidx.compose.ui.Modifier
1313
import androidx.compose.ui.unit.dp
1414
import androidx.paging.LoadState
1515
import androidx.paging.compose.LazyPagingItems
16-
import androidx.paging.compose.itemKey
1716
import com.mrl.pixiv.common.data.Illust
17+
import com.mrl.pixiv.common.kts.itemIndexKey
1818
import com.mrl.pixiv.common.repository.viewmodel.bookmark.BookmarkState
1919
import com.mrl.pixiv.common.repository.viewmodel.bookmark.isBookmark
2020
import com.mrl.pixiv.common.router.NavigateToHorizontalPictureScreen
@@ -40,7 +40,7 @@ fun LazyGridScope.illustGrid(
4040
}
4141
items(
4242
illusts.itemCount,
43-
key = { index -> illusts.itemKey { "${index}_${it.id}" }(index) }
43+
key = illusts.itemIndexKey { index, item -> "${index}_${item.id}" }
4444
) { index ->
4545
val illust = illusts[index] ?: return@items
4646
val isBookmarked = illust.isBookmark
@@ -81,7 +81,7 @@ fun LazyStaggeredGridScope.illustGrid(
8181
}
8282
items(
8383
illusts.itemCount,
84-
key = { index -> illusts.itemKey { "${index}_${it.id}" }(index) }
84+
key = illusts.itemIndexKey { index, item -> "${index}_${item.id}" }
8585
) { index ->
8686
val illust = illusts[index] ?: return@items
8787
val isBookmarked = illust.isBookmark
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.mrl.pixiv.common.kts
2+
3+
import androidx.paging.compose.LazyPagingItems
4+
import androidx.paging.compose.itemKey
5+
6+
inline fun <T : Any> LazyPagingItems<T>.itemIndexKey(
7+
crossinline key: (index: Int, item: T) -> Any
8+
): (index: Int) -> Any {
9+
return { index ->
10+
itemKey { key(index, it) }(index)
11+
}
12+
}

feature/comment/src/commonMain/kotlin/com/mrl/pixiv/comment/CommentScreen.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
3939
import androidx.paging.LoadState
4040
import androidx.paging.compose.LazyPagingItems
4141
import androidx.paging.compose.collectAsLazyPagingItems
42-
import androidx.paging.compose.itemKey
4342
import com.mrl.pixiv.comment.components.CommentInput
4443
import com.mrl.pixiv.comment.components.CommentInputPlaceholder
4544
import com.mrl.pixiv.comment.components.CommentItem
4645
import com.mrl.pixiv.common.data.comment.Comment
4746
import com.mrl.pixiv.common.kts.VSpacer
4847
import com.mrl.pixiv.common.kts.hPadding
48+
import com.mrl.pixiv.common.kts.itemIndexKey
4949
import com.mrl.pixiv.common.repository.BlockingRepositoryV2
5050
import com.mrl.pixiv.common.repository.CommentRepository
5151
import com.mrl.pixiv.common.router.CommentType
@@ -191,7 +191,7 @@ fun CommentScreen(
191191
) {
192192
items(
193193
count = comments.itemCount,
194-
key = comments.itemKey { it.id }
194+
key = comments.itemIndexKey { index, item -> "${index}_${item.id}" }
195195
) { index ->
196196
val comment = comments[index] ?: return@items
197197
val isBlocked = BlockingRepositoryV2.collectCommentBlockAsState(comment.id)
@@ -368,7 +368,7 @@ private fun RepliesContent(
368368

369369
items(
370370
count = replies.itemCount,
371-
key = replies.itemKey { it.id }
371+
key = replies.itemIndexKey { index, item -> "${index}_${item.id}" }
372372
) { index ->
373373
val comment = replies[index] ?: return@items
374374
Column(

feature/follow/src/commonMain/kotlin/com/mrl/pixiv/follow/FollowingScreen.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ import androidx.compose.ui.graphics.RectangleShape
4747
import androidx.compose.ui.unit.dp
4848
import androidx.paging.LoadState
4949
import androidx.paging.compose.collectAsLazyPagingItems
50-
import androidx.paging.compose.itemKey
5150
import com.mrl.pixiv.common.analytics.logEvent
5251
import com.mrl.pixiv.common.compose.IllustGridDefaults
5352
import com.mrl.pixiv.common.compose.layout.isWidthAtLeastMedium
@@ -56,6 +55,7 @@ import com.mrl.pixiv.common.compose.rememberThrottleClick
5655
import com.mrl.pixiv.common.compose.ui.illust.SquareIllustItem
5756
import com.mrl.pixiv.common.compose.ui.image.UserAvatar
5857
import com.mrl.pixiv.common.data.Illust
58+
import com.mrl.pixiv.common.kts.itemIndexKey
5959
import com.mrl.pixiv.common.kts.spaceBy
6060
import com.mrl.pixiv.common.repository.isSelf
6161
import com.mrl.pixiv.common.repository.viewmodel.bookmark.BookmarkState
@@ -222,7 +222,7 @@ fun FollowingScreenBody(
222222
) {
223223
items(
224224
followingUsers.itemCount,
225-
key = followingUsers.itemKey { it.user.id }
225+
key = followingUsers.itemIndexKey { index, user -> "${index}_${user.user.id}" }
226226
) {
227227
val userPreview = followingUsers[it] ?: return@items
228228
FollowingUserCard(
@@ -251,8 +251,8 @@ fun FollowingScreenBody(
251251
verticalArrangement = 10f.spaceBy,
252252
) {
253253
items(
254-
followingUsers.itemCount,
255-
key = followingUsers.itemKey { it.user.id }
254+
count = followingUsers.itemCount,
255+
key = followingUsers.itemIndexKey { index, item -> "${index}_${item.user.id}" }
256256
) {
257257
val userPreview = followingUsers[it] ?: return@items
258258
FollowingUserCard(

feature/main/src/commonMain/kotlin/com/mrl/pixiv/home/components/RecommendGrid.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import androidx.compose.runtime.Composable
88
import androidx.compose.ui.Modifier
99
import androidx.compose.ui.unit.dp
1010
import androidx.paging.compose.LazyPagingItems
11-
import androidx.paging.compose.itemKey
1211
import com.mrl.pixiv.common.compose.RecommendGridDefaults
1312
import com.mrl.pixiv.common.compose.ui.illust.RectangleIllustItem
1413
import com.mrl.pixiv.common.data.Illust
14+
import com.mrl.pixiv.common.kts.itemIndexKey
1515
import com.mrl.pixiv.common.repository.viewmodel.bookmark.BookmarkState
1616
import com.mrl.pixiv.common.repository.viewmodel.bookmark.isBookmark
1717
import com.mrl.pixiv.common.router.NavigateToHorizontalPictureScreen
@@ -32,7 +32,10 @@ fun RecommendGrid(
3232
horizontalArrangement = layoutParams.horizontalArrangement,
3333
modifier = Modifier.fillMaxSize()
3434
) {
35-
items(recommendImageList.itemCount, key = recommendImageList.itemKey { it.id }) {
35+
items(
36+
count = recommendImageList.itemCount,
37+
key = recommendImageList.itemIndexKey { index, item -> "${index}_${item.id}" }
38+
) {
3639
val illust = recommendImageList[it] ?: return@items
3740
val isBookmarked = illust.isBookmark
3841
RectangleIllustItem(

feature/main/src/commonMain/kotlin/com/mrl/pixiv/latest/CollectionPage.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ import androidx.compose.ui.Modifier
3030
import androidx.compose.ui.unit.dp
3131
import androidx.paging.LoadState
3232
import androidx.paging.compose.collectAsLazyPagingItems
33-
import androidx.paging.compose.itemKey
3433
import com.mrl.pixiv.collection.CollectionAction
3534
import com.mrl.pixiv.collection.CollectionViewModel
3635
import com.mrl.pixiv.collection.components.FilterDialog
3736
import com.mrl.pixiv.common.compose.RecommendGridDefaults
3837
import com.mrl.pixiv.common.compose.ui.illust.RectangleIllustItem
3938
import com.mrl.pixiv.common.data.Restrict
39+
import com.mrl.pixiv.common.kts.itemIndexKey
4040
import com.mrl.pixiv.common.kts.spaceBy
4141
import com.mrl.pixiv.common.repository.viewmodel.bookmark.BookmarkState
4242
import com.mrl.pixiv.common.repository.viewmodel.bookmark.isBookmark
@@ -97,7 +97,7 @@ fun CollectionPage(
9797
}
9898
items(
9999
count = userBookmarksIllusts.itemCount,
100-
key = userBookmarksIllusts.itemKey { it.id }
100+
key = userBookmarksIllusts.itemIndexKey { index, item -> "${index}_${item.id}" }
101101
) { index ->
102102
val illust = userBookmarksIllusts[index] ?: return@items
103103
val isBookmarked = illust.isBookmark

feature/main/src/commonMain/kotlin/com/mrl/pixiv/latest/TrendingPage.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import androidx.compose.ui.unit.dp
2323
import androidx.lifecycle.compose.collectAsStateWithLifecycle
2424
import androidx.paging.LoadState
2525
import androidx.paging.compose.collectAsLazyPagingItems
26-
import androidx.paging.compose.itemKey
2726
import com.mrl.pixiv.common.compose.RecommendGridDefaults
2827
import com.mrl.pixiv.common.compose.ui.illust.RectangleIllustItem
2928
import com.mrl.pixiv.common.data.Restrict
29+
import com.mrl.pixiv.common.kts.itemIndexKey
3030
import com.mrl.pixiv.common.kts.spaceBy
3131
import com.mrl.pixiv.common.repository.viewmodel.bookmark.BookmarkState
3232
import com.mrl.pixiv.common.repository.viewmodel.bookmark.isBookmark
@@ -83,7 +83,7 @@ fun TrendingPage(
8383
}
8484
items(
8585
count = illustsFollowing.itemCount,
86-
key = illustsFollowing.itemKey { it.id }
86+
key = illustsFollowing.itemIndexKey { index, item -> "${index}_${item.id}" }
8787
) { index ->
8888
val illust = illustsFollowing[index] ?: return@items
8989
val isBookmarked = illust.isBookmark

feature/main/src/commonMain/kotlin/com/mrl/pixiv/search/result/SearchResultScreen.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ import androidx.compose.ui.Modifier
3434
import androidx.compose.ui.unit.dp
3535
import androidx.paging.LoadState
3636
import androidx.paging.compose.collectAsLazyPagingItems
37-
import androidx.paging.compose.itemKey
3837
import com.mrl.pixiv.common.analytics.logEvent
3938
import com.mrl.pixiv.common.compose.IllustGridDefaults
4039
import com.mrl.pixiv.common.compose.ui.illust.illustGrid
40+
import com.mrl.pixiv.common.kts.itemIndexKey
4141
import com.mrl.pixiv.common.kts.spaceBy
4242
import com.mrl.pixiv.common.repository.viewmodel.follow.isFollowing
4343
import com.mrl.pixiv.common.router.NavigationManager
@@ -192,8 +192,8 @@ fun SearchResultsScreen(
192192
verticalArrangement = 10f.spaceBy,
193193
) {
194194
items(
195-
userSearchResults.itemCount,
196-
key = userSearchResults.itemKey { it.user.id }
195+
count = userSearchResults.itemCount,
196+
key = userSearchResults.itemIndexKey { index, item -> "${index}_${item.user.id}" }
197197
) { index ->
198198
val userPreview = userSearchResults[index] ?: return@items
199199
FollowingUserCard(

0 commit comments

Comments
 (0)