Skip to content

Commit

Permalink
Update Jetpack Room to support KMP
Browse files Browse the repository at this point in the history
  • Loading branch information
kirich1409 committed Jun 18, 2024
1 parent 40bbe32 commit 66694e7
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 6 deletions.
2 changes: 1 addition & 1 deletion core/database/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ kotlin {
sourceSets {
commonMain.dependencies {
implementation(libs.kotlinx.datetime)
implementation(libs.androidx.room.ktx)
}

androidMain.dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.room.ktx)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,32 @@ import androidx.room.TypeConverters
import dev.androidbroadcast.news.database.dao.ArticleDao
import dev.androidbroadcast.news.database.models.ArticleDBO
import dev.androidbroadcast.news.database.utils.Converters
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

class NewsDatabase internal constructor(private val database: NewsRoomDatabase) {
val articlesDao: ArticleDao
get() = database.articlesDao()
}

@Database(entities = [ArticleDBO::class], version = 1)
@Database(entities = [ArticleDBO::class], version = 2)
@TypeConverters(Converters::class)
internal abstract class NewsRoomDatabase : RoomDatabase() {
abstract fun articlesDao(): ArticleDao
}

fun NewsDatabase(applicationContext: Context): NewsDatabase {
fun NewsDatabase(
applicationContext: Context,
dispatcher: CoroutineDispatcher = Dispatchers.IO,
): NewsDatabase {
val newsRoomDatabase =
Room.databaseBuilder(
checkNotNull(applicationContext.applicationContext),
NewsRoomDatabase::class.java,
"news"
).build()
)
.setQueryCoroutineContext(dispatcher)
.fallbackToDestructiveMigration(dropAllTables = false)
.build()
return NewsDatabase(newsRoomDatabase)
}
11 changes: 10 additions & 1 deletion core/opennews-api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
@file:OptIn(ExperimentalKotlinGradlePluginApi::class)

import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
alias(libs.plugins.kotlinSerialization)
alias(libs.plugins.kotlinMultiplatform)
Expand All @@ -6,7 +11,11 @@ plugins {
}

kotlin {
androidTarget()
androidTarget {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
}
}

sourceSets {
commonMain.dependencies {
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ kotlinx-coroutines = "1.9.0-RC"
retrofitAdaptersResult = "1.0.9"
appcompat = "1.7.0"
material = "1.12.0"
room = "2.6.1"
room = "2.7.0-alpha04"
ksp = "2.0.0-1.0.21"
dagger = "2.51.1"
javax-inject = "1"
Expand Down
76 changes: 76 additions & 0 deletions schemas/dev.androidbroadcast.news.database.NewsRoomDatabase/2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "f00a51e45d5213422f6abd8bb4b66326",
"entities": [
{
"tableName": "articles",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`author` TEXT, `title` TEXT, `description` TEXT, `url` TEXT, `urlToImage` TEXT, `publishedAt` INTEGER, `content` TEXT, `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `sourceid` TEXT, `sourcename` TEXT)",
"fields": [
{
"fieldPath": "author",
"columnName": "author",
"affinity": "TEXT"
},
{
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT"
},
{
"fieldPath": "description",
"columnName": "description",
"affinity": "TEXT"
},
{
"fieldPath": "url",
"columnName": "url",
"affinity": "TEXT"
},
{
"fieldPath": "urlToImage",
"columnName": "urlToImage",
"affinity": "TEXT"
},
{
"fieldPath": "publishedAt",
"columnName": "publishedAt",
"affinity": "INTEGER"
},
{
"fieldPath": "content",
"columnName": "content",
"affinity": "TEXT"
},
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "source.id",
"columnName": "sourceid",
"affinity": "TEXT"
},
{
"fieldPath": "source.name",
"columnName": "sourcename",
"affinity": "TEXT"
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
}
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'f00a51e45d5213422f6abd8bb4b66326')"
]
}
}

0 comments on commit 66694e7

Please sign in to comment.