forked from OuterTune/OuterTune
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
app/src/main/java/com/dd3boh/outertune/ui/component/TranslationDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.dd3boh.outertune.ui.component | ||
|
||
import androidx.compose.foundation.layout.* | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.rounded.Translate | ||
import androidx.compose.material3.* | ||
import androidx.compose.runtime.* | ||
import androidx.compose.runtime.saveable.rememberSaveable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import com.dd3boh.outertune.R | ||
import com.dd3boh.outertune.db.entities.LyricsEntity | ||
import com.dd3boh.outertune.models.MediaMetadata | ||
import com.dd3boh.outertune.viewmodels.LyricsMenuViewModel | ||
import kotlinx.coroutines.launch | ||
import me.bush.translator.Language | ||
import me.bush.translator.Translator | ||
|
||
@Composable | ||
fun TranslationDialog( | ||
onDismiss: () -> Unit, | ||
lyricsProvider: () -> LyricsEntity?, | ||
mediaMetadataProvider: () -> MediaMetadata, | ||
viewModel: LyricsMenuViewModel = hiltViewModel(), | ||
) { | ||
val scope = rememberCoroutineScope() | ||
var selectedLanguage by rememberSaveable { | ||
mutableStateOf(Language.ENGLISH) | ||
} | ||
var translatedLyrics by rememberSaveable { | ||
mutableStateOf("") | ||
} | ||
|
||
DefaultDialog( | ||
onDismiss = onDismiss, | ||
icon = { Icon(imageVector = Icons.Rounded.Translate, contentDescription = null) }, | ||
title = { Text(stringResource(R.string.translate_lyrics)) }, // Add a new string resource | ||
buttons = { | ||
TextButton( | ||
onClick = onDismiss | ||
) { | ||
Text(stringResource(android.R.string.cancel)) | ||
} | ||
|
||
Spacer(Modifier.width(8.dp)) | ||
|
||
TextButton( | ||
onClick = { | ||
scope.launch { | ||
translateLyrics(lyricsProvider()?.lyrics.orEmpty(), selectedLanguage) { result -> | ||
translatedLyrics = result | ||
} | ||
} | ||
} | ||
) { | ||
Text(stringResource(R.string.translate)) // Add a new string resource | ||
} | ||
} | ||
) { | ||
// Add language selection and display translated lyrics here | ||
} | ||
} | ||
|
||
private suspend fun translateLyrics( | ||
text: String, | ||
destinationLanguage: Language, | ||
onResult: (String) -> Unit | ||
) { | ||
val translator = Translator() | ||
try { | ||
val translation = translator.translate(text, destinationLanguage) | ||
onResult(translation.translatedText) | ||
} catch (e: Exception) { | ||
onResult("Translation Error") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters