diff --git a/data/src/androidTest/java/com/wakeup/data/DataUnitTest.kt b/data/src/androidTest/java/com/wakeup/data/DataUnitTest.kt index 25e9226..98ef951 100644 --- a/data/src/androidTest/java/com/wakeup/data/DataUnitTest.kt +++ b/data/src/androidTest/java/com/wakeup/data/DataUnitTest.kt @@ -10,7 +10,7 @@ import androidx.test.filters.SmallTest import com.wakeup.data.database.MogleDatabase import com.wakeup.data.database.dao.MomentDao import com.wakeup.data.database.entity.MomentEntity -import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentPictureXRef import com.wakeup.data.database.entity.PictureEntity import com.wakeup.data.database.entity.PlaceEntity import com.wakeup.data.source.local.moment.MomentLocalDataSourceImpl @@ -44,7 +44,7 @@ class DataUnitTest { @Test fun saveMomentAndGetMoments(): Unit = runBlocking { - val pictureId = momentDao.savePicture(listOf( + val pictureId = momentDao.savePictures(listOf( PictureEntity( bitmap = "picture1".toByteArray() ), @@ -79,31 +79,31 @@ class DataUnitTest { date = System.currentTimeMillis() )) - momentDao.saveMomentPicture(listOf( - MomentPictureEntity( + momentDao.saveMomentPictureXRefs(listOf( + MomentPictureXRef( momentId = momentId1, pictureId = pictureId[0] ), - MomentPictureEntity( + MomentPictureXRef( momentId = momentId1, pictureId = pictureId[1] ), - MomentPictureEntity( + MomentPictureXRef( momentId = momentId1, pictureId = pictureId[2] ) )) - momentDao.saveMomentPicture(listOf( - MomentPictureEntity( + momentDao.saveMomentPictureXRefs(listOf( + MomentPictureXRef( momentId = momentId2, pictureId = pictureId[0] ), - MomentPictureEntity( + MomentPictureXRef( momentId = momentId2, pictureId = pictureId[1] ), - MomentPictureEntity( + MomentPictureXRef( momentId = momentId2, pictureId = pictureId[2] ) diff --git a/data/src/main/java/com/wakeup/data/database/MogleDatabase.kt b/data/src/main/java/com/wakeup/data/database/MogleDatabase.kt index 2785117..8ad50c9 100644 --- a/data/src/main/java/com/wakeup/data/database/MogleDatabase.kt +++ b/data/src/main/java/com/wakeup/data/database/MogleDatabase.kt @@ -5,21 +5,21 @@ import androidx.room.RoomDatabase import com.wakeup.data.database.dao.MomentDao import com.wakeup.data.database.entity.GlobeEntity import com.wakeup.data.database.entity.MomentEntity -import com.wakeup.data.database.entity.MomentGlobeEntity -import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentGlobeXRef +import com.wakeup.data.database.entity.MomentPictureXRef import com.wakeup.data.database.entity.PictureEntity @Database( entities = [ MomentEntity::class, GlobeEntity::class, - MomentGlobeEntity::class, - MomentPictureEntity::class, + MomentGlobeXRef::class, + MomentPictureXRef::class, PictureEntity::class ], version = 1, exportSchema = false ) -abstract class MogleDatabase: RoomDatabase() { +abstract class MogleDatabase : RoomDatabase() { abstract fun momentDao(): MomentDao } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt b/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt index ec1fa9d..4e67e46 100644 --- a/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt +++ b/data/src/main/java/com/wakeup/data/database/dao/MomentDao.kt @@ -5,18 +5,22 @@ import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query +import androidx.room.Transaction import com.wakeup.data.database.entity.GlobeEntity import com.wakeup.data.database.entity.MomentEntity -import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentGlobeXRef +import com.wakeup.data.database.entity.MomentPictureXRef +import com.wakeup.data.database.entity.MomentWithGlobesAndPictures import com.wakeup.data.database.entity.PictureEntity @Dao interface MomentDao { + @Transaction @Query( """ SELECT * FROM moment - WHERE mainAddress LIKE '%' || :query || '%' + WHERE mainAddress LIKE '%' || :query || '%' OR detailAddress LIKE '%' || :query || '%' OR content LIKE '%' || :query || '%' ORDER BY @@ -24,8 +28,9 @@ interface MomentDao { CASE WHEN :sortType = 1 THEN date END ASC """ ) - fun getMoments(sortType: Int = 0, query: String): PagingSource + fun getMoments(sortType: Int = 0, query: String): PagingSource + @Transaction @Query( """ SELECT *, @@ -38,26 +43,30 @@ interface MomentDao { ORDER BY distance """ ) - fun getMomentsByNearestDistance(query: String, lat: Double?, lng: Double?): PagingSource + fun getMomentsByNearestDistance( + query: String, + lat: Double?, + lng: Double?, + ): PagingSource - @Query( - "SELECT * FROM picture WHERE id IN" + - "(SELECT picture_id FROM moment_picture WHERE moment_id = :momentId)" - ) - suspend fun getPictures(momentId: Long): List + @Query("SELECT globe_id FROM globe WHERE name = :globeName") + suspend fun getGlobeIdByName(globeName: String): Long - @Query( - "SELECT * FROM globe WHERE id IN " + - "(SELECT globe_id FROM moment_globe WHERE moment_id = :momentId)" - ) - suspend fun getGlobes(momentId: Long): List + @Query("SELECT picture_id FROM picture WHERE bitmap = :bitmap") + suspend fun getPictureIdByByteArray(bitmap: ByteArray): Long @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun saveMoment(moment: MomentEntity): Long + @Insert(onConflict = OnConflictStrategy.IGNORE) + suspend fun savePictures(pictures: List): List + @Insert(onConflict = OnConflictStrategy.REPLACE) - suspend fun savePicture(picture: List): List + suspend fun saveMomentPictureXRefs(momentPictures: List) + + @Insert(onConflict = OnConflictStrategy.IGNORE) + suspend fun saveGlobes(globes: List): List @Insert(onConflict = OnConflictStrategy.REPLACE) - suspend fun saveMomentPicture(momentPictures: List) + suspend fun saveMomentGlobeXRef(momentGlobe: MomentGlobeXRef) } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/entity/GlobeEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/GlobeEntity.kt index 700c52d..e965646 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/GlobeEntity.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/GlobeEntity.kt @@ -10,6 +10,6 @@ import androidx.room.PrimaryKey indices = [Index(value = ["name"], unique = true)] ) data class GlobeEntity( - @PrimaryKey(autoGenerate = true) val id: Long = 0L, + @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "globe_id") val id: Long = 0L, @ColumnInfo(name = "name") val name: String, ) diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentEntity.kt index 187be65..e0410b1 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/MomentEntity.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/MomentEntity.kt @@ -12,16 +12,16 @@ import androidx.room.PrimaryKey foreignKeys = [ ForeignKey( entity = PictureEntity::class, - parentColumns = ["id"], + parentColumns = ["picture_id"], childColumns = ["thumbnail_id"], - onDelete = CASCADE + onUpdate = CASCADE ) ] ) data class MomentEntity( - @PrimaryKey(autoGenerate = true) val id: Long = 0L, + @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "moment_id") val id: Long = 0L, @Embedded val place: PlaceEntity, - @ColumnInfo(name = "thumbnail_id") val thumbnailId: Long?, + @ColumnInfo(name = "thumbnail_id", index = true) val thumbnailId: Long?, @ColumnInfo(name = "content") val content: String, @ColumnInfo(name = "date") val date: Long, ) \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeXRef.kt similarity index 58% rename from data/src/main/java/com/wakeup/data/database/entity/MomentGlobeEntity.kt rename to data/src/main/java/com/wakeup/data/database/entity/MomentGlobeXRef.kt index 4b96352..96966c4 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeEntity.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/MomentGlobeXRef.kt @@ -3,27 +3,26 @@ package com.wakeup.data.database.entity import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.ForeignKey -import androidx.room.PrimaryKey @Entity( tableName = "moment_globe", foreignKeys = [ ForeignKey( entity = MomentEntity::class, - parentColumns = ["id"], + parentColumns = ["moment_id"], childColumns = ["moment_id"], - onDelete = ForeignKey.CASCADE + onUpdate = ForeignKey.CASCADE ), ForeignKey( entity = GlobeEntity::class, - parentColumns = ["id"], + parentColumns = ["globe_id"], childColumns = ["globe_id"], - onDelete = ForeignKey.CASCADE + onUpdate = ForeignKey.CASCADE ) - ] + ], + primaryKeys = ["moment_id", "globe_id"] ) -data class MomentGlobeEntity( - @PrimaryKey(autoGenerate = true) val id: Long = 0L, - @ColumnInfo(name = "moment_id", index = true) val momentId: Int, - @ColumnInfo(name = "globe_id", index = true) val globeId: Int, +data class MomentGlobeXRef( + @ColumnInfo(name = "moment_id", index = true) val momentId: Long, + @ColumnInfo(name = "globe_id", index = true) val globeId: Long, ) diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentPictureEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentPictureXRef.kt similarity index 67% rename from data/src/main/java/com/wakeup/data/database/entity/MomentPictureEntity.kt rename to data/src/main/java/com/wakeup/data/database/entity/MomentPictureXRef.kt index f0ac243..313dafc 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/MomentPictureEntity.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/MomentPictureXRef.kt @@ -3,27 +3,26 @@ package com.wakeup.data.database.entity import androidx.room.ColumnInfo import androidx.room.Entity import androidx.room.ForeignKey -import androidx.room.PrimaryKey @Entity( tableName = "moment_picture", foreignKeys = [ ForeignKey( entity = MomentEntity::class, - parentColumns = ["id"], + parentColumns = ["moment_id"], childColumns = ["moment_id"], - onDelete = ForeignKey.CASCADE + onUpdate = ForeignKey.CASCADE ), ForeignKey( entity = PictureEntity::class, - parentColumns = ["id"], + parentColumns = ["picture_id"], childColumns = ["picture_id"], - onDelete = ForeignKey.CASCADE + onUpdate = ForeignKey.CASCADE ) - ] + ], + primaryKeys = ["moment_id", "picture_id"] ) -data class MomentPictureEntity( - @PrimaryKey(autoGenerate = true) val id: Long = 0L, +data class MomentPictureXRef( @ColumnInfo(name = "moment_id", index = true) val momentId: Long, @ColumnInfo(name = "picture_id", index = true) val pictureId: Long, ) \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/entity/MomentWithGlobesAndPictures.kt b/data/src/main/java/com/wakeup/data/database/entity/MomentWithGlobesAndPictures.kt new file mode 100644 index 0000000..20b610b --- /dev/null +++ b/data/src/main/java/com/wakeup/data/database/entity/MomentWithGlobesAndPictures.kt @@ -0,0 +1,23 @@ +package com.wakeup.data.database.entity + +import androidx.room.Embedded +import androidx.room.Junction +import androidx.room.Relation + +data class MomentWithGlobesAndPictures( + @Embedded val moment: MomentEntity, + @Relation( + parentColumn = "moment_id", + entity = GlobeEntity::class, + entityColumn = "globe_id", + associateBy = Junction(MomentGlobeXRef::class) + ) + val globeList: List, + @Relation( + parentColumn = "moment_id", + entity = PictureEntity::class, + entityColumn = "picture_id", + associateBy = Junction(MomentPictureXRef::class) + ) + val pictureList: List, +) \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/entity/PictureEntity.kt b/data/src/main/java/com/wakeup/data/database/entity/PictureEntity.kt index 90f28c8..7cf1956 100644 --- a/data/src/main/java/com/wakeup/data/database/entity/PictureEntity.kt +++ b/data/src/main/java/com/wakeup/data/database/entity/PictureEntity.kt @@ -10,7 +10,7 @@ import androidx.room.PrimaryKey indices = [Index(value = ["bitmap"], unique = true)] ) data class PictureEntity( - @PrimaryKey(autoGenerate = true) val id: Long = 0L, + @PrimaryKey(autoGenerate = true) @ColumnInfo(name = "picture_id") val id: Long = 0L, @ColumnInfo(name = "bitmap") val bitmap: ByteArray, ) { override fun equals(other: Any?): Boolean { @@ -30,4 +30,5 @@ data class PictureEntity( result = 31 * result + bitmap.contentHashCode() return result } + } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/database/mapper/MomentEntityMapper.kt b/data/src/main/java/com/wakeup/data/database/mapper/MomentEntityMapper.kt index dd3f3a1..7dee849 100644 --- a/data/src/main/java/com/wakeup/data/database/mapper/MomentEntityMapper.kt +++ b/data/src/main/java/com/wakeup/data/database/mapper/MomentEntityMapper.kt @@ -2,6 +2,7 @@ package com.wakeup.data.database.mapper import com.wakeup.data.database.entity.GlobeEntity import com.wakeup.data.database.entity.MomentEntity +import com.wakeup.data.database.entity.MomentWithGlobesAndPictures import com.wakeup.data.database.entity.PictureEntity import com.wakeup.domain.model.Moment import com.wakeup.domain.model.Place @@ -17,6 +18,18 @@ fun MomentEntity.toDomain(pictures: List, globes: List, globes: List): Moment { + return Moment( + id = moment.id, + place = moment.place.toDomain(), + pictures = pictures.map { it.toDomain() }, + content = moment.content, + globes = globes.map { it.toDomain() }, + date = moment.date + ) +} + + fun Moment.toEntity(place: Place, thumbnailId: Long?): MomentEntity { return MomentEntity( place = place.toEntity(), diff --git a/data/src/main/java/com/wakeup/data/di/DataSourceModule.kt b/data/src/main/java/com/wakeup/data/di/DataSourceModule.kt index 56eff53..96bd44c 100644 --- a/data/src/main/java/com/wakeup/data/di/DataSourceModule.kt +++ b/data/src/main/java/com/wakeup/data/di/DataSourceModule.kt @@ -15,11 +15,11 @@ interface DataSourceModule { @Binds fun bindMomentLocalDataSource( - momentLocalDataSourceImpl: MomentLocalDataSourceImpl + momentLocalDataSourceImpl: MomentLocalDataSourceImpl, ): MomentLocalDataSource @Binds fun bindPlaceSearchRemoteDataSource( - placeSearchRemoteDataSourceImpl: PlaceSearchRemoteDataSourceImpl + placeSearchRemoteDataSourceImpl: PlaceSearchRemoteDataSourceImpl, ): PlaceSearchRemoteDataSource } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/di/DatabaseModule.kt b/data/src/main/java/com/wakeup/data/di/DatabaseModule.kt index c26ead0..7543d88 100644 --- a/data/src/main/java/com/wakeup/data/di/DatabaseModule.kt +++ b/data/src/main/java/com/wakeup/data/di/DatabaseModule.kt @@ -3,6 +3,7 @@ package com.wakeup.data.di import android.content.Context import androidx.room.Room import com.wakeup.data.database.MogleDatabase +import com.wakeup.data.database.dao.MomentDao import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -17,12 +18,14 @@ object DatabaseModule { @Singleton @Provides fun provideAppDatabase( - @ApplicationContext context: Context + @ApplicationContext context: Context, ): MogleDatabase = Room .databaseBuilder(context, MogleDatabase::class.java, "mogle.db") .build() @Singleton @Provides - fun provideMomentDao(mogleDatabase: MogleDatabase) = mogleDatabase.momentDao() + fun provideMomentDao( + mogleDatabase: MogleDatabase, + ): MomentDao = mogleDatabase.momentDao() } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/di/NetworkModule.kt b/data/src/main/java/com/wakeup/data/di/NetworkModule.kt index e46243c..51901e6 100644 --- a/data/src/main/java/com/wakeup/data/di/NetworkModule.kt +++ b/data/src/main/java/com/wakeup/data/di/NetworkModule.kt @@ -62,7 +62,7 @@ object NetworkModule { @Singleton fun provideKakaoRetrofit( okHttpClient: OkHttpClient, - converterFactory: Converter.Factory + converterFactory: Converter.Factory, ): Retrofit { return Retrofit.Builder() .baseUrl(KAKAO_BASE_URL) @@ -76,5 +76,4 @@ object NetworkModule { fun providePlaceSearchService(retrofit: Retrofit): PlaceSearchService { return retrofit.create(PlaceSearchService::class.java) } - } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/di/RepositoryModule.kt b/data/src/main/java/com/wakeup/data/di/RepositoryModule.kt index 8c6486d..f66b11a 100644 --- a/data/src/main/java/com/wakeup/data/di/RepositoryModule.kt +++ b/data/src/main/java/com/wakeup/data/di/RepositoryModule.kt @@ -15,11 +15,11 @@ interface RepositoryModule { @Binds fun bindMomentRepository( - momentRepositoryImpl: MomentRepositoryImpl + momentRepositoryImpl: MomentRepositoryImpl, ): MomentRepository @Binds fun bindPlaceSearchRepository( - placeSearchRepositoryImpl: PlaceSearchRepositoryImpl + placeSearchRepositoryImpl: PlaceSearchRepositoryImpl, ): PlaceSearchRepository } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/repository/MomentRepositoryImpl.kt b/data/src/main/java/com/wakeup/data/repository/MomentRepositoryImpl.kt index a85b32a..903752a 100644 --- a/data/src/main/java/com/wakeup/data/repository/MomentRepositoryImpl.kt +++ b/data/src/main/java/com/wakeup/data/repository/MomentRepositoryImpl.kt @@ -2,32 +2,47 @@ package com.wakeup.data.repository import androidx.paging.PagingData import androidx.paging.map -import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.GlobeEntity +import com.wakeup.data.database.entity.MomentGlobeXRef +import com.wakeup.data.database.entity.MomentPictureXRef import com.wakeup.data.database.mapper.toDomain import com.wakeup.data.database.mapper.toEntity import com.wakeup.data.source.local.moment.MomentLocalDataSource import com.wakeup.domain.model.Location import com.wakeup.domain.model.Moment -import com.wakeup.domain.model.Place import com.wakeup.domain.model.SortType -import com.wakeup.domain.model.Picture import com.wakeup.domain.repository.MomentRepository +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.map -import timber.log.Timber +import kotlinx.coroutines.launch import javax.inject.Inject class MomentRepositoryImpl @Inject constructor( private val localDataSource: MomentLocalDataSource, ) : MomentRepository { - override fun getMoments(sort: SortType, query: String, myLocation: Location?): Flow> = + init { + // todo: globe_test_code (must remove to release) + CoroutineScope(Dispatchers.IO).launch { + localDataSource.saveGlobes(listOf( + GlobeEntity(name = "default"), + GlobeEntity(name = "globe 1"), + GlobeEntity(name = "globe 2"), + GlobeEntity(name = "globe 3")) + ) + } + } + + override fun getMoments( + sort: SortType, + query: String, + myLocation: Location?, + ): Flow> = localDataSource.getMoments(sort, query, myLocation?.toEntity()).map { pagingData -> - pagingData.map { momentEntity -> - momentEntity.toDomain( - localDataSource.getPictures(momentEntity.id), - localDataSource.getGlobes(momentEntity.id) - ) + pagingData.map { momentInfo -> + momentInfo.toDomain(momentInfo.pictureList, momentInfo.globeList) } } @@ -35,17 +50,21 @@ class MomentRepositoryImpl @Inject constructor( if (moment.pictures.isEmpty()) { localDataSource.saveMoment(moment.toEntity(moment.place, null)) return - } else { - val pictureIndexes = - localDataSource.savePicture(moment.pictures.map { it.toEntity() }) - val momentIndex = - localDataSource.saveMoment(moment.toEntity(moment.place, pictureIndexes[0])) - - localDataSource.saveMomentPicture( - pictureIndexes.map { pictureId -> - MomentPictureEntity(momentId = momentIndex, pictureId = pictureId) - } - ) } + val pictureIndexes = + localDataSource.savePictures(moment.pictures.map { it.toEntity() }) + // 정책: moment 추가할 때 항상 globe 하나 선택해서 추가(default 도 하나 선택해서 추가 임). + val globeIndex = localDataSource.getGlobeId(moment.globes[0].name) + val momentIndex = + localDataSource.saveMoment(moment.toEntity(moment.place, pictureIndexes[0])) + + localDataSource.saveMomentPictures( + pictureIndexes.map { pictureId -> + MomentPictureXRef(momentId = momentIndex, pictureId = pictureId) + } + ) + localDataSource.saveMomentGlobe( + MomentGlobeXRef(momentId = momentIndex, globeId = globeIndex) + ) } } diff --git a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSource.kt b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSource.kt index e0662de..17ffe41 100644 --- a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSource.kt +++ b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSource.kt @@ -2,24 +2,32 @@ package com.wakeup.data.source.local.moment import androidx.paging.PagingData import com.wakeup.data.database.entity.GlobeEntity +import com.wakeup.data.database.entity.LocationEntity import com.wakeup.data.database.entity.MomentEntity -import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentGlobeXRef +import com.wakeup.data.database.entity.MomentPictureXRef +import com.wakeup.data.database.entity.MomentWithGlobesAndPictures import com.wakeup.data.database.entity.PictureEntity -import com.wakeup.data.database.entity.LocationEntity import com.wakeup.domain.model.SortType import kotlinx.coroutines.flow.Flow interface MomentLocalDataSource { - fun getMoments(sortType: SortType, query: String, myLocation: LocationEntity?): Flow> + fun getMoments(sortType: SortType, query: String, myLocation: LocationEntity?): Flow> + +/* suspend fun getPictures(momentId: Long): List - suspend fun getPictures(momentId: Long): List + suspend fun getGlobes(momentId: Long): List*/ - suspend fun getGlobes(momentId: Long): List + suspend fun getGlobeId(globeName: String): Long suspend fun saveMoment(moment: MomentEntity): Long - suspend fun savePicture(picture: List): List + suspend fun savePictures(pictures: List): List + + suspend fun saveMomentPictures(momentPictures :List) + + suspend fun saveGlobes(globes: List): List - suspend fun saveMomentPicture(MomentPictures :List) + suspend fun saveMomentGlobe(momentGlobe :MomentGlobeXRef) } \ No newline at end of file diff --git a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt index fd93f77..30f1032 100644 --- a/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt +++ b/data/src/main/java/com/wakeup/data/source/local/moment/MomentLocalDataSourceImpl.kt @@ -5,10 +5,12 @@ import androidx.paging.PagingConfig import androidx.paging.PagingData import com.wakeup.data.database.dao.MomentDao import com.wakeup.data.database.entity.GlobeEntity +import com.wakeup.data.database.entity.LocationEntity import com.wakeup.data.database.entity.MomentEntity -import com.wakeup.data.database.entity.MomentPictureEntity +import com.wakeup.data.database.entity.MomentGlobeXRef +import com.wakeup.data.database.entity.MomentPictureXRef +import com.wakeup.data.database.entity.MomentWithGlobesAndPictures import com.wakeup.data.database.entity.PictureEntity -import com.wakeup.data.database.entity.LocationEntity import com.wakeup.domain.model.SortType import kotlinx.coroutines.flow.Flow import javax.inject.Inject @@ -16,7 +18,11 @@ import javax.inject.Inject class MomentLocalDataSourceImpl @Inject constructor( private val momentDao: MomentDao, ) : MomentLocalDataSource { - override fun getMoments(sortType: SortType, query: String, myLocation: LocationEntity?): Flow> = + override fun getMoments( + sortType: SortType, + query: String, + myLocation: LocationEntity?, + ): Flow> = Pager( config = PagingConfig( pageSize = ITEMS_PER_PAGE, @@ -26,30 +32,47 @@ class MomentLocalDataSourceImpl @Inject constructor( ), pagingSourceFactory = { when (sortType) { - SortType.NEAREST -> momentDao.getMomentsByNearestDistance(query, myLocation?.latitude, myLocation?.longitude) + SortType.NEAREST -> momentDao.getMomentsByNearestDistance(query, + myLocation?.latitude, + myLocation?.longitude) else -> momentDao.getMoments(sortType.ordinal, query) } } ).flow - override suspend fun getPictures(momentId: Long): List = - momentDao.getPictures(momentId) + override suspend fun getGlobeId(globeName: String): Long { + return momentDao.getGlobeIdByName(globeName) + } - override suspend fun getGlobes(momentId: Long): List = - momentDao.getGlobes(momentId) + override suspend fun saveMoment(moment: MomentEntity): Long { + return momentDao.saveMoment(moment) + } - override suspend fun saveMoment(moment: MomentEntity): Long = - momentDao.saveMoment(moment) + override suspend fun savePictures(pictures: List): List { + val indexResult = momentDao.savePictures(pictures).toMutableList() + indexResult.forEachIndexed { pictureIndex, id -> + if (id == EXIST_INSERT_ERROR_CODE) { + indexResult[pictureIndex] = momentDao.getPictureIdByByteArray(pictures[pictureIndex].bitmap) + } + } + return indexResult.toList() + } + + override suspend fun saveMomentPictures(momentPictures: List) { + momentDao.saveMomentPictureXRefs(momentPictures) + } - override suspend fun savePicture(picture: List): List = - momentDao.savePicture(picture) + override suspend fun saveGlobes(globes: List): List { + return momentDao.saveGlobes(globes) + } - override suspend fun saveMomentPicture(MomentPictures: List) { - momentDao.saveMomentPicture(MomentPictures) + override suspend fun saveMomentGlobe(momentGlobe: MomentGlobeXRef) { + momentDao.saveMomentGlobeXRef(momentGlobe) } companion object { const val PREFETCH_PAGE = 2 const val ITEMS_PER_PAGE = 10 + const val EXIST_INSERT_ERROR_CODE = -1L } } \ No newline at end of file diff --git a/presentation/src/main/java/com/wakeup/presentation/ui/addmoment/AddMomentViewModel.kt b/presentation/src/main/java/com/wakeup/presentation/ui/addmoment/AddMomentViewModel.kt index c5d4369..e3464e3 100644 --- a/presentation/src/main/java/com/wakeup/presentation/ui/addmoment/AddMomentViewModel.kt +++ b/presentation/src/main/java/com/wakeup/presentation/ui/addmoment/AddMomentViewModel.kt @@ -2,11 +2,11 @@ package com.wakeup.presentation.ui.addmoment import android.content.Intent import androidx.lifecycle.ViewModel -import com.wakeup.presentation.model.LocationModel import androidx.lifecycle.viewModelScope import com.wakeup.domain.usecase.SaveMomentUseCase import com.wakeup.presentation.mapper.toDomain import com.wakeup.presentation.model.GlobeModel +import com.wakeup.presentation.model.LocationModel import com.wakeup.presentation.model.MomentModel import com.wakeup.presentation.model.PictureModel import com.wakeup.presentation.model.PlaceModel @@ -114,7 +114,7 @@ class AddMomentViewModel @Inject constructor( date = selectedDate.value ).toDomain() ) - Timber.d("${pictures.value[0]}") + //Timber.d("${pictures.value[0]}") } } } \ No newline at end of file