livevoice-sdk-react-native 2.0.1 → 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 (207) hide show
  1. package/README.md +105 -66
  2. package/android/build.gradle +5 -4
  3. package/android/src/main/java/io/livevoice/react/LiveVoiceModule.kt +122 -188
  4. package/android/src/main/java/io/livevoice/react/__SDK.kt +2 -2
  5. package/ios/LiveVoiceRNBridge.swift +362 -0
  6. package/ios/LiveVoiceSdkReactNative.mm +187 -158
  7. package/ios/__SDK.h +1 -1
  8. package/ios/livevoice_spm_backport.rb +202 -0
  9. package/lib/module/components/BrandingView.js +58 -39
  10. package/lib/module/components/BrandingView.js.map +1 -1
  11. package/lib/module/components/ChannelView.js +355 -44
  12. package/lib/module/components/ChannelView.js.map +1 -1
  13. package/lib/module/components/ErrorView.js +5 -6
  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 +311 -133
  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 +180 -244
  22. package/lib/module/components/PlayButtonView.js.map +1 -1
  23. package/lib/module/components/ReconnectingView.js +31 -53
  24. package/lib/module/components/ReconnectingView.js.map +1 -1
  25. package/lib/module/icons/branding-ios.png +0 -0
  26. package/lib/module/icons/camera-off-android.png +0 -0
  27. package/lib/module/icons/camera-off-android@2x.png +0 -0
  28. package/lib/module/icons/camera-off-android@3x.png +0 -0
  29. package/lib/module/icons/cpu-android.png +0 -0
  30. package/lib/module/icons/cpu-android@2x.png +0 -0
  31. package/lib/module/icons/cpu-android@3x.png +0 -0
  32. package/lib/module/icons/cpu-ios.png +0 -0
  33. package/lib/module/icons/cpu-ios@2x.png +0 -0
  34. package/lib/module/icons/cpu-ios@3x.png +0 -0
  35. package/lib/module/icons/lock-android.png +0 -0
  36. package/lib/module/icons/lock-android@2x.png +0 -0
  37. package/lib/module/icons/lock-android@3x.png +0 -0
  38. package/lib/module/icons/lock-ios.png +0 -0
  39. package/lib/module/icons/lock-ios@2x.png +0 -0
  40. package/lib/module/icons/lock-ios@3x.png +0 -0
  41. package/lib/module/icons/mic-off-android.png +0 -0
  42. package/lib/module/icons/mic-off-android@2x.png +0 -0
  43. package/lib/module/icons/mic-off-android@3x.png +0 -0
  44. package/lib/module/icons/mic-slash-ios.png +0 -0
  45. package/lib/module/icons/mic-slash-ios@2x.png +0 -0
  46. package/lib/module/icons/mic-slash-ios@3x.png +0 -0
  47. package/lib/module/icons/video-slash-ios.png +0 -0
  48. package/lib/module/icons/video-slash-ios@2x.png +0 -0
  49. package/lib/module/icons/video-slash-ios@3x.png +0 -0
  50. package/lib/module/index.js +11 -6
  51. package/lib/module/index.js.map +1 -1
  52. package/lib/module/sdk/Bridge.js +236 -0
  53. package/lib/module/sdk/Bridge.js.map +1 -0
  54. package/lib/module/sdk/Event.js +160 -0
  55. package/lib/module/sdk/Event.js.map +1 -0
  56. package/lib/module/sdk/Lifecycle.js +331 -0
  57. package/lib/module/sdk/Lifecycle.js.map +1 -0
  58. package/lib/module/sdk/LiveVoiceSdk.js +262 -268
  59. package/lib/module/sdk/LiveVoiceSdk.js.map +1 -1
  60. package/lib/module/sdk/LiveVoiceState.js +208 -40
  61. package/lib/module/sdk/LiveVoiceState.js.map +1 -1
  62. package/lib/module/sdk/LogEntries.js +99 -0
  63. package/lib/module/sdk/LogEntries.js.map +1 -0
  64. package/lib/module/sdk/Models.js +2 -0
  65. package/lib/module/{util/UIState.js.map → sdk/Models.js.map} +1 -1
  66. package/lib/module/{util/PlayingState.js → sdk/NativeSdkVersions.js} +9 -8
  67. package/lib/module/sdk/NativeSdkVersions.js.map +1 -0
  68. package/lib/module/sdk/Observables.js +391 -0
  69. package/lib/module/sdk/Observables.js.map +1 -0
  70. package/lib/module/sdk/Passwords.js +298 -0
  71. package/lib/module/sdk/Passwords.js.map +1 -0
  72. package/lib/module/sdk/Playback.js +35 -0
  73. package/lib/module/sdk/Playback.js.map +1 -0
  74. package/lib/module/specs/NativeLiveVoiceSdk.js.map +1 -1
  75. package/lib/module/util/Colors.js +2 -1
  76. package/lib/module/util/Colors.js.map +1 -1
  77. package/lib/module/util/Translations.js +34 -11
  78. package/lib/module/util/Translations.js.map +1 -1
  79. package/lib/module/util/freeze.js +26 -0
  80. package/lib/module/util/freeze.js.map +1 -0
  81. package/lib/module/util/logger.js +79 -41
  82. package/lib/module/util/logger.js.map +1 -1
  83. package/lib/typescript/src/components/BrandingView.d.ts +4 -0
  84. package/lib/typescript/src/components/BrandingView.d.ts.map +1 -1
  85. package/lib/typescript/src/components/ChannelView.d.ts +29 -3
  86. package/lib/typescript/src/components/ChannelView.d.ts.map +1 -1
  87. package/lib/typescript/src/components/ErrorView.d.ts +5 -3
  88. package/lib/typescript/src/components/ErrorView.d.ts.map +1 -1
  89. package/lib/typescript/src/components/LiveVoiceSpinner.d.ts +16 -0
  90. package/lib/typescript/src/components/LiveVoiceSpinner.d.ts.map +1 -0
  91. package/lib/typescript/src/components/LiveVoiceView.d.ts +81 -21
  92. package/lib/typescript/src/components/LiveVoiceView.d.ts.map +1 -1
  93. package/lib/typescript/src/components/PasswordPrompt.d.ts +13 -0
  94. package/lib/typescript/src/components/PasswordPrompt.d.ts.map +1 -0
  95. package/lib/typescript/src/components/PlayButtonView.d.ts +12 -329
  96. package/lib/typescript/src/components/PlayButtonView.d.ts.map +1 -1
  97. package/lib/typescript/src/components/ReconnectingView.d.ts +6 -3
  98. package/lib/typescript/src/components/ReconnectingView.d.ts.map +1 -1
  99. package/lib/typescript/src/index.d.ts +15 -6
  100. package/lib/typescript/src/index.d.ts.map +1 -1
  101. package/lib/typescript/src/sdk/Bridge.d.ts +36 -0
  102. package/lib/typescript/src/sdk/Bridge.d.ts.map +1 -0
  103. package/lib/typescript/src/sdk/Event.d.ts +97 -0
  104. package/lib/typescript/src/sdk/Event.d.ts.map +1 -0
  105. package/lib/typescript/src/sdk/Lifecycle.d.ts +191 -0
  106. package/lib/typescript/src/sdk/Lifecycle.d.ts.map +1 -0
  107. package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts +195 -190
  108. package/lib/typescript/src/sdk/LiveVoiceSdk.d.ts.map +1 -1
  109. package/lib/typescript/src/sdk/LiveVoiceState.d.ts +77 -9
  110. package/lib/typescript/src/sdk/LiveVoiceState.d.ts.map +1 -1
  111. package/lib/typescript/src/sdk/LogEntries.d.ts +28 -0
  112. package/lib/typescript/src/sdk/LogEntries.d.ts.map +1 -0
  113. package/lib/typescript/src/sdk/Models.d.ts +114 -0
  114. package/lib/typescript/src/sdk/Models.d.ts.map +1 -0
  115. package/lib/typescript/src/sdk/NativeSdkVersions.d.ts +6 -0
  116. package/lib/typescript/src/sdk/NativeSdkVersions.d.ts.map +1 -0
  117. package/lib/typescript/src/sdk/Observables.d.ts +123 -0
  118. package/lib/typescript/src/sdk/Observables.d.ts.map +1 -0
  119. package/lib/typescript/src/sdk/Passwords.d.ts +72 -0
  120. package/lib/typescript/src/sdk/Passwords.d.ts.map +1 -0
  121. package/lib/typescript/src/sdk/Playback.d.ts +9 -0
  122. package/lib/typescript/src/sdk/Playback.d.ts.map +1 -0
  123. package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts +34 -31
  124. package/lib/typescript/src/specs/NativeLiveVoiceSdk.d.ts.map +1 -1
  125. package/lib/typescript/src/util/Colors.d.ts.map +1 -1
  126. package/lib/typescript/src/util/Translations.d.ts +30 -8
  127. package/lib/typescript/src/util/Translations.d.ts.map +1 -1
  128. package/lib/typescript/src/util/freeze.d.ts +6 -0
  129. package/lib/typescript/src/util/freeze.d.ts.map +1 -0
  130. package/lib/typescript/src/util/logger.d.ts +17 -7
  131. package/lib/typescript/src/util/logger.d.ts.map +1 -1
  132. package/livevoice-sdk-react-native.podspec +29 -4
  133. package/package.json +12 -9
  134. package/src/components/BrandingView.tsx +42 -55
  135. package/src/components/ChannelView.tsx +329 -38
  136. package/src/components/ErrorView.tsx +9 -12
  137. package/src/components/LiveVoiceSpinner.tsx +87 -0
  138. package/src/components/LiveVoiceView.tsx +319 -201
  139. package/src/components/PasswordPrompt.tsx +144 -0
  140. package/src/components/PlayButtonView.tsx +165 -269
  141. package/src/components/ReconnectingView.tsx +28 -81
  142. package/src/icons/branding-ios.png +0 -0
  143. package/src/icons/camera-off-android.png +0 -0
  144. package/src/icons/camera-off-android@2x.png +0 -0
  145. package/src/icons/camera-off-android@3x.png +0 -0
  146. package/src/icons/cpu-android.png +0 -0
  147. package/src/icons/cpu-android@2x.png +0 -0
  148. package/src/icons/cpu-android@3x.png +0 -0
  149. package/src/icons/cpu-ios.png +0 -0
  150. package/src/icons/cpu-ios@2x.png +0 -0
  151. package/src/icons/cpu-ios@3x.png +0 -0
  152. package/src/icons/lock-android.png +0 -0
  153. package/src/icons/lock-android@2x.png +0 -0
  154. package/src/icons/lock-android@3x.png +0 -0
  155. package/src/icons/lock-ios.png +0 -0
  156. package/src/icons/lock-ios@2x.png +0 -0
  157. package/src/icons/lock-ios@3x.png +0 -0
  158. package/src/icons/mic-off-android.png +0 -0
  159. package/src/icons/mic-off-android@2x.png +0 -0
  160. package/src/icons/mic-off-android@3x.png +0 -0
  161. package/src/icons/mic-slash-ios.png +0 -0
  162. package/src/icons/mic-slash-ios@2x.png +0 -0
  163. package/src/icons/mic-slash-ios@3x.png +0 -0
  164. package/src/icons/video-slash-ios.png +0 -0
  165. package/src/icons/video-slash-ios@2x.png +0 -0
  166. package/src/icons/video-slash-ios@3x.png +0 -0
  167. package/src/index.tsx +92 -6
  168. package/src/sdk/Bridge.ts +259 -0
  169. package/src/sdk/Event.ts +187 -0
  170. package/src/sdk/Lifecycle.ts +366 -0
  171. package/src/sdk/LiveVoiceSdk.ts +285 -309
  172. package/src/sdk/LiveVoiceState.ts +230 -60
  173. package/src/sdk/LogEntries.ts +88 -0
  174. package/src/sdk/Models.ts +129 -0
  175. package/src/{util/PlayingState.ts → sdk/NativeSdkVersions.ts} +7 -6
  176. package/src/sdk/Observables.ts +414 -0
  177. package/src/sdk/Passwords.ts +286 -0
  178. package/src/sdk/Playback.ts +30 -0
  179. package/src/specs/NativeLiveVoiceSdk.ts +42 -38
  180. package/src/util/Colors.ts +2 -1
  181. package/src/util/Translations.ts +34 -12
  182. package/src/util/freeze.ts +23 -0
  183. package/src/util/logger.ts +79 -52
  184. package/lib/module/components/AudioOutputSwitchView.js +0 -138
  185. package/lib/module/components/AudioOutputSwitchView.js.map +0 -1
  186. package/lib/module/components/index.js +0 -24
  187. package/lib/module/components/index.js.map +0 -1
  188. package/lib/module/icons/mic.png +0 -0
  189. package/lib/module/util/ForegroundServiceConfig.js +0 -38
  190. package/lib/module/util/ForegroundServiceConfig.js.map +0 -1
  191. package/lib/module/util/PlayingState.js.map +0 -1
  192. package/lib/module/util/UIState.js +0 -4
  193. package/lib/typescript/src/components/AudioOutputSwitchView.d.ts +0 -6
  194. package/lib/typescript/src/components/AudioOutputSwitchView.d.ts.map +0 -1
  195. package/lib/typescript/src/components/index.d.ts +0 -9
  196. package/lib/typescript/src/components/index.d.ts.map +0 -1
  197. package/lib/typescript/src/util/ForegroundServiceConfig.d.ts +0 -19
  198. package/lib/typescript/src/util/ForegroundServiceConfig.d.ts.map +0 -1
  199. package/lib/typescript/src/util/PlayingState.d.ts +0 -7
  200. package/lib/typescript/src/util/PlayingState.d.ts.map +0 -1
  201. package/lib/typescript/src/util/UIState.d.ts +0 -86
  202. package/lib/typescript/src/util/UIState.d.ts.map +0 -1
  203. package/src/components/AudioOutputSwitchView.tsx +0 -146
  204. package/src/components/index.tsx +0 -21
  205. package/src/icons/mic.png +0 -0
  206. package/src/util/ForegroundServiceConfig.tsx +0 -42
  207. package/src/util/UIState.ts +0 -106
@@ -11,261 +11,379 @@
11
11
  //
12
12
  //===----------------------------------------------------------------------===//
13
13
 
14
+ import { Fragment, useEffect, useState } from 'react';
15
+ import { Platform, StyleSheet, Text, View, useColorScheme } from 'react-native';
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';
14
22
  import {
15
- startChannel,
16
- stopChannel,
17
- reconnect,
18
- startSubtitles,
19
- } from '../sdk/LiveVoiceSdk';
20
- import {
21
- type Claims,
22
- getCurrentUiState,
23
- onUiStateChanged,
24
- getCurrentClaims,
25
- } from '../sdk/LiveVoiceState';
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';
26
35
  import { BrandingView } from './BrandingView';
36
+ import { ChannelView } from './ChannelView';
27
37
  import { ErrorView } from './ErrorView';
38
+ import { LiveVoiceSpinner } from './LiveVoiceSpinner';
39
+ import { PasswordPrompt } from './PasswordPrompt';
28
40
  import { ReconnectingView } from './ReconnectingView';
29
41
 
30
- import { PlayingState } from "../util/PlayingState";
31
-
32
- import { Fragment, useEffect, useState } from "react";
33
- import { View, ActivityIndicator, Text, useColorScheme } from "react-native";
34
-
35
- import { COLORS } from "../util/Colors";
36
- import { Translations } from "../util/Translations";
37
- import { type Channel, type UiState } from "../util/UIState";
38
- import { ChannelView } from "./ChannelView";
39
-
40
- /** Per-cell helpers passed to {@link LiveVoiceViewProps.renderChannel}, mirroring the native SDKs. */
41
- export interface ChannelCellHelpers {
42
- /** Whether this is the last channel in the (filtered) list — e.g. to omit a trailing divider. */
43
- isLastInList: boolean;
44
- /** Toggle audio for this channel: starts it if stopped, stops it if playing. */
45
- onAudioPress: () => void;
46
- }
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
+ };
47
79
 
48
- /** Renders a custom channel cell. See {@link LiveVoiceViewProps.renderChannel}. */
49
- export type RenderChannel = (
50
- channel: Channel,
51
- helpers: ChannelCellHelpers,
52
- ) => React.ReactNode;
80
+ /** Why the list is empty: the event has no channels, or `filter` removed them all. */
81
+ export type LiveVoiceEmptyReason = 'noChannels' | 'noMatches';
53
82
 
54
- export interface LiveVoiceViewProps {
55
- isDarkMode?: boolean;
83
+ /**
84
+ * The props of {@link LiveVoiceView}. All are optional; without any, the view shows the default
85
+ * UI.
86
+ */
87
+ export type LiveVoiceViewProps = {
56
88
  /**
57
- * Return true for channels that should be displayed by the drop-in view.
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.
58
93
  */
59
- filter?: (channel: Channel) => boolean;
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;
60
99
  /**
61
- * Render a custom channel cell instead of the default one.
100
+ * Draws a channel instead of the default cell.
62
101
  *
63
- * Requires an API key that permits custom UI. If a `renderChannel` is provided but the joined
64
- * event's key doesn't allow custom UI, the view shows the invalid-api-key error instead — the
65
- * same gating the native SDKs apply. When omitted, the default cell is used.
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.
66
104
  */
67
- renderChannel?: RenderChannel;
68
- }
69
-
70
- function useLiveVoiceViewState(): {
71
- claims: Claims | null;
72
- uiState: UiState;
73
- } {
74
- const [uiState, setUiState] = useState<UiState>(() => getCurrentUiState());
75
- const [claims, setClaims] = useState<Claims | null>(() =>
76
- getCurrentUiState().type === 'ready' ? getCurrentClaims() : null,
77
- );
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
+ };
78
122
 
123
+ function useViewModel(): ViewModel {
124
+ const [model, setModel] = useState<ViewModel>(() => getCurrentViewModel());
79
125
  useEffect(() => {
80
- // Claims belong to the current `ready` session. Read them fresh on every
81
- // `ready` transition and clear them otherwise, so a stale value (e.g.
82
- // `customUiAllowed` from a previously joined API key) can never outlive the
83
- // session that produced it and keep the gate open after switching keys.
84
- const apply = (nextUiState: UiState) => {
85
- setUiState(nextUiState);
86
- setClaims(nextUiState.type === 'ready' ? getCurrentClaims() : null);
87
- };
88
-
89
- const subscription = onUiStateChanged(apply);
90
-
91
- apply(getCurrentUiState());
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();
131
+ }, []);
132
+ return model;
133
+ }
92
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);
142
+ useEffect(() => {
143
+ const id = registerPresenter();
144
+ setPresenter(id);
145
+ const update = () => setRequest(promptFor(id));
146
+ const stop = observePrompt(update);
147
+ update();
93
148
  return () => {
94
- subscription.remove();
149
+ stop();
150
+ // A prompt this view showed gives up with the password error.
151
+ unregisterPresenter(id);
95
152
  };
96
153
  }, []);
97
-
98
- return { uiState, claims };
154
+ return { request, presenter };
99
155
  }
100
156
 
101
- export const LiveVoiceView: React.FC<LiveVoiceViewProps> = ({
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
+ };
179
+
180
+ const LiveVoiceParts: React.FC<LiveVoiceViewProps> = ({
102
181
  isDarkMode,
182
+ onCommandError,
103
183
  filter,
104
- renderChannel,
184
+ renderChannelCell,
185
+ renderLoading,
186
+ renderReconnecting,
187
+ renderError,
188
+ renderEmpty,
105
189
  }) => {
106
- const systemDarkMode = useColorScheme() === "dark";
190
+ const systemDarkMode = useColorScheme() === 'dark';
107
191
  const dark = isDarkMode ?? systemDarkMode;
108
- const { uiState, claims } = useLiveVoiceViewState();
192
+ const { part, features } = useViewModel();
109
193
 
110
- switch (uiState.type) {
111
- case "ready": {
112
- const backgroundColor = dark ? COLORS.cardBgDark : COLORS.cardBg;
113
- const dividerColor = dark ? COLORS.dividerDark : COLORS.divider;
114
- const visibleChannels =
115
- filter == null ? uiState.channels : uiState.channels.filter(filter);
116
- const customUiAllowed = claims?.customUiAllowed ?? false;
117
-
118
- // A custom cell was provided but the joined event's key doesn't permit custom UI.
119
- // Mirror the native SDKs and surface the invalid-api-key error instead of rendering it.
120
- if (renderChannel != null && !customUiAllowed) {
121
- return (
122
- <ErrorView
123
- state={{
124
- type: "error",
125
- message: Translations.sdkErrorInvalidApiKey(),
126
- canBeRetried: false,
127
- }}
128
- isDarkMode={dark}
129
- onPress={() => {}}
130
- />
131
- );
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
+ });
132
207
  }
208
+ });
209
+ };
133
210
 
134
- const showBranding = claims === null || !claims.hideBranding;
135
- const emptyText =
136
- uiState.channels.length === 0 || filter == null
137
- ? Translations.noChannels()
138
- : Translations.sdkNoMatchingChannels();
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
+ }
139
231
 
140
- const emptyView = (
141
- <View style={{ paddingVertical: 30, paddingHorizontal: 16 }}>
142
- <Text
143
- style={{
144
- fontSize: 14,
145
- fontWeight: "500",
146
- textAlign: "center",
147
- color: dark ? COLORS.white : COLORS.black,
148
- }}
149
- >
150
- {emptyText}
151
- </Text>
152
- </View>
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} />;
237
+
238
+ case 'reconnecting':
239
+ if (typeof renderReconnecting === 'function') return <>{renderReconnecting(deepFreeze(publicRetry(part.retry)))}</>;
240
+ return <ReconnectingView retry={part.retry} isDarkMode={dark} />;
241
+
242
+ case 'error': {
243
+ const { error } = part;
244
+ if (typeof renderError === 'function') {
245
+ return <>{renderError(error, error.isRetryable ? () => reconnect() : null)}</>;
246
+ }
247
+ return (
248
+ <ErrorView
249
+ message={error.message}
250
+ onRetry={error.isRetryable ? () => perform(reconnect(), 'retry') : null}
251
+ isDarkMode={dark}
252
+ />
153
253
  );
254
+ }
154
255
 
155
- // Custom cells get full control (no card chrome / dividers), like the native SDKs.
156
- if (renderChannel != null) {
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)}</>;
157
262
  return (
158
- <View style={{ width: "100%" }}>
159
- {visibleChannels.length === 0
160
- ? emptyView
161
- : visibleChannels.map((channel: Channel, index: number) => (
162
- <Fragment key={channel.id}>
163
- {renderChannel(channel, {
164
- isLastInList: index === visibleChannels.length - 1,
165
- onAudioPress: () =>
166
- channel.playingState === PlayingState.PLAYING
167
- ? stopChannel()
168
- : startChannel(channel.id),
169
- })}
170
- </Fragment>
171
- ))}
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
+ }
271
+
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
+ ))}
172
287
  {showBranding && <BrandingView isDarkMode={dark} />}
173
288
  </View>
174
289
  );
175
290
  }
176
291
 
177
- // Default cells.
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;
178
295
  return (
179
- <View style={{ width: "100%" }}>
180
- <View
181
- style={{
182
- backgroundColor: backgroundColor,
183
- borderRadius: 8,
184
- overflow: "hidden",
185
- }}
186
- >
187
- {visibleChannels.length === 0 ? (
188
- emptyView
189
- ) : (
190
- visibleChannels.map((channel: Channel, index: number) => (
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 (
191
301
  <View key={channel.id}>
192
302
  <ChannelView
193
303
  playingState={channel.playingState}
194
304
  isDarkMode={dark}
195
- muted={channel.isAudioMuted}
305
+ isDimmed={part.inReconnectGrace}
306
+ brandColor={brandColor}
196
307
  isOnline={channel.isOnline}
197
308
  hasAudio={channel.hasAudio}
198
309
  hasSubtitles={channel.hasSubtitles}
310
+ status={part.status.get(channel.id) ?? null}
311
+ muteMark={part.muteMark.get(channel.id) ?? null}
312
+ isPasswordProtected={channel.isPasswordProtected}
199
313
  channelName={channel.name}
200
- onAudioPress={() =>
201
- channel.playingState === PlayingState.PLAYING
202
- ? stopChannel()
203
- : startChannel(channel.id)
204
- }
205
- onSubtitlesPress={() => startSubtitles(channel.id)}
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
+ )}
206
321
  />
207
- {index < visibleChannels.length - 1 && (
208
- <View
209
- style={{
210
- height: 1,
211
- backgroundColor: dividerColor,
212
- marginHorizontal: 10,
213
- }}
214
- />
322
+ {index < visible.length - 1 && (
323
+ <View style={divider} />
215
324
  )}
216
325
  </View>
217
- ))
218
- )}
326
+ );
327
+ })}
219
328
  {showBranding && <BrandingView isDarkMode={dark} />}
220
329
  </View>
221
330
  </View>
222
331
  );
223
332
  }
333
+ }
334
+ };
224
335
 
225
- case "loading":
226
- const loadingBackgroundColor = dark
227
- ? COLORS.cardBgDark
228
- : COLORS.cardBg;
229
-
230
- return (
231
- <View
232
- style={{
233
- width: "100%",
234
- padding: 10,
235
- borderRadius: 8,
236
- justifyContent: "center",
237
- alignItems: "center",
238
- backgroundColor: loadingBackgroundColor,
239
- }}
240
- >
241
- <ActivityIndicator size={40} color={COLORS.primary} />
242
- </View>
243
- );
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
+ }
244
350
 
245
- case "error":
246
- return (
247
- <ErrorView
248
- state={uiState}
249
- isDarkMode={dark}
250
- onPress={() => {
251
- reconnect();
252
- }}
253
- />
254
- );
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
+ );
255
368
 
256
- case "reconnecting":
257
- return (
258
- <ReconnectingView
259
- state={uiState}
260
- isDarkMode={dark}
261
- onPress={() => {
262
- reconnect();
263
- }}
264
- />
265
- );
266
- default:
267
- return <View />;
268
- }
269
- };
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
+ );
270
375
 
271
- export default LiveVoiceView;
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
+ );