Skip to content

Commit

Permalink
Option to skip similar entries in the history (closes #272)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Jun 16, 2023
1 parent 58d6976 commit 8173344
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
7 changes: 7 additions & 0 deletions app/src/main/java/com/bnyro/translate/db/dao/HistoryDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ interface HistoryDao {
@Query("SELECT * FROM HistoryItem")
fun getAll(): List<HistoryItem>

@Query("SELECT EXISTS(SELECT * FROM HistoryItem WHERE insertedText = :insertedText AND sourceLanguageCode = :sourceLanguage AND targetLanguageCode = :targetLanguage)")
fun existsSimilar(
insertedText: String,
sourceLanguage: String,
targetLanguage: String
): Boolean

@Insert
fun insertAll(vararg historyItems: HistoryItem)

Expand Down
31 changes: 20 additions & 11 deletions app/src/main/java/com/bnyro/translate/ui/models/TranslationModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,26 @@ class TranslationModel : ViewModel() {
) {
return
}

val historyItem = HistoryItem(
sourceLanguageCode = sourceLanguage.code,
sourceLanguageName = sourceLanguage.name,
targetLanguageCode = targetLanguage.code,
targetLanguageName = targetLanguage.name,
insertedText = insertedText,
translatedText = translation.translatedText
)

query {
Db.historyDao().insertAll(
HistoryItem(
sourceLanguageCode = sourceLanguage.code,
sourceLanguageName = sourceLanguage.name,
targetLanguageCode = targetLanguage.code,
targetLanguageName = targetLanguage.name,
insertedText = insertedText,
translatedText = translation.translatedText
)
)
// don't create new entry if a similar one exists
if (Preferences.get(Preferences.skipSimilarHistoryKey, true) && Db.historyDao()
.existsSimilar(
historyItem.insertedText,
historyItem.sourceLanguageCode,
historyItem.targetLanguageCode
)
) return@query
Db.historyDao().insertAll(historyItem)
}
}

Expand Down Expand Up @@ -194,7 +203,7 @@ class TranslationModel : ViewModel() {
}

private fun getCurrentEngine() = TranslationEngines.engines[
Preferences.get(Preferences.apiTypeKey, 0)
Preferences.get(Preferences.apiTypeKey, 0)
]

private fun getEnabledEngines() = TranslationEngines.engines.filter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ fun SettingsScreen(
preferenceTitle = stringResource(R.string.compact_history),
preferenceSummary = stringResource(R.string.compact_history_summary)
)

SwitchPreference(
preferenceKey = Preferences.skipSimilarHistoryKey,
defaultValue = true,
preferenceTitle = stringResource(R.string.skip_similar_entries),
preferenceSummary = stringResource(R.string.skip_similar_entries_desc)
)
}

item {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/bnyro/translate/util/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ object Preferences {
const val apiKey = "apiKey"
const val apiTypeKey = "apiTypeKey"
const val historyEnabledKey = "historyEnabledKey"
const val skipSimilarHistoryKey = "skipSimilarHistory"
const val translateAutomatically = "translateAutomatically"
const val fetchDelay = "fetchDelay"
const val compactHistory = "compactHistory"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<string name="required">Required</string>
<string name="optional">Optional</string>
<string name="compact_history">Compact history</string>
<string name="skip_similar_entries">Skip similar entries</string>
<string name="skip_similar_entries_desc">Only store unique entries in the history, based on insertion and languages.</string>
<string name="compact_history_summary">Only show the first 3 text lines per default in the history</string>
<string name="simultaneous_translation">Simultaneous translation</string>
<string name="simultaneous_translation_summary">Fetch results from multiple engines at the same time.</string>
Expand Down

0 comments on commit 8173344

Please sign in to comment.