From 0c0757aee472377b00cc1c59328b3f52ee669423 Mon Sep 17 00:00:00 2001 From: DatLag Date: Tue, 16 Apr 2024 16:53:47 +0200 Subject: [PATCH] added progress indicator on translation --- .../component/TranslateButton.android.kt | 42 +++++++++++++------ .../medium/component/TranslateButton.kt | 5 ++- gradle/libs.versions.toml | 10 ++--- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/composeApp/src/androidMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/component/TranslateButton.android.kt b/composeApp/src/androidMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/component/TranslateButton.android.kt index 1fe0285..275342d 100644 --- a/composeApp/src/androidMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/component/TranslateButton.android.kt +++ b/composeApp/src/androidMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/component/TranslateButton.android.kt @@ -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 @@ -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") @@ -60,6 +61,7 @@ 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 = { @@ -67,19 +69,27 @@ actual fun TranslateButton(text: String, onTranslation: (String?) -> Unit) { 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) } @@ -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)) } diff --git a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/component/TranslateButton.kt b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/component/TranslateButton.kt index 1c32442..9d2ae52 100644 --- a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/component/TranslateButton.kt +++ b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/navigation/screen/medium/component/TranslateButton.kt @@ -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) \ No newline at end of file +expect fun TranslateButton( + text: String, + onTranslation: (String?) -> Unit, +) \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 69e7783..b2af8b8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -24,7 +24,7 @@ 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" @@ -32,13 +32,13 @@ 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"