-
Notifications
You must be signed in to change notification settings - Fork 0
홈 화면 갤러리 목록에 페이징을 적용한다 #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
527a1e5
6293752
ce182f4
1bab8ad
400a9a3
e4d430f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package com.example.metasearch.core.data.impl.datasource | ||
|
|
||
| import android.content.ContentResolver | ||
| import android.content.ContentUris | ||
| import android.content.Context | ||
| import android.provider.MediaStore | ||
| import androidx.paging.PagingSource | ||
| import androidx.paging.PagingState | ||
| import com.example.metasearch.core.model.GalleryImageModel | ||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.withContext | ||
|
|
||
| class GalleryPagingSource( | ||
| private val context: Context, | ||
| private val ioDispatcher: CoroutineDispatcher, | ||
| ) : PagingSource<Int, GalleryImageModel>() { | ||
|
|
||
| override suspend fun load(params: LoadParams<Int>): LoadResult<Int, GalleryImageModel> { | ||
| return withContext(ioDispatcher) { | ||
| try { | ||
| val offset = params.key ?: 0 | ||
| val limit = params.loadSize | ||
| val imageList = mutableListOf<GalleryImageModel>() | ||
|
|
||
| val projection = arrayOf( | ||
| MediaStore.Images.Media._ID, | ||
| MediaStore.Images.Media.DATE_ADDED, | ||
| ) | ||
|
|
||
| val queryArgs = android.os.Bundle().apply { | ||
| putInt(ContentResolver.QUERY_ARG_LIMIT, limit) | ||
| putInt(ContentResolver.QUERY_ARG_OFFSET, offset) | ||
| putStringArray(ContentResolver.QUERY_ARG_SORT_COLUMNS, arrayOf(MediaStore.Images.Media.DATE_ADDED)) | ||
| putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING) | ||
| } | ||
|
|
||
| context.contentResolver.query( | ||
| MediaStore.Images.Media.EXTERNAL_CONTENT_URI, | ||
| projection, | ||
| queryArgs, | ||
| null, | ||
| )?.use { cursor -> | ||
| val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID) | ||
| val dateColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_ADDED) | ||
|
|
||
| while (cursor.moveToNext()) { | ||
| val id = cursor.getLong(idColumn) | ||
| val date = cursor.getLong(dateColumn) | ||
| val uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id) | ||
| imageList.add(GalleryImageModel(id = id, uriString = uri.toString(), dateAdded = date)) | ||
| } | ||
| } | ||
|
|
||
| LoadResult.Page( | ||
| data = imageList, | ||
| prevKey = if (offset == 0) null else offset - limit, | ||
| nextKey = if (imageList.size < limit) null else offset + imageList.size, | ||
| ) | ||
| } catch (e: Exception) { | ||
| LoadResult.Error(e) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun getRefreshKey(state: PagingState<Int, GalleryImageModel>): Int? { | ||
| return state.anchorPosition?.let { anchorPosition -> | ||
| state.closestPageToPosition(anchorPosition)?.prevKey?.plus(state.config.pageSize) | ||
| ?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(state.config.pageSize) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.example.metasearch.core.model | ||
|
|
||
| import androidx.compose.runtime.Stable | ||
|
|
||
| @Stable | ||
| data class GalleryImageModel( | ||
| val id: Long, // MediaStore._ID | ||
| val uriString: String, | ||
| val dateAdded: Long, | ||
| ) |
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -8,14 +8,15 @@ import androidx.compose.foundation.layout.Box | |||||||||
| import androidx.compose.foundation.layout.Column | ||||||||||
| import androidx.compose.foundation.layout.PaddingValues | ||||||||||
| import androidx.compose.foundation.layout.Row | ||||||||||
| import androidx.compose.foundation.layout.WindowInsets | ||||||||||
| import androidx.compose.foundation.layout.fillMaxSize | ||||||||||
| import androidx.compose.foundation.layout.fillMaxWidth | ||||||||||
| import androidx.compose.foundation.layout.padding | ||||||||||
| import androidx.compose.foundation.layout.size | ||||||||||
| import androidx.compose.foundation.layout.statusBarsPadding | ||||||||||
| import androidx.compose.foundation.lazy.LazyRow | ||||||||||
| import androidx.compose.foundation.lazy.grid.GridCells | ||||||||||
| import androidx.compose.foundation.lazy.grid.LazyVerticalGrid | ||||||||||
| import androidx.compose.foundation.lazy.grid.items | ||||||||||
| import androidx.compose.foundation.lazy.items | ||||||||||
| import androidx.compose.material3.Icon | ||||||||||
| import androidx.compose.material3.MaterialTheme | ||||||||||
|
|
@@ -27,10 +28,14 @@ import androidx.compose.ui.Modifier | |||||||||
| import androidx.compose.ui.res.painterResource | ||||||||||
| import androidx.compose.ui.res.stringResource | ||||||||||
| import androidx.compose.ui.unit.dp | ||||||||||
| import androidx.core.net.toUri | ||||||||||
| import androidx.paging.LoadState | ||||||||||
| import androidx.paging.PagingData.Companion.from | ||||||||||
| import androidx.paging.compose.collectAsLazyPagingItems | ||||||||||
| import androidx.paging.compose.itemKey | ||||||||||
| import com.example.metasearch.core.designsystem.annotation.DevicePreview | ||||||||||
| import com.example.metasearch.core.designsystem.theme.MetaSearchTheme | ||||||||||
| import com.example.metasearch.core.designsystem.theme.Neutral500 | ||||||||||
| import com.example.metasearch.core.model.GalleryImageModel | ||||||||||
| import com.example.metasearch.core.ui.MetaSearchScaffold | ||||||||||
| import com.example.metasearch.core.ui.component.MetaSearchLoadingIndicator | ||||||||||
| import com.example.metasearch.core.ui.component.MetaSearchSquareImage | ||||||||||
|
|
@@ -41,6 +46,7 @@ import com.example.metasearch.feature.screens.component.MetaSearchMainBottomBar | |||||||||
| import com.example.metasearch.feature.screens.component.MetaSearchMainTabItem | ||||||||||
| import com.slack.circuit.codegen.annotations.CircuitInject | ||||||||||
| import dagger.hilt.android.components.ActivityRetainedComponent | ||||||||||
| import kotlinx.coroutines.flow.flowOf | ||||||||||
|
|
||||||||||
| @CircuitInject(HomeScreen::class, ActivityRetainedComponent::class) | ||||||||||
| @Composable | ||||||||||
|
|
@@ -50,9 +56,11 @@ fun HomeUi( | |||||||||
| ) { | ||||||||||
| MetaSearchScaffold( | ||||||||||
| modifier = modifier.fillMaxSize(), | ||||||||||
| contentWindowInsets = WindowInsets(bottom = 0), | ||||||||||
| bottomBar = { | ||||||||||
| MetaSearchMainBottomBar( | ||||||||||
| modifier = modifier, | ||||||||||
| modifier = modifier | ||||||||||
| .padding(bottom = MetaSearchTheme.spacing.spacing3), | ||||||||||
|
Comment on lines
+62
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid reusing the outer The 🐛 Proposed fix MetaSearchMainBottomBar(
- modifier = modifier
+ modifier = Modifier
.padding(bottom = MetaSearchTheme.spacing.spacing3),📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
| currentTab = MetaSearchMainTabItem.HOME, | ||||||||||
| onTabSelected = { | ||||||||||
| state.eventSink(HomeUiEvent.OnTabClick(it.screen)) | ||||||||||
|
|
@@ -72,8 +80,12 @@ private fun HomeUiContent( | |||||||||
| state: HomeUiState, | ||||||||||
| innerPadding: PaddingValues, | ||||||||||
| ) { | ||||||||||
| val lazyPagingItems = state.images.collectAsLazyPagingItems() | ||||||||||
|
|
||||||||||
| Column( | ||||||||||
| modifier = Modifier.padding(innerPadding), | ||||||||||
| modifier = Modifier | ||||||||||
| .fillMaxSize() | ||||||||||
| .statusBarsPadding(), | ||||||||||
| ) { | ||||||||||
| HomeHeader( | ||||||||||
| onUploadClick = { | ||||||||||
|
|
@@ -145,28 +157,37 @@ private fun HomeUiContent( | |||||||||
| modifier = Modifier.padding(MetaSearchTheme.spacing.spacing2), | ||||||||||
| text = stringResource( | ||||||||||
| R.string.home_screen_gallery_grid_view_title, | ||||||||||
| state.images.size, | ||||||||||
| lazyPagingItems.itemCount, | ||||||||||
| ), | ||||||||||
| color = Neutral500, | ||||||||||
| ) | ||||||||||
| Box( | ||||||||||
| modifier = Modifier.fillMaxSize(), | ||||||||||
| modifier = Modifier.weight(1f), | ||||||||||
| ) { | ||||||||||
| LazyVerticalGrid( | ||||||||||
| modifier = Modifier.fillMaxSize(), | ||||||||||
| columns = GridCells.Fixed(5), | ||||||||||
| contentPadding = PaddingValues( | ||||||||||
| bottom = innerPadding.calculateBottomPadding() + 16.dp, | ||||||||||
| ), | ||||||||||
| ) { | ||||||||||
| items(state.images) { uri -> | ||||||||||
| MetaSearchSquareImage( | ||||||||||
| model = uri, | ||||||||||
| onClick = { | ||||||||||
| state.eventSink(HomeUiEvent.OnImageClick(uri.toString())) | ||||||||||
| }, | ||||||||||
| ) | ||||||||||
| items( | ||||||||||
| count = lazyPagingItems.itemCount, | ||||||||||
| key = lazyPagingItems.itemKey { it.id }, | ||||||||||
| ) { index -> | ||||||||||
| val item = lazyPagingItems[index] | ||||||||||
| if (item != null) { | ||||||||||
| MetaSearchSquareImage( | ||||||||||
| model = item.uriString, | ||||||||||
| onClick = { | ||||||||||
| state.eventSink(HomeUiEvent.OnImageClick(item.uriString)) | ||||||||||
| }, | ||||||||||
| ) | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if (state.isGalleryLoading) { | ||||||||||
| if (lazyPagingItems.loadState.refresh is LoadState.Loading) { | ||||||||||
| MetaSearchLoadingIndicator(modifier = Modifier.align(Alignment.Center)) | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
@@ -177,6 +198,14 @@ private fun HomeUiContent( | |||||||||
| @Composable | ||||||||||
| private fun HomeUiPreview() { | ||||||||||
| MetaSearchTheme { | ||||||||||
| val fakeImages = List(20) { index -> | ||||||||||
| GalleryImageModel( | ||||||||||
| id = index.toLong(), | ||||||||||
| uriString = "android.resource://com.example.metasearch/drawable/ic_launcher_foreground", | ||||||||||
| dateAdded = System.currentTimeMillis(), | ||||||||||
| ) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| HomeUi( | ||||||||||
| state = HomeUiState( | ||||||||||
| isExpanded = true, | ||||||||||
|
|
@@ -225,9 +254,7 @@ private fun HomeUiPreview() { | |||||||||
| // isHomeDisplay = true, | ||||||||||
| // ), | ||||||||||
| // ), | ||||||||||
| images = List(20) { | ||||||||||
| "android.resource://com.example.metasearch/feature/home/drawable/ic_launcher_foreground".toUri() | ||||||||||
| }, | ||||||||||
| images = flowOf(from(fakeImages)), | ||||||||||
| eventSink = {}, | ||||||||||
| ), | ||||||||||
| ) | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Potential negative prevKey value.
When
offset < limit(e.g., on initial load withinitialLoadSize=60butpageSize=30),prevKey = offset - limitwould produce a negative value. This should be clamped to avoid invalid page keys.🐛 Proposed fix
LoadResult.Page( data = imageList, - prevKey = if (offset == 0) null else offset - limit, + prevKey = if (offset == 0) null else (offset - limit).coerceAtLeast(0).takeIf { it < offset }, nextKey = if (imageList.size < limit) null else offset + imageList.size, )Alternatively, a simpler approach:
📝 Committable suggestion
🤖 Prompt for AI Agents