Skip to content

Commit

Permalink
feat: you can select a text, and start tts read from that sentence
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Nov 4, 2024
1 parent 7e95976 commit 1e0f8c2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -556,6 +558,8 @@ open class BrowserActivity : FragmentActivity(), BrowserController {
actionModeMenuViewModel.finish()
}
}

is ActionModeMenuState.ReadFromHere -> readFromThisSentence()

is Gpt -> {
val gptAction = config.gptActionList[state.gptActionIndex]
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -574,4 +574,5 @@
<string name="bold">Bold</string>
<string name="deepl_translate_by_paragraph">Deepl by Paragraph</string>
<string name="dialog_message_remove_highlight">Remove highlight?</string>
<string name="read_from_here">Read from here</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -506,4 +506,5 @@
<string name="bold">粗體字</string>
<string name="deepl_translate_by_paragraph">Deepl by Paragraph</string>
<string name="dialog_message_remove_highlight">Remove highlight?</string>
<string name="read_from_here">Read from here</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -595,4 +595,5 @@
<string name="bold">Bold</string>
<string name="deepl_translate_by_paragraph">Deepl by Paragraph</string>
<string name="dialog_message_remove_highlight">Remove highlight?</string>
<string name="read_from_here">From here</string>
</resources>

0 comments on commit 1e0f8c2

Please sign in to comment.