Skip to content

Commit

Permalink
feat: add add article to read list
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Sep 21, 2024
1 parent 44d960e commit 7957f57
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,8 @@ open class BrowserActivity : FragmentActivity(), BrowserController {
externalSearchViewModel.setButtonVisibility(true)
}

ACTION_READ_ALOUD -> readArticle()

null -> {}
else -> addAlbum()
}
Expand Down Expand Up @@ -2542,7 +2544,7 @@ open class BrowserActivity : FragmentActivity(), BrowserController {

private fun readArticle() {
lifecycleScope.launch {
ttsViewModel.readText(ninjaWebView.getRawText())
ttsViewModel.readArticle(ninjaWebView.getRawText())
}
}

Expand Down Expand Up @@ -2711,5 +2713,6 @@ open class BrowserActivity : FragmentActivity(), BrowserController {

companion object {
private const val K_SHOULD_LOAD_TAB_STATE = "k_should_load_tab_state"
const val ACTION_READ_ALOUD = "action_read_aloud"
}
}
8 changes: 8 additions & 0 deletions app/src/main/java/info/plateaukao/einkbro/unit/IntentUnit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ object IntentUnit {
)
}

fun readCurrentArticle(activity: Activity) {
activity.startActivity(
Intent(activity, BrowserActivity::class.java).apply {
action = BrowserActivity.ACTION_READ_ALOUD
}
)
}

fun launchNewBrowser(activity: Activity, url: String) {
val intent = Intent(activity, ExtraBrowserActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Pause
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.runtime.Composable
Expand All @@ -33,6 +34,7 @@ import icu.xmc.edgettslib.entity.dummyVoiceItem
import info.plateaukao.einkbro.R
import info.plateaukao.einkbro.service.TtsManager
import info.plateaukao.einkbro.unit.IntentUnit
import info.plateaukao.einkbro.view.NinjaToast
import info.plateaukao.einkbro.view.compose.MyTheme
import info.plateaukao.einkbro.view.compose.SelectableText
import info.plateaukao.einkbro.view.dialog.TtsLanguageDialog
Expand Down Expand Up @@ -63,6 +65,7 @@ class TtsSettingDialogFragment : ComposeDialogFragment() {
okAction = { dismiss() },
gotoSettingAction = { IntentUnit.gotoSystemTtsSettings(requireActivity()) },
pauseOrResumeAction = { ttsViewModel.pauseOrResume(); dismiss() },
addToReadListAction = this::readCurrentArticle,
showLocaleDialog = {
TtsLanguageDialog(requireContext()).show(ttsManager.getAvailableLanguages())
},
Expand All @@ -79,6 +82,12 @@ class TtsSettingDialogFragment : ComposeDialogFragment() {
}
}
}

private fun readCurrentArticle() {
IntentUnit.readCurrentArticle(requireActivity())
NinjaToast.show(requireContext(), R.string.added_to_read_list)
dismiss()
}
}

private val speedRateValueList = listOf(
Expand Down Expand Up @@ -107,6 +116,7 @@ private fun MainTtsSettingDialog(
okAction: () -> Unit,
onVoiceSelected: (VoiceItem) -> Unit,
pauseOrResumeAction : () -> Unit,
addToReadListAction: () -> Unit,
showLocaleDialog: () -> Unit,
showTtsTypeDialog: () -> Unit,
showEttsVoiceDialog: () -> Unit,
Expand Down Expand Up @@ -216,7 +226,8 @@ private fun MainTtsSettingDialog(
showSystemSetting = selectedType == TtsType.SYSTEM,
gotoSettingAction = gotoSettingAction,
pauseOrResumeAction = pauseOrResumeAction,
okAction = okAction,
addToReadListAction = addToReadListAction,
dismissAction = okAction,
)
}
}
Expand All @@ -226,8 +237,9 @@ fun TtsDialogButtonBar(
isPlaying: Boolean,
showSystemSetting: Boolean,
pauseOrResumeAction: () -> Unit,
addToReadListAction: () -> Unit,
gotoSettingAction: () -> Unit,
okAction: () -> Unit,
dismissAction: () -> Unit,
) {
Column {
HorizontalSeparator()
Expand All @@ -249,23 +261,29 @@ fun TtsDialogButtonBar(
)
}
} else {
VerticalSeparator()
IconButton(
onClick = {
pauseOrResumeAction()
},
modifier = Modifier.wrapContentWidth()
) {
Icon(
if (isPlaying) Icons.Default.Pause else Icons.Default.PlayArrow,
"pause or resume"
)
if (isPlaying) {
VerticalSeparator()
IconButton(
onClick = addToReadListAction,
modifier = Modifier.wrapContentWidth()
) {
Icon( Icons.Default.Add, "Add to read list")
}
IconButton(
onClick = pauseOrResumeAction,
modifier = Modifier.wrapContentWidth()
) {
Icon(
if (isPlaying) Icons.Default.Pause else Icons.Default.PlayArrow,
"pause or resume"
)
}
}
}
VerticalSeparator()
TextButton(
modifier = Modifier.wrapContentWidth(),
onClick = okAction
onClick = dismissAction
) {
Text(
stringResource(id = R.string.close),
Expand Down Expand Up @@ -294,7 +312,8 @@ fun PreviewMainTtsDialog() {
showLocaleDialog = {},
showTtsTypeDialog = {},
showEttsVoiceDialog = {},
selectedEttsVoice = dummyVoiceItem
selectedEttsVoice = dummyVoiceItem,
addToReadListAction = {}
)
}
}
61 changes: 32 additions & 29 deletions app/src/main/java/info/plateaukao/einkbro/viewmodel/TtsViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,24 @@ class TtsViewModel : ViewModel(), KoinComponent {
private val type: TtsType
get() = if (useOpenAiTts()) TtsType.GPT else config.ttsType

fun readText(text: String) {
if (isReading()) {
stop()
return
}
private val articlesToBeRead: MutableList<String> = mutableListOf()

fun readArticle(text: String) {
articlesToBeRead.add(text)
if (isReading()) { return }

viewModelScope.launch {
while (articlesToBeRead.isNotEmpty()) {
val article = articlesToBeRead.removeAt(0)

when (type) {
TtsType.ETTS,
TtsType.GPT,
-> readByEngine(type, text)
when (type) {
TtsType.ETTS,
TtsType.GPT,
-> readByEngine(type, article)

TtsType.SYSTEM -> readBySystemTts(text)
TtsType.SYSTEM -> readBySystemTts(article)
}
}
}

// if (Build.MODEL.startsWith("Pixel 8")) {
Expand All @@ -68,18 +74,17 @@ class TtsViewModel : ViewModel(), KoinComponent {
// }
}

private fun readBySystemTts(text: String) {
private suspend fun readBySystemTts(text: String) {
_speakingState.value = true
ttsManager.readText(text)
viewModelScope.launch {
while (ttsManager.isSpeaking()) {
delay(2000)
}
_speakingState.value = false

while (ttsManager.isSpeaking()) {
delay(2000)
}
_speakingState.value = false
}

private fun readByEngine(ttsType: TtsType, text: String) {
private suspend fun readByEngine(ttsType: TtsType, text: String) {
_speakingState.value = true
audioFileChannel = Channel(1)
val processedText = text.replace("\\n", " ").replace("\\\"", "").replace("\\t", "")
Expand Down Expand Up @@ -119,19 +124,17 @@ class TtsViewModel : ViewModel(), KoinComponent {
audioFileChannel?.close()
}

viewModelScope.launch(Dispatchers.IO) {
var index = 0
for (file in audioFileChannel!!) {
Log.d("TtsViewModel", "play audio $index")
playAudioFile(file)
//delay(100)
index++
if (audioFileChannel?.isClosedForSend == true && audioFileChannel?.isEmpty == true
) break
}
_speakingState.value = false
audioFileChannel = null
var index = 0
for (file in audioFileChannel!!) {
Log.d("TtsViewModel", "play audio $index")
playAudioFile(file)
//delay(100)
index++
if (audioFileChannel?.isClosedForSend == true && audioFileChannel?.isEmpty == true
) break
}
_speakingState.value = false
audioFileChannel = null
}

private val fetchSemaphore = Semaphore(3)
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 @@ -558,4 +558,5 @@
<string name="setting_tts_voice">Voice</string>
<string name="system_settings">System Settings</string>
<string name="other_voices">Other Voices...</string>
<string name="added_to_read_list">Added to read list</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 @@ -490,4 +490,5 @@
<string name="system_settings">系統設定</string>
<string name="other_voices">其他語音...</string>
<string name="close">關閉</string>
<string name="added_to_read_list">加入朗讀清單</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 @@ -577,4 +577,5 @@
<string name="system_settings">System Settings</string>
<string name="other_voices">Other Voices...</string>
<string name="close">Close</string>
<string name="added_to_read_list">Added to read list</string>
</resources>

0 comments on commit 7957f57

Please sign in to comment.