@dr33m/react-native-readium 5.0.0-rc.22 → 5.0.0-rc.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -276,28 +276,31 @@ abstract class BaseReaderFragment : Fragment() {
276
276
  private fun applyTTSDecoration(locator: Locator) {
277
277
  if (!isNavigatorReady) return
278
278
  val decorableNavigator = navigator as? DecorableNavigator ?: return
279
- // Call applyDecorations directlyit is a regular (non-suspend) function in
280
- // Readium 3.x. The test app (VisualReaderFragment) does the same: no launch {},
281
- // no channel routing. Wrapping in viewScope.launch defers the call to the next
282
- // dispatch cycle and was preventing the highlight from ever appearing.
283
- decorableNavigator.applyDecorations(
284
- listOf(
285
- Decoration(
286
- id = "tts",
287
- locator = locator,
288
- style = Decoration.Style.Highlight(
289
- tint = android.graphics.Color.argb(89, 0xF2, 0xCA, 0x50) // #f2ca50 @ 35%
279
+ // applyDecorations is suspend in Readium 3.x must be called from a coroutine.
280
+ // viewLifecycleOwner.lifecycleScope dispatches to Dispatchers.Main, matching
281
+ // the observeWhenStarted pattern used by the Readium test app.
282
+ viewLifecycleOwner.lifecycleScope.launch {
283
+ decorableNavigator.applyDecorations(
284
+ listOf(
285
+ Decoration(
286
+ id = "tts",
287
+ locator = locator,
288
+ style = Decoration.Style.Highlight(
289
+ tint = android.graphics.Color.argb(89, 0xF2, 0xCA, 0x50) // #f2ca50 @ 35%
290
+ )
290
291
  )
291
- )
292
- ),
293
- "tts"
294
- )
292
+ ),
293
+ "tts"
294
+ )
295
+ }
295
296
  }
296
297
 
297
298
  private fun clearTTSDecoration() {
298
299
  if (!isNavigatorReady) return
299
300
  val decorableNavigator = navigator as? DecorableNavigator ?: return
300
- decorableNavigator.applyDecorations(emptyList(), "tts")
301
+ viewLifecycleOwner.lifecycleScope.launch {
302
+ decorableNavigator.applyDecorations(emptyList(), "tts")
303
+ }
301
304
  }
302
305
 
303
306
  override fun onDestroyView() {
@@ -3,7 +3,9 @@ package com.reactnativereadium.reader
3
3
  import android.app.Application
4
4
  import kotlinx.coroutines.CoroutineScope
5
5
  import kotlinx.coroutines.Job
6
+ import kotlinx.coroutines.flow.distinctUntilChanged
6
7
  import kotlinx.coroutines.flow.launchIn
8
+ import kotlinx.coroutines.flow.map
7
9
  import kotlinx.coroutines.flow.onEach
8
10
  import kotlinx.coroutines.launch
9
11
  import org.readium.navigator.media.common.MediaNavigator
@@ -108,8 +110,14 @@ class TTSManager(
108
110
  }
109
111
  .launchIn(this)
110
112
 
111
- // Observe current locator
112
- ttsNavigator.currentLocator
113
+ // Observe utterance-level locator (sentence, not word).
114
+ // TtsNavigator.currentLocator returns tokenLocator ?: utteranceLocator — the
115
+ // word-level locator — which has incomplete CSS selectors and cannot be rendered
116
+ // by the EPUB decorator. utteranceLocator is the sentence-level locator and
117
+ // matches what the Readium test app (TtsViewModel.highlight) uses.
118
+ ttsNavigator.location
119
+ .map { it.utteranceLocator }
120
+ .distinctUntilChanged()
113
121
  .onEach { locator ->
114
122
  val text = locator.text.highlight ?: ""
115
123
  onUtterance(locator, text)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dr33m/react-native-readium",
3
- "version": "5.0.0-rc.22",
3
+ "version": "5.0.0-rc.23",
4
4
  "description": "A react-native wrapper for https://readium.org/",
5
5
  "main": "lib/src/index",
6
6
  "types": "lib/src/index.d.ts",