livevoice-sdk-react-native 2.0.0 → 3.0.0

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 (221) hide show
  1. package/README.md +158 -7
  2. package/android/build.gradle +18 -6
  3. package/android/src/main/java/io/livevoice/react/LiveVoiceModule.kt +121 -221
  4. package/android/src/main/java/io/livevoice/react/__SDK.kt +17 -3
  5. package/ios/LiveVoiceRNBridge.swift +362 -0
  6. package/ios/LiveVoiceSdkReactNative.mm +247 -145
  7. package/ios/__SDK.h +16 -2
  8. package/ios/livevoice_spm_backport.rb +202 -0
  9. package/lib/module/components/BrandingView.js +71 -50
  10. package/lib/module/components/BrandingView.js.map +1 -1
  11. package/lib/module/components/ChannelView.js +368 -54
  12. package/lib/module/components/ChannelView.js.map +1 -1
  13. package/lib/module/components/ErrorView.js +17 -16
  14. package/lib/module/components/ErrorView.js.map +1 -1
  15. package/lib/module/components/LiveVoiceSpinner.js +81 -0
  16. package/lib/module/components/LiveVoiceSpinner.js.map +1 -0
  17. package/lib/module/components/LiveVoiceView.js +351 -104
  18. package/lib/module/components/LiveVoiceView.js.map +1 -1
  19. package/lib/module/components/PasswordPrompt.js +165 -0
  20. package/lib/module/components/PasswordPrompt.js.map +1 -0
  21. package/lib/module/components/PlayButtonView.js +192 -254
  22. package/lib/module/components/PlayButtonView.js.map +1 -1
  23. package/lib/module/components/ReconnectingView.js +43 -62
  24. package/lib/module/components/ReconnectingView.js.map +1 -1
  25. package/lib/module/components/WarningView.js +12 -10
  26. package/lib/module/components/WarningView.js.map +1 -1
  27. package/lib/module/icons/branding-ios.png +0 -0
  28. package/lib/module/icons/camera-off-android.png +0 -0
  29. package/lib/module/icons/camera-off-android@2x.png +0 -0
  30. package/lib/module/icons/camera-off-android@3x.png +0 -0
  31. package/lib/module/icons/cpu-android.png +0 -0
  32. package/lib/module/icons/cpu-android@2x.png +0 -0
  33. package/lib/module/icons/cpu-android@3x.png +0 -0
  34. package/lib/module/icons/cpu-ios.png +0 -0
  35. package/lib/module/icons/cpu-ios@2x.png +0 -0
  36. package/lib/module/icons/cpu-ios@3x.png +0 -0
  37. package/lib/module/icons/lock-android.png +0 -0
  38. package/lib/module/icons/lock-android@2x.png +0 -0
  39. package/lib/module/icons/lock-android@3x.png +0 -0
  40. package/lib/module/icons/lock-ios.png +0 -0
  41. package/lib/module/icons/lock-ios@2x.png +0 -0
  42. package/lib/module/icons/lock-ios@3x.png +0 -0
  43. package/lib/module/icons/mic-off-android.png +0 -0
  44. package/lib/module/icons/mic-off-android@2x.png +0 -0
  45. package/lib/module/icons/mic-off-android@3x.png +0 -0
  46. package/lib/module/icons/mic-slash-ios.png +0 -0
  47. package/lib/module/icons/mic-slash-ios@2x.png +0 -0
  48. package/lib/module/icons/mic-slash-ios@3x.png +0 -0
  49. package/lib/module/icons/video-slash-ios.png +0 -0
  50. package/lib/module/icons/video-slash-ios@2x.png +0 -0
  51. package/lib/module/icons/video-slash-ios@3x.png +0 -0
  52. package/lib/module/index.js +24 -225
  53. package/lib/module/index.js.map +1 -1
  54. package/lib/module/sdk/Bridge.js +236 -0
  55. package/lib/module/sdk/Bridge.js.map +1 -0
  56. package/lib/module/sdk/Event.js +160 -0
  57. package/lib/module/sdk/Event.js.map +1 -0
  58. package/lib/module/sdk/Lifecycle.js +331 -0
  59. package/lib/module/sdk/Lifecycle.js.map +1 -0
  60. package/lib/module/sdk/LiveVoiceSdk.js +365 -0
  61. package/lib/module/sdk/LiveVoiceSdk.js.map +1 -0
  62. package/lib/module/sdk/LiveVoiceState.js +234 -0
  63. package/lib/module/sdk/LiveVoiceState.js.map +1 -0
  64. package/lib/module/sdk/LogEntries.js +99 -0
  65. package/lib/module/sdk/LogEntries.js.map +1 -0
  66. package/lib/module/sdk/Models.js +2 -0
  67. package/lib/module/{util/UIState.js.map → sdk/Models.js.map} +1 -1
  68. package/lib/module/sdk/NativeSdkVersions.js +24 -0
  69. package/lib/module/sdk/NativeSdkVersions.js.map +1 -0
  70. package/lib/module/sdk/Observables.js +391 -0
  71. package/lib/module/sdk/Observables.js.map +1 -0
  72. package/lib/module/sdk/Passwords.js +298 -0
  73. package/lib/module/sdk/Passwords.js.map +1 -0
  74. package/lib/module/sdk/Playback.js +35 -0
  75. package/lib/module/sdk/Playback.js.map +1 -0
  76. package/lib/module/specs/NativeLiveVoiceSdk.js +12 -10
  77. package/lib/module/specs/NativeLiveVoiceSdk.js.map +1 -1
  78. package/lib/module/util/Colors.js +14 -11
  79. package/lib/module/util/Colors.js.map +1 -1
  80. package/lib/module/util/Translations.js +46 -21
  81. package/lib/module/util/Translations.js.map +1 -1
  82. package/lib/module/util/freeze.js +26 -0
  83. package/lib/module/util/freeze.js.map +1 -0
  84. package/lib/module/util/logger.js +114 -0
  85. package/lib/module/util/logger.js.map +1 -0
  86. package/lib/typescript/src/components/BrandingView.d.ts +4 -0
  87. package/lib/typescript/src/components/BrandingView.d.ts.map +1 -1
  88. package/lib/typescript/src/components/ChannelView.d.ts +29 -3
  89. package/lib/typescript/src/components/ChannelView.d.ts.map +1 -1
  90. package/lib/typescript/src/components/ErrorView.d.ts +5 -3
  91. package/lib/typescript/src/components/ErrorView.d.ts.map +1 -1
  92. package/lib/typescript/src/components/LiveVoiceSpinner.d.ts +16 -0
  93. package/lib/typescript/src/components/LiveVoiceSpinner.d.ts.map +1 -0
  94. package/lib/typescript/src/components/LiveVoiceView.d.ts +85 -3
  95. package/lib/typescript/src/components/LiveVoiceView.d.ts.map +1 -1
  96. package/lib/typescript/src/components/PasswordPrompt.d.ts +13 -0
  97. package/lib/typescript/src/components/PasswordPrompt.d.ts.map +1 -0
  98. package/lib/typescript/src/components/PlayButtonView.d.ts +12 -329
  99. package/lib/typescript/src/components/PlayButtonView.d.ts.map +1 -1
  100. package/lib/typescript/src/components/ReconnectingView.d.ts +6 -3
  101. package/lib/typescript/src/components/ReconnectingView.d.ts.map +1 -1
  102. package/lib/typescript/src/components/WarningView.d.ts.map +1 -1
  103. package/lib/typescript/src/index.d.ts +15 -144
  104. package/lib/typescript/src/index.d.ts.map +1 -1
  105. package/lib/typescript/src/sdk/Bridge.d.ts +36 -0
  106. package/lib/typescript/src/sdk/Bridge.d.ts.map +1 -0
  107. package/lib/typescript/src/sdk/Event.d.ts +97 -0
  108. package/lib/typescript/src/sdk/Event.d.ts.map +1 -0
  109. package/lib/typescript/src/sdk/Lifecycle.d.ts +191 -0
  110. package/lib/typescript/src/sdk/Lifecycle.d.ts.map +1 -0
  111. package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts +215 -0
  112. package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts.map +1 -0
  113. package/lib/typescript/src/sdk/LiveVoiceState.d.ts +80 -0
  114. package/lib/typescript/src/sdk/LiveVoiceState.d.ts.map +1 -0
  115. package/lib/typescript/src/sdk/LogEntries.d.ts +28 -0
  116. package/lib/typescript/src/sdk/LogEntries.d.ts.map +1 -0
  117. package/lib/typescript/src/sdk/Models.d.ts +114 -0
  118. package/lib/typescript/src/sdk/Models.d.ts.map +1 -0
  119. package/lib/typescript/src/sdk/NativeSdkVersions.d.ts +6 -0
  120. package/lib/typescript/src/sdk/NativeSdkVersions.d.ts.map +1 -0
  121. package/lib/typescript/src/sdk/Observables.d.ts +123 -0
  122. package/lib/typescript/src/sdk/Observables.d.ts.map +1 -0
  123. package/lib/typescript/src/sdk/Passwords.d.ts +72 -0
  124. package/lib/typescript/src/sdk/Passwords.d.ts.map +1 -0
  125. package/lib/typescript/src/sdk/Playback.d.ts +9 -0
  126. package/lib/typescript/src/sdk/Playback.d.ts.map +1 -0
  127. package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts +35 -29
  128. package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts.map +1 -1
  129. package/lib/typescript/src/util/Colors.d.ts.map +1 -1
  130. package/lib/typescript/src/util/Translations.d.ts +30 -8
  131. package/lib/typescript/src/util/Translations.d.ts.map +1 -1
  132. package/lib/typescript/src/util/freeze.d.ts +6 -0
  133. package/lib/typescript/src/util/freeze.d.ts.map +1 -0
  134. package/lib/typescript/src/util/logger.d.ts +33 -0
  135. package/lib/typescript/src/util/logger.d.ts.map +1 -0
  136. package/livevoice-sdk-react-native.podspec +41 -16
  137. package/package.json +26 -14
  138. package/src/components/BrandingView.tsx +55 -66
  139. package/src/components/ChannelView.tsx +342 -48
  140. package/src/components/ErrorView.tsx +21 -22
  141. package/src/components/LiveVoiceSpinner.tsx +87 -0
  142. package/src/components/LiveVoiceView.tsx +360 -151
  143. package/src/components/PasswordPrompt.tsx +144 -0
  144. package/src/components/PlayButtonView.tsx +177 -279
  145. package/src/components/ReconnectingView.tsx +40 -86
  146. package/src/components/WarningView.tsx +12 -10
  147. package/src/icons/branding-ios.png +0 -0
  148. package/src/icons/camera-off-android.png +0 -0
  149. package/src/icons/camera-off-android@2x.png +0 -0
  150. package/src/icons/camera-off-android@3x.png +0 -0
  151. package/src/icons/cpu-android.png +0 -0
  152. package/src/icons/cpu-android@2x.png +0 -0
  153. package/src/icons/cpu-android@3x.png +0 -0
  154. package/src/icons/cpu-ios.png +0 -0
  155. package/src/icons/cpu-ios@2x.png +0 -0
  156. package/src/icons/cpu-ios@3x.png +0 -0
  157. package/src/icons/lock-android.png +0 -0
  158. package/src/icons/lock-android@2x.png +0 -0
  159. package/src/icons/lock-android@3x.png +0 -0
  160. package/src/icons/lock-ios.png +0 -0
  161. package/src/icons/lock-ios@2x.png +0 -0
  162. package/src/icons/lock-ios@3x.png +0 -0
  163. package/src/icons/mic-off-android.png +0 -0
  164. package/src/icons/mic-off-android@2x.png +0 -0
  165. package/src/icons/mic-off-android@3x.png +0 -0
  166. package/src/icons/mic-slash-ios.png +0 -0
  167. package/src/icons/mic-slash-ios@2x.png +0 -0
  168. package/src/icons/mic-slash-ios@3x.png +0 -0
  169. package/src/icons/video-slash-ios.png +0 -0
  170. package/src/icons/video-slash-ios@2x.png +0 -0
  171. package/src/icons/video-slash-ios@3x.png +0 -0
  172. package/src/index.tsx +105 -252
  173. package/src/sdk/Bridge.ts +259 -0
  174. package/src/sdk/Event.ts +187 -0
  175. package/src/sdk/Lifecycle.ts +366 -0
  176. package/src/sdk/LiveVoiceSdk.ts +384 -0
  177. package/src/sdk/LiveVoiceState.ts +259 -0
  178. package/src/sdk/LogEntries.ts +88 -0
  179. package/src/sdk/Models.ts +129 -0
  180. package/src/sdk/NativeSdkVersions.ts +21 -0
  181. package/src/sdk/Observables.ts +414 -0
  182. package/src/sdk/Passwords.ts +286 -0
  183. package/src/sdk/Playback.ts +30 -0
  184. package/src/specs/NativeLiveVoiceSdk.ts +56 -46
  185. package/src/util/Colors.ts +14 -11
  186. package/src/util/Translations.ts +46 -22
  187. package/src/util/freeze.ts +23 -0
  188. package/src/util/logger.ts +119 -0
  189. package/android/app/build/generated/source/codegen/java/com/facebook/fbreact/specs/NativeLiveVoiceSdkSpec.java +0 -186
  190. package/android/app/build/generated/source/codegen/jni/CMakeLists.txt +0 -28
  191. package/android/app/build/generated/source/codegen/jni/RNLiveVoiceSpec-generated.cpp +0 -235
  192. package/android/app/build/generated/source/codegen/jni/RNLiveVoiceSpec.h +0 -31
  193. package/android/app/build/generated/source/codegen/jni/react/renderer/components/RNLiveVoiceSpec/RNLiveVoiceSpecJSI.h +0 -341
  194. package/android/src/main/java/io/livevoice/react/Mappings.kt +0 -85
  195. package/android/src/main/java/io/livevoice/react/NativeModels.kt +0 -79
  196. package/lib/module/components/AudioOutputSwitchView.js +0 -138
  197. package/lib/module/components/AudioOutputSwitchView.js.map +0 -1
  198. package/lib/module/components/index.js +0 -22
  199. package/lib/module/components/index.js.map +0 -1
  200. package/lib/module/icons/mic.png +0 -0
  201. package/lib/module/util/ForegroundServiceConfig.js +0 -28
  202. package/lib/module/util/ForegroundServiceConfig.js.map +0 -1
  203. package/lib/module/util/PlayingState.js +0 -21
  204. package/lib/module/util/PlayingState.js.map +0 -1
  205. package/lib/module/util/UIState.js +0 -4
  206. package/lib/typescript/src/components/AudioOutputSwitchView.d.ts +0 -6
  207. package/lib/typescript/src/components/AudioOutputSwitchView.d.ts.map +0 -1
  208. package/lib/typescript/src/components/index.d.ts +0 -9
  209. package/lib/typescript/src/components/index.d.ts.map +0 -1
  210. package/lib/typescript/src/util/ForegroundServiceConfig.d.ts +0 -12
  211. package/lib/typescript/src/util/ForegroundServiceConfig.d.ts.map +0 -1
  212. package/lib/typescript/src/util/PlayingState.d.ts +0 -7
  213. package/lib/typescript/src/util/PlayingState.d.ts.map +0 -1
  214. package/lib/typescript/src/util/UIState.d.ts +0 -66
  215. package/lib/typescript/src/util/UIState.d.ts.map +0 -1
  216. package/src/components/AudioOutputSwitchView.tsx +0 -146
  217. package/src/components/index.tsx +0 -19
  218. package/src/icons/mic.png +0 -0
  219. package/src/util/ForegroundServiceConfig.tsx +0 -32
  220. package/src/util/PlayingState.ts +0 -18
  221. package/src/util/UIState.ts +0 -89
@@ -1,180 +1,389 @@
1
- /*
2
- * This source file is part of the LiveVoice SDK project
3
- *
4
- * Copyright (c) LiveVoice GmbH
5
- * Licensed under the MIT license
6
- *
7
- * See LICENSE for license information
8
- *
9
- * SPDX-License-Identifier: MIT
10
- */
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
+ import { Fragment, useEffect, useState } from 'react';
15
+ import { Platform, StyleSheet, Text, View, useColorScheme } from 'react-native';
11
16
 
17
+ import { LiveVoiceError, normalizeError, publicRetry, type LiveVoiceRetry } from '../sdk/Lifecycle';
18
+ import { reconnect, startSubtitles, stopSubtitles } from '../sdk/LiveVoiceSdk';
19
+ import { getCurrentViewModel, onViewModelChanged, type ViewModel } from '../sdk/LiveVoiceState';
20
+ import { type LiveVoiceChannel } from '../sdk/Models';
21
+ import { getCurrentConnection, getCurrentEvent } from '../sdk/Observables';
12
22
  import {
13
- type UiState,
14
- type AvailableSubtitle,
15
- getCurrentUiState,
16
- onUiStateChanged,
17
- startChannel,
18
- stopChannel,
19
- reconnect,
20
- onAvailableSubtitleConfigsChanged,
21
- onSubtitlesChanged,
22
- getCurrentClaims,
23
- BrandingView,
24
- ErrorView,
25
- ReconnectingView,
26
- startSubtitles,
27
- } from "livevoice-sdk-react-native";
28
-
29
- import { PlayingState } from "../util/PlayingState";
30
-
31
- import { useEffect, useState } from "react";
32
- import { View, ActivityIndicator } from "react-native";
33
-
34
- import { COLORS } from "../util/Colors";
35
- import { type Channel } from "../util/UIState";
36
- import { ChannelView } from "./ChannelView";
37
-
38
- export interface LiveVoiceViewProps {
39
- isDarkMode?: boolean;
40
- }
23
+ answerPrompt,
24
+ observePrompt,
25
+ promptFor,
26
+ registerPresenter,
27
+ unregisterPresenter,
28
+ type PromptRequest,
29
+ } from '../sdk/Passwords';
30
+ import { togglePlayback } from '../sdk/Playback';
31
+ import { COLORS } from '../util/Colors';
32
+ import { deepFreeze } from '../util/freeze';
33
+ import { writeLogEntry } from '../util/logger';
34
+ import { Translations } from '../util/Translations';
35
+ import { BrandingView } from './BrandingView';
36
+ import { ChannelView } from './ChannelView';
37
+ import { ErrorView } from './ErrorView';
38
+ import { LiveVoiceSpinner } from './LiveVoiceSpinner';
39
+ import { PasswordPrompt } from './PasswordPrompt';
40
+ import { ReconnectingView } from './ReconnectingView';
41
41
 
42
- export const LiveVoiceView: React.FC<LiveVoiceViewProps> = ({ isDarkMode }) => {
43
- const [uiState, setUiState] = useState<UiState>(() => getCurrentUiState());
42
+ /**
43
+ * One channel of the list, handed to {@link LiveVoiceViewProps.renderChannelCell}. Only the view
44
+ * creates cells.
45
+ */
46
+ export type LiveVoiceChannelCell = {
47
+ /** The channel this cell shows. */
48
+ readonly channel: LiveVoiceChannel;
49
+ /** Whether this is the last channel in the (filtered) list, for example to omit a divider. */
50
+ readonly isLast: boolean;
51
+ /**
52
+ * Whether the default cell would let a tap start the channel: `false` while the channel is
53
+ * unavailable (offline, without audio) and during the first seconds of a reconnect.
54
+ */
55
+ readonly isEnabled: boolean;
56
+ /**
57
+ * The cell's action, as a tap on the default cell: starts the channel if it is stopped, stops
58
+ * it if it is playing, decided by the SDK from the current playback state.
59
+ *
60
+ * A start resolves when local playback is confirmed, a stop when local playback has stopped;
61
+ * the `channel.playingState` handed to the cell follows shortly. A start needs a `ready`
62
+ * connection and an online channel with audio. A stop is allowed whenever an event is joined,
63
+ * joining, reconnecting or failed. Errors reach only the caller, never `onCommandError`.
64
+ *
65
+ * @throws {LiveVoiceError} `not_joined` when the connection does not allow the action;
66
+ * `api_key_invalid` without the custom-UI permission. For a start: `channel_offline` while the
67
+ * channel is offline; `channel_unavailable` for a channel without audio, while reconnecting, or
68
+ * one that is not part of the joined event; `channel_password_required`,
69
+ * `channel_wrong_password` and `channel_limit_reached`; `timed_out` after 10 seconds (entering
70
+ * a password does not count); `leave_incomplete` when an unfinished stop holds it up for more
71
+ * than 5 seconds; `cancelled` when another start, a stop, a second toggle, a leave or a new
72
+ * join ended it. A connection loss during a start can fail it with `cancelled` or
73
+ * `network_failed`; observe the connection to follow recovery, the rejection and the
74
+ * connection update need not arrive in a particular order. For a stop, the errors of
75
+ * {@link stopChannel}. See {@link LiveVoiceErrorCode}.
76
+ */
77
+ togglePlayback(): Promise<void>;
78
+ };
44
79
 
45
- useEffect(() => {
46
- let subscription: { remove: () => void } | undefined = onUiStateChanged(
47
- (data: UiState) => {
48
- setUiState(data);
49
- },
50
- );
51
- return () => {
52
- subscription?.remove();
53
- subscription = undefined;
54
- };
55
- }, []);
80
+ /** Why the list is empty: the event has no channels, or `filter` removed them all. */
81
+ export type LiveVoiceEmptyReason = 'noChannels' | 'noMatches';
82
+
83
+ /**
84
+ * The props of {@link LiveVoiceView}. All are optional; without any, the view shows the default
85
+ * UI.
86
+ */
87
+ export type LiveVoiceViewProps = {
88
+ /**
89
+ * Receives the failures of the default parts' actions: a default cell's audio and subtitle
90
+ * taps and the default error part's retry. Never `cancelled` or `superseded`. Custom parts'
91
+ * actions report to their callers instead. Without it, the failures go to the SDK log as
92
+ * `LV_COMMAND_ERROR`, which `addLogListener` delivers.
93
+ */
94
+ onCommandError?: (error: LiveVoiceError) => void;
95
+ /** Forces the light or dark appearance; the system's by default. */
96
+ isDarkMode?: boolean;
97
+ /** Return `true` for the channels to list. It only hides channels; it starts nothing. */
98
+ filter?: (channel: LiveVoiceChannel) => boolean;
99
+ /**
100
+ * Draws a channel instead of the default cell.
101
+ *
102
+ * Every render prop except `renderLoading` needs an API key that permits custom UI. Without
103
+ * that permission the view shows the invalid-API-key error instead of your parts.
104
+ */
105
+ renderChannelCell?: (cell: LiveVoiceChannelCell) => React.ReactNode;
106
+ /**
107
+ * Draws the loading part, shown while the connection is `idle`, `joining` or `leaving`. Works
108
+ * for every API key.
109
+ */
110
+ renderLoading?: () => React.ReactNode;
111
+ /** Draws the part shown while the connection recovers. The reconnect runs by itself. */
112
+ renderReconnecting?: (retry: LiveVoiceRetry) => React.ReactNode;
113
+ /**
114
+ * Draws the error part, also for a join that failed before it authenticated. `retry` is present
115
+ * exactly when the error is retryable; it reconnects, and fails with `not_joined` once the join
116
+ * has ended.
117
+ */
118
+ renderError?: (error: LiveVoiceError, retry: (() => Promise<void>) | null) => React.ReactNode;
119
+ /** Draws the empty list. */
120
+ renderEmpty?: (reason: LiveVoiceEmptyReason) => React.ReactNode;
121
+ };
56
122
 
123
+ function useViewModel(): ViewModel {
124
+ const [model, setModel] = useState<ViewModel>(() => getCurrentViewModel());
57
125
  useEffect(() => {
58
- let subscription: { remove: () => void } | undefined =
59
- onAvailableSubtitleConfigsChanged((data: [AvailableSubtitle]) => {
60
- console.log(
61
- "available subtitles changed! …" + JSON.stringify(data).slice(-20),
62
- );
63
- });
64
- return () => {
65
- subscription?.remove();
66
- subscription = undefined;
67
- };
126
+ // The permission travels with the channels of the same join, so a previous key's permission
127
+ // never keeps the gate open for another join's channels.
128
+ const subscription = onViewModelChanged(setModel);
129
+ setModel(getCurrentViewModel());
130
+ return () => subscription.remove();
68
131
  }, []);
132
+ return model;
133
+ }
69
134
 
135
+ /**
136
+ * The password request this view presents, if any. The most recently shown view that is still
137
+ * mounted presents the built-in prompt, also for requests that did not start from it.
138
+ */
139
+ function usePasswordPrompt(): { request: PromptRequest | null; presenter: number | null } {
140
+ const [presenter, setPresenter] = useState<number | null>(null);
141
+ const [request, setRequest] = useState<PromptRequest | null>(null);
70
142
  useEffect(() => {
71
- let subscription: { remove: () => void } | undefined = onSubtitlesChanged(
72
- (data: [String]) => {
73
- console.log(
74
- "available subtitles changed! …" + JSON.stringify(data).slice(-20),
75
- );
76
- },
77
- );
143
+ const id = registerPresenter();
144
+ setPresenter(id);
145
+ const update = () => setRequest(promptFor(id));
146
+ const stop = observePrompt(update);
147
+ update();
78
148
  return () => {
79
- subscription?.remove();
80
- subscription = undefined;
149
+ stop();
150
+ // A prompt this view showed gives up with the password error.
151
+ unregisterPresenter(id);
81
152
  };
82
153
  }, []);
154
+ return { request, presenter };
155
+ }
83
156
 
84
- const claims = getCurrentClaims();
157
+ /**
158
+ * The LiveVoice channel list: the joined event's channels with their play and subtitle buttons,
159
+ * and loading, reconnecting, error and empty parts. Every part can be replaced; see
160
+ * {@link LiveVoiceViewProps}. It also presents the built-in password prompt when no
161
+ * password provider is set.
162
+ */
163
+ export const LiveVoiceView: React.FC<LiveVoiceViewProps> = props => {
164
+ const systemDarkMode = useColorScheme() === 'dark';
165
+ const { request } = usePasswordPrompt();
166
+ return (
167
+ <>
168
+ <LiveVoiceParts {...props} />
169
+ {request !== null && (
170
+ <PasswordPrompt
171
+ request={request}
172
+ isDarkMode={props.isDarkMode ?? systemDarkMode}
173
+ onAnswer={password => answerPrompt(request.requestId, password)}
174
+ />
175
+ )}
176
+ </>
177
+ );
178
+ };
85
179
 
86
- switch (uiState.type) {
87
- case "ready":
88
- const backgroundColor = isDarkMode ? COLORS.cardBgDark : COLORS.cardBg;
89
- const dividerColor = isDarkMode ? COLORS.dividerDark : COLORS.divider;
180
+ const LiveVoiceParts: React.FC<LiveVoiceViewProps> = ({
181
+ isDarkMode,
182
+ onCommandError,
183
+ filter,
184
+ renderChannelCell,
185
+ renderLoading,
186
+ renderReconnecting,
187
+ renderError,
188
+ renderEmpty,
189
+ }) => {
190
+ const systemDarkMode = useColorScheme() === 'dark';
191
+ const dark = isDarkMode ?? systemDarkMode;
192
+ const { part, features } = useViewModel();
90
193
 
91
- return (
92
- <View style={{ width: "100%" }}>
93
- <View
94
- style={{
95
- backgroundColor: backgroundColor,
96
- borderRadius: 8,
97
- overflow: "hidden",
98
- }}
99
- >
100
- {uiState.channels.map((channel: Channel, index: number) => (
101
- <View key={index}>
102
- <ChannelView
103
- playingState={channel.playingState}
104
- isDarkMode={isDarkMode}
105
- muted={channel.isAudioMuted}
106
- isOnline={channel.isOnline}
107
- hasAudio={channel.hasAudio}
108
- hasSubtitles={channel.hasSubtitles}
109
- channelName={channel.name}
110
- onAudioPress={() =>
111
- channel.playingState === PlayingState.PLAYING
112
- ? stopChannel()
113
- : startChannel(channel.id)
114
- }
115
- onSubtitlesPress={() => startSubtitles(channel.id)}
116
- />
117
- {index < uiState.channels.length - 1 && (
118
- <View
119
- style={{
120
- height: 1,
121
- backgroundColor: dividerColor,
122
- marginHorizontal: 10,
123
- }}
124
- />
125
- )}
126
- </View>
127
- ))}
128
- </View>
194
+ const perform = (operation: Promise<void>, source: 'cell' | 'subtitles' | 'retry', channelId?: number) => {
195
+ void Promise.resolve(operation).catch(error => {
196
+ const failure = normalizeError(error);
197
+ if (failure.code === 'cancelled' || failure.code === 'superseded') return;
198
+ if (onCommandError) onCommandError(failure);
199
+ else {
200
+ writeLogEntry('LV_COMMAND_ERROR', 'error', {
201
+ source,
202
+ channelId: channelId?.toString(),
203
+ code: failure.code,
204
+ retryable: String(failure.isRetryable),
205
+ reason: failure.detail.reason,
206
+ });
207
+ }
208
+ });
209
+ };
129
210
 
130
- {(claims === null || !claims.hideBranding) && (
131
- <BrandingView isDarkMode={isDarkMode} />
132
- )}
133
- </View>
134
- );
211
+ // Only a provided function counts; `renderLoading` works for every key.
212
+ const customParts = Object.entries({
213
+ channelCell: renderChannelCell,
214
+ reconnecting: renderReconnecting,
215
+ error: renderError,
216
+ empty: renderEmpty,
217
+ }).filter(([, prop]) => typeof prop === 'function').map(([part]) => part);
218
+ const usesCustomParts = customParts.length > 0;
219
+ const denied = usesCustomParts && features !== null && !features.customUiAllowed;
220
+ const deniedParts = denied ? customParts.join(',') : null;
221
+ useEffect(() => {
222
+ // Once each time the view turns to the invalid-key error for its custom parts.
223
+ if (deniedParts === null) return;
224
+ const connection = getCurrentConnection();
225
+ const eventId = getCurrentEvent()?.eventId ?? ('eventId' in connection ? connection.eventId : undefined);
226
+ writeLogEntry('LV_CUSTOM_UI_DENIED', 'error', { parts: deniedParts, eventId: eventId?.toString() });
227
+ }, [deniedParts]);
228
+ if (denied) {
229
+ return <ErrorView message={new LiveVoiceError('api_key_invalid').message} onRetry={null} isDarkMode={dark} />;
230
+ }
135
231
 
136
- case "loading":
137
- const loadingBackgroundColor = isDarkMode
138
- ? COLORS.cardBgDark
139
- : COLORS.cardBg;
232
+ switch (part.kind) {
233
+ case 'loading':
234
+ if (typeof renderLoading === 'function') return <>{renderLoading()}</>;
235
+ // Custom parts get a neutral spinner, never the default card.
236
+ return usesCustomParts ? <NeutralLoading dark={dark} /> : <DefaultLoading dark={dark} />;
140
237
 
141
- return (
142
- <View
143
- style={{
144
- width: "100%",
145
- padding: 10,
146
- borderRadius: 8,
147
- justifyContent: "center",
148
- alignItems: "center",
149
- backgroundColor: loadingBackgroundColor,
150
- }}
151
- >
152
- <ActivityIndicator size={40} color={COLORS.primary} />
153
- </View>
154
- );
238
+ case 'reconnecting':
239
+ if (typeof renderReconnecting === 'function') return <>{renderReconnecting(deepFreeze(publicRetry(part.retry)))}</>;
240
+ return <ReconnectingView retry={part.retry} isDarkMode={dark} />;
155
241
 
156
- case "error":
242
+ case 'error': {
243
+ const { error } = part;
244
+ if (typeof renderError === 'function') {
245
+ return <>{renderError(error, error.isRetryable ? () => reconnect() : null)}</>;
246
+ }
157
247
  return (
158
248
  <ErrorView
159
- state={uiState}
160
- onPress={() => {
161
- reconnect();
162
- }}
249
+ message={error.message}
250
+ onRetry={error.isRetryable ? () => perform(reconnect(), 'retry') : null}
251
+ isDarkMode={dark}
163
252
  />
164
253
  );
254
+ }
255
+
256
+ case 'ready': {
257
+ const visible = filter == null ? part.channels : part.channels.filter(filter);
258
+ const showBranding = !(features?.hideBranding ?? false);
259
+ if (visible.length === 0) {
260
+ const reason: LiveVoiceEmptyReason = part.channels.length === 0 ? 'noChannels' : 'noMatches';
261
+ if (typeof renderEmpty === 'function') return <>{renderEmpty(reason)}</>;
262
+ return (
263
+ <View style={{ width: '100%' }}>
264
+ <View style={{ backgroundColor: dark ? COLORS.cardBgDark : COLORS.cardBg, borderRadius: 8, overflow: 'hidden' }}>
265
+ <DefaultEmpty reason={reason} dark={dark} />
266
+ {showBranding && <BrandingView isDarkMode={dark} />}
267
+ </View>
268
+ </View>
269
+ );
270
+ }
165
271
 
166
- case "reconnecting":
272
+ // Custom cells get full control (no card chrome or dividers), like the native SDKs.
273
+ if (typeof renderChannelCell === 'function') {
274
+ return (
275
+ <View style={{ width: '100%' }}>
276
+ {visible.map((channel, index) => (
277
+ <Fragment key={channel.id}>
278
+ {renderChannelCell(Object.freeze({
279
+ channel: deepFreeze({ ...channel, dependentChannels: channel.dependentChannels.map(d => ({ ...d })) }),
280
+ isLast: index === visible.length - 1,
281
+ isEnabled: part.isEnabled.get(channel.id) ?? false,
282
+ // Native checks the custom-UI permission again when this runs.
283
+ togglePlayback: () => togglePlayback(channel.id, 'custom'),
284
+ }))}
285
+ </Fragment>
286
+ ))}
287
+ {showBranding && <BrandingView isDarkMode={dark} />}
288
+ </View>
289
+ );
290
+ }
291
+
292
+ const divider = rowDivider(dark);
293
+ // The event's colours, for every key; LiveVoice orange without branding.
294
+ const brandColor = part.brandColors === null ? null : dark ? part.brandColors.dark : part.brandColors.light;
167
295
  return (
168
- <ReconnectingView
169
- state={uiState}
170
- onPress={() => {
171
- reconnect();
172
- }}
173
- />
296
+ <View style={{ width: '100%' }}>
297
+ <View style={{ backgroundColor: dark ? COLORS.cardBgDark : COLORS.cardBg, borderRadius: 8, overflow: 'hidden' }}>
298
+ {visible.map((channel, index) => {
299
+ const subtitlesOpen = part.subtitlesOpen.get(channel.id) ?? false;
300
+ return (
301
+ <View key={channel.id}>
302
+ <ChannelView
303
+ playingState={channel.playingState}
304
+ isDarkMode={dark}
305
+ isDimmed={part.inReconnectGrace}
306
+ brandColor={brandColor}
307
+ isOnline={channel.isOnline}
308
+ hasAudio={channel.hasAudio}
309
+ hasSubtitles={channel.hasSubtitles}
310
+ status={part.status.get(channel.id) ?? null}
311
+ muteMark={part.muteMark.get(channel.id) ?? null}
312
+ isPasswordProtected={channel.isPasswordProtected}
313
+ channelName={channel.name}
314
+ subtitlesOpen={subtitlesOpen}
315
+ accessibility={part.accessibility.get(channel.id) ?? null}
316
+ onAudioPress={() => perform(togglePlayback(channel.id, 'default'), 'cell', channel.id)}
317
+ // The subtitle half toggles this channel's subtitles; play never touches them.
318
+ onSubtitlesPress={() => perform(
319
+ subtitlesOpen ? stopSubtitles() : startSubtitles({ channelId: channel.id }), 'subtitles', channel.id,
320
+ )}
321
+ />
322
+ {index < visible.length - 1 && (
323
+ <View style={divider} />
324
+ )}
325
+ </View>
326
+ );
327
+ })}
328
+ {showBranding && <BrandingView isDarkMode={dark} />}
329
+ </View>
330
+ </View>
174
331
  );
175
- default:
176
- return <View />;
332
+ }
177
333
  }
178
334
  };
179
335
 
180
- export default LiveVoiceView;
336
+ /**
337
+ * The line between default cells, inset 12 at both sides: iOS SwiftUI `Divider()`, a hairline in
338
+ * the system separator colour (`LiveVoiceView.swift`); Android `HorizontalDivider`, 1 dp in
339
+ * `onBackgroundInactiveLight` (`DefaultChannel.kt`).
340
+ */
341
+ function rowDivider(dark: boolean) {
342
+ return Platform.OS === 'ios'
343
+ ? {
344
+ height: StyleSheet.hairlineWidth,
345
+ backgroundColor: dark ? 'rgba(84, 84, 88, 0.6)' : 'rgba(60, 60, 67, 0.29)',
346
+ marginHorizontal: 12,
347
+ }
348
+ : { height: 1, backgroundColor: dark ? '#495057' : '#E7E7E7', marginHorizontal: 12 };
349
+ }
350
+
351
+ const DefaultLoading: React.FC<{ dark: boolean }> = ({ dark }) => (
352
+ <View
353
+ style={{
354
+ width: '100%',
355
+ padding: 10,
356
+ borderRadius: 8,
357
+ justifyContent: 'center',
358
+ alignItems: 'center',
359
+ backgroundColor: dark ? COLORS.cardBgDark : COLORS.cardBg,
360
+ }}
361
+ >
362
+ {/* Neutral, like the native SDKs: the brand colour is for the play buttons. */}
363
+ <View style={{ opacity: 0.5 }}>
364
+ <LiveVoiceSpinner size={36} color={dark ? COLORS.white : COLORS.black} />
365
+ </View>
366
+ </View>
367
+ );
368
+
369
+ /** The loading part next to a host's custom parts: a spinner without the default card. */
370
+ const NeutralLoading: React.FC<{ dark: boolean }> = ({ dark }) => (
371
+ <View style={{ width: '100%', padding: 10, alignItems: 'center', opacity: 0.5 }}>
372
+ <LiveVoiceSpinner size={36} color={dark ? COLORS.white : COLORS.black} />
373
+ </View>
374
+ );
375
+
376
+ const DefaultEmpty: React.FC<{ reason: LiveVoiceEmptyReason; dark: boolean }> = ({ reason, dark }) => (
377
+ <View style={{ paddingVertical: 30, paddingHorizontal: 16 }}>
378
+ <Text
379
+ style={{
380
+ fontSize: 14,
381
+ fontWeight: '500',
382
+ textAlign: 'center',
383
+ color: dark ? COLORS.white : COLORS.black,
384
+ }}
385
+ >
386
+ {reason === 'noChannels' ? Translations.noChannels() : Translations.sdkNoMatchingChannels()}
387
+ </Text>
388
+ </View>
389
+ );