Skip to content

Commit

Permalink
added progress indicator on translation
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Apr 16, 2024
1 parent 10cf182 commit 0c0757a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Translate
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.google.mlkit.common.model.DownloadConditions
import com.google.mlkit.nl.translate.TranslateLanguage
import com.google.mlkit.nl.translate.Translation
Expand All @@ -20,7 +18,10 @@ import io.github.aakira.napier.Napier
import java.util.Locale

@Composable
actual fun TranslateButton(text: String, onTranslation: (String?) -> Unit) {
actual fun TranslateButton(
text: String,
onTranslation: (String?) -> Unit,
) {
val locale = remember { Locale.getDefault() }
if (locale.language.equals(Locale.forLanguageTag("en").language, ignoreCase = true)) {
Napier.e("Language is english")
Expand Down Expand Up @@ -60,26 +61,35 @@ actual fun TranslateButton(text: String, onTranslation: (String?) -> Unit) {
}
var enabled by remember { mutableStateOf(true) }
var translated by remember { mutableStateOf(false) }
var progress by remember { mutableStateOf(false) }

TextButton(
onClick = {
if (translated) {
translated = false
onTranslation(null)
} else {
progress = true
enabled = false

englishLocaleTranslator
.downloadModelIfNeeded(downloadConditions)
.addOnFailureListener {
progress = false
enabled = false
}.addOnSuccessListener {
enabled = true

englishLocaleTranslator
.translate(text)
.addOnFailureListener {
progress = false
enabled = true

translated = false
onTranslation(null)
}.addOnSuccessListener {
progress = false
enabled = true

translated = true
onTranslation(it)
}
Expand All @@ -88,11 +98,19 @@ actual fun TranslateButton(text: String, onTranslation: (String?) -> Unit) {
},
enabled = enabled
) {
Icon(
modifier = Modifier.size(ButtonDefaults.IconSize),
imageVector = Icons.Default.Translate,
contentDescription = null
)
if (progress) {
CircularProgressIndicator(
modifier = Modifier.size(ButtonDefaults.IconSize),
strokeWidth = 2.dp,
color = LocalContentColor.current
)
} else {
Icon(
modifier = Modifier.size(ButtonDefaults.IconSize),
imageVector = Icons.Default.Translate,
contentDescription = null
)
}
Spacer(modifier = Modifier.size(ButtonDefaults.IconSpacing))
Text(stringResource(SharedRes.strings.translate))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ package dev.datlag.aniflow.ui.navigation.screen.medium.component
import androidx.compose.runtime.Composable

@Composable
expect fun TranslateButton(text: String, onTranslation: (String?) -> Unit)
expect fun TranslateButton(
text: String,
onTranslation: (String?) -> Unit,
)
10 changes: 5 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ firebase-android-crashlytics = "18.6.3"
flowredux = "1.2.1"
google-identity = "1.1.0"
haze = "0.7.0"
html-converter = "0.9.4"
html-converter = "0.9.5"
kache = "2.1.0"
kasechange = "1.4.1"
kmpalette = "3.1.0"
kodein = "7.21.2"
kolor = "1.4.4"
konfig = "0.15.1"
kotlin = "1.9.23"
ksp = "1.9.22-1.0.17"
ktor = "2.3.9"
ktorfit = "1.12.0"
ksp = "1.9.23-1.0.20"
ktor = "2.3.10"
ktorfit = "1.13.0"
moko-resources = "0.24.0-alpha-5"
multidex = "2.0.1"
napier = "2.7.1"
oidc = "0.9.1"
oidc = "0.9.2"
sekret = "2.0.0-alpha-04"
serialization = "1.6.3"
splashscreen = "1.0.1"
Expand Down

0 comments on commit 0c0757a

Please sign in to comment.