From 6ce60c924e5847cda87766179ddf1b1a0fc55e85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AA=85=EC=84=9D?= Date: Thu, 9 Feb 2023 09:13:31 +0900 Subject: [PATCH] =?UTF-8?q?Issues=20#287=20feat:=20data-content=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/data-content/.gitignore | 1 + data/data-content/build.gradle.kts | 23 +++++++++++++++ .../datasource/GalleryImageDataSource.kt} | 28 +++++++++++++------ .../datasource}/GalleryImagePagingSource.kt | 8 +++--- .../data/content/di/ContentModule.kt | 22 +++++++++++++++ .../lighthouse/data/content/di/DataModule.kt | 19 +++++++++++++ .../GalleryImageContentRepositoryImpl.kt | 25 +++++++++++++++++ .../repository/GalleryImageRepositoryImpl.kt | 25 ----------------- .../gallery/GalleryImageContentRepository.kt | 10 +++++++ .../gallery/GalleryImageRepositoryImpl.kt | 16 +++++++++++ .../repository/GalleryImageRepository.kt | 3 +- 11 files changed, 141 insertions(+), 39 deletions(-) create mode 100644 data/data-content/.gitignore create mode 100644 data/data-content/build.gradle.kts rename data/{data/src/main/java/com/lighthouse/datasource/gallery/GalleryImageLocalSourceImpl.kt => data-content/src/main/java/com/lighthouse/data/content/datasource/GalleryImageDataSource.kt} (71%) rename data/{data/src/main/java/com/lighthouse/datasource/gallery => data-content/src/main/java/com/lighthouse/data/content/datasource}/GalleryImagePagingSource.kt (82%) create mode 100644 data/data-content/src/main/java/com/lighthouse/data/content/di/ContentModule.kt create mode 100644 data/data-content/src/main/java/com/lighthouse/data/content/di/DataModule.kt create mode 100644 data/data-content/src/main/java/com/lighthouse/data/content/repository/GalleryImageContentRepositoryImpl.kt delete mode 100644 data/data/src/main/java/com/lighthouse/repository/GalleryImageRepositoryImpl.kt create mode 100644 data/data/src/main/java/com/lighthouse/repository/gallery/GalleryImageContentRepository.kt create mode 100644 data/data/src/main/java/com/lighthouse/repository/gallery/GalleryImageRepositoryImpl.kt diff --git a/data/data-content/.gitignore b/data/data-content/.gitignore new file mode 100644 index 000000000..42afabfd2 --- /dev/null +++ b/data/data-content/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/data/data-content/build.gradle.kts b/data/data-content/build.gradle.kts new file mode 100644 index 000000000..8cbd5656f --- /dev/null +++ b/data/data-content/build.gradle.kts @@ -0,0 +1,23 @@ +@Suppress("DSL_SCOPE_VIOLATION") +plugins { + id("beep.android.library") + id("beep.android.hilt") + alias(libs.plugins.ksp) +} + +android { + namespace = "com.lighthouse.data.preference" +} + +dependencies { + implementation(projects.core) + implementation(projects.coreAndroid) + implementation(projects.model) + implementation(projects.common) + implementation(projects.commonAndroid) + implementation(projects.data) + + implementation(libs.androidX.paging.runtime) + + implementation(libs.timber) +} diff --git a/data/data/src/main/java/com/lighthouse/datasource/gallery/GalleryImageLocalSourceImpl.kt b/data/data-content/src/main/java/com/lighthouse/data/content/datasource/GalleryImageDataSource.kt similarity index 71% rename from data/data/src/main/java/com/lighthouse/datasource/gallery/GalleryImageLocalSourceImpl.kt rename to data/data-content/src/main/java/com/lighthouse/data/content/datasource/GalleryImageDataSource.kt index ee1b109da..72c9f0d27 100644 --- a/data/data/src/main/java/com/lighthouse/datasource/gallery/GalleryImageLocalSourceImpl.kt +++ b/data/data-content/src/main/java/com/lighthouse/data/content/datasource/GalleryImageDataSource.kt @@ -1,4 +1,4 @@ -package com.lighthouse.datasource.gallery +package com.lighthouse.data.content.datasource import android.content.ContentResolver import android.content.ContentUris @@ -7,14 +7,17 @@ import android.os.Bundle import android.provider.MediaStore import android.webkit.MimeTypeMap import com.lighthouse.beep.model.gallery.GalleryImage +import com.lighthouse.common.mapper.toDomain +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import java.util.Date import javax.inject.Inject -class GalleryImageLocalSourceImpl @Inject constructor( +internal class GalleryImageDataSource @Inject constructor( private val contentResolver: ContentResolver -) : GalleryImageLocalSource { +) { - override suspend fun getImages(page: Int, limit: Int): List { + suspend fun getImages(page: Int, limit: Int): List = withContext(Dispatchers.IO) { val projection = arrayOf( MediaStore.Images.Media._ID, MediaStore.Images.Media.DATE_ADDED @@ -30,8 +33,14 @@ class GalleryImageLocalSourceImpl @Inject constructor( val queryArgs = Bundle().apply { putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection) putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, selectionArg) - putStringArray(ContentResolver.QUERY_ARG_SORT_COLUMNS, arrayOf(MediaStore.Images.Media.DATE_ADDED)) - putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING) + putStringArray( + ContentResolver.QUERY_ARG_SORT_COLUMNS, + arrayOf(MediaStore.Images.Media.DATE_ADDED) + ) + putInt( + ContentResolver.QUERY_ARG_SORT_DIRECTION, + ContentResolver.QUERY_SORT_DIRECTION_DESCENDING + ) putInt(ContentResolver.QUERY_ARG_OFFSET, offset) putInt(ContentResolver.QUERY_ARG_LIMIT, limit) } @@ -59,12 +68,13 @@ class GalleryImageLocalSourceImpl @Inject constructor( while (it.moveToNext()) { val id = it.getLong(idColumn) - val contentUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id) + val contentUri = + ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id) val dateAdded = it.getLong(dateAddedColumn) val date = Date(dateAdded * 1000) - list.add(GalleryImage(id, contentUri.toString(), date)) + list.add(GalleryImage(id, contentUri.toDomain(), date)) } } - return list + return@withContext list } } diff --git a/data/data/src/main/java/com/lighthouse/datasource/gallery/GalleryImagePagingSource.kt b/data/data-content/src/main/java/com/lighthouse/data/content/datasource/GalleryImagePagingSource.kt similarity index 82% rename from data/data/src/main/java/com/lighthouse/datasource/gallery/GalleryImagePagingSource.kt rename to data/data-content/src/main/java/com/lighthouse/data/content/datasource/GalleryImagePagingSource.kt index ab78a6af6..70398c161 100644 --- a/data/data/src/main/java/com/lighthouse/datasource/gallery/GalleryImagePagingSource.kt +++ b/data/data-content/src/main/java/com/lighthouse/data/content/datasource/GalleryImagePagingSource.kt @@ -1,11 +1,11 @@ -package com.lighthouse.datasource.gallery +package com.lighthouse.data.content.datasource import androidx.paging.PagingSource import androidx.paging.PagingState import com.lighthouse.beep.model.gallery.GalleryImage -class GalleryImagePagingSource( - private val localSource: GalleryImageLocalSource, +internal class GalleryImagePagingSource( + private val dataSource: GalleryImageDataSource, private val page: Int, private val limit: Int ) : PagingSource() { @@ -19,7 +19,7 @@ class GalleryImagePagingSource( override suspend fun load(params: LoadParams): LoadResult { val current = params.key ?: page - val results = localSource.getImages(current, params.loadSize) + val results = dataSource.getImages(current, params.loadSize) return try { LoadResult.Page( data = results, diff --git a/data/data-content/src/main/java/com/lighthouse/data/content/di/ContentModule.kt b/data/data-content/src/main/java/com/lighthouse/data/content/di/ContentModule.kt new file mode 100644 index 000000000..34f4b8d0d --- /dev/null +++ b/data/data-content/src/main/java/com/lighthouse/data/content/di/ContentModule.kt @@ -0,0 +1,22 @@ +package com.lighthouse.data.content.di + +import android.content.ContentResolver +import android.content.Context +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.qualifiers.ApplicationContext +import dagger.hilt.components.SingletonComponent + +@Suppress("unused") +@Module +@InstallIn(SingletonComponent::class) +internal class ContentModule { + + @Provides + fun provideContentResolver( + @ApplicationContext context: Context + ): ContentResolver { + return context.contentResolver + } +} diff --git a/data/data-content/src/main/java/com/lighthouse/data/content/di/DataModule.kt b/data/data-content/src/main/java/com/lighthouse/data/content/di/DataModule.kt new file mode 100644 index 000000000..a34ec2c78 --- /dev/null +++ b/data/data-content/src/main/java/com/lighthouse/data/content/di/DataModule.kt @@ -0,0 +1,19 @@ +package com.lighthouse.data.content.di + +import com.lighthouse.data.content.repository.GalleryImageContentRepositoryImpl +import com.lighthouse.repository.gallery.GalleryImageContentRepository +import dagger.Binds +import dagger.Module +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent + +@Suppress("unused") +@Module +@InstallIn(SingletonComponent::class) +internal abstract class DataModule { + + @Binds + abstract fun bindsGalleryImageContentRepository( + galleryImageContentRepositoryImpl: GalleryImageContentRepositoryImpl + ): GalleryImageContentRepository +} diff --git a/data/data-content/src/main/java/com/lighthouse/data/content/repository/GalleryImageContentRepositoryImpl.kt b/data/data-content/src/main/java/com/lighthouse/data/content/repository/GalleryImageContentRepositoryImpl.kt new file mode 100644 index 000000000..47305a96a --- /dev/null +++ b/data/data-content/src/main/java/com/lighthouse/data/content/repository/GalleryImageContentRepositoryImpl.kt @@ -0,0 +1,25 @@ +package com.lighthouse.data.content.repository + +import androidx.paging.Pager +import androidx.paging.PagingConfig +import androidx.paging.PagingData +import com.lighthouse.beep.model.gallery.GalleryImage +import com.lighthouse.data.content.datasource.GalleryImageDataSource +import com.lighthouse.data.content.datasource.GalleryImagePagingSource +import com.lighthouse.repository.gallery.GalleryImageContentRepository +import kotlinx.coroutines.flow.Flow +import javax.inject.Inject + +internal class GalleryImageContentRepositoryImpl @Inject constructor( + private val dataSource: GalleryImageDataSource +) : GalleryImageContentRepository { + + override fun getImages(pageSize: Int): Flow> { + return Pager( + config = PagingConfig(pageSize = pageSize, enablePlaceholders = false), + pagingSourceFactory = { + GalleryImagePagingSource(dataSource, 0, pageSize) + } + ).flow + } +} diff --git a/data/data/src/main/java/com/lighthouse/repository/GalleryImageRepositoryImpl.kt b/data/data/src/main/java/com/lighthouse/repository/GalleryImageRepositoryImpl.kt deleted file mode 100644 index 7a829445e..000000000 --- a/data/data/src/main/java/com/lighthouse/repository/GalleryImageRepositoryImpl.kt +++ /dev/null @@ -1,25 +0,0 @@ -package com.lighthouse.repository - -import androidx.paging.Pager -import androidx.paging.PagingConfig -import androidx.paging.PagingData -import com.lighthouse.beep.model.gallery.GalleryImage -import com.lighthouse.datasource.gallery.GalleryImageLocalSource -import com.lighthouse.datasource.gallery.GalleryImagePagingSource -import com.lighthouse.domain.repository.GalleryImageRepository -import kotlinx.coroutines.flow.Flow -import javax.inject.Inject - -class GalleryImageRepositoryImpl @Inject constructor( - private val localSource: GalleryImageLocalSource -) : GalleryImageRepository { - - override fun getImages(pageSize: Int): Flow> { - return Pager( - config = PagingConfig(pageSize = pageSize, enablePlaceholders = false), - pagingSourceFactory = { - GalleryImagePagingSource(localSource, 0, pageSize) - } - ).flow - } -} diff --git a/data/data/src/main/java/com/lighthouse/repository/gallery/GalleryImageContentRepository.kt b/data/data/src/main/java/com/lighthouse/repository/gallery/GalleryImageContentRepository.kt new file mode 100644 index 000000000..88cac87ec --- /dev/null +++ b/data/data/src/main/java/com/lighthouse/repository/gallery/GalleryImageContentRepository.kt @@ -0,0 +1,10 @@ +package com.lighthouse.repository.gallery + +import androidx.paging.PagingData +import com.lighthouse.beep.model.gallery.GalleryImage +import kotlinx.coroutines.flow.Flow + +interface GalleryImageContentRepository { + + fun getImages(pageSize: Int): Flow> +} diff --git a/data/data/src/main/java/com/lighthouse/repository/gallery/GalleryImageRepositoryImpl.kt b/data/data/src/main/java/com/lighthouse/repository/gallery/GalleryImageRepositoryImpl.kt new file mode 100644 index 000000000..e5662fec5 --- /dev/null +++ b/data/data/src/main/java/com/lighthouse/repository/gallery/GalleryImageRepositoryImpl.kt @@ -0,0 +1,16 @@ +package com.lighthouse.repository.gallery + +import androidx.paging.PagingData +import com.lighthouse.beep.model.gallery.GalleryImage +import com.lighthouse.domain.repository.GalleryImageRepository +import kotlinx.coroutines.flow.Flow +import javax.inject.Inject + +class GalleryImageRepositoryImpl @Inject constructor( + private val galleryImageContentRepository: GalleryImageContentRepository +) : GalleryImageRepository { + + override fun getImages(pageSize: Int): Flow> { + return galleryImageContentRepository.getImages(pageSize) + } +} diff --git a/domain/src/main/java/com/lighthouse/domain/repository/GalleryImageRepository.kt b/domain/src/main/java/com/lighthouse/domain/repository/GalleryImageRepository.kt index 92a0b1f76..45e86aefd 100644 --- a/domain/src/main/java/com/lighthouse/domain/repository/GalleryImageRepository.kt +++ b/domain/src/main/java/com/lighthouse/domain/repository/GalleryImageRepository.kt @@ -5,5 +5,6 @@ import com.lighthouse.beep.model.gallery.GalleryImage import kotlinx.coroutines.flow.Flow interface GalleryImageRepository { - fun getImages(pageSize: Int = 10): Flow> + + fun getImages(pageSize: Int): Flow> }