Skip to content

Commit

Permalink
feat: add button to swap languages in translation dialog (closes #466)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Dec 8, 2024
1 parent ae0cdb9 commit 44c69d2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,7 @@ fun SwapLanguagesButton(viewModel: TranslationModel) {

IconButton(
onClick = {
if (viewModel.availableLanguages.isEmpty()) return@IconButton
if (!switchBtnEnabled) return@IconButton
val temp = viewModel.sourceLanguage
viewModel.sourceLanguage = viewModel.targetLanguage
viewModel.targetLanguage = temp

if (viewModel.translation.translatedText.isNotEmpty()) {
viewModel.insertedText = viewModel.translation.translatedText
viewModel.translation = Translation("")
}

viewModel.translateNow(context)
if (switchBtnEnabled) viewModel.swapLanguages(context)
}
) {
Icon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material.icons.filled.DoneAll
import androidx.compose.material.icons.filled.Share
import androidx.compose.material.icons.filled.SwapHoriz
import androidx.compose.material.icons.filled.VolumeUp
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -95,6 +96,10 @@ fun TranslationField(

Spacer(modifier = Modifier.weight(1f))

if (isSourceField && showLanguageSelector) {
SwapLanguagesButton(translationModel)
}

var copyImageVector by remember {
mutableStateOf(Icons.Default.ContentCopy)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,21 @@ class TranslationModel : ViewModel() {
)
}

fun swapLanguages(context: Context) {
if (availableLanguages.isEmpty()) return

val temp = sourceLanguage
sourceLanguage = targetLanguage
targetLanguage = temp

if (translation.translatedText.isNotEmpty()) {
insertedText = translation.translatedText
translation = Translation("")
}

translateNow(context)
}

fun playAudio(languageCode: String, text: String) {
releaseMediaPlayer()

Expand Down

0 comments on commit 44c69d2

Please sign in to comment.