Skip to content

Commit

Permalink
feat: simultaneous translation in quick translation dialog (closes #462)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Nov 6, 2024
1 parent ee5c83e commit dd782c4
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 51 deletions.
72 changes: 48 additions & 24 deletions app/src/main/java/com/bnyro/translate/ui/ShareActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.FilterChip
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.stringResource
Expand All @@ -40,6 +44,7 @@ import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.ext.parcelable
import com.bnyro.translate.ui.components.AppHeader
import com.bnyro.translate.ui.components.DialogButton
import com.bnyro.translate.ui.views.SimTranslationDialogComponent
import com.bnyro.translate.ui.views.TranslationComponent
import com.bnyro.translate.util.ImageHelper

Expand All @@ -57,31 +62,50 @@ class ShareActivity : BaseActivity() {
translationModel.refresh(this@ShareActivity)
}

AlertDialog(modifier = Modifier
.heightIn(
max = if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) screenHeight * 2 / 3 else Dp.Unspecified
)
.padding(horizontal = 10.dp), properties = DialogProperties(
dismissOnClickOutside = false, usePlatformDefaultWidth = false
), onDismissRequest = { finish() }, confirmButton = {
DialogButton(
text = stringResource(R.string.okay)
) {
finish()
}
}, dismissButton = {
DialogButton(text = stringResource(R.string.clear)) {
translationModel.clearTranslation()
AlertDialog(
modifier = Modifier
.heightIn(
max = if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT)
screenHeight * 2 / 3 else Dp.Unspecified
)
.padding(horizontal = 10.dp),
properties = DialogProperties(
dismissOnClickOutside = false,
usePlatformDefaultWidth = false
),
onDismissRequest = { finish() },
confirmButton = {
DialogButton(
text = stringResource(R.string.okay)
) {
finish()
}
},
dismissButton = {
DialogButton(text = stringResource(R.string.clear)) {
translationModel.clearTranslation()
}
},
title = {
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
AppHeader(modifier = Modifier.weight(1f))

if (translationModel.simTranslationEnabled) {
SimTranslationDialogComponent(translationModel)
}
}
},
text = {
TranslationComponent(
modifier = Modifier.fillMaxSize(),
viewModel = translationModel,
showLanguageSelector = true
)
}
}, title = {
AppHeader()
}, text = {
TranslationComponent(
modifier = Modifier.fillMaxSize(),
viewModel = translationModel,
showLanguageSelector = true
)
})
)
}

setFinishOnTouchOutside(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ import com.bnyro.translate.R
import com.bnyro.translate.ui.MainActivity

@Composable
fun AppHeader() {
fun AppHeader(modifier: Modifier = Modifier) {
val context = LocalContext.current

Row(
modifier = Modifier.clickable(interactionSource = remember {
modifier = modifier.clickable(interactionSource = remember {
MutableInteractionSource()
}, indication = null) {
val intent = Intent(context, MainActivity::class.java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.bnyro.translate.R
import com.bnyro.translate.const.ThemeMode
import com.bnyro.translate.obj.ListPreferenceOption
import com.bnyro.translate.ui.MainActivity
import com.bnyro.translate.ui.components.prefs.ListPreferenceDialog
import com.bnyro.translate.util.Preferences

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import com.bnyro.translate.obj.ListPreferenceOption
import com.bnyro.translate.ui.components.ListPreferenceDialog
import com.bnyro.translate.util.Preferences

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.bnyro.translate.ui.components
package com.bnyro.translate.ui.components.prefs

import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
Expand All @@ -26,11 +26,12 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import com.bnyro.translate.R
import com.bnyro.translate.obj.ListPreferenceOption
import com.bnyro.translate.ui.components.SelectableItem
import com.bnyro.translate.util.Preferences

@Composable
fun ListPreferenceDialog(
preferenceKey: String,
preferenceKey: String?,
onDismissRequest: () -> Unit,
options: List<ListPreferenceOption>,
currentValue: Int? = null,
Expand All @@ -49,10 +50,12 @@ fun ListPreferenceDialog(
SelectableItem(
text = if (it.value == currentValue) "${it.name}" else it.name,
onClick = {
Preferences.put(
preferenceKey,
it.value.toString()
)
if (preferenceKey != null) {
Preferences.put(
preferenceKey,
it.value.toString()
)
}
onOptionSelected.invoke(it)
onDismissRequest.invoke()
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ fun EngineSelectionDialog(
},
text = {
MultiSelectList(
titles = simEngines.filter {
it.supportsSimTranslation
}.map { it.name },
titles = simEngines.map { it.name },
selectedItems = selectedItems,
onChange = {
selectedItems = it
Expand Down
32 changes: 17 additions & 15 deletions app/src/main/java/com/bnyro/translate/ui/models/TranslationModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ import com.bnyro.translate.util.JsonHelper
import com.bnyro.translate.util.Preferences
import com.bnyro.translate.util.TessHelper
import java.io.File
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.encodeToString
Expand Down Expand Up @@ -70,12 +71,13 @@ class TranslationModel : ViewModel() {
var translation by mutableStateOf(Translation(""))

var translatedTexts = TranslationEngines.engines
.associate { it.name to Translation("") }
.toMutableMap()
.associate { it.name to Translation("") }
.toMutableMap()

var bookmarkedLanguages by mutableStateOf(listOf<Language>())

var translating by mutableStateOf(false)
private var simTranslationJobs = emptyList<Job>()

private var mediaPlayer: MediaPlayer? = null
private var audioFile: File? = null
Expand Down Expand Up @@ -116,7 +118,7 @@ class TranslationModel : ViewModel() {
.associate { it.name to Translation("") }
.toMutableMap()

CoroutineScope(Dispatchers.IO).launch {
viewModelScope.launch(Dispatchers.IO) {
val translation = try {
engine.translate(
insertedText,
Expand All @@ -141,23 +143,23 @@ class TranslationModel : ViewModel() {
if (simTranslationEnabled) simTranslation()
}

private fun simTranslation() {
enabledSimEngines.forEach {
if (it != engine) {
CoroutineScope(Dispatchers.IO).launch {
val translation = try {
it.translate(
private fun simTranslation() = viewModelScope.launch(Dispatchers.IO) {
// cancel old jobs from previous translation
simTranslationJobs.forEach { if (it.isActive) it.cancel() }

simTranslationJobs = enabledSimEngines
.filter { it != engine }
.map {
async {
runCatching {
translatedTexts[it.name] = it.translate(
insertedText,
sourceLanguage.code,
targetLanguage.code
)
} catch (e: Exception) {
return@launch
}
translatedTexts[it.name] = translation
}
}
}
}

private fun saveToHistory() {
Expand Down Expand Up @@ -223,7 +225,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 @@ -28,8 +28,12 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.bnyro.translate.R
import com.bnyro.translate.obj.ListPreferenceOption
import com.bnyro.translate.obj.Translation
import com.bnyro.translate.ui.components.prefs.ListPreferenceDialog
import com.bnyro.translate.ui.models.TranslationModel

@Composable
Expand Down Expand Up @@ -60,3 +64,42 @@ fun SimTranslationComponent(
}
}
}

@Composable
fun SimTranslationDialogComponent(
viewModel: TranslationModel
) {
var selected by remember {
mutableStateOf(viewModel.engine)
}
var showSelectionDialog by remember {
mutableStateOf(false)
}

ElevatedFilterChip(
selected = true,
onClick = {
showSelectionDialog = true
},
label = {
Text(selected.name)
},
modifier = Modifier
.padding(5.dp, 0.dp)
)

if (showSelectionDialog) {
ListPreferenceDialog(
preferenceKey = null,
title = stringResource(R.string.selected_engine),
onDismissRequest = { showSelectionDialog = false },
options = viewModel.enabledSimEngines.mapIndexed { index, engine ->
ListPreferenceOption(engine.name, index, selected == engine)
}
) { engineOption ->
selected = viewModel.enabledSimEngines[engineOption.value]
viewModel.engine = selected
viewModel.translation = viewModel.translatedTexts[selected.name] ?: Translation("")
}
}
}

0 comments on commit dd782c4

Please sign in to comment.