Skip to content

Commit

Permalink
Room Database insert
Browse files Browse the repository at this point in the history
  • Loading branch information
yeseoRyu committed Jan 9, 2025
1 parent 9064ea9 commit 09e1690
Show file tree
Hide file tree
Showing 27 changed files with 335 additions and 123 deletions.
21 changes: 6 additions & 15 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.hilt.android)
kotlin("kapt")

alias(libs.plugins.ksp)
}
android {
namespace = "com.flab.deepsleep"
Expand Down Expand Up @@ -48,9 +47,8 @@ android {
}
}
dependencies {
kapt(libs.hilt.android.compiler)
kapt(libs.hilt.compiler)
kapt(libs.androidx.hilt.compiler)
ksp(libs.hilt.compiler)
ksp(libs.room.compiler)
implementation(libs.retrofit)
implementation(libs.gson)
implementation(libs.hilt.android)
Expand All @@ -67,6 +65,8 @@ dependencies {
implementation(libs.kotlinx.coroutines.android)
implementation(libs.okhttp)
implementation(libs.okhttp.profiler)
implementation(libs.room.runtime)
implementation(libs.room.ktx)

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
Expand All @@ -77,15 +77,6 @@ dependencies {
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
}
kapt {
correctErrorTypes = true
useBuildCache = false
showProcessorStats = true

arguments {
arg("dagger.hilt.android.internal.disableAndroidSuperclassValidation", "true")
}
}
hilt {
enableAggregatingTask = false
}
}
25 changes: 18 additions & 7 deletions app/src/main/java/com/flab/deepsleep/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.flab.deepsleep

import PhotoAdapter
import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
Expand All @@ -11,10 +12,11 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.flab.deepsleep.databinding.ActivityMainBinding
import com.flab.deepsleep.ui.photo.PhotoViewModel
import com.flab.deepsleep.utils.RecyclerItemClickListener
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
Expand All @@ -26,6 +28,20 @@ class MainActivity : AppCompatActivity() {
private val photoRecyclerView: RecyclerView by lazy { binding.photosRecyclerView }
private val photoAdapter: PhotoAdapter by lazy { PhotoAdapter() }

private fun setRecyclerView() {
photoRecyclerView.layoutManager = GridLayoutManager(this, 2)
photoRecyclerView.adapter = photoAdapter
photoRecyclerView.addOnItemTouchListener(
RecyclerItemClickListener(this, photoRecyclerView) { _, position ->
val photo = photoAdapter.snapshot()[position]
photo?.let {
photoViewModel.insertPhoto(photo)
Toast.makeText(this, "즐겨찾기 추가", Toast.LENGTH_SHORT).show()
}
}
)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
Expand All @@ -45,6 +61,7 @@ class MainActivity : AppCompatActivity() {
photoViewModel.searchPhotos(text.toString())
}


/* 에러 관찰 */
photoViewModel.errorMessage.observe(this, Observer { it ->
it?.let {
Expand All @@ -53,11 +70,6 @@ class MainActivity : AppCompatActivity() {
})
}

private fun setRecyclerView() {
photoRecyclerView.layoutManager = LinearLayoutManager(this)
photoRecyclerView.adapter = photoAdapter
}

private fun showErrorDialog(message: String) {
AlertDialog.Builder(this)
.setTitle("Error")
Expand All @@ -67,5 +79,4 @@ class MainActivity : AppCompatActivity() {
}
.show()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.flab.deepsleep.data.api

import com.flab.deepsleep.data.entity.photos.SinglePhoto

import com.flab.deepsleep.data.entity.search.SearchPhotos
import com.flab.deepsleep.data.entity.photos.SearchPhotos
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/flab/deepsleep/data/di/HiltModule.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.flab.deepsleep.data.di

import android.content.Context
import com.flab.deepsleep.BuildConfig
import com.flab.deepsleep.data.api.UnplashService
import com.flab.deepsleep.data.entity.room.AppDatabase
import com.flab.deepsleep.data.repository.db.PhotoRepository
import com.flab.deepsleep.data.repository.db.OffLinePhotoRepository
import com.localebro.okhttpprofiler.OkHttpProfilerInterceptor
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
Expand Down Expand Up @@ -58,4 +63,17 @@ object HiltModule {
fun provideUnplashService(retrofit: Retrofit): UnplashService {
return HiltModule.retrofit.create(UnplashService::class.java)
}

/*-- Room Database --*/
@Provides
@Singleton
fun provideItemsRepository(database: AppDatabase): PhotoRepository {
return OffLinePhotoRepository(database.photoDao())
}

@Provides
@Singleton
fun provideDatabase(@ApplicationContext context: Context): AppDatabase {
return AppDatabase.getDatabase(context)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.flab.deepsleep.data.entity.search
package com.flab.deepsleep.data.entity.photos
import com.google.gson.annotations.SerializedName

data class Results(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.flab.deepsleep.data.entity.search
package com.flab.deepsleep.data.entity.photos
import com.google.gson.annotations.SerializedName

data class SearchPhotos(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.flab.deepsleep.data.entity.search
package com.flab.deepsleep.data.entity.photos

import com.flab.deepsleep.data.entity.photos.Urls
import com.google.gson.annotations.SerializedName

data class SearchUser(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.flab.deepsleep.data.entity.photos

import com.flab.deepsleep.data.entity.room.Photo
import com.google.gson.annotations.SerializedName

data class SinglePhoto(
Expand Down Expand Up @@ -33,24 +34,15 @@ data class SinglePhoto(
val urls: Urls?,
@SerializedName("user")
val user: User?
){
fun toDefault(singlePhoto: SinglePhoto): SinglePhoto{
return SinglePhoto(
id = singlePhoto?.id,
description = singlePhoto?.description,
color = singlePhoto?.color,
createdAt = singlePhoto?.createdAt,
downloads = 0,
height = 0,
width = 0,
blurHash = singlePhoto?.blurHash,
likedByUser = false,
likes = 0,
publicDomain = true,
updatedAt = singlePhoto?.updatedAt,
exif = singlePhoto?.exif,
urls = singlePhoto?.urls,
user = singlePhoto?.user
)
}
}
)

fun SinglePhoto.toPhoto(): Photo {
return Photo(
id = this.id?.toIntOrNull() ?: 0,
likes = this.likes,
urls = this.urls?.raw,
createdAt = this.createdAt,
username = this.user?.username,
isLike = true
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.flab.deepsleep.data.entity.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [Photo::class], version = 2)
abstract class AppDatabase : RoomDatabase() {
abstract fun photoDao(): PhotoDao

companion object {
@Volatile
private var Instance: AppDatabase? = null

fun getDatabase(context: Context): AppDatabase {
return Instance ?: synchronized(this) {
Room.databaseBuilder(context, AppDatabase::class.java, "photo_database")
.build()
.also { Instance = it }
}
}
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/com/flab/deepsleep/data/entity/room/Photo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.flab.deepsleep.data.entity.room

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "photo")
data class Photo(
@PrimaryKey(autoGenerate = true) val pk: Int = 0,
@ColumnInfo(name = "id") val id: Int,
@ColumnInfo(name = "likes") val likes: Int,
@ColumnInfo(name = "urls") val urls: String?,
@ColumnInfo(name = "created_at") val createdAt: String?,
@ColumnInfo(name = "username") val username: String?,
@ColumnInfo(name = "is_like", defaultValue = "0") val isLike: Boolean = false,
)
20 changes: 20 additions & 0 deletions app/src/main/java/com/flab/deepsleep/data/entity/room/PhotoDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.flab.deepsleep.data.entity.room

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import kotlinx.coroutines.flow.Flow

@Dao
interface PhotoDao {
@Query("SELECT * FROM `photo` order by pk DESC")
fun getAll(): Flow<List<Photo>>

@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(vararg like: Photo)

@Delete
suspend fun delete(like: Photo)
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.flab.deepsleep.data.repository.db

import com.flab.deepsleep.data.entity.room.Photo
import com.flab.deepsleep.data.entity.room.PhotoDao
import kotlinx.coroutines.flow.Flow

class OffLinePhotoRepository(private val photoDao: PhotoDao) : PhotoRepository {
override fun getAllPhotos(): Flow<List<Photo>> = photoDao.getAll()
override suspend fun insertPhoto(like: Photo) = photoDao.insert(like)
override suspend fun deletePhoto(like: Photo) = photoDao.delete(like)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.flab.deepsleep.data.repository.db

import com.flab.deepsleep.data.entity.room.Photo
import kotlinx.coroutines.flow.Flow

interface PhotoRepository {
fun getAllPhotos(): Flow<List<Photo>>
suspend fun insertPhoto(like: Photo)
suspend fun deletePhoto(like: Photo)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.flab.deepsleep.data.repository.photo

import com.flab.deepsleep.data.entity.photos.SinglePhoto
import com.flab.deepsleep.data.entity.photos.SearchPhotos

interface UnplashRepository {
suspend fun getRandomPhotos(count: Int): List<SinglePhoto>
suspend fun getSearchPhotos(query: String): SearchPhotos
suspend fun getSinglePhotoById(photoId: String): SinglePhoto
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.flab.deepsleep.data.repo
package com.flab.deepsleep.data.repository.photo

import com.flab.deepsleep.BuildConfig
import com.flab.deepsleep.data.api.UnplashService
import com.flab.deepsleep.data.entity.photos.SinglePhoto
import com.flab.deepsleep.data.entity.search.SearchPhotos
import com.flab.deepsleep.data.entity.photos.SearchPhotos
import javax.inject.Inject

class UnplashRepositoryImpl @Inject constructor(private val unplashService: UnplashService) :
Expand Down
Loading

0 comments on commit 09e1690

Please sign in to comment.