livevoice-sdk-react-native 2.0.0 → 2.0.1

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.
Files changed (97) hide show
  1. package/README.md +119 -7
  2. package/android/build.gradle +14 -3
  3. package/android/src/main/java/io/livevoice/react/LiveVoiceModule.kt +89 -123
  4. package/android/src/main/java/io/livevoice/react/__SDK.kt +16 -2
  5. package/ios/LiveVoiceSdkReactNative.mm +110 -37
  6. package/ios/__SDK.h +16 -2
  7. package/lib/module/components/AudioOutputSwitchView.js +13 -13
  8. package/lib/module/components/AudioOutputSwitchView.js.map +1 -1
  9. package/lib/module/components/BrandingView.js +13 -11
  10. package/lib/module/components/BrandingView.js.map +1 -1
  11. package/lib/module/components/ChannelView.js +14 -11
  12. package/lib/module/components/ChannelView.js.map +1 -1
  13. package/lib/module/components/ErrorView.js +12 -10
  14. package/lib/module/components/ErrorView.js.map +1 -1
  15. package/lib/module/components/LiveVoiceView.js +141 -72
  16. package/lib/module/components/LiveVoiceView.js.map +1 -1
  17. package/lib/module/components/PlayButtonView.js +12 -10
  18. package/lib/module/components/PlayButtonView.js.map +1 -1
  19. package/lib/module/components/ReconnectingView.js +16 -13
  20. package/lib/module/components/ReconnectingView.js.map +1 -1
  21. package/lib/module/components/WarningView.js +12 -10
  22. package/lib/module/components/WarningView.js.map +1 -1
  23. package/lib/module/components/index.js +12 -10
  24. package/lib/module/components/index.js.map +1 -1
  25. package/lib/module/index.js +14 -220
  26. package/lib/module/index.js.map +1 -1
  27. package/lib/module/sdk/LiveVoiceSdk.js +371 -0
  28. package/lib/module/sdk/LiveVoiceSdk.js.map +1 -0
  29. package/lib/module/sdk/LiveVoiceState.js +66 -0
  30. package/lib/module/sdk/LiveVoiceState.js.map +1 -0
  31. package/lib/module/specs/NativeLiveVoiceSdk.js +12 -10
  32. package/lib/module/specs/NativeLiveVoiceSdk.js.map +1 -1
  33. package/lib/module/util/Colors.js +12 -10
  34. package/lib/module/util/Colors.js.map +1 -1
  35. package/lib/module/util/ForegroundServiceConfig.js +21 -11
  36. package/lib/module/util/ForegroundServiceConfig.js.map +1 -1
  37. package/lib/module/util/PlayingState.js +12 -10
  38. package/lib/module/util/PlayingState.js.map +1 -1
  39. package/lib/module/util/Translations.js +12 -10
  40. package/lib/module/util/Translations.js.map +1 -1
  41. package/lib/module/util/logger.js +76 -0
  42. package/lib/module/util/logger.js.map +1 -0
  43. package/lib/typescript/src/components/AudioOutputSwitchView.d.ts.map +1 -1
  44. package/lib/typescript/src/components/BrandingView.d.ts.map +1 -1
  45. package/lib/typescript/src/components/ChannelView.d.ts.map +1 -1
  46. package/lib/typescript/src/components/ErrorView.d.ts.map +1 -1
  47. package/lib/typescript/src/components/LiveVoiceView.d.ts +22 -0
  48. package/lib/typescript/src/components/LiveVoiceView.d.ts.map +1 -1
  49. package/lib/typescript/src/components/PlayButtonView.d.ts.map +1 -1
  50. package/lib/typescript/src/components/ReconnectingView.d.ts.map +1 -1
  51. package/lib/typescript/src/components/WarningView.d.ts.map +1 -1
  52. package/lib/typescript/src/components/index.d.ts.map +1 -1
  53. package/lib/typescript/src/index.d.ts +6 -144
  54. package/lib/typescript/src/index.d.ts.map +1 -1
  55. package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts +210 -0
  56. package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts.map +1 -0
  57. package/lib/typescript/src/sdk/LiveVoiceState.d.ts +12 -0
  58. package/lib/typescript/src/sdk/LiveVoiceState.d.ts.map +1 -0
  59. package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts +3 -0
  60. package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts.map +1 -1
  61. package/lib/typescript/src/util/Colors.d.ts.map +1 -1
  62. package/lib/typescript/src/util/ForegroundServiceConfig.d.ts +7 -0
  63. package/lib/typescript/src/util/ForegroundServiceConfig.d.ts.map +1 -1
  64. package/lib/typescript/src/util/PlayingState.d.ts.map +1 -1
  65. package/lib/typescript/src/util/Translations.d.ts.map +1 -1
  66. package/lib/typescript/src/util/UIState.d.ts +22 -2
  67. package/lib/typescript/src/util/UIState.d.ts.map +1 -1
  68. package/lib/typescript/src/util/logger.d.ts +23 -0
  69. package/lib/typescript/src/util/logger.d.ts.map +1 -0
  70. package/livevoice-sdk-react-native.podspec +14 -14
  71. package/package.json +16 -7
  72. package/src/components/AudioOutputSwitchView.tsx +13 -13
  73. package/src/components/BrandingView.tsx +13 -11
  74. package/src/components/ChannelView.tsx +14 -11
  75. package/src/components/ErrorView.tsx +12 -10
  76. package/src/components/LiveVoiceView.tsx +183 -92
  77. package/src/components/PlayButtonView.tsx +12 -10
  78. package/src/components/ReconnectingView.tsx +20 -13
  79. package/src/components/WarningView.tsx +12 -10
  80. package/src/components/index.tsx +12 -10
  81. package/src/index.tsx +19 -252
  82. package/src/sdk/LiveVoiceSdk.ts +408 -0
  83. package/src/sdk/LiveVoiceState.ts +89 -0
  84. package/src/specs/NativeLiveVoiceSdk.ts +16 -10
  85. package/src/util/Colors.ts +12 -10
  86. package/src/util/ForegroundServiceConfig.tsx +21 -11
  87. package/src/util/PlayingState.ts +12 -10
  88. package/src/util/Translations.ts +12 -10
  89. package/src/util/UIState.ts +30 -13
  90. package/src/util/logger.ts +92 -0
  91. package/android/app/build/generated/source/codegen/java/com/facebook/fbreact/specs/NativeLiveVoiceSdkSpec.java +0 -186
  92. package/android/app/build/generated/source/codegen/jni/CMakeLists.txt +0 -28
  93. package/android/app/build/generated/source/codegen/jni/RNLiveVoiceSpec-generated.cpp +0 -235
  94. package/android/app/build/generated/source/codegen/jni/RNLiveVoiceSpec.h +0 -31
  95. package/android/app/build/generated/source/codegen/jni/react/renderer/components/RNLiveVoiceSpec/RNLiveVoiceSpecJSI.h +0 -341
  96. package/android/src/main/java/io/livevoice/react/Mappings.kt +0 -85
  97. package/android/src/main/java/io/livevoice/react/NativeModels.kt +0 -79
package/README.md CHANGED
@@ -1,13 +1,125 @@
1
- # livevoice-sdk-react-native
1
+ # LiveVoice SDK for React Native
2
2
 
3
3
  The React Native SDK for integrating the LiveVoice service into your app.
4
4
 
5
5
  ## Overview
6
6
 
7
- The LiveVoice SDK enables your app to browse and listen to your configured LiveVoice channels as
8
- well as present a channel's live subtitles if available.
7
+ The LiveVoice SDK enables your app to browse and listen to your configured LiveVoice channels,
8
+ as well as present a channel's live subtitles where available.
9
9
 
10
- Using the LiveVoice SDK requires an account with SDK access enabled. Depending on your subscription,
11
- the SDK allows for hiding the LiveVoice branding or using a fully customized presentation. For more
12
- information contact sdk-support@livevoice.io . Once SDK access has been granted, you can see your
13
- personal API-key at the bottom of your account page at https://livevoice.io/en/account .
10
+ Using the LiveVoice SDK requires an account with SDK access enabled. Depending on your
11
+ subscription, the SDK allows for hiding the LiveVoice branding or building a fully customized
12
+ presentation. For more information contact sdk-support@livevoice.io. Once SDK access has been
13
+ granted, you can find your personal API key in the API key section of your account page at
14
+ https://livevoice.io/en/account/api.
15
+
16
+ ## Features
17
+
18
+ - Browse and play the channels of a LiveVoice event
19
+ - Live subtitles where the channel provides them
20
+ - A drop-in `LiveVoiceView`, or a fully custom presentation with your own channel cells
21
+ - Audio output selection (speaker, receiver, headphones)
22
+ - Optional branding control and custom UI (subscription-dependent)
23
+
24
+ ## Requirements
25
+
26
+ - React Native 0.80 or newer
27
+ - iOS 15.1 or newer
28
+ - Android 7.0 (API level 24) or newer
29
+
30
+ ## Installation
31
+
32
+ ```sh
33
+ npm install livevoice-sdk-react-native
34
+ # or
35
+ yarn add livevoice-sdk-react-native
36
+ ```
37
+
38
+ On iOS, install the native pods from your app's `ios/` directory:
39
+
40
+ ```sh
41
+ pod install
42
+ ```
43
+
44
+ ## Usage
45
+
46
+ ```tsx
47
+ import {
48
+ initialize,
49
+ joinEvent,
50
+ leaveEvent,
51
+ LiveVoiceView,
52
+ } from 'livevoice-sdk-react-native';
53
+
54
+ // Initialize the SDK once, early in your app's lifecycle.
55
+ initialize();
56
+
57
+ // Join an event to load its channels. Arguments: (joinCode, password, apiKey).
58
+ joinEvent('YOUR_JOIN_CODE', null /* no password */, 'YOUR_API_KEY');
59
+
60
+ // Render the drop-in UI anywhere in your component tree.
61
+ function Channels() {
62
+ return <LiveVoiceView />;
63
+ }
64
+
65
+ // Leave the event when you no longer display any channels.
66
+ leaveEvent();
67
+ ```
68
+
69
+ `joinEvent(joinCode, password, apiKey)` takes an optional listener `password` (pass `null` when
70
+ the event has none). The join code and password come from the event page of the admin web
71
+ interface; the API key is on your account page.
72
+
73
+ ### Custom channel cells
74
+
75
+ To render your own channel rows instead of the built-in ones, pass `renderChannel` to
76
+ `LiveVoiceView`.
77
+
78
+ ```tsx
79
+ import { LiveVoiceView, PlayingState } from 'livevoice-sdk-react-native';
80
+
81
+ // Custom cells require an API key that permits
82
+ // custom UI. If you provide renderChannel but the
83
+ // joined key isn't allowed, the SDK shows the
84
+ // invalid-api-key error instead of your cells.
85
+ <LiveVoiceView
86
+ renderChannel={(channel, { isLastInList, onAudioPress }) => (
87
+ <MyCustomCell
88
+ name={channel.name}
89
+ online={channel.isOnline}
90
+ playing={channel.playingState === PlayingState.PLAYING}
91
+ isLast={isLastInList}
92
+ onPlayPress={onAudioPress}
93
+ />
94
+ )}
95
+ />;
96
+ ```
97
+
98
+ ### Android background playback
99
+
100
+ On Android, keeping playback alive in the background requires a foreground service. It is
101
+ disabled by default — enable it by passing a configuration to `initialize`:
102
+
103
+ ```tsx
104
+ import {
105
+ initialize,
106
+ serviceEnabledWithDefaultMessage,
107
+ serviceEnabledWithMessage,
108
+ } from 'livevoice-sdk-react-native';
109
+
110
+ // Enable the foreground service with the SDK's default notification message.
111
+ initialize(serviceEnabledWithDefaultMessage);
112
+
113
+ // Or provide your own text; %s becomes the playing channel's name.
114
+ initialize(serviceEnabledWithMessage('Channel %s is playing'));
115
+ ```
116
+
117
+ ## Examples and support
118
+
119
+ The [example apps](https://github.com/LiveVoiceOfficial/livevoice-sdk-examples) are the best
120
+ place to start — each is a working integration you can run and read. Find your API key on your
121
+ [account page](https://livevoice.io/en/account/api). Questions: sdk-support@livevoice.io.
122
+
123
+ ## License
124
+
125
+ MIT
@@ -1,3 +1,16 @@
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the LiveVoice SDK project
4
+ //
5
+ // Copyright (c) LiveVoice GmbH
6
+ // Licensed under the MIT license
7
+ //
8
+ // See LICENSE for license information
9
+ //
10
+ // SPDX-License-Identifier: MIT
11
+ //
12
+ //===----------------------------------------------------------------------===//
13
+
1
14
  buildscript {
2
15
  ext.LiveVoiceSdkReactNative = [
3
16
  kotlinVersion: "2.0.21",
@@ -76,7 +89,6 @@ android {
76
89
  project.repositories {
77
90
  google()
78
91
  mavenCentral()
79
- maven { url "https://jitpack.io" }
80
92
  maven { url "https://sdks.livevoice.io/android" }
81
93
  }
82
94
  }
@@ -84,9 +96,8 @@ android {
84
96
 
85
97
  dependencies {
86
98
  implementation "com.facebook.react:react-android"
87
- implementation("io.livevoice.sdk:android:2.0.0") {
99
+ implementation("io.livevoice.sdk:android:2.0.2") {
88
100
  exclude group: 'org.jspecify', module: 'jspecify'
89
101
  }
90
- implementation("com.jakewharton.timber:timber:5.0.1")
91
102
  implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0")
92
103
  }
@@ -13,7 +13,7 @@
13
13
 
14
14
  package io.livevoice.react
15
15
 
16
- import android.annotation.SuppressLint
16
+ import android.util.Log
17
17
  import androidx.activity.ComponentActivity
18
18
  import androidx.appcompat.app.AppCompatActivity
19
19
  import androidx.lifecycle.lifecycleScope
@@ -22,37 +22,19 @@ import com.facebook.react.module.annotations.ReactModule
22
22
  import com.facebook.react.modules.systeminfo.ReactNativeVersion
23
23
  import io.livevoice.sdk.android.publicApi.core.LiveVoice
24
24
  import io.livevoice.sdk.android.publicApi.core.model.ForegroundServiceConfig
25
- import io.livevoice.sdk.android.publicApi.core.model.LiveVoiceOutput
26
25
  import io.livevoice.sdk.android.publicApi.core.model.channel.LiveVoicePlayingState.Connecting
27
26
  import io.livevoice.sdk.android.publicApi.core.model.channel.LiveVoicePlayingState.Playing
28
27
  import io.livevoice.sdk.android.publicApi.core.model.NotificationText
29
28
  import io.livevoice.sdk.android.publicApi.core.model.SdkPlatform
30
- import io.livevoice.sdk.android.publicApi.core.model.subtitle.SdkSubtitleLanguage
31
- import io.livevoice.sdk.android.publicApi.core.model.subtitle.SdkSubtitleState
32
- import io.livevoice.sdk.android.publicApi.core.model.SdkUiState
33
29
  import io.livevoice.sdk.android.publicApi.core.model.SdkUiState.Ready
34
- import io.livevoice.sdk.android.publicApi.core.model.exceptions.SdkUninitializedException
35
- import kotlinx.serialization.encodeToString
36
- import kotlinx.coroutines.flow.collectLatest
30
+ import io.livevoice.sdk.android.publicApi.core.model.exceptions.SdkNotConnectedException
31
+ import io.livevoice.sdk.android.publicApi.serialization.LiveVoiceSerialization
37
32
  import kotlinx.coroutines.launch
38
- import kotlinx.serialization.ExperimentalSerializationApi
39
- import kotlinx.serialization.json.ClassDiscriminatorMode
40
- import kotlinx.serialization.json.Json
41
- import timber.log.Timber
42
33
 
43
- @SuppressLint("LogNotTimber")
44
34
  @ReactModule(name = LiveVoiceModule.NAME)
45
35
  class LiveVoiceModule(reactContext: ReactApplicationContext) :
46
36
  NativeLiveVoiceSdkSpec(reactContext) {
47
37
 
48
- private val timber = Timber.tag("[SDK]")
49
-
50
- @OptIn(ExperimentalSerializationApi::class)
51
- private val json = Json {
52
- classDiscriminator = "type"
53
- classDiscriminatorMode = ClassDiscriminatorMode.ALL_JSON_OBJECTS
54
- }
55
-
56
38
  override fun getName(): String {
57
39
  return NAME
58
40
  }
@@ -60,15 +42,14 @@ class LiveVoiceModule(reactContext: ReactApplicationContext) :
60
42
  override fun initialize(androidServiceConfig: String?) {
61
43
  val parsedServiceConfig = when (androidServiceConfig) {
62
44
  null -> ForegroundServiceConfig.Disabled
63
- "DEFAULT_MESSAGE" -> ForegroundServiceConfig.enabledWithDefaultMessage
45
+ DEFAULT_MESSAGE_SENTINEL -> ForegroundServiceConfig.enabledWithDefaultMessage
64
46
  else -> ForegroundServiceConfig.Enabled(NotificationText.RawValue(androidServiceConfig))
65
47
  }
66
48
 
67
- Timber.plant(Timber.DebugTree())
68
49
  try {
69
50
  val activity: AppCompatActivity =
70
51
  currentActivity as? AppCompatActivity ?: run {
71
- timber.e("initialize failed: currentActivity null or not an AppCompatActivity")
52
+ logError(EVENT_BRIDGE_ERROR, "action" to "initialize", "reason" to "missingActivity")
72
53
  return
73
54
  }
74
55
 
@@ -85,139 +66,91 @@ class LiveVoiceModule(reactContext: ReactApplicationContext) :
85
66
  )
86
67
  )
87
68
 
88
- timber.d("finished initializing")
69
+ logInfo(EVENT_INIT, "platformVersion" to reactVersion)
89
70
 
90
- //observe ui-state changes
91
- launchUiStateObservation(activity)
92
- launchOutputModeObservation(activity)
93
- launchAvailableSubtitleObservation(activity)
71
+ //forward serialized state and events from the SDK
72
+ launchObservations(activity)
94
73
 
95
74
  } catch (ex: Exception) {
96
- timber.e(ex, "initialize failed")
75
+ logError(EVENT_BRIDGE_ERROR, ex, "action" to "initialize")
97
76
  }
98
77
  }
99
78
 
100
- private fun launchUiStateObservation(activity: AppCompatActivity) {
79
+ /**
80
+ * Forward the SDK's serialized state and events to JS. The SDK owns the JSON serialization
81
+ * (see [LiveVoiceSerialization]) so the wire format is identical across Android and iOS.
82
+ */
83
+ private fun launchObservations(activity: AppCompatActivity) {
101
84
  activity.lifecycleScope.launch {
102
- LiveVoice.uiState.collect { newState: SdkUiState ->
103
- val uiStateString = json.encodeToString(newState.mapToTransferable())
104
- timber.d("New state %s", uiStateString)
105
-
106
- emitOnUiStateChanged(uiStateString) //it's there, even if intelliJ doesn't highlight it!
107
- }
85
+ LiveVoiceSerialization.viewState.collect { emitOnUiStateChanged(it) }
108
86
  }
109
- }
110
-
111
- private fun launchOutputModeObservation(activity: AppCompatActivity) {
112
87
  activity.lifecycleScope.launch {
113
- LiveVoice.outputMode.collect { newOutputMode: LiveVoiceOutput ->
114
- emitOnAudioOutputChanged(newOutputMode == LiveVoiceOutput.SPEAKER)
115
- }
88
+ LiveVoiceSerialization.audioOutputIsSpeaker.collect { emitOnAudioOutputChanged(it) }
116
89
  }
117
- }
118
-
119
- private fun launchAvailableSubtitleObservation(activity: AppCompatActivity) {
120
90
  activity.lifecycleScope.launch {
121
- LiveVoice.availableSubtitles.collectLatest { subtitles ->
122
- val encodedSubtitles = json.encodeToString(subtitles.map { it.mapToNative() })
123
- emitOnAvailableSubtitleConfigsChanged(encodedSubtitles)
124
- }
91
+ LiveVoiceSerialization.availableSubtitles.collect { emitOnAvailableSubtitleConfigsChanged(it) }
125
92
  }
126
93
  activity.lifecycleScope.launch {
127
- emitOnSubtitlesChanged("[]")
128
- //collectLatest will cancel previous lambda when new subtitleState arrives
129
- LiveVoice.subtitleState.collectLatest { state: SdkSubtitleState ->
130
- if (state is SdkSubtitleState.Enabled) {
131
- //forward/emit all subtitle fragments that arrive!
132
- state.fragments.collect { fragments: List<String> ->
133
- emitOnSubtitlesChanged(json.encodeToString(fragments))
134
- }
135
- }
136
- }
94
+ LiveVoiceSerialization.subtitles.collect { emitOnSubtitlesChanged(it) }
95
+ }
96
+ activity.lifecycleScope.launch {
97
+ LiveVoiceSerialization.subtitleState.collect { emitOnSubtitleStateChanged(it) }
137
98
  }
138
99
  }
139
100
 
140
- override fun startChannel(id: Double) {
141
- val state = getStateOrNull<Ready>("startChannel") ?: return
142
- val longId = id.toLong()
143
-
144
- timber.d("calling startChannel($longId)")
145
- state.onStartChannel(longId)
146
- }
101
+ override fun startChannel(id: Double) =
102
+ whenConnected("startChannel") { LiveVoice.startChannel(id.toLong()) }
147
103
 
148
104
  override fun stopChannel() {
149
- val state = getStateOrNull<Ready>("stopChannel") ?: return
150
- val channel = state.channels.firstOrNull { it.playingState in listOf(Connecting, Playing) }
105
+ val ready = LiveVoice.uiState.value as? Ready ?: run {
106
+ logWarn(EVENT_ACTION_IGNORED, "action" to "stopChannel", "reason" to "wrongState")
107
+ return
108
+ }
109
+ val channel = ready.channels.firstOrNull { it.playingState in listOf(Connecting, Playing) }
151
110
  ?: run {
152
- timber.w("No channel in connecting or playing state to stop!")
111
+ logWarn(EVENT_ACTION_IGNORED, "action" to "stopChannel", "reason" to "noActiveChannel")
153
112
  return
154
113
  }
155
114
 
156
- timber.d("calling stopChannel(${channel.id})")
157
- state.onStopChannel(channel.id)
115
+ whenConnected("stopChannel") { LiveVoice.stopChannel(channel.id) }
158
116
  }
159
117
 
160
- override fun getCurrentUiState(): String {
161
- return try {
162
- timber.d("Getting current ui-state")
163
- val currentState: SdkUiState = LiveVoice.uiState.value
164
- json.encodeToString(currentState.mapToTransferable())
165
- } catch (ex: Exception) {
166
- timber.w(ex, "Tried accessing state without prior initialization, returning default!")
167
- json.encodeToString(NativeLoading)
168
- }
169
- }
118
+ override fun getCurrentUiState(): String = LiveVoiceSerialization.currentViewState()
170
119
 
171
- override fun getCurrentAudioState(): Boolean {
172
- return try {
173
- LiveVoice.outputMode.value == LiveVoiceOutput.SPEAKER
174
- } catch (ex: Exception) {
175
- timber.w(ex, "Tried accessing audio state without prior initialization, returning default!")
176
- false
177
- }
178
- }
120
+ override fun getCurrentAudioState(): Boolean = LiveVoiceSerialization.currentAudioOutputIsSpeaker()
179
121
 
180
- override fun getCurrentSubtitles(): String {
181
- val value = LiveVoice.subtitleState.value
182
- if (value !is SdkSubtitleState.Enabled) return "[]"
122
+ override fun getCurrentSubtitles(): String = LiveVoiceSerialization.currentSubtitles()
183
123
 
184
- return json.encodeToString(value.fragments.value)
185
- }
124
+ override fun getCurrentSubtitleState(): String = LiveVoiceSerialization.currentSubtitleState()
186
125
 
187
- override fun getCurrentAvailableSubtitleConfigs(): String {
188
- val value = LiveVoice.availableSubtitles.value
189
- return json.encodeToString(value.map { it.mapToNative() })
190
- }
126
+ override fun getCurrentAvailableSubtitleConfigs(): String =
127
+ LiveVoiceSerialization.currentAvailableSubtitles()
191
128
 
192
- override fun getCurrentClaims(): String? {
193
- val claims = LiveVoice.claims.value
194
- return json.encodeToString(claims ?: return null)
195
- }
129
+ override fun getCurrentClaims(): String? = LiveVoiceSerialization.currentClaims()
196
130
 
197
131
  override fun setLiveVoiceUseSpeaker(value: Boolean) {
198
132
  LiveVoice.changeOutput(value)
199
133
  }
200
134
 
201
- override fun startSubtitles(channelId: Double) {
202
- val state = getStateOrNull<Ready>("startSubtitles") ?: return
203
- val sdkSubtitleLanguage = SdkSubtitleLanguage.Transcript
204
-
135
+ override fun startSubtitles(channelId: Double) = whenConnected("startSubtitles") {
205
136
  val nullableChannelId = if (channelId.toLong() == -1L) null else channelId.toLong()
206
- state.onStartSubtitles(nullableChannelId)
137
+ LiveVoice.startSubtitles(nullableChannelId)
207
138
  }
208
139
 
209
- override fun stopSubtitles() {
210
- val state = getStateOrNull<Ready>("stopSubtitles") ?: return
211
- state.onStopSubtitles()
212
- }
140
+ override fun stopSubtitles() = whenConnected("stopSubtitles") { LiveVoice.stopSubtitles() }
213
141
 
214
142
  override fun openLiveVoiceWebsite() {
215
143
  LiveVoice.openLiveVoiceWebsite()
216
144
  }
217
145
 
146
+ override fun getSdkVersion(): String = Version.SDK_VERSION
147
+
218
148
  override fun joinEvent(joinCode: String, password: String?, apiKey: String) {
219
149
  val scope = (reactApplicationContext.currentActivity as? ComponentActivity)?.lifecycleScope
220
- ?: error("Could not create scope to connect! activity: ${reactApplicationContext.currentActivity}")
150
+ ?: run {
151
+ logError(EVENT_BRIDGE_ERROR, "action" to "joinEvent", "reason" to "missingActivity")
152
+ error("Could not create scope to connect! activity: ${reactApplicationContext.currentActivity}")
153
+ }
221
154
 
222
155
  scope.launch {
223
156
  LiveVoice.joinEvent(joinCode, password, apiKey)
@@ -286,22 +219,55 @@ class LiveVoiceModule(reactContext: ReactApplicationContext) :
286
219
  LiveVoice.translations.accessibilityChannelStop()
287
220
 
288
221
 
289
- private inline fun <reified T : SdkUiState> getStateOrNull(methodName: String): T? {
290
- val currentState = try {
291
- LiveVoice.uiState.value
292
- } catch (ex: SdkUninitializedException) {
293
- timber.e(ex, "Tried accessing state without prior initialization")
294
- null
222
+ /** Runs [block] against the connected SDK, logging instead of crashing if not connected. */
223
+ private inline fun whenConnected(methodName: String, block: () -> Unit) {
224
+ try {
225
+ block()
226
+ } catch (ex: SdkNotConnectedException) {
227
+ logError(EVENT_BRIDGE_ERROR, ex, "action" to methodName, "reason" to "notConnected")
295
228
  }
229
+ }
230
+
231
+ private fun logInfo(event: String, vararg fields: Pair<String, Any?>) {
232
+ Log.i(NAME, formatMessage(event, *fields))
233
+ }
296
234
 
297
- val state = currentState as? T
298
- if (state == null) {
299
- timber.e("$methodName called in wrong state $currentState")
235
+ private fun logWarn(event: String, vararg fields: Pair<String, Any?>) {
236
+ Log.w(NAME, formatMessage(event, *fields))
237
+ }
238
+
239
+ private fun logError(event: String, vararg fields: Pair<String, Any?>) {
240
+ Log.e(NAME, formatMessage(event, *fields))
241
+ }
242
+
243
+ private fun logError(event: String, throwable: Throwable, vararg fields: Pair<String, Any?>) {
244
+ Log.e(NAME, formatMessage(event, *fields), throwable)
245
+ }
246
+
247
+ private fun formatMessage(event: String, vararg fields: Pair<String, Any?>): String {
248
+ val fieldText = fields
249
+ .filter { it.second != null }
250
+ .sortedBy { it.first }
251
+ .joinToString(" ") { (key, value) -> "$key=$value" }
252
+
253
+ return if (fieldText.isEmpty()) {
254
+ event
255
+ } else {
256
+ "$event $fieldText"
300
257
  }
301
- return state
302
258
  }
303
259
 
304
260
  companion object {
261
+ private const val EVENT_ACTION_IGNORED = "LV_ACTION_IGNORED"
262
+ private const val EVENT_BRIDGE_ERROR = "LV_BRIDGE_ERROR"
263
+ private const val EVENT_INIT = "LV_INIT"
305
264
  const val NAME = "LiveVoiceModule"
265
+
266
+ /**
267
+ * Sentinel the JS layer sends when the foreground service is enabled without a custom
268
+ * message. Must stay in sync with `serviceEnabledWithDefaultMessage` in
269
+ * `src/util/ForegroundServiceConfig.tsx`.
270
+ */
271
+ const val DEFAULT_MESSAGE_SENTINEL = "LV_NOTIFICATION_DEFAULT_MESSAGE"
306
272
  }
307
273
  }
@@ -1,6 +1,20 @@
1
- // AUTO-GENERATED FILE
1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the LiveVoice SDK project
4
+ //
5
+ // Copyright (c) LiveVoice GmbH
6
+ // Licensed under the MIT license
7
+ //
8
+ // See LICENSE for license information
9
+ //
10
+ // SPDX-License-Identifier: MIT
11
+ //
12
+ //===----------------------------------------------------------------------===//
13
+
14
+ // GENERATED CODE - DO NOT MODIFY BY HAND.
15
+ // Generated by tool/generate-native-versions.mjs from package.json.
2
16
  package io.livevoice.react
3
17
 
4
18
  object Version {
5
- const val SDK_VERSION = "2.0.0"
19
+ const val SDK_VERSION = "2.0.1"
6
20
  }