Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Sep 29, 2024
1 parent 58888a9 commit c175dc4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,12 @@ class TranslateRepository : KoinComponent {
sourceLanguage: String = "auto",
): String? {
if (authKey == null) {
authKey = getAuthKey()
try {
authKey = getAuthKey()
} catch (e: Exception) {
Log.d("TranslateRepository", "ppTranslate: $e")
return ""
}
}
val key = authKey?.toByteArray(Charsets.UTF_8) ?: return ""

Expand Down Expand Up @@ -493,6 +498,6 @@ data class Signature(val ts: Long, val msg: String)

data class ImageTranslateResult(
val imageId: String,
val renderedImage: String
val renderedImage: String,
)

5 changes: 4 additions & 1 deletion app/src/main/java/info/plateaukao/einkbro/unit/HelperUnit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,10 @@ object HelperUnit {
}

fun processedTextToChunks(text: String): MutableList<String> {
val processedText = text.replace("\\n", " ").replace("\\\"", "").replace("\\t", "").replace("\\", "")
val processedText = text.replace("\\n", " ")
.replace("\\\"", "")
.replace("\\t", "")
.replace("\\", "")
val sentences = processedText.split("(?<=\\.)(?!\\d)|(?<=。)|(?<=?)|(?<=\\?)".toRegex())
val chunks = sentences.fold(mutableListOf<String>()) { acc, sentence ->
if (acc.isEmpty() || (acc.last() + sentence).getWordCount() > 60) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ class TtsViewModel : ViewModel(), KoinComponent {
private val _showCurrentText = MutableStateFlow(config.ttsShowCurrentText)
val showCurrentText: StateFlow<Boolean> = _showCurrentText.asStateFlow()

private val _showTranslation = MutableStateFlow(config.ttsShowTextTranslation)
val showTranslation: StateFlow<Boolean> = _showTranslation.asStateFlow()

private val openaiRepository: OpenAiRepository by lazy { OpenAiRepository() }

private val translateRepository: TranslateRepository by lazy { TranslateRepository() }
Expand Down Expand Up @@ -127,7 +124,7 @@ class TtsViewModel : ViewModel(), KoinComponent {

private val translationSeparator = "\n---\n"
private fun insertTranslationText(text: String) {
if (_showTranslation.value) {
if (config.ttsShowTextTranslation) {
viewModelScope.launch {
val translatedText = translateRepository.gTranslateWithApi(text, config.translationLanguage.value)
_currentReadingContent.value = "$text$translationSeparator$translatedText"
Expand Down Expand Up @@ -252,7 +249,6 @@ class TtsViewModel : ViewModel(), KoinComponent {

fun toggleShowTranslation() {
config::ttsShowTextTranslation.toggle()
_showTranslation.value = config.ttsShowTextTranslation

insertTranslationText(_currentReadingContent.value)
}
Expand Down

0 comments on commit c175dc4

Please sign in to comment.