From 1e0f8c29b6d230ae3a520c6e59ffce19758cc0c3 Mon Sep 17 00:00:00 2001 From: Daniel Kao Date: Tue, 5 Nov 2024 01:44:44 +0800 Subject: [PATCH] feat: you can select a text, and start tts read from that sentence --- .../plateaukao/einkbro/activity/BrowserActivity.kt | 14 ++++++++++++++ .../einkbro/viewmodel/ActionModeMenuViewModel.kt | 10 ++++++++++ app/src/main/res/values-zh-rCN/strings.xml | 1 + app/src/main/res/values-zh-rTW/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 27 insertions(+) diff --git a/app/src/main/java/info/plateaukao/einkbro/activity/BrowserActivity.kt b/app/src/main/java/info/plateaukao/einkbro/activity/BrowserActivity.kt index 6ce22236..c023c684 100755 --- a/app/src/main/java/info/plateaukao/einkbro/activity/BrowserActivity.kt +++ b/app/src/main/java/info/plateaukao/einkbro/activity/BrowserActivity.kt @@ -62,6 +62,7 @@ import androidx.compose.ui.text.input.TextFieldValue import androidx.core.graphics.Insets import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import androidx.core.view.get import androidx.core.view.isVisible import androidx.fragment.app.FragmentActivity import androidx.lifecycle.lifecycleScope @@ -136,6 +137,7 @@ import info.plateaukao.einkbro.view.viewControllers.FabImageViewController import info.plateaukao.einkbro.view.viewControllers.OverviewDialogController import info.plateaukao.einkbro.view.viewControllers.TouchAreaViewController import info.plateaukao.einkbro.view.viewControllers.TwoPaneController +import info.plateaukao.einkbro.viewmodel.ActionModeMenuState import info.plateaukao.einkbro.viewmodel.ActionModeMenuState.DeeplTranslate import info.plateaukao.einkbro.viewmodel.ActionModeMenuState.GoogleTranslate import info.plateaukao.einkbro.viewmodel.ActionModeMenuState.Gpt @@ -556,6 +558,8 @@ open class BrowserActivity : FragmentActivity(), BrowserController { actionModeMenuViewModel.finish() } } + + is ActionModeMenuState.ReadFromHere -> readFromThisSentence() is Gpt -> { val gptAction = config.gptActionList[state.gptActionIndex] @@ -624,6 +628,16 @@ open class BrowserActivity : FragmentActivity(), BrowserController { } } + private fun readFromThisSentence() { + lifecycleScope.launch { + val selectedSentence = ebWebView.getSelectedText() + val fullText = ebWebView.getRawText() + // read from selected sentence to the end of the article + val startIndex = fullText.indexOf(selectedSentence) + ttsViewModel.readArticle(fullText.substring(startIndex)) + } + } + private suspend fun updateTranslationInput() { // need to handle where data is from: ebWebView or twoPaneController.getSecondWebView() with(translationViewModel) { diff --git a/app/src/main/java/info/plateaukao/einkbro/viewmodel/ActionModeMenuViewModel.kt b/app/src/main/java/info/plateaukao/einkbro/viewmodel/ActionModeMenuViewModel.kt index e0201ed3..edb3e307 100644 --- a/app/src/main/java/info/plateaukao/einkbro/viewmodel/ActionModeMenuViewModel.kt +++ b/app/src/main/java/info/plateaukao/einkbro/viewmodel/ActionModeMenuViewModel.kt @@ -7,6 +7,7 @@ import android.graphics.Point import android.view.ActionMode import androidx.compose.material.icons.Icons import androidx.compose.material.icons.outlined.EditNote +import androidx.compose.material.icons.outlined.RecordVoiceOver import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableStateOf import androidx.core.content.ContextCompat @@ -104,6 +105,14 @@ class ActionModeMenuViewModel : ViewModel(), KoinComponent { val menuInfos = resolveInfos.map { it.toMenuInfo(packageManager) }.toMutableList() + menuInfos.add( + 0, + MenuInfo( + context.getString(R.string.read_from_here), + imageVector = Icons.Outlined.RecordVoiceOver, + action = { _actionModeMenuState.value = ActionModeMenuState.ReadFromHere } + ) + ) if (configManager.imageApiKey.isNotEmpty()) { menuInfos.add( 0, @@ -249,6 +258,7 @@ sealed class ActionModeMenuState { data object DeeplTranslate : ActionModeMenuState() data object Papago : ActionModeMenuState() data object Naver : ActionModeMenuState() + data object ReadFromHere: ActionModeMenuState() class SplitSearch(val stringFormat: String) : ActionModeMenuState() class Tts(val text: String) : ActionModeMenuState() class HighlightText(val highlightStyle: HighlightStyle) : ActionModeMenuState() diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 02475272..d050da87 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -574,4 +574,5 @@ Bold Deepl by Paragraph Remove highlight? + Read from here diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index f71e8b1e..14660970 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -506,4 +506,5 @@ 粗體字 Deepl by Paragraph Remove highlight? + Read from here diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cc34ea1a..0aebba52 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -595,4 +595,5 @@ Bold Deepl by Paragraph Remove highlight? + From here