Skip to content

Commit 4c8c8da

Browse files
authored
Merge branch 'main' into t3/inline-audio-agent-files
2 parents d096795 + 7bafc40 commit 4c8c8da

41 files changed

Lines changed: 4487 additions & 306 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public final class T3NativeControlsModule: Module {
1010
private var filePresentation: T3NativeFilePresentation?
1111

1212
public func definition() -> ModuleDefinition {
13+
Events("videoPlaybackChange")
1314
Constants {
1415
if #available(iOS 26.0, *) {
1516
return [
@@ -247,10 +248,16 @@ public final class T3NativeControlsModule: Module {
247248
userInfo: [NSLocalizedDescriptionKey: "The video preview is no longer available."]
248249
)
249250
}
250-
let presentation = T3NativeVideoPresentation(identifier: identifier, url: url, title: title) { [weak self] error in
251-
self?.videoPresentation = nil
252-
if let error { promise.reject(error) } else { promise.resolve(nil) }
253-
}
251+
let presentation = T3NativeVideoPresentation(
252+
identifier: identifier, url: url, title: title,
253+
onPlaybackChange: { [weak self] isPlaying in
254+
self?.sendEvent("videoPlaybackChange", ["identifier": identifier, "isPlaying": isPlaying])
255+
},
256+
completion: { [weak self] error in
257+
self?.videoPresentation = nil
258+
if let error { promise.reject(error) } else { promise.resolve(nil) }
259+
}
260+
)
254261
videoPresentation = presentation
255262
presentation.present(from: presenter, sources: presentationSources, sourceIdentifier: sourceIdentifier)
256263
}

‎apps/mobile/modules/t3-native-controls/ios/T3NativeVideoPresentation.swift‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ final class T3NativeVideoPresentation: NSObject, AVPlayerViewControllerDelegate,
77
private let controller = AVPlayerViewController()
88
private let completion: (Error?) -> Void
99
private var itemObservation: NSKeyValueObservation?
10+
private var playbackObservation: NSKeyValueObservation?
1011
private var backgroundObserver: NSObjectProtocol?
1112
private var playbackError: Error?
1213
private var presented = false
@@ -27,7 +28,8 @@ final class T3NativeVideoPresentation: NSObject, AVPlayerViewControllerDelegate,
2728
private weak var fullScreenController: UIViewController?
2829
private var embedded = false
2930

30-
init(identifier: String, url: URL, title: String, completion: @escaping (Error?) -> Void) {
31+
init(identifier: String, url: URL, title: String,
32+
onPlaybackChange: @escaping (Bool) -> Void, completion: @escaping (Error?) -> Void) {
3133
self.identifier = identifier
3234
self.completion = completion
3335
super.init()
@@ -38,6 +40,12 @@ final class T3NativeVideoPresentation: NSObject, AVPlayerViewControllerDelegate,
3840
metadata.value = title as NSString
3941
item.externalMetadata = [metadata]
4042
controller.player = AVPlayer(playerItem: item)
43+
playbackObservation = controller.player?.observe(\.timeControlStatus, options: [.new]) { player, _ in
44+
// Waiting keeps the playback request alive through buffering and seeks.
45+
if player.timeControlStatus != .waitingToPlayAtSpecifiedRate {
46+
onPlaybackChange(player.timeControlStatus == .playing)
47+
}
48+
}
4149
controller.delegate = self
4250
controller.overrideUserInterfaceStyle = .dark
4351
controller.allowsPictureInPicturePlayback = false
@@ -148,6 +156,7 @@ final class T3NativeVideoPresentation: NSObject, AVPlayerViewControllerDelegate,
148156
controller.removeFromParent()
149157
}
150158
itemObservation = nil
159+
playbackObservation = nil
151160
controller.player = nil
152161
if let backgroundObserver { NotificationCenter.default.removeObserver(backgroundObserver) }
153162
backgroundObserver = nil

‎apps/mobile/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
"expo-sqlite": "~58.0.13",
120120
"expo-symbols": "~58.0.4",
121121
"expo-updates": "~58.0.17",
122-
"expo-video": "~58.0.11",
122+
"expo-video": "58.0.11",
123123
"expo-web-browser": "~58.0.6",
124124
"expo-widgets": "58.0.20",
125125
"react": "catalog:",
@@ -179,6 +179,7 @@
179179
"react-native-reanimated",
180180
"@react-native-menu/menu",
181181
"expo-audio",
182+
"expo-video",
182183
"expo-network"
183184
]
184185
}

‎apps/mobile/src/components/FilePreviewModal.tsx‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import {
1212
import { Keyboard } from "react-native";
1313

1414
import { loadLocalAttachmentPreview } from "../lib/localAttachmentPreview";
15+
import {
16+
downloadAndShareAttachment,
17+
nativeViewerErrorMessage,
18+
shareLocalAttachment,
19+
} from "../lib/attachmentDownload";
1520
import { useRefreshAssetUrl } from "../state/assets";
1621
import { filePreviewGallery, filePreviewKey, filePreviewName } from "../lib/filePreviewGallery";
1722
import { FilePreviewGallery } from "./FilePreviewGallery";
@@ -122,6 +127,32 @@ function ResolvedFilePreview(props: {
122127
[items, resolved],
123128
);
124129
const ready = selected && gallery.length === items.length;
130+
const onOpenError = (error: unknown) => {
131+
if (props.onOpenError) return props.onOpenError(error);
132+
Dialog.alert("Could not open preview", nativeViewerErrorMessage(error), [
133+
{ text: "Cancel", style: "cancel" },
134+
...(selected?.kind === "document"
135+
? [
136+
{
137+
text: "Save or share",
138+
onPress: () => {
139+
const input = {
140+
attachment: {
141+
name: filePreviewName(selected),
142+
mimeType: selected.mimeType ?? "application/octet-stream",
143+
},
144+
signal: new AbortController().signal,
145+
};
146+
const sharing = /^(file|content):/.test(selected.uri)
147+
? shareLocalAttachment({ ...input, uri: selected.uri })
148+
: downloadAndShareAttachment({ ...input, url: selected.uri });
149+
void sharing.catch(() => Dialog.alert("Could not share file", "Please try again."));
150+
},
151+
},
152+
]
153+
: []),
154+
]);
155+
};
125156
return (
126157
<>
127158
{items.map((item) => (
@@ -137,7 +168,7 @@ function ResolvedFilePreview(props: {
137168
<FilePreview
138169
source={{ ...selected, gallery }}
139170
onRequestClose={props.onRequestClose}
140-
onOpenError={props.onOpenError}
171+
onOpenError={onOpenError}
141172
/>
142173
) : null}
143174
</>

‎apps/mobile/src/components/MediaVideoPlayer.tsx‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useIsFocused } from "@react-navigation/native";
22
import { useEvent } from "expo";
33
import { useVideoPlayer, VideoView } from "expo-video";
4-
import { useEffect, useEffectEvent, useRef, useState } from "react";
4+
import { useEffect, useEffectEvent, useMemo, useRef, useState } from "react";
55
import { AppState, Pressable, View } from "react-native";
66

77
import { AppActivityIndicator } from "./ui/AppActivityIndicator";
@@ -12,12 +12,14 @@ import { useMediaActions } from "../state/mediaActions";
1212
import { type MediaActionsSource } from "../lib/mediaActionsSource";
1313
import { useFullscreenVideoOpen, useMediaAudioSessionHold } from "../lib/videoAudioSession";
1414
import { MediaActionsMenu } from "./MediaActionsMenu";
15+
import { createAudioPlaybackSession } from "../lib/audioSession";
1516

1617
function LoadedMediaVideo(props: {
1718
readonly uri: string;
1819
readonly resolvePlaybackUri?: () => Promise<string | null>;
1920
readonly playRequested: boolean;
2021
readonly paused: boolean;
22+
readonly onPlaybackStart?: () => void;
2123
}) {
2224
const focused = useIsFocused();
2325
const fullscreenVideoOpen = useFullscreenVideoOpen();
@@ -31,28 +33,61 @@ function LoadedMediaVideo(props: {
3133
player.staysActiveInBackground = false;
3234
player.bufferOptions = { preferredForwardBufferDuration: 5 };
3335
});
36+
const playback = useMemo(() => createAudioPlaybackSession(player, true), [player]);
37+
const previousPlaybackRequested = useRef(false);
38+
const onPlaybackStart = useEffectEvent(() => props.onPlaybackStart?.());
39+
useEffect(() => {
40+
const subscription = player.addListener("playingChange", ({ isPlaying }) => {
41+
if (isPlaying && !active.current && !fullscreen.current) playback.pause();
42+
else playback.playingChanged(isPlaying);
43+
});
44+
const requested = player.addListener("playbackRequestedChange", ({ isPlaybackRequested }) => {
45+
const wasPlaybackRequested = previousPlaybackRequested.current;
46+
previousPlaybackRequested.current = isPlaybackRequested;
47+
if (!isPlaybackRequested) {
48+
if (playback.requested) playback.pause();
49+
} else if (!active.current && !fullscreen.current) playback.pause();
50+
else {
51+
if (!wasPlaybackRequested) onPlaybackStart();
52+
playback.playingChanged(true);
53+
}
54+
});
55+
const ended = player.addListener("playToEnd", () => playback.pause());
56+
const failed = player.addListener("statusChange", ({ status }) => {
57+
if (status === "error") playback.pause();
58+
});
59+
return () => {
60+
subscription.remove();
61+
requested.remove();
62+
ended.remove();
63+
failed.remove();
64+
playback.pause();
65+
previousPlaybackRequested.current = false;
66+
};
67+
}, [player, playback]);
3468
const { status } = useEvent(player, "statusChange", { status: player.status });
3569
const { isPlaying } = useEvent(player, "playingChange", { isPlaying: player.playing });
3670
useMediaAudioSessionHold(isPlaying);
3771
const loadSource = useEffectEvent(async (signal: AbortSignal) => {
3872
const uri = props.resolvePlaybackUri ? await props.resolvePlaybackUri() : props.uri;
3973
if (signal.aborted) return;
4074
if (uri === null) throw new Error("Video unavailable");
41-
player.pause();
75+
playback.pause();
76+
previousPlaybackRequested.current = false;
4277
await player.replaceAsync({ uri, contentType: "progressive" });
43-
if (!signal.aborted && props.playRequested && active.current) player.play();
78+
if (!signal.aborted && props.playRequested && active.current) await playback.play();
4479
});
4580

4681
useEffect(() => {
4782
active.current = focused && !paused && AppState.currentState === "active";
48-
if (!focused || paused || (!active.current && !fullscreen.current)) player.pause();
83+
if (!focused || paused || (!active.current && !fullscreen.current)) playback.pause();
4984
// RN reports both leaving the app and Android fullscreen as background; native handling distinguishes them.
5085
const subscription = AppState.addEventListener("change", (state) => {
5186
active.current = focused && !paused && state === "active";
52-
if (state === "inactive" || (state === "background" && !fullscreen.current)) player.pause();
87+
if (state === "inactive" || (state === "background" && !fullscreen.current)) playback.pause();
5388
});
5489
return () => subscription.remove();
55-
}, [focused, player, paused]);
90+
}, [focused, playback, paused]);
5691

5792
useEffect(() => {
5893
const controller = new AbortController();
@@ -117,6 +152,7 @@ interface MediaVideoPlayerProps {
117152
readonly expanded?: boolean;
118153
readonly autoPlay?: boolean;
119154
readonly paused?: boolean;
155+
readonly onPlaybackStart?: () => void;
120156
readonly actionsSource?: MediaActionsSource;
121157
}
122158

@@ -138,6 +174,7 @@ function MediaVideoPlayerContent(props: MediaVideoPlayerProps) {
138174
resolvePlaybackUri={props.resolvePlaybackUri}
139175
playRequested={!props.expanded || props.autoPlay === true}
140176
paused={(props.paused ?? false) || mediaActions.sharing}
177+
onPlaybackStart={props.onPlaybackStart}
141178
/>
142179
) : (
143180
<MediaActionsMenu media={mediaActions} inModal={props.expanded} style={{ flex: 1 }}>
@@ -150,7 +187,10 @@ function MediaVideoPlayerContent(props: MediaVideoPlayerProps) {
150187
accessibilityState={{ disabled: props.uri === null || props.unavailable === true }}
151188
// Stays pressable so the long-press menu still opens on a failed or unsigned tile.
152189
onPress={() => {
153-
if (props.uri !== null && !props.unavailable) setPlaybackUri(props.uri);
190+
if (props.uri !== null && !props.unavailable) {
191+
props.onPlaybackStart?.();
192+
setPlaybackUri(props.uri);
193+
}
154194
}}
155195
className="flex-1 items-center justify-center px-4"
156196
>

‎apps/mobile/src/components/VideoPreviewModal.ios.tsx‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import { fullscreenVideoClosed, fullscreenVideoOpened } from "../lib/videoAudioS
1111
import { mediaVideoPreviewUri, type VideoPreviewSource } from "../lib/videoPreviewSource";
1212
import { useAssetUrlState, useRefreshAssetUrl } from "../state/assets";
1313
import { usePreparedConnection } from "../state/session";
14+
import {
15+
acquireAudioSession,
16+
activateAudioSession,
17+
isAudioSessionOwner,
18+
releaseAudioSession,
19+
} from "../lib/audioSession";
1420

1521
export type { VideoPreviewSource } from "../lib/videoPreviewSource";
1622

@@ -22,6 +28,10 @@ const NativeControls = requireNativeModule<{
2228
identifier: string,
2329
): Promise<void>;
2430
dismissVideo(identifier: string): Promise<void>;
31+
addListener(
32+
event: "videoPlaybackChange",
33+
listener: (event: { identifier: string; isPlaying: boolean }) => void,
34+
): { remove(): void };
2535
}>("T3NativeControls");
2636

2737
function NativeVideoPreview(props: {
@@ -71,6 +81,29 @@ function NativeVideoPreview(props: {
7181
useEffect(() => {
7282
if (localAttachment === null && playbackUrl === null) return;
7383
const controller = new AbortController();
84+
let token: ReturnType<typeof acquireAudioSession> | null = null;
85+
const release = () => {
86+
if (token) void releaseAudioSession(token).catch(() => {});
87+
token = null;
88+
};
89+
const acquire = () => {
90+
const next = acquireAudioSession("playback", () => {
91+
release();
92+
void NativeControls.dismissVideo(identifier).catch(() => {});
93+
});
94+
if (!isAudioSessionOwner(next)) {
95+
void NativeControls.dismissVideo(identifier).catch(() => {});
96+
return null;
97+
}
98+
token = next;
99+
return next;
100+
};
101+
const subscription = NativeControls.addListener("videoPlaybackChange", (event) => {
102+
if (event.identifier !== identifier || controller.signal.aborted) return;
103+
if (event.isPlaying) {
104+
if (!token) acquire();
105+
} else release();
106+
});
74107
let ready = false;
75108
void (async () => {
76109
const file =
@@ -82,6 +115,13 @@ function NativeVideoPreview(props: {
82115
file?.dispose();
83116
return;
84117
}
118+
const current = acquire();
119+
if (!current || !(await activateAudioSession(current, true)) || controller.signal.aborted) {
120+
release();
121+
file?.dispose();
122+
if (!controller.signal.aborted) onRequestClose();
123+
return;
124+
}
85125
ready = true;
86126
const presented = await settlePromise(() =>
87127
NativeControls.presentVideo(
@@ -93,9 +133,11 @@ function NativeVideoPreview(props: {
93133
);
94134
// Native completion follows dismissal, so local playback keeps its file lease.
95135
file?.dispose();
136+
release();
96137
if (AsyncResult.isFailure(presented)) throw squashAtomCommandFailure(presented);
97138
if (!controller.signal.aborted) onRequestClose();
98139
})().catch((error: unknown) => {
140+
release();
99141
if (controller.signal.aborted) return;
100142
// AVKit gives no retry, so re-mint now; the cached URL may simply have expired.
101143
if (ready) void refreshAssetUrl();
@@ -111,6 +153,8 @@ function NativeVideoPreview(props: {
111153
});
112154
return () => {
113155
controller.abort();
156+
subscription.remove();
157+
release();
114158
void NativeControls.dismissVideo(identifier).catch(() => undefined);
115159
};
116160
}, [localAttachment, name, source.sourceIdentifier, playbackUrl, identifier]);

‎apps/mobile/src/components/ui/AppSymbol.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ import IconPlug from "@tabler/icons-react-native/IconPlug";
9999
import IconPhoto from "@tabler/icons-react-native/IconPhoto";
100100
import IconPin from "@tabler/icons-react-native/IconPin";
101101
import IconPinnedOff from "@tabler/icons-react-native/IconPinnedOff";
102+
import IconPlayerPause from "@tabler/icons-react-native/IconPlayerPause";
102103
import IconPlayerPlay from "@tabler/icons-react-native/IconPlayerPlay";
103104
import IconPlayerStopFilled from "@tabler/icons-react-native/IconPlayerStopFilled";
104105
import IconPlus from "@tabler/icons-react-native/IconPlus";
@@ -240,6 +241,7 @@ const ANDROID_ICON_BY_SF_SYMBOL = {
240241
pin: IconPin,
241242
"pin.slash": IconPinnedOff,
242243
play: IconPlayerPlay,
244+
pause: IconPlayerPause,
243245
plus: IconPlus,
244246
minus: IconMinus,
245247
"slash.circle": IconBan,

0 commit comments

Comments
 (0)