I am trying to create an image gallery get data using paging3 library.
Since there is no log in load of GalleryPagingSource, The load call does not work
So my guess is that the load function of pagingSource is not working. Can you tell me which part is wrong?
data Pojo
data class GalleryPhoto(
var id: Long? = null ,
var filepath: String? = null,
var uri: Uri? = null,
var name: String? = null,
var date: String? = null,
var size: Int? = null
)
Activity
val galleryPhotoRepository: GalleryPhotoRepository = GalleryPhotoRepository(GalleryModel(), applicationContext)
lifecycleScope.launch {
viewModel.getGalleryPagingImages(galleryPhotoRepository)
viewModel.customGalleryPhotoList.cachedIn(this).collectLatest{
Timber.d("Test Checked asdf $it")
gridImagePickerAdapter.submitData(it)
}
}
ViewModel
suspend fun getGalleryPagingImages(galleryPhotoRepository: GalleryPhotoRepository) {
Timber.d("Test Checked asdf ~!")
galleryPhotoRepository.getTodoContentItemsByPaging()
.cachedIn(viewModelScope)
.collect {
_customGalleryPhotoList.value = it
}
}
repository
class GalleryPhotoRepository(private val todoDao: GalleryModel, val context: Context) {
fun getTodoContentItemsByPaging(): Flow<PagingData<ImagePojo.GalleryPhoto>> {
return Pager(
config = PagingConfig(pageSize = 50),
pagingSourceFactory = { GalleryPagingSource(todoDao, context) }
).flow
}
}
pagingSource
class GalleryPagingSource(
private val dao: GalleryModel,
val context : Context
): PagingSource<Int, ImagePojo.GalleryPhoto>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ImagePojo.GalleryPhoto> {
val page = params.key ?: 1
return try {
//items 에서 이미지 변환
val items = dao.getAllPhotos(page, params.loadSize, context)
Timber.d("Test CC ${items[0]}")
LoadResult.Page(
data = items,
prevKey = if (page == 1) null else page - 1,
nextKey = if (items.isEmpty()) null else page + (params.loadSize / 10)
)
} catch (e: Exception) {
Timber.d("Test CC $e")
return LoadResult.Error(e)
}
}
override fun getRefreshKey(state: PagingState<Int, ImagePojo.GalleryPhoto>): Int? {
return state.anchorPosition?.let { anchorPosition ->
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1)
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1)
}
}
}
GalleryModel
class GalleryModel {
fun getAllPhotos(
page: Int,
loadSize: Int,
context: Context
): MutableList<ImagePojo.GalleryPhoto> {
val galleryPhotoList = mutableListOf<ImagePojo.GalleryPhoto>()
// 외장 메모리에 있는 URI를 받도록 함
val uriExternal: Uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
// 커서에 가져올 정보에 대해서 지정한다.
val query: Cursor?
val projection = arrayOf(
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.DISPLAY_NAME, // 이름
MediaStore.Images.ImageColumns.SIZE, // 크기
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.DATE_ADDED, // 추가된 날짜
MediaStore.Images.ImageColumns._ID
)
val resolver = context.contentResolver
var selection: String? = null
var selectionArgs: Array<String>? = null
query = resolver?.query(
uriExternal,
projection,
selection,
selectionArgs,
"${MediaStore.Images.ImageColumns.DATE_ADDED} DESC"
)
query?.use { cursor ->
val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns._ID)
val nameColumn =
cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DISPLAY_NAME)
val filePathColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA)
val sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.SIZE)
val dateColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATE_TAKEN)
val id = cursor.getLong(idColumn)
val filepath = cursor.getString(filePathColumn)
val name = cursor.getString(nameColumn)
val size = cursor.getInt(sizeColumn)
val date = cursor.getString(dateColumn)
val contentUri = ContentUris.withAppendedId(uriExternal, id)
while (cursor.moveToNext() && cursor.position < loadSize * page) {
if (cursor.position >= (page - 1) * loadSize) {
galleryPhotoList.add(
ImagePojo.GalleryPhoto(
id,
filepath = filepath,
uri = contentUri,
name = name,
date = date ?: "",
size = size
))
}
}
}
for(i in 0 until galleryPhotoList.size) {
Timber.d("Test CC ! ${galleryPhotoList[i].uri}")
}
return galleryPhotoList
}
}