Skip to content

Commit 465cd43

Browse files
committed
refactor: SystemTTS ListManager
- Update list data with tryEmit for better performance - Update `tagData` field of `SpeechRule` when updating SystemTTS - Update `target` field of `SpeechRule` and reset tag when clearing tag data - Modify data flow in update function for better stability
1 parent 0b3726e commit 465cd43

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

app/src/main/java/com/github/jing332/tts_server_android/compose/systts/list/ListManagerScreen.kt

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,15 @@ internal fun ListManagerScreen(
120120
tagData = config.speechRule.tagData.toString(),
121121
onDismissRequest = { showTagClearDialog = null },
122122
onConfirm = {
123-
config.speechRule.target = SpeechTarget.ALL
124-
config.speechRule.resetTag()
125-
dbm.systemTtsV2.update(systts)
123+
dbm.systemTtsV2.update(
124+
systts.copy(
125+
config = config.copy(
126+
speechRule = config.speechRule.copy(
127+
target = SpeechTarget.ALL,
128+
).apply { resetTag() },
129+
)
130+
)
131+
)
126132
if (systts.isEnabled) SystemTtsService.notifyUpdateConfig()
127133
showTagClearDialog = null
128134
}
@@ -135,9 +141,9 @@ internal fun ListManagerScreen(
135141
context.longToast(R.string.systts_drag_tip_msg)
136142
}
137143

138-
val model = systts.copy()
139-
val config = model.config as TtsConfigurationDTO
144+
val config = systts.config as TtsConfigurationDTO
140145
if (config.speechRule.target == SpeechTarget.BGM) return
146+
val ruleData = config.speechRule.copy()
141147

142148
if (config.speechRule.target == SpeechTarget.TAG) dbm.speechRuleDao.getByRuleId(
143149
config.speechRule.tagRuleId
@@ -148,34 +154,34 @@ internal fun ListManagerScreen(
148154
val nextIndex = (idx + 1)
149155
val newTag = keys.getOrNull(nextIndex)
150156
if (newTag == null) {
151-
if (config.speechRule.isTagDataEmpty()) {
152-
config.speechRule.target = SpeechTarget.ALL
153-
config.speechRule.resetTag()
157+
if (ruleData.isTagDataEmpty()) {
158+
ruleData.target = SpeechTarget.ALL
159+
ruleData.resetTag()
154160
} else {
155-
showTagClearDialog = model
161+
showTagClearDialog = systts
156162
return
157163
}
158164
} else {
159-
config.speechRule.tag = newTag
165+
ruleData.tag = newTag
160166
runCatching {
161-
config.speechRule.tagName =
162-
SpeechRuleEngine.getTagName(context, speechRule, info = config.speechRule)
167+
ruleData.tagName =
168+
SpeechRuleEngine.getTagName(context, speechRule, info = ruleData)
163169
}.onFailure {
164-
config.speechRule.tagName = ""
170+
ruleData.tagName = ""
165171
context.displayErrorDialog(it)
166172
}
167173

168174
}
169175
}
170176
else {
171-
dbm.speechRuleDao.getByRuleId(config.speechRule.tagRuleId)?.let {
172-
config.speechRule.target = SpeechTarget.TAG
173-
config.speechRule.tag = it.tags.keys.first()
177+
dbm.speechRuleDao.getByRuleId(ruleData.tagRuleId)?.let {
178+
ruleData.target = SpeechTarget.TAG
179+
ruleData.tag = it.tags.keys.first()
174180
}
175181
}
176182

177-
dbm.systemTtsV2.update(model)
178-
if (model.isEnabled) SystemTtsService.notifyUpdateConfig()
183+
dbm.systemTtsV2.update(systts.copy(config = systts.ttsConfig.copy(speechRule = ruleData)))
184+
if (systts.isEnabled) SystemTtsService.notifyUpdateConfig()
179185
}
180186

181187
var deleteTts by remember { mutableStateOf<SystemTtsV2?>(null) }

app/src/main/java/com/github/jing332/tts_server_android/compose/systts/list/ListManagerViewModel.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,16 @@ import android.content.Context
44
import android.util.Log
55
import androidx.lifecycle.ViewModel
66
import androidx.lifecycle.viewModelScope
7-
import com.github.jing332.common.utils.FileUtils.readAllText
87
import com.github.jing332.database.dbm
98
import com.github.jing332.database.entities.AbstractListGroup.Companion.DEFAULT_GROUP_ID
109
import com.github.jing332.database.entities.systts.GroupWithSystemTts
1110
import com.github.jing332.database.entities.systts.SystemTtsV2
1211
import com.github.jing332.database.entities.systts.TtsConfigurationDTO
1312
import com.github.jing332.tts_server_android.R
1413
import com.github.jing332.tts_server_android.conf.SystemTtsConfig
15-
import com.github.jing332.tts_server_android.constant.AppConst
1614
import kotlinx.coroutines.Dispatchers
1715
import kotlinx.coroutines.flow.MutableStateFlow
1816
import kotlinx.coroutines.flow.StateFlow
19-
import kotlinx.coroutines.flow.collectLatest
2017
import kotlinx.coroutines.flow.conflate
2118
import kotlinx.coroutines.launch
2219
import org.burnoutcrew.reorderable.ItemPosition
@@ -36,16 +33,16 @@ class ListManagerViewModel : ViewModel() {
3633
init {
3734
viewModelScope.launch(Dispatchers.IO) {
3835
dbm.systemTtsV2.updateAllOrder()
39-
dbm.systemTtsV2.flowAllGroupWithTts().conflate().collectLatest {
36+
dbm.systemTtsV2.flowAllGroupWithTts().conflate().collect {
4037
Log.d(TAG, "update list: ${it.size}")
41-
_list.value = it
38+
_list.tryEmit(it)
4239
}
4340
}
4441
}
4542

4643
fun updateTtsEnabled(
4744
item: SystemTtsV2,
48-
enabled: Boolean
45+
enabled: Boolean,
4946
) {
5047
if (!SystemTtsConfig.isVoiceMultipleEnabled.value && enabled) {
5148
val itemConfig = (item.config as? TtsConfigurationDTO)
@@ -70,7 +67,7 @@ class ListManagerViewModel : ViewModel() {
7067

7168
fun updateGroupEnable(
7269
item: GroupWithSystemTts,
73-
enabled: Boolean
70+
enabled: Boolean,
7471
) {
7572
if (!SystemTtsConfig.isGroupMultipleEnabled.value && enabled) {
7673
list.value.forEach {

0 commit comments

Comments
 (0)