Skip to content

Commit

Permalink
Switch back to synchronous SqlDelight
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedre committed Nov 18, 2024
1 parent 17a34fc commit ccfca6b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
1 change: 0 additions & 1 deletion common/translation/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ sqldelight {
packageName.set("com.quran.mobile.translation.data")
schemaOutputDirectory.set(file("src/main/sqldelight/databases"))
verifyMigrations.set(true)
generateAsync.set(true)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.quran.mobile.translation.data

import app.cash.sqldelight.async.coroutines.awaitAsOne
import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import com.quran.mobile.translation.mapper.LocalTranslationMapper
Expand All @@ -10,6 +9,7 @@ import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.withContext
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -28,37 +28,45 @@ class TranslationsDataSource @Inject constructor(translationsDatabase: Translati
fun translations(): StateFlow<List<LocalTranslation>?> = translations

suspend fun updateTranslations(items: List<LocalTranslation>) {
translationsQueries.transaction {
items.forEach {
translationsQueries.update(
id = it.id,
name = it.name,
translator = it.translator,
translatorForeign = it.translatorForeign,
filename = it.filename,
url = it.url,
languageCode = it.languageCode,
version = it.version.toLong(),
minimumRequiredVersion = it.minimumVersion.toLong(),
userDisplayOrder = it.displayOrder.toLong()
)
withContext(Dispatchers.IO) {
translationsQueries.transaction {
items.forEach {
translationsQueries.update(
id = it.id,
name = it.name,
translator = it.translator,
translatorForeign = it.translatorForeign,
filename = it.filename,
url = it.url,
languageCode = it.languageCode,
version = it.version.toLong(),
minimumRequiredVersion = it.minimumVersion.toLong(),
userDisplayOrder = it.displayOrder.toLong()
)
}
}
}
}

suspend fun removeTranslation(filename: String) {
translationsQueries.deleteByFileName(filename)
withContext(Dispatchers.IO) {
translationsQueries.deleteByFileName(filename)
}
}

suspend fun removeTranslationsById(ids: List<Long>) {
translationsQueries.transaction {
ids.forEach {
translationsQueries.deleteById(it)
withContext(Dispatchers.IO) {
translationsQueries.transaction {
ids.forEach {
translationsQueries.deleteById(it)
}
}
}
}

suspend fun maximumDisplayOrder(): Long {
return translationsQueries.greatestDisplayOrder().awaitAsOne().MAX ?: 0
return withContext(Dispatchers.IO) {
translationsQueries.greatestDisplayOrder().executeAsOne().MAX ?: 0
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.quran.mobile.translation.di

import android.content.Context
import app.cash.sqldelight.async.coroutines.synchronous
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
import com.quran.data.di.AppScope
import com.quran.mobile.di.qualifier.ApplicationContext
Expand All @@ -20,7 +19,7 @@ class TranslationDataModule {
fun provideTranslationDatabase(@ApplicationContext context: Context): TranslationsDatabase {
return TranslationsDatabase(
AndroidSqliteDriver(
schema = TranslationsDatabase.Schema.synchronous(),
schema = TranslationsDatabase.Schema,
context = context,
name = "translations.db"
)
Expand Down

0 comments on commit ccfca6b

Please sign in to comment.