Skip to content

Commit

Permalink
Merge pull request #22 from Team-HMH/feat/database
Browse files Browse the repository at this point in the history
[feat/database] : Room Database 추가 작업
  • Loading branch information
kez-lab authored Jan 1, 2024
2 parents 2b4e60d + a644c59 commit 85c079e
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 9 deletions.
8 changes: 6 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ android {
}

dependencies {
// Feature
implementation(projects.feature.onboarding)

// Core
implementation(projects.core.common)
implementation(projects.core.database)

// Firebase
implementation(platform(libs.firebase))
implementation(libs.bundles.firebase)

// Splash
implementation(libs.splash.screen)

implementation(projects.feature.onboarding)
}
5 changes: 5 additions & 0 deletions build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ java {
dependencies {
compileOnly(libs.agp)
compileOnly(libs.kotlin.gradleplugin)
compileOnly(libs.ksp.gradlePlugin)
}

gradlePlugin {
Expand All @@ -33,6 +34,10 @@ gradlePlugin {
id = "com.hmh.hamyeonham.hilt"
implementationClass = "com.hmh.hamyeonham.plugin.AndroidHiltPlugin"
}
create("android-room") {
id = "com.hmh.hamyeonham.room"
implementationClass = "com.hmh.hamyeonham.plugin.AndroidRoomConventionPlugin"
}
create("kotlin-serialization") {
id = "com.hmh.hamyeonham.serialization"
implementationClass = "com.hmh.hamyeonham.plugin.KotlinSerializationPlugin"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.hmh.hamyeonham.plugin

import com.google.devtools.ksp.gradle.KspExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.getByType
import org.gradle.process.CommandLineArgumentProvider
import java.io.File

class AndroidRoomConventionPlugin : Plugin<Project> {

override fun apply(target: Project) = with(target) {
with(plugins) {
apply("com.google.devtools.ksp")
}

val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")

extensions.configure<KspExtension> {
// The schemas directory contains a schema file for each version of the Room database.
// This is required to enable Room auto migrations.
// See https://developer.android.com/reference/kotlin/androidx/room/AutoMigration.
arg(RoomSchemaArgProvider(File(projectDir, "schemas")))
}

dependencies {
add("implementation", libs.findLibrary("room.runtime").get())
add("implementation", libs.findLibrary("room.ktx").get())
add("ksp", libs.findLibrary("room.compiler").get())
}
}


/**
* https://issuetracker.google.com/issues/132245929
* [Export schemas](https://developer.android.com/training/data-storage/room/migrating-db-versions#export-schemas)
*/
class RoomSchemaArgProvider(
@get:InputDirectory
@get:PathSensitive(PathSensitivity.RELATIVE)
val schemaDir: File,
) : CommandLineArgumentProvider {
override fun asArguments() = listOf("room.schemaLocation=${schemaDir.path}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@ internal fun Project.configureAndroidCommonPlugin() {
"implementation"(libs.findLibrary("appcompat").get())
"implementation"(libs.findBundle("lifecycle").get())
"implementation"(libs.findLibrary("material").get())
"implementation"(libs.findLibrary("timber").get())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Parcelable
import timber.log.Timber
import android.util.Log
import java.io.Serializable
import kotlin.properties.ReadOnlyProperty

Expand Down Expand Up @@ -63,7 +63,7 @@ inline fun <reified T : Parcelable> Intent.getCompatibleParcelableExtra(key: Str
getParcelableExtra(key)
}
} catch (e: ClassCastException) {
Timber.e("IntentExtensions", "Failed to cast Parcelable object", e)
Log.e("IntentExtensions", "Failed to cast Parcelable object", e)
null
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.hmh.hamyeonham.common.lifecycle

import android.util.Log
import androidx.annotation.MainThread
import androidx.annotation.Nullable
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import timber.log.Timber
import java.util.concurrent.atomic.AtomicBoolean

class SingleLiveEvent<T> : MutableLiveData<T>() {
private val pending: AtomicBoolean = AtomicBoolean(false)

override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
if (hasActiveObservers()) {
Timber.w(TAG, "Multiple observers registered but only one will be notified of changes.")
Log.w(TAG, "Multiple observers registered but only one will be notified of changes.")
}

// Observe the internal MutableLiveData
Expand Down
1 change: 1 addition & 0 deletions core/database/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
11 changes: 11 additions & 0 deletions core/database/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
hmh("feature")
hmh("room")
}

android {
namespace = "com.hmh.hamyeonham.core.database"
}

dependencies {}
Empty file.
21 changes: 21 additions & 0 deletions core/database/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
1 change: 1 addition & 0 deletions core/database/schemas/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
4 changes: 4 additions & 0 deletions core/database/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.hmh.hamyeonham.core.database

import android.content.Context
import androidx.room.Room
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object DatabaseModule {
@Provides
@Singleton
fun providesNiaDatabase(
@ApplicationContext context: Context,
): HMHRoomDatabase = Room.databaseBuilder(
context,
HMHRoomDatabase::class.java,
"hmh-android-database",
).build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.hmh.hamyeonham.core.database

import androidx.room.Database
import androidx.room.RoomDatabase
import com.hmh.hamyeonham.core.database.dao.SampleDao
import com.hmh.hamyeonham.core.database.model.Sample

@Database(entities = [Sample::class], version = 1, exportSchema = false)
abstract class HMHRoomDatabase : RoomDatabase() {
abstract fun sampleDao(): SampleDao
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.hmh.hamyeonham.core.database.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import com.hmh.hamyeonham.core.database.model.Sample

@Dao
interface SampleDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insertSampleList(entities: List<Sample>): List<Long>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.hmh.hamyeonham.core.database.model

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

@Entity
data class Sample(
@PrimaryKey(autoGenerate = true) val id: Int,
val sample: String
)
9 changes: 7 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ kotlinx-serialization-json = "1.6.0"
kotlinx-serialization-converter = "1.0.0"
kotlinx-coroutines = "1.7.3"
kotlinx-datetime = "0.4.1"
ksp = "1.8.21-1.0.11"

# android
corektx = "1.12.0"
Expand Down Expand Up @@ -68,7 +69,7 @@ flipper = "0.236.0"
soloader = "0.10.5"
okhttp = "4.12.0"
retrofit = "2.9.0"
timber = "5.0.1"
room = "2.6.1"
coil = "2.5.0"
lottie = "6.0.1"
ktlint = "11.6.1"
Expand All @@ -91,6 +92,7 @@ kotlin-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-s
kotlin-coroutines-google-play = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-play-services", version.ref = "kotlinx-coroutines" }
kotlin-coroutines = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-android", version.ref = "kotlinx-coroutines" }
kotlin-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime", version.ref = "kotlinx-datetime" }
ksp-gradlePlugin = { group = "com.google.devtools.ksp", name = "com.google.devtools.ksp.gradle.plugin", version.ref = "ksp" }

# android
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "corektx" }
Expand Down Expand Up @@ -181,7 +183,10 @@ okhttp-logging-interceptor = { module = "com.squareup.okhttp3:logging-intercepto
retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }
retrofit-kotlin-serialization-converter = { group = "com.jakewharton.retrofit", name = "retrofit2-kotlinx-serialization-converter", version.ref = "kotlinx-serialization-converter" }

timber = { module = "com.jakewharton.timber:timber", version.ref = "timber" }
# Room
room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }
room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" }
room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "room" }

coil-core = { module = "io.coil-kt:coil", version.ref = "coil" }

Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ rootProject.name = "HMH-Android"
include(":app")
include(":core:common")
include(":feature:onboarding")
include(":core:database")

0 comments on commit 85c079e

Please sign in to comment.