Skip to content

Commit

Permalink
More robustly manages readout commands
Browse files Browse the repository at this point in the history
  • Loading branch information
hufman committed Feb 10, 2024
1 parent 881a4b1 commit 652076a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ class ReadoutController(val name: String, val speechEvent: RHMIEvent.ActionEvent
}

fun loadLines(lines: List<String>) {
stop()
this.lines.value = lines
this.lineIndex.value = 0
nextLineIndex = 0
desiredState = ReadoutState.IDLE
}

fun play() {
Expand All @@ -140,11 +140,17 @@ class ReadoutController(val name: String, val speechEvent: RHMIEvent.ActionEvent

private fun readLine() {
val line = lines.value.getOrNull(lineIndex.value) ?: ""
val data = RHMIModel.RaListModel.RHMIListConcrete(2)
data.addRow(arrayOf(line, name))
Log.d(TAG, "Starting readout from $name: ${data[0][0]}")
speechList.setValue(data, 0, 1, 1)
speechEvent.triggerEvent()
if (line.isNotBlank()) {
val data = RHMIModel.RaListModel.RHMIListConcrete(2)
data.addRow(arrayOf(line, name))
Log.d(TAG, "Starting readout from $name: ${data[0][0]}")
try {
speechList.setValue(data, 0, 1, 1)
speechEvent.triggerEvent()
} catch (e: Exception) {
Log.e(TAG, "Error while reading: ", e)
}
}

// cue the next line to play
if (lineIndex.value < lines.value.size - 1) {
Expand All @@ -165,15 +171,21 @@ class ReadoutController(val name: String, val speechEvent: RHMIEvent.ActionEvent
commandEvent.triggerEvent()
}
fun stop() {
desiredState = ReadoutState.IDLE
_stop()
if (desiredState != ReadoutState.IDLE) {
desiredState = ReadoutState.IDLE
_stop()
}
}
private fun _stop() {
Log.d(TAG, "Cancelling $name readout")
val data = RHMIModel.RaListModel.RHMIListConcrete(2).apply {
addRow(arrayOf(ReadoutCommand.STOP.value, name))
}
commandList.setValue(data, 0, 1, 1)
commandEvent.triggerEvent()
try {
commandList.setValue(data, 0, 1, 1)
commandEvent.triggerEvent()
} catch (e: Exception) {
Log.e(TAG, "Error while stopping: ", e)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class ReadoutView(state: RHMIState.ToolbarState, val controller: ReadoutControll

globalCoroutineScope.launch {
model.article.collectLatest { article -> // new article was opened in ReadView
controller.stop()
val htmlContents = Utils.parseHtml(article?.article?.fullContent ?: "")
val contents = Utils.formatForReadout(htmlContents)
controller.loadLines(contents)
Expand Down

0 comments on commit 652076a

Please sign in to comment.