diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 1ea4e6f55..d6fc18705 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -97,7 +97,7 @@ dependencies { implementation("com.squareup.okhttp3:logging-interceptor:4.12.0") // Room database - implementation("androidx.room:room-runtime:2.5.1") + implementation("androidx.room:room-ktx:2.5.1") ksp("androidx.room:room-compiler:2.5.1") // Tesseract OCR diff --git a/app/src/main/java/com/bnyro/translate/db/dao/HistoryDao.kt b/app/src/main/java/com/bnyro/translate/db/dao/HistoryDao.kt index 81ebc3e51..e2c4987f1 100644 --- a/app/src/main/java/com/bnyro/translate/db/dao/HistoryDao.kt +++ b/app/src/main/java/com/bnyro/translate/db/dao/HistoryDao.kt @@ -27,10 +27,10 @@ import com.bnyro.translate.db.obj.HistoryItemType @Dao interface HistoryDao { @Query("SELECT * FROM HistoryItem WHERE itemType = :type") - fun getAll(type: HistoryItemType): List + suspend fun getAll(type: HistoryItemType): List @Query("SELECT EXISTS(SELECT * FROM HistoryItem WHERE insertedText = :insertedText AND sourceLanguageCode = :sourceLanguage AND targetLanguageCode = :targetLanguage AND itemType = :itemType)") - fun existsSimilar( + suspend fun existsSimilar( insertedText: String, sourceLanguage: String, targetLanguage: String, @@ -38,11 +38,11 @@ interface HistoryDao { ): Boolean @Insert - fun insertAll(vararg historyItems: HistoryItem) + suspend fun insertAll(vararg historyItems: HistoryItem) @Delete - fun delete(historyItem: HistoryItem) + suspend fun delete(historyItem: HistoryItem) @Query("DELETE FROM HistoryItem WHERE itemType = :type") - fun deleteAll(type: HistoryItemType) + suspend fun deleteAll(type: HistoryItemType) } diff --git a/app/src/main/java/com/bnyro/translate/db/dao/LanguageBookmarksDao.kt b/app/src/main/java/com/bnyro/translate/db/dao/LanguageBookmarksDao.kt index fce12318c..275bb6d3b 100644 --- a/app/src/main/java/com/bnyro/translate/db/dao/LanguageBookmarksDao.kt +++ b/app/src/main/java/com/bnyro/translate/db/dao/LanguageBookmarksDao.kt @@ -26,14 +26,14 @@ import com.bnyro.translate.db.obj.Language @Dao interface LanguageBookmarksDao { @Query("SELECT * FROM Language") - fun getAll(): List + suspend fun getAll(): List @Insert - fun insertAll(vararg languages: Language) + suspend fun insertAll(vararg languages: Language) @Delete - fun delete(language: Language) + suspend fun delete(language: Language) @Query("DELETE FROM Language") - fun deleteAll() + suspend fun deleteAll() } diff --git a/app/src/main/java/com/bnyro/translate/ext/AwaitQuery.kt b/app/src/main/java/com/bnyro/translate/ext/AwaitQuery.kt deleted file mode 100644 index 189386914..000000000 --- a/app/src/main/java/com/bnyro/translate/ext/AwaitQuery.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2023 You Apps - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.bnyro.translate.ext - -fun awaitQuery(action: () -> T): T { - var value: T? = null - Thread { - value = action.invoke() - }.apply { - start() - join() - } - return value!! -} diff --git a/app/src/main/java/com/bnyro/translate/ext/Query.kt b/app/src/main/java/com/bnyro/translate/ext/Query.kt deleted file mode 100644 index 095de6301..000000000 --- a/app/src/main/java/com/bnyro/translate/ext/Query.kt +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2023 You Apps - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.bnyro.translate.ext - -fun query(dbQuery: () -> Unit) { - Thread { - dbQuery.invoke() - }.start() -} diff --git a/app/src/main/java/com/bnyro/translate/ui/components/LanguageSelector.kt b/app/src/main/java/com/bnyro/translate/ui/components/LanguageSelector.kt index ae47176bc..88824cd83 100644 --- a/app/src/main/java/com/bnyro/translate/ui/components/LanguageSelector.kt +++ b/app/src/main/java/com/bnyro/translate/ui/components/LanguageSelector.kt @@ -39,6 +39,7 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip @@ -50,9 +51,10 @@ import androidx.compose.ui.unit.sp import com.bnyro.translate.DatabaseHolder import com.bnyro.translate.R import com.bnyro.translate.db.obj.Language -import com.bnyro.translate.ext.query import com.bnyro.translate.ui.dialogs.FullscreenDialog import com.bnyro.translate.ui.models.TranslationModel +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch @OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class) @Composable @@ -64,6 +66,8 @@ fun LanguageSelector( useElevatedButton: Boolean = true, onClick: (Language) -> Unit ) { + val scope = rememberCoroutineScope() + var showDialog by remember { mutableStateOf(false) } @@ -199,7 +203,7 @@ fun LanguageSelector( viewModel.bookmarkedLanguages.filter { language -> it != language } - query { + scope.launch(Dispatchers.IO) { DatabaseHolder.Db.languageBookmarksDao().delete(it) } } @@ -235,14 +239,13 @@ fun LanguageSelector( onPinnedChange = { viewModel.bookmarkedLanguages = if (viewModel.bookmarkedLanguages.contains(it)) { - query { + scope.launch(Dispatchers.IO) { DatabaseHolder.Db.languageBookmarksDao().delete(it) } viewModel.bookmarkedLanguages - it } else { - query { - DatabaseHolder.Db.languageBookmarksDao() - .insertAll(it) + scope.launch(Dispatchers.IO) { + DatabaseHolder.Db.languageBookmarksDao().insertAll(it) } viewModel.bookmarkedLanguages + it } diff --git a/app/src/main/java/com/bnyro/translate/ui/dialogs/PrivacyPolicyDialog.kt b/app/src/main/java/com/bnyro/translate/ui/dialogs/PrivacyPolicyDialog.kt index 937d66fa1..9d51a4820 100644 --- a/app/src/main/java/com/bnyro/translate/ui/dialogs/PrivacyPolicyDialog.kt +++ b/app/src/main/java/com/bnyro/translate/ui/dialogs/PrivacyPolicyDialog.kt @@ -33,9 +33,10 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import com.bnyro.translate.R import com.bnyro.translate.const.AboutLinks -import com.bnyro.translate.ext.query import com.bnyro.translate.util.ClipboardHelper import java.net.URL +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext @Composable fun PrivacyPolicyDialog( @@ -48,7 +49,7 @@ fun PrivacyPolicyDialog( } LaunchedEffect(Unit) { - query { + withContext(Dispatchers.IO) { privacyPolicyHtml = try { URL(AboutLinks.PRIVACY_POLICY).readText() } catch (e: Exception) { diff --git a/app/src/main/java/com/bnyro/translate/ui/models/HistoryModel.kt b/app/src/main/java/com/bnyro/translate/ui/models/HistoryModel.kt index 16b01e772..57574b741 100644 --- a/app/src/main/java/com/bnyro/translate/ui/models/HistoryModel.kt +++ b/app/src/main/java/com/bnyro/translate/ui/models/HistoryModel.kt @@ -20,10 +20,12 @@ package com.bnyro.translate.ui.models import android.annotation.SuppressLint import androidx.compose.runtime.mutableStateListOf import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.bnyro.translate.DatabaseHolder.Companion.Db import com.bnyro.translate.db.obj.HistoryItem import com.bnyro.translate.db.obj.HistoryItemType -import com.bnyro.translate.ext.query +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch @SuppressLint("MutableCollectionMutableState") class HistoryModel : ViewModel() { @@ -32,7 +34,7 @@ class HistoryModel : ViewModel() { fun fetchItems(itemType: HistoryItemType) { items.clear() - query { + viewModelScope.launch(Dispatchers.IO) { items.addAll(Db.historyDao().getAll(itemType).reversed()) } } @@ -40,15 +42,15 @@ class HistoryModel : ViewModel() { fun clearItems(itemType: HistoryItemType) { items.clear() - query { + viewModelScope.launch(Dispatchers.IO) { Db.historyDao().deleteAll(itemType) } } fun deleteItem(historyItem: HistoryItem) { - items.removeAll { it.id != historyItem.id } + items.removeAll { it.id == historyItem.id } - query { + viewModelScope.launch(Dispatchers.IO) { Db.historyDao().delete(historyItem) } } diff --git a/app/src/main/java/com/bnyro/translate/ui/models/TranslationModel.kt b/app/src/main/java/com/bnyro/translate/ui/models/TranslationModel.kt index 605700218..017b3b4b6 100644 --- a/app/src/main/java/com/bnyro/translate/ui/models/TranslationModel.kt +++ b/app/src/main/java/com/bnyro/translate/ui/models/TranslationModel.kt @@ -35,8 +35,6 @@ import com.bnyro.translate.const.TranslationEngines import com.bnyro.translate.db.obj.HistoryItem import com.bnyro.translate.db.obj.HistoryItemType import com.bnyro.translate.db.obj.Language -import com.bnyro.translate.ext.awaitQuery -import com.bnyro.translate.ext.query import com.bnyro.translate.obj.Translation import com.bnyro.translate.util.JsonHelper import com.bnyro.translate.util.Preferences @@ -181,7 +179,7 @@ class TranslationModel : ViewModel() { itemType = itemType ) - query { + viewModelScope.launch(Dispatchers.IO) { // don't create new entry if a similar one exists if (Preferences.get(Preferences.skipSimilarHistoryKey, true) && Db.historyDao() .existsSimilar( @@ -190,7 +188,7 @@ class TranslationModel : ViewModel() { historyItem.targetLanguageCode, itemType = itemType ) - ) return@query + ) return@launch Db.historyDao().insertAll(historyItem) } @@ -247,10 +245,8 @@ class TranslationModel : ViewModel() { fetchBookmarkedLanguages() } - private fun fetchBookmarkedLanguages() { - bookmarkedLanguages = awaitQuery { - Db.languageBookmarksDao().getAll() - } + private fun fetchBookmarkedLanguages() = viewModelScope.launch(Dispatchers.IO) { + bookmarkedLanguages = Db.languageBookmarksDao().getAll() } fun processImage(context: Context, uri: Uri?) {