From bd3172b3427ef39a7d220d30d3f94add55a8ceb9 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 29 Jul 2024 16:51:13 +0200 Subject: [PATCH] fix: downloaded OCR languages models UI, autoselect first downloaded OCR model --- .../bnyro/translate/ui/components/LanguageSelector.kt | 5 ++--- .../java/com/bnyro/translate/ui/models/TessModel.kt | 3 --- .../java/com/bnyro/translate/ui/views/TessSettings.kt | 10 ++++++---- 3 files changed, 8 insertions(+), 10 deletions(-) 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 88824cd83..1707fb6a3 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 @@ -261,7 +261,6 @@ fun LanguageSelector( private fun validateFilter(language: Language, query: String): Boolean { if (query.isEmpty()) return true - val lowerQuery = query.lowercase() - return language.name.lowercase().contains(lowerQuery) || - language.code.lowercase().contains(lowerQuery) + return language.name.lowercase().contains(query, ignoreCase = true) || + language.code.lowercase().contains(query, ignoreCase = true) } diff --git a/app/src/main/java/com/bnyro/translate/ui/models/TessModel.kt b/app/src/main/java/com/bnyro/translate/ui/models/TessModel.kt index 0385921c0..fff4b81d7 100644 --- a/app/src/main/java/com/bnyro/translate/ui/models/TessModel.kt +++ b/app/src/main/java/com/bnyro/translate/ui/models/TessModel.kt @@ -20,7 +20,6 @@ package com.bnyro.translate.ui.models import android.content.Context import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope @@ -28,12 +27,10 @@ import com.bnyro.translate.obj.TessLanguage import com.bnyro.translate.util.TessHelper import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext class TessModel: ViewModel() { var availableLanguages by mutableStateOf(emptyList()) var downloadedLanguages by mutableStateOf(emptyList()) - var notYetDownloadedLanguages by mutableStateOf(emptyList()) fun init(context: Context) { downloadedLanguages = TessHelper.getDownloadedLanguages(context) diff --git a/app/src/main/java/com/bnyro/translate/ui/views/TessSettings.kt b/app/src/main/java/com/bnyro/translate/ui/views/TessSettings.kt index 434aa4abc..fe2366ea9 100644 --- a/app/src/main/java/com/bnyro/translate/ui/views/TessSettings.kt +++ b/app/src/main/java/com/bnyro/translate/ui/views/TessSettings.kt @@ -99,17 +99,15 @@ fun TessSettings( } LaunchedEffect(query, tessModel.availableLanguages, tessModel.downloadedLanguages) { - val lowerQuery = query.lowercase() - filteredAvailableLanguages = tessModel.availableLanguages.filter { tessLang -> - tessLang.path.replace(TessHelper.DATA_FILE_SUFFIX, "").contains(lowerQuery) && + tessLang.path.replace(TessHelper.DATA_FILE_SUFFIX, "").contains(query, ignoreCase = true) && tessModel.downloadedLanguages.none { tessLang.path.replace(TessHelper.DATA_FILE_SUFFIX, "") == it } } filteredDownloadedLanguages = tessModel.downloadedLanguages.filter { lang -> - lowerQuery.contains(lang) + lang.contains(query, ignoreCase = true) } } @@ -117,6 +115,10 @@ fun TessSettings( override fun onReceive(context: Context, intent: Intent) { // Refresh the downloaded languages every time a download finishes tessModel.refreshDownloadedLanguages(context) + if (tessModel.downloadedLanguages.size == 1) { + selectedLanguage = tessModel.downloadedLanguages.first() + Preferences.put(Preferences.tessLanguageKey, selectedLanguage) + } } }