Skip to content

Commit

Permalink
Added remembering of selected recognition type in OCR by #1366
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Sep 27, 2024
1 parent 431c839 commit 59c5ede
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,6 @@ interface SettingsInteractor : SimpleSettingsInteractor {

suspend fun setInitialOCRLanguageCodes(list: List<String>)

suspend fun getInitialOCRLanguageCodes(): List<String>

suspend fun setScreensWithBrightnessEnforcement(data: String)

suspend fun toggleConfettiEnabled()
Expand Down Expand Up @@ -202,4 +200,6 @@ interface SettingsInteractor : SimpleSettingsInteractor {

suspend fun toggleIsSystemBarsVisibleBySwipe()

suspend fun setInitialOcrMode(mode: Int)

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ interface SettingsProvider {

fun getNeedToShowTelegramGroupDialog(): Flow<Boolean>

suspend fun getInitialOCRLanguageCodes(): List<String>

suspend fun getInitialOcrMode(): Int

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class RecognizeTextViewModel @Inject constructor(
private val _selectedLanguages = mutableStateOf(listOf(OCRLanguage.Default))
val selectedLanguages by _selectedLanguages

private var isRecognitionTypeSet = false
private val _recognitionType = mutableStateOf(RecognitionType.Standard)
val recognitionType by _recognitionType

Expand Down Expand Up @@ -172,6 +173,12 @@ class RecognizeTextViewModel @Inject constructor(
) {
loadingJob = viewModelScope.launch {
delay(200L)
if (!isRecognitionTypeSet) {
_recognitionType.update {
RecognitionType.entries[settingsManager.getInitialOcrMode()]
}
isRecognitionTypeSet = true
}
val data = imageTextReader.getLanguages(recognitionType)
_selectedLanguages.update { ocrLanguages ->
val list = ocrLanguages.toMutableList()
Expand Down Expand Up @@ -284,6 +291,9 @@ class RecognizeTextViewModel @Inject constructor(

fun setRecognitionType(recognitionType: RecognitionType) {
_recognitionType.update { recognitionType }
viewModelScope.launch {
settingsManager.setInitialOcrMode(recognitionType.ordinal)
}
loadLanguages()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.ICON_SHAPE
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.IMAGE_PICKER_MODE
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.IMAGE_SCALE_MODE
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.INITIAL_OCR_CODES
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.INITIAL_OCR_MODE
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.INVERT_THEME
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.IS_LINK_PREVIEW_ENABLED
import ru.tech.imageresizershrinker.feature.settings.data.SettingKeys.IS_SYSTEM_BARS_VISIBLE_BY_SWIPE
Expand Down Expand Up @@ -722,6 +723,12 @@ internal class AndroidSettingsManager @Inject constructor(
}
}

override suspend fun getInitialOcrMode(): Int {
return dataStore.data.first().let { prefs ->
prefs[INITIAL_OCR_MODE] ?: 1
}
}

override suspend fun setScreensWithBrightnessEnforcement(data: String) {
dataStore.edit {
it[SCREENS_WITH_BRIGHTNESS_ENFORCEMENT] = data
Expand Down Expand Up @@ -1031,6 +1038,12 @@ internal class AndroidSettingsManager @Inject constructor(
}
}

override suspend fun setInitialOcrMode(mode: Int) {
dataStore.edit {
it[INITIAL_OCR_MODE] = mode
}
}

private suspend fun setFavoriteScreens(data: List<Int>) {
dataStore.edit { prefs ->
prefs[FAVORITE_SCREENS] = data.joinToString("/") { it.toString() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,5 @@ internal object SettingKeys {
val DEFAULT_RESIZE_TYPE = intPreferencesKey("DEFAULT_RESIZE_TYPE")
val SYSTEM_BARS_VISIBILITY = intPreferencesKey("SYSTEM_BARS_VISIBILITY")
val IS_SYSTEM_BARS_VISIBLE_BY_SWIPE = booleanPreferencesKey("IS_SYSTEM_BARS_VISIBLE_BY_SWIPE")
val INITIAL_OCR_MODE = intPreferencesKey("INITIAL_OCR_MODE")
}

0 comments on commit 59c5ede

Please sign in to comment.