Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
feat(mobile): add record screen and upload status UI
Richie McIlroy2db989f5fe24
diff --git a/apps/mobile/app/record.tsx b/apps/mobile/app/record.tsxnew file mode 100644index 000000000..93cb640c5--- /dev/null+++ b/apps/mobile/app/record.tsx@@ -0,0 +1,1058 @@+import {+ type CameraType,+ useCameraPermissions,+ useMicrophonePermissions,+} from "expo-camera";+import * as Device from "expo-device";+import { router, Stack } from "expo-router";+import { SymbolView } from "expo-symbols";+import { useCallback, useEffect, useRef, useState } from "react";+import {+ ActionSheetIOS,+ ActivityIndicator,+ AppState,+ KeyboardAvoidingView,+ Linking,+ Modal,+ Pressable,+ ScrollView,+ StatusBar,+ StyleSheet,+ Text,+ TextInput,+ View,+} from "react-native";+import {+ SafeAreaView,+ useSafeAreaInsets,+} from "react-native-safe-area-context";+import { TeleprompterOverlay } from "@/recording/TeleprompterOverlay";+import {+ clamp,+ countWords,+ formatRecordingDuration,+ teleprompterDefaults,+ teleprompterLimits,+} from "@/recording/teleprompter";+import { colors, fonts, radius, squircle } from "@/theme";+import { useRecordingUploads } from "@/uploads/recording-upload-provider";+import CapRecorderView, {+ type CapRecorderErrorEvent,+ type CapRecorderSegmentEvent,+} from "../modules/cap-recorder";++type RecorderPhase = "ready" | "starting" | "recording" | "finishing";++const recordingVideoBitrate = 2_500_000;+const recordingSegmentDurationSeconds = 2;+const recordingMonthNames = [+ "Jan",+ "Feb",+ "Mar",+ "Apr",+ "May",+ "Jun",+ "Jul",+ "Aug",+ "Sep",+ "Oct",+ "Nov",+ "Dec",+];++const createRecordingFileName = (recordedAt: Date) => {+ const month = recordingMonthNames[recordedAt.getMonth()];+ const hours = String(recordedAt.getHours()).padStart(2, "0");+ const minutes = String(recordedAt.getMinutes()).padStart(2, "0");+ return `Cap Recording - ${recordedAt.getDate()} ${month} ${recordedAt.getFullYear()} at ${hours}.${minutes}.mp4`;+};++type CircleButtonProps = {+ accessibilityLabel: string;+ disabled?: boolean;+ onPress: () => void;+ symbol: "camera.rotate" | "text.alignleft" | "xmark";+};++function CircleButton({+ accessibilityLabel,+ disabled = false,+ onPress,+ symbol,+}: CircleButtonProps) {+ return (+ <Pressable+ accessibilityRole="button"+ accessibilityLabel={accessibilityLabel}+ accessibilityState={{ disabled }}+ disabled={disabled}+ hitSlop={8}+ onPress={onPress}+ style={({ pressed }) => [+ styles.circleButton,+ pressed ? styles.circleButtonPressed : null,+ disabled ? styles.circleButtonDisabled : null,+ ]}+ >+ <SymbolView+ name={symbol}+ size={18}+ tintColor={colors.white}+ weight="semibold"+ />+ </Pressable>+ );+}++type StepperRowProps = {+ label: string;+ value: string;+ onDecrease: () => void;+ onIncrease: () => void;+ decreaseDisabled: boolean;+ increaseDisabled: boolean;+};++function StepperRow({+ label,+ value,+ onDecrease,+ onIncrease,+ decreaseDisabled,+ increaseDisabled,+}: StepperRowProps) {+ return (+ <View style={styles.stepperRow}>+ <View style={styles.stepperCopy}>+ <Text style={styles.stepperLabel}>{label}</Text>+ <Text style={styles.stepperValue}>{value}</Text>+ </View>+ <View style={styles.stepperControls}>+ <Pressable+ accessibilityRole="button"+ accessibilityLabel={`Decrease ${label.toLowerCase()}`}+ accessibilityState={{ disabled: decreaseDisabled }}+ disabled={decreaseDisabled}+ onPress={onDecrease}+ style={({ pressed }) => [+ styles.stepperButton,+ pressed ? styles.stepperButtonPressed : null,+ decreaseDisabled ? styles.stepperButtonDisabled : null,+ ]}+ >+ <SymbolView+ name="minus"+ size={15}+ tintColor={colors.gray12}+ weight="semibold"+ />+ </Pressable>+ <Pressable+ accessibilityRole="button"+ accessibilityLabel={`Increase ${label.toLowerCase()}`}+ accessibilityState={{ disabled: increaseDisabled }}+ disabled={increaseDisabled}+ onPress={onIncrease}+ style={({ pressed }) => [+ styles.stepperButton,+ pressed ? styles.stepperButtonPressed : null,+ increaseDisabled ? styles.stepperButtonDisabled : null,+ ]}+ >+ <SymbolView+ name="plus"+ size={15}+ tintColor={colors.gray12}+ weight="semibold"+ />+ </Pressable>+ </View>+ </View>+ );+}++export default function RecordScreen() {+ const recordingUploads = useRecordingUploads();+ const insets = useSafeAreaInsets();+ const cameraRef = useRef<CapRecorderView>(null);+ const permissionRequestStarted = useRef(false);+ const discardRecording = useRef(false);+ const activeRecordingId = useRef<string | null>(null);+ const recordingStartedAt = useRef<number | null>(null);+ const [cameraPermission, requestCameraPermission, getCameraPermission] =+ useCameraPermissions();+ const [+ microphonePermission,+ requestMicrophonePermission,+ getMicrophonePermission,+ ] = useMicrophonePermissions();+ const [permissionRequestFinished, setPermissionRequestFinished] =+ useState(false);+ const [phase, setPhase] = useState<RecorderPhase>("ready");+ const [cameraReady, setCameraReady] = useState(false);+ const [facing, setFacing] = useState<CameraType>("front");+ const [elapsedSeconds, setElapsedSeconds] = useState(0);+ const [error, setError] = useState<string | null>(null);+ const [script, setScript] = useState("");+ const [fontSize, setFontSize] = useState<number>(+ teleprompterDefaults.fontSize,+ );+ const [wordsPerMinute, setWordsPerMinute] = useState<number>(+ teleprompterDefaults.wordsPerMinute,+ );+ const [teleprompterPlaying, setTeleprompterPlaying] = useState(false);+ const [teleprompterRestartKey, setTeleprompterRestartKey] = useState(0);+ const [editorVisible, setEditorVisible] = useState(false);+ const [draftScript, setDraftScript] = useState("");+ const [draftFontSize, setDraftFontSize] = useState(fontSize);+ const [draftWordsPerMinute, setDraftWordsPerMinute] =+ useState(wordsPerMinute);++ const permissionsGranted =+ cameraPermission?.granted === true &&+ microphonePermission?.granted === true;+ const isPhysicalDevice = Device.isDevice;+ const permissionStateLoaded =+ cameraPermission !== null && microphonePermission !== null;+ const cameraActive = permissionsGranted;+ const hasScript = script.trim().length > 0;+ const waitingForPermission =+ !permissionStateLoaded || !permissionRequestFinished;+ const requestPermissions = useCallback(async () => {+ setPermissionRequestFinished(false);+ const nextCameraPermission = cameraPermission?.granted+ ? cameraPermission+ : await requestCameraPermission();+ if (nextCameraPermission.granted) {+ if (!microphonePermission?.granted) {+ await requestMicrophonePermission();+ }+ }+ setPermissionRequestFinished(true);+ }, [+ cameraPermission,+ microphonePermission,+ requestCameraPermission,+ requestMicrophonePermission,+ ]);++ useEffect(() => {+ if (!isPhysicalDevice) return;+ if (!permissionStateLoaded || permissionRequestStarted.current) return;+ permissionRequestStarted.current = true;+ if (permissionsGranted) {+ setPermissionRequestFinished(true);+ return;+ }+ void requestPermissions().catch(() => {+ setPermissionRequestFinished(true);+ });+ }, [+ isPhysicalDevice,+ permissionStateLoaded,+ permissionsGranted,+ requestPermissions,+ ]);++ useEffect(() => {+ if (!isPhysicalDevice) return;+ const subscription = AppState.addEventListener("change", (state) => {+ if (state !== "active") return;+ void Promise.all([getCameraPermission(), getMicrophonePermission()])+ .catch(() => undefined)+ .finally(() => setPermissionRequestFinished(true));+ });+ return () => subscription.remove();+ }, [getCameraPermission, getMicrophonePermission, isPhysicalDevice]);++ useEffect(() => {+ if (phase !== "recording" || recordingStartedAt.current === null) return;+ const updateElapsed = () => {+ if (recordingStartedAt.current === null) return;+ setElapsedSeconds((Date.now() - recordingStartedAt.current) / 1000);+ };+ updateElapsed();+ const interval = setInterval(updateElapsed, 500);+ return () => clearInterval(interval);+ }, [phase]);++ const startRecording = useCallback(async () => {+ if (!cameraRef.current || !cameraReady || phase !== "ready") return;+ discardRecording.current = false;+ setElapsedSeconds(0);+ setError(null);+ setPhase("starting");+ let createdId: string | null = null;+ try {+ const created = await recordingUploads.beginRecording({+ fileName: createRecordingFileName(new Date()),+ width: 720,+ height: 1280,+ fps: 30,+ });+ createdId = created.id;+ activeRecordingId.current = created.id;+ await cameraRef.current.startRecording({+ recordingId: created.id,+ videoBitrate: recordingVideoBitrate,+ segmentDurationSeconds: recordingSegmentDurationSeconds,+ });+ recordingStartedAt.current = Date.now();+ setPhase("recording");+ setTeleprompterRestartKey((current) => current + 1);+ setTeleprompterPlaying(hasScript);+ } catch (recordingError) {+ if (createdId) {+ await recordingUploads.discardRecording(createdId);+ }+ activeRecordingId.current = null;+ recordingStartedAt.current = null;+ setTeleprompterPlaying(false);+ setError(+ recordingError instanceof Error+ ? recordingError.message+ : "Cap could not start the camera recording.",+ );+ setPhase("ready");+ }+ }, [cameraReady, hasScript, phase, recordingUploads]);++ const stopRecording = useCallback(async () => {+ if (phase !== "recording") return;+ setPhase("finishing");+ setTeleprompterPlaying(false);+ const id = activeRecordingId.current;+ try {+ const result = await cameraRef.current?.stopRecording();+ if (!id || !result)+ throw new Error("The camera did not finish recording.");+ if (discardRecording.current) {+ await recordingUploads.discardRecording(id);+ activeRecordingId.current = null;+ recordingStartedAt.current = null;+ router.back();+ return;+ }+ recordingUploads.finishRecording(id, result);+ activeRecordingId.current = null;+ recordingStartedAt.current = null;+ setElapsedSeconds(0);+ setPhase("ready");+ } catch (recordingError) {+ if (id) {+ await recordingUploads.discardRecording(id);+ }+ activeRecordingId.current = null;+ recordingStartedAt.current = null;+ setError(+ recordingError instanceof Error+ ? recordingError.message+ : "The camera stopped unexpectedly.",+ );+ setPhase("ready");+ }+ }, [phase, recordingUploads]);++ const closeRecorder = useCallback(() => {+ if (phase === "recording") {+ ActionSheetIOS.showActionSheetWithOptions(+ {+ cancelButtonIndex: 1,+ destructiveButtonIndex: 0,+ message: "This recording will not be uploaded.",+ options: ["Stop and discard", "Keep recording"],+ title: "Discard recording?",+ userInterfaceStyle: "dark",+ },+ (index) => {+ if (index !== 0) return;+ discardRecording.current = true;+ void stopRecording();+ },+ );+ return;+ }+ if (phase === "starting" || phase === "finishing") return;+ router.back();+ }, [phase, stopRecording]);++ const openEditor = useCallback(() => {+ if (phase !== "ready") return;+ setTeleprompterPlaying(false);+ setDraftScript(script);+ setDraftFontSize(fontSize);+ setDraftWordsPerMinute(wordsPerMinute);+ setEditorVisible(true);+ }, [fontSize, phase, script, wordsPerMinute]);++ const applyTeleprompterDraft = useCallback(() => {+ setScript(draftScript);+ setFontSize(draftFontSize);+ setWordsPerMinute(draftWordsPerMinute);+ setTeleprompterRestartKey((current) => current + 1);+ setEditorVisible(false);+ }, [draftFontSize, draftScript, draftWordsPerMinute]);++ const handleCameraReady = useCallback(() => {+ setCameraReady(true);+ }, []);+ const handleRecordingSegment = useCallback(+ ({ nativeEvent }: { nativeEvent: CapRecorderSegmentEvent }) => {+ const id = activeRecordingId.current;+ if (id && !discardRecording.current) {+ recordingUploads.addSegment(id, nativeEvent);+ }+ },+ [recordingUploads],+ );+ const handleRecordingError = useCallback(+ ({ nativeEvent }: { nativeEvent: CapRecorderErrorEvent }) => {+ setError(nativeEvent.message);+ const id = activeRecordingId.current;+ if (!id) {+ setCameraReady(false);+ return;+ }+ activeRecordingId.current = null;+ recordingStartedAt.current = null;+ setElapsedSeconds(0);+ setTeleprompterPlaying(false);+ setPhase("ready");+ void recordingUploads.discardRecording(id);+ },+ [recordingUploads],+ );++ if (!isPhysicalDevice) {+ return (+ <View style={styles.permissionScreen}>+ <Stack.Screen options={recorderScreenOptions} />+ <StatusBar barStyle="light-content" />+ <Pressable+ accessibilityRole="button"+ accessibilityLabel="Close recorder"+ onPress={() => router.back()}+ style={[styles.permissionClose, { top: insets.top + 8 }]}+ >+ <SymbolView+ name="xmark"+ size={18}+ tintColor={colors.white}+ weight="semibold"+ />+ </Pressable>+ <View+ accessibilityLabel="Camera unavailable in Simulator"+ style={styles.permissionContent}+ >+ <View style={styles.permissionIcon}>+ <SymbolView+ name="video.fill"+ size={32}+ tintColor={colors.white}+ weight="medium"+ />+ </View>+ <Text style={styles.permissionTitle}>Use a physical iPhone</Text>+ <Text style={styles.permissionText}>+ Camera and microphone recording is unavailable in iOS Simulator. Run+ Cap on an iPhone to preview and record video.+ </Text>+ </View>+ </View>+ );+ }++ if (!permissionsGranted) {+ const missingAccess = cameraPermission?.granted+ ? "microphone"+ : "camera and microphone";+ return (+ <View style={styles.permissionScreen}>+ <Stack.Screen options={recorderScreenOptions} />+ <StatusBar barStyle="light-content" />+ <Pressable+ accessibilityRole="button"+ accessibilityLabel="Close recorder"+ onPress={() => router.back()}+ style={[styles.permissionClose, { top: insets.top + 8 }]}+ >+ <SymbolView+ name="xmark"+ size={18}+ tintColor={colors.white}+ weight="semibold"+ />+ </Pressable>+ <View style={styles.permissionContent}>+ <View style={styles.permissionIcon}>+ <SymbolView+ name="video.fill"+ size={32}+ tintColor={colors.white}+ weight="medium"+ />+ </View>+ <Text style={styles.permissionTitle}>Camera and microphone</Text>+ <Text style={styles.permissionText}>+ Cap needs {missingAccess} access to record your video. Your+ teleprompter stays on screen and is not recorded.+ </Text>+ {waitingForPermission ? (+ <ActivityIndicator color={colors.white} />+ ) : (+ <Pressable+ accessibilityRole="button"+ accessibilityLabel="Open Settings"+ onPress={() => void Linking.openSettings()}+ style={({ pressed }) => [+ styles.permissionButton,+ pressed ? styles.permissionButtonPressed : null,+ ]}+ >+ <Text style={styles.permissionButtonText}>Open Settings</Text>+ </Pressable>+ )}+ </View>+ </View>+ );+ }++ return (+ <View style={styles.screen}>+ <Stack.Screen options={recorderScreenOptions} />+ <StatusBar barStyle="light-content" />+ <CapRecorderView+ active={cameraActive}+ facing={facing}+ onCameraReady={handleCameraReady}+ onRecordingError={handleRecordingError}+ onRecordingSegment={handleRecordingSegment}+ ref={cameraRef}+ style={StyleSheet.absoluteFill}+ />+ <View+ pointerEvents="box-none"+ style={[styles.topBar, { paddingTop: insets.top + 8 }]}+ >+ <CircleButton+ accessibilityLabel="Close recorder"+ disabled={phase === "starting" || phase === "finishing"}+ onPress={closeRecorder}+ symbol="xmark"+ />+ <View+ accessibilityLabel={`Recording time ${formatRecordingDuration(elapsedSeconds)}`}+ style={styles.timerPill}+ >+ {phase === "recording" || phase === "finishing" ? (+ <View style={styles.recordingDot} />+ ) : null}+ <Text style={styles.timerText}>+ {formatRecordingDuration(elapsedSeconds)}+ </Text>+ </View>+ <View style={styles.topBarSpacer} />+ </View>+ {hasScript ? (+ <TeleprompterOverlay+ fontSize={fontSize}+ onTogglePlayback={() => setTeleprompterPlaying((current) => !current)}+ playing={teleprompterPlaying}+ restartKey={teleprompterRestartKey}+ script={script}+ wordsPerMinute={wordsPerMinute}+ />+ ) : null}+ {error ? (+ <View accessibilityRole="alert" style={styles.errorPill}>+ <Text style={styles.errorText}>{error}</Text>+ </View>+ ) : null}+ <View+ pointerEvents="box-none"+ style={[styles.bottomControls, { paddingBottom: insets.bottom + 18 }]}+ >+ <CircleButton+ accessibilityLabel={+ hasScript ? "Edit teleprompter" : "Add teleprompter"+ }+ disabled={phase !== "ready"}+ onPress={openEditor}+ symbol="text.alignleft"+ />+ <Pressable+ accessibilityRole="button"+ accessibilityLabel={+ phase === "recording" ? "Stop recording" : "Start recording"+ }+ accessibilityState={{+ disabled:+ !cameraReady || phase === "starting" || phase === "finishing",+ }}+ disabled={+ !cameraReady || phase === "starting" || phase === "finishing"+ }+ onPress={() => {+ if (phase === "recording") void stopRecording();+ else void startRecording();+ }}+ style={({ pressed }) => [+ styles.captureButton,+ pressed ? styles.captureButtonPressed : null,+ ]}+ >+ {phase === "starting" || phase === "finishing" ? (+ <ActivityIndicator color={colors.white} />+ ) : (+ <View+ style={+ phase === "recording" ? styles.captureStop : styles.captureStart+ }+ />+ )}+ </Pressable>+ <CircleButton+ accessibilityLabel="Switch camera"+ disabled={phase !== "ready"}+ onPress={() => {+ setCameraReady(false);+ setFacing((current) => (current === "front" ? "back" : "front"));+ }}+ symbol="camera.rotate"+ />+ </View>+ <Modal+ allowSwipeDismissal+ animationType="slide"+ onRequestClose={() => setEditorVisible(false)}+ presentationStyle="formSheet"+ visible={editorVisible}+ >+ <SafeAreaView style={styles.editorSafeArea}>+ <KeyboardAvoidingView+ behavior="padding"+ style={styles.editorKeyboard}+ >+ <View style={styles.editorHeader}>+ <Pressable+ accessibilityRole="button"+ accessibilityLabel="Cancel"+ onPress={() => setEditorVisible(false)}+ style={styles.editorHeaderButton}+ >+ <Text style={styles.editorCancel}>Cancel</Text>+ </Pressable>+ <Text style={styles.editorTitle}>Teleprompter</Text>+ <Pressable+ accessibilityRole="button"+ accessibilityLabel="Done"+ onPress={applyTeleprompterDraft}+ style={styles.editorHeaderButton}+ >+ <Text style={styles.editorDone}>Done</Text>+ </Pressable>+ </View>+ <ScrollView+ contentContainerStyle={styles.editorContent}+ keyboardShouldPersistTaps="handled"+ >+ <TextInput+ autoFocus+ multiline+ onChangeText={setDraftScript}+ placeholder="Paste or type your script…"+ placeholderTextColor={colors.gray9}+ selectionColor={colors.blue9}+ spellCheck+ style={styles.scriptInput}+ value={draftScript}+ />+ <Text style={styles.wordCount}>+ {countWords(draftScript)} words · The overlay is not recorded+ </Text>+ <View style={styles.settingsGroup}>+ <StepperRow+ decreaseDisabled={+ draftWordsPerMinute <=+ teleprompterLimits.wordsPerMinute.minimum+ }+ increaseDisabled={+ draftWordsPerMinute >=+ teleprompterLimits.wordsPerMinute.maximum+ }+ label="Scroll speed"+ onDecrease={() =>+ setDraftWordsPerMinute((current) =>+ clamp(+ current - teleprompterLimits.wordsPerMinute.step,+ teleprompterLimits.wordsPerMinute.minimum,+ teleprompterLimits.wordsPerMinute.maximum,+ ),+ )+ }+ onIncrease={() =>+ setDraftWordsPerMinute((current) =>+ clamp(+ current + teleprompterLimits.wordsPerMinute.step,+ teleprompterLimits.wordsPerMinute.minimum,+ teleprompterLimits.wordsPerMinute.maximum,+ ),+ )+ }+ value={`${draftWordsPerMinute} WPM`}+ />+ <View style={styles.settingsDivider} />+ <StepperRow+ decreaseDisabled={+ draftFontSize <= teleprompterLimits.fontSize.minimum+ }+ increaseDisabled={+ draftFontSize >= teleprompterLimits.fontSize.maximum+ }+ label="Text size"+ onDecrease={() =>+ setDraftFontSize((current) =>+ clamp(+ current - teleprompterLimits.fontSize.step,+ teleprompterLimits.fontSize.minimum,+ teleprompterLimits.fontSize.maximum,+ ),+ )+ }+ onIncrease={() =>+ setDraftFontSize((current) =>+ clamp(+ current + teleprompterLimits.fontSize.step,+ teleprompterLimits.fontSize.minimum,+ teleprompterLimits.fontSize.maximum,+ ),+ )+ }+ value={`${draftFontSize} pt`}+ />+ </View>+ </ScrollView>+ </KeyboardAvoidingView>+ </SafeAreaView>+ </Modal>+ </View>+ );+}++const styles = StyleSheet.create({+ stackContent: {+ backgroundColor: colors.black,+ },+ screen: {+ flex: 1,+ backgroundColor: colors.black,+ },+ topBar: {+ position: "absolute",+ top: 0,+ left: 0,+ right: 0,+ zIndex: 5,+ paddingHorizontal: 16,+ flexDirection: "row",+ alignItems: "center",+ justifyContent: "space-between",+ },+ topBarSpacer: {+ width: 46,+ height: 46,+ },+ circleButton: {+ width: 46,+ height: 46,+ borderRadius: radius.full,+ alignItems: "center",+ justifyContent: "center",+ backgroundColor: "rgba(8,10,14,0.48)",+ borderWidth: StyleSheet.hairlineWidth,+ borderColor: "rgba(255,255,255,0.2)",+ },+ circleButtonPressed: {+ backgroundColor: "rgba(8,10,14,0.66)",+ transform: [{ scale: 0.96 }],+ },+ circleButtonDisabled: {+ opacity: 0.38,+ },+ timerPill: {+ height: 34,+ minWidth: 82,+ borderRadius: radius.full,+ paddingHorizontal: 13,+ alignItems: "center",+ justifyContent: "center",+ flexDirection: "row",+ gap: 7,+ backgroundColor: "rgba(8,10,14,0.48)",+ borderWidth: StyleSheet.hairlineWidth,+ borderColor: "rgba(255,255,255,0.16)",+ },+ recordingDot: {+ width: 7,+ height: 7,+ borderRadius: radius.full,+ backgroundColor: "#ff453a",+ },+ timerText: {+ fontFamily: fonts.medium,+ fontSize: 14,+ fontVariant: ["tabular-nums"],+ color: colors.white,+ },+ bottomControls: {+ position: "absolute",+ left: 0,+ right: 0,+ bottom: 0,+ zIndex: 5,+ paddingHorizontal: 28,+ paddingTop: 24,+ flexDirection: "row",+ alignItems: "center",+ justifyContent: "space-between",+ backgroundColor: "rgba(0,0,0,0.12)",+ },+ captureButton: {+ width: 82,+ height: 82,+ borderRadius: radius.full,+ borderWidth: 5,+ borderColor: "rgba(255,255,255,0.92)",+ alignItems: "center",+ justifyContent: "center",+ },+ captureButtonPressed: {+ transform: [{ scale: 0.95 }],+ },+ captureStart: {+ width: 62,+ height: 62,+ borderRadius: radius.full,+ backgroundColor: "#ff3b30",+ },+ captureStop: {+ width: 31,+ height: 31,+ borderRadius: radius.sm,+ backgroundColor: "#ff3b30",+ ...squircle,+ },+ errorPill: {+ position: "absolute",+ zIndex: 6,+ left: 24,+ right: 24,+ bottom: 146,+ borderRadius: radius.md,+ backgroundColor: "rgba(102,15,20,0.86)",+ borderWidth: StyleSheet.hairlineWidth,+ borderColor: "rgba(255,255,255,0.18)",+ paddingHorizontal: 14,+ paddingVertical: 12,+ ...squircle,+ },+ errorText: {+ fontFamily: fonts.medium,+ fontSize: 13,+ lineHeight: 18,+ textAlign: "center",+ color: colors.white,+ },+ permissionScreen: {+ flex: 1,+ backgroundColor: "#08090c",+ },+ permissionClose: {+ position: "absolute",+ left: 16,+ zIndex: 2,+ width: 46,+ height: 46,+ borderRadius: radius.full,+ alignItems: "center",+ justifyContent: "center",+ backgroundColor: "rgba(255,255,255,0.1)",+ },+ permissionContent: {+ flex: 1,+ alignItems: "center",+ justifyContent: "center",+ paddingHorizontal: 34,+ },+ permissionIcon: {+ width: 72,+ height: 72,+ borderRadius: radius.xl,+ alignItems: "center",+ justifyContent: "center",+ backgroundColor: "rgba(255,255,255,0.1)",+ ...squircle,+ },+ permissionTitle: {+ marginTop: 22,+ fontFamily: fonts.medium,+ fontSize: 26,+ lineHeight: 32,+ color: colors.white,+ textAlign: "center",+ },+ permissionText: {+ marginTop: 10,+ marginBottom: 24,+ fontFamily: fonts.regular,+ fontSize: 16,+ lineHeight: 23,+ color: "rgba(255,255,255,0.66)",+ textAlign: "center",+ },+ permissionButton: {+ height: 50,+ minWidth: 170,+ borderRadius: radius.full,+ paddingHorizontal: 24,+ alignItems: "center",+ justifyContent: "center",+ backgroundColor: colors.white,+ },+ permissionButtonPressed: {+ opacity: 0.82,+ },+ permissionButtonText: {+ fontFamily: fonts.medium,+ fontSize: 16,+ color: colors.gray12,+ },+ editorSafeArea: {+ flex: 1,+ backgroundColor: colors.appBackground,+ },+ editorKeyboard: {+ flex: 1,+ },+ editorHeader: {+ height: 56,+ paddingHorizontal: 12,+ flexDirection: "row",+ alignItems: "center",+ justifyContent: "space-between",+ borderBottomWidth: StyleSheet.hairlineWidth,+ borderBottomColor: colors.gray5,+ backgroundColor: colors.gray1,+ },+ editorHeaderButton: {+ minWidth: 64,+ height: 44,+ justifyContent: "center",+ },+ editorCancel: {+ fontFamily: fonts.regular,+ fontSize: 16,+ color: colors.blue11,+ },+ editorDone: {+ fontFamily: fonts.medium,+ fontSize: 16,+ color: colors.blue11,+ textAlign: "right",+ },+ editorTitle: {+ fontFamily: fonts.medium,+ fontSize: 17,+ color: colors.gray12,+ },+ editorContent: {+ padding: 16,+ paddingBottom: 36,+ },+ scriptInput: {+ minHeight: 260,+ borderRadius: radius.lg,+ borderWidth: StyleSheet.hairlineWidth,+ borderColor: colors.gray5,+ paddingHorizontal: 18,+ paddingVertical: 16,+ fontFamily: fonts.regular,+ fontSize: 20,+ lineHeight: 28,+ color: colors.gray12,+ backgroundColor: colors.white,+ textAlignVertical: "top",+ ...squircle,+ },+ wordCount: {+ marginTop: 9,+ marginHorizontal: 4,+ fontFamily: fonts.regular,+ fontSize: 13,+ color: colors.gray9,+ },+ settingsGroup: {+ marginTop: 22,+ borderRadius: radius.lg,+ borderWidth: StyleSheet.hairlineWidth,+ borderColor: colors.gray5,+ backgroundColor: colors.white,+ overflow: "hidden",+ ...squircle,+ },+ settingsDivider: {+ height: StyleSheet.hairlineWidth,+ marginLeft: 16,+ backgroundColor: colors.gray5,+ },+ stepperRow: {+ minHeight: 68,+ paddingHorizontal: 16,+ flexDirection: "row",+ alignItems: "center",+ justifyContent: "space-between",+ gap: 16,+ },+ stepperCopy: {+ flex: 1,+ },+ stepperLabel: {+ fontFamily: fonts.medium,+ fontSize: 15,+ color: colors.gray12,+ },+ stepperValue: {+ marginTop: 2,+ fontFamily: fonts.regular,+ fontSize: 13,+ color: colors.gray9,+ },+ stepperControls: {+ flexDirection: "row",+ borderRadius: radius.sm,+ borderWidth: StyleSheet.hairlineWidth,+ borderColor: colors.gray5,+ overflow: "hidden",+ },+ stepperButton: {+ width: 42,+ height: 34,+ alignItems: "center",+ justifyContent: "center",+ backgroundColor: colors.gray2,+ },+ stepperButtonPressed: {+ backgroundColor: colors.gray4,+ },+ stepperButtonDisabled: {+ opacity: 0.32,+ },+});++const recorderScreenOptions = {+ animation: "fade" as const,+ contentStyle: styles.stackContent,+ gestureEnabled: false,+ presentation: "fullScreenModal" as const,+};diff --git a/apps/mobile/src/components/recording-upload-status.tsx b/apps/mobile/src/components/recording-upload-status.tsxnew file mode 100644index 000000000..3fe82d3f2--- /dev/null+++ b/apps/mobile/src/components/recording-upload-status.tsx@@ -0,0 +1,97 @@+import { router, useSegments } from "expo-router";+import { SymbolView } from "expo-symbols";+import { Pressable, StyleSheet, Text, View } from "react-native";+import { useSafeAreaInsets } from "react-native-safe-area-context";+import { useAuth } from "@/auth/AuthContext";+import { colors, fonts, radius } from "@/theme";+import { useRecordingUploads } from "@/uploads/recording-upload-provider";+import { recordingUploadProgress } from "@/uploads/recording-upload-queue";++export function RecordingUploadStatus() {+ const insets = useSafeAreaInsets();+ const auth = useAuth();+ const segments = useSegments();+ const { queue, retryRecording } = useRecordingUploads();+ const job = [...queue.jobs].reverse()[0];+ if (!job || auth.status !== "signedIn" || job.status === "recording") {+ return null;+ }++ const percent = Math.round(recordingUploadProgress(job) * 100);+ const canRetry = job.status === "failed" && job.durationSeconds !== null;+ const isRecorder = segments[0] === "record";+ const label =+ job.status === "failed"+ ? canRetry+ ? "Upload paused · Tap to retry"+ : "Recording could not be recovered"+ : job.status === "processing"+ ? "Creating your share link"+ : job.status === "complete"+ ? "Recording ready"+ : `Uploading recording · ${percent}%`;+ const symbol =+ job.status === "failed"+ ? "exclamationmark.icloud.fill"+ : job.status === "complete"+ ? "checkmark.icloud.fill"+ : "icloud.and.arrow.up.fill";++ return (+ <View pointerEvents="box-none" style={styles.container}>+ <Pressable+ accessibilityRole="button"+ accessibilityLabel={label}+ onPress={() => {+ if (canRetry) retryRecording(job.id);+ else if (job.status === "complete") {+ router.push({ pathname: "/caps/[id]", params: { id: job.id } });+ }+ }}+ style={({ pressed }) => [+ styles.pill,+ isRecorder+ ? { top: insets.top + 62 }+ : { bottom: insets.bottom + 82 },+ pressed ? styles.pillPressed : null,+ ]}+ >+ <SymbolView name={symbol} size={16} tintColor={colors.white} />+ <Text numberOfLines={1} style={styles.label}>+ {label}+ </Text>+ </Pressable>+ </View>+ );+}++const styles = StyleSheet.create({+ container: {+ ...StyleSheet.absoluteFillObject,+ zIndex: 100,+ alignItems: "center",+ justifyContent: "flex-end",+ },+ pill: {+ position: "absolute",+ maxWidth: "88%",+ height: 42,+ paddingHorizontal: 15,+ borderRadius: radius.full,+ borderCurve: "continuous",+ flexDirection: "row",+ alignItems: "center",+ gap: 8,+ backgroundColor: "rgba(18,18,20,0.9)",+ boxShadow: "0 5px 18px rgba(0,0,0,0.2)",+ },+ pillPressed: {+ opacity: 0.82,+ transform: [{ scale: 0.98 }],+ },+ label: {+ color: colors.white,+ fontFamily: fonts.medium,+ fontSize: 14,+ },+});diff --git a/apps/mobile/src/screens/record.test.tsx b/apps/mobile/src/screens/record.test.tsxnew file mode 100644index 000000000..ff0303e6a--- /dev/null+++ b/apps/mobile/src/screens/record.test.tsx@@ -0,0 +1,414 @@+import React, { type ReactElement, type ReactNode } from "react";+import { Linking, TextInput } from "react-native";+import TestRenderer, { act, type ReactTestRenderer } from "react-test-renderer";+import { beforeEach, describe, expect, it, vi } from "vitest";+import { TeleprompterOverlay } from "@/recording/TeleprompterOverlay";+import RecordScreen from "../../app/record";+import CapRecorderView from "../../modules/cap-recorder";++type HostProps = {+ children?: ReactNode;+ [key: string]: unknown;+};++const cameraState = vi.hoisted(() => ({+ startRecording: vi.fn(() => Promise.resolve()),+ stopRecording: vi.fn(),+}));++const permissionState = vi.hoisted(() => ({+ cameraGranted: true,+ microphoneGranted: true,+ getCamera: vi.fn(),+ getMicrophone: vi.fn(),+ requestCamera: vi.fn(),+ requestMicrophone: vi.fn(),+}));++const routerState = vi.hoisted(() => ({+ back: vi.fn(),+}));++const stackState = vi.hoisted(() => ({+ options: [] as unknown[],+}));++const deviceState = vi.hoisted(() => ({+ isDevice: true,+}));++const uploadState = vi.hoisted(() => ({+ beginRecording: vi.fn(() =>+ Promise.resolve({ id: "cap_123", shareUrl: "https://cap.so/s/cap_123" }),+ ),+ addSegment: vi.fn(),+ finishRecording: vi.fn(),+ discardRecording: vi.fn(() => Promise.resolve()),+ failRecording: vi.fn(),+ retryRecording: vi.fn(),+ queue: { jobs: [] },+}));++(+ globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }+).IS_REACT_ACT_ENVIRONMENT = true;++vi.mock("react-native", async () => {+ const React = await import("react");+ const createHost =+ (name: string) =>+ ({ children, ...props }: HostProps) =>+ React.createElement(name, props, children);++ return {+ ActionSheetIOS: { showActionSheetWithOptions: vi.fn() },+ ActivityIndicator: createHost("ActivityIndicator"),+ AppState: {+ addEventListener: vi.fn(() => ({ remove: vi.fn() })),+ },+ KeyboardAvoidingView: createHost("KeyboardAvoidingView"),+ Linking: { openSettings: vi.fn(() => Promise.resolve()) },+ Modal: createHost("Modal"),+ Pressable: createHost("Pressable"),+ ScrollView: createHost("ScrollView"),+ StatusBar: createHost("StatusBar"),+ StyleSheet: {+ absoluteFill: {},+ absoluteFillObject: {},+ create: <T extends Record<string, unknown>>(styles: T) => styles,+ hairlineWidth: 1,+ },+ Text: createHost("Text"),+ TextInput: createHost("TextInput"),+ View: createHost("View"),+ };+});++vi.mock("react-native-safe-area-context", async () => {+ const React = await import("react");+ return {+ SafeAreaView: ({ children, ...props }: HostProps) =>+ React.createElement("SafeAreaView", props, children),+ useSafeAreaInsets: () => ({ bottom: 24, left: 0, right: 0, top: 48 }),+ };+});++vi.mock("expo-camera", async () => {+ const permission = {+ canAskAgain: true,+ expires: "never",+ granted: true,+ status: "granted",+ };+ const permissionResponse = (granted: boolean) => ({+ ...permission,+ granted,+ status: granted ? "granted" : "denied",+ });+ return {+ useCameraPermissions: () => [+ permissionResponse(permissionState.cameraGranted),+ permissionState.requestCamera,+ permissionState.getCamera,+ ],+ useMicrophonePermissions: () => [+ permissionResponse(permissionState.microphoneGranted),+ permissionState.requestMicrophone,+ permissionState.getMicrophone,+ ],+ };+});++vi.mock("../../modules/cap-recorder", async () => {+ const React = await import("react");+ const CapRecorderView = React.forwardRef<unknown, HostProps>((props, ref) => {+ React.useImperativeHandle(ref, () => ({+ startRecording: cameraState.startRecording,+ stopRecording: cameraState.stopRecording,+ }));+ const onCameraReady = props.onCameraReady;+ React.useEffect(() => {+ if (typeof onCameraReady === "function") onCameraReady();+ }, [onCameraReady]);+ return React.createElement("CapRecorderView", props);+ });+ return { default: CapRecorderView };+});++vi.mock("expo-device", () => ({+ get isDevice() {+ return deviceState.isDevice;+ },+}));++vi.mock("expo-router", async () => {+ const React = await import("react");+ return {+ router: routerState,+ Stack: {+ Screen: (props: HostProps) => {+ stackState.options.push(props.options);+ return React.createElement("StackScreen", props);+ },+ },+ };+});++vi.mock("expo-symbols", async () => {+ const React = await import("react");+ return {+ SymbolView: (props: HostProps) => React.createElement("SymbolView", props),+ };+});++vi.mock("@/recording/TeleprompterOverlay", async () => {+ const React = await import("react");+ return {+ TeleprompterOverlay: (props: HostProps) =>+ React.createElement("TeleprompterOverlay", props),+ };+});++vi.mock("@/uploads/recording-upload-provider", () => ({+ useRecordingUploads: () => uploadState,+}));++const renderComponent = async (+ node: ReactElement,+): Promise<ReactTestRenderer> => {+ let renderer: ReactTestRenderer | null = null;+ await act(async () => {+ renderer = TestRenderer.create(node);+ });+ return renderer as unknown as ReactTestRenderer;+};++describe("RecordScreen", () => {+ beforeEach(() => {+ cameraState.startRecording.mockReset();+ cameraState.startRecording.mockResolvedValue(undefined);+ cameraState.stopRecording.mockReset();+ cameraState.stopRecording.mockResolvedValue({+ durationSeconds: 4.2,+ segmentCount: 2,+ totalBytes: 1_400_000,+ });+ routerState.back.mockReset();+ stackState.options.length = 0;+ uploadState.beginRecording.mockReset();+ uploadState.beginRecording.mockResolvedValue({+ id: "cap_123",+ shareUrl: "https://cap.so/s/cap_123",+ });+ uploadState.addSegment.mockReset();+ uploadState.finishRecording.mockReset();+ uploadState.discardRecording.mockReset();+ uploadState.discardRecording.mockResolvedValue(undefined);+ permissionState.cameraGranted = true;+ permissionState.microphoneGranted = true;+ deviceState.isDevice = true;+ const grantedPermission = {+ canAskAgain: true,+ expires: "never",+ granted: true,+ status: "granted",+ };+ permissionState.getCamera.mockReset();+ permissionState.getCamera.mockResolvedValue(grantedPermission);+ permissionState.getMicrophone.mockReset();+ permissionState.getMicrophone.mockResolvedValue(grantedPermission);+ permissionState.requestCamera.mockReset();+ permissionState.requestCamera.mockResolvedValue(grantedPermission);+ permissionState.requestMicrophone.mockReset();+ permissionState.requestMicrophone.mockResolvedValue(grantedPermission);+ });++ it("renders the native segmented iOS camera preview", async () => {+ const renderer = await renderComponent(React.createElement(RecordScreen));+ const camera = renderer.root.findByType(CapRecorderView);++ expect(camera.props).toMatchObject({+ active: true,+ facing: "front",+ });+ expect(+ renderer.root.findByProps({ accessibilityLabel: "Add teleprompter" }),+ ).toBeTruthy();+ });++ it("keeps native modal options stable when the camera becomes ready", async () => {+ await renderComponent(React.createElement(RecordScreen));++ expect(stackState.options.length).toBeGreaterThan(1);+ expect(new Set(stackState.options).size).toBe(1);+ });++ it("keeps the native modal mounted while permissions become granted", async () => {+ const grantedPermission = {+ canAskAgain: true,+ expires: "never",+ granted: true,+ status: "granted",+ };+ permissionState.cameraGranted = false;+ permissionState.microphoneGranted = false;+ permissionState.requestCamera.mockImplementationOnce(async () => {+ permissionState.cameraGranted = true;+ return grantedPermission;+ });+ permissionState.requestMicrophone.mockImplementationOnce(async () => {+ permissionState.microphoneGranted = true;+ return grantedPermission;+ });++ const renderer = await renderComponent(React.createElement(RecordScreen));++ expect(permissionState.requestCamera).toHaveBeenCalledTimes(1);+ expect(permissionState.requestMicrophone).toHaveBeenCalledTimes(1);+ expect(renderer.root.findAllByType(CapRecorderView)).toHaveLength(1);+ expect(new Set(stackState.options).size).toBe(1);+ });++ it("does not mount camera capture in iOS Simulator", async () => {+ deviceState.isDevice = false;+ permissionState.cameraGranted = false;+ permissionState.microphoneGranted = false;++ const renderer = await renderComponent(React.createElement(RecordScreen));++ expect(+ renderer.root.findByProps({+ accessibilityLabel: "Camera unavailable in Simulator",+ }),+ ).toBeTruthy();+ expect(renderer.root.findAllByType(CapRecorderView)).toHaveLength(0);+ expect(permissionState.requestCamera).not.toHaveBeenCalled();+ expect(permissionState.requestMicrophone).not.toHaveBeenCalled();+ });++ it("offers the native Settings recovery when camera access is denied", async () => {+ permissionState.cameraGranted = false;+ permissionState.requestCamera.mockResolvedValueOnce({+ canAskAgain: false,+ expires: "never",+ granted: false,+ status: "denied",+ });+ const renderer = await renderComponent(React.createElement(RecordScreen));++ expect(permissionState.requestCamera).toHaveBeenCalledTimes(1);+ const settingsButton = renderer.root.findByProps({+ accessibilityLabel: "Open Settings",+ });+ await act(async () => {+ settingsButton.props.onPress();+ await Promise.resolve();+ });+ expect(Linking.openSettings).toHaveBeenCalledTimes(1);+ });++ it("streams optimized segments and returns to a ready camera after stop", async () => {+ const renderer = await renderComponent(React.createElement(RecordScreen));++ await act(async () => {+ renderer.root+ .findByProps({ accessibilityLabel: "Add teleprompter" })+ .props.onPress();+ });+ await act(async () => {+ renderer.root+ .findByType(TextInput)+ .props.onChangeText("This is a short camera script");+ });+ await act(async () => {+ renderer.root.findByProps({ accessibilityLabel: "Done" }).props.onPress();+ });++ expect(renderer.root.findByType(TeleprompterOverlay).props).toMatchObject({+ fontSize: 30,+ script: "This is a short camera script",+ wordsPerMinute: 150,+ });++ await act(async () => {+ renderer.root+ .findByProps({ accessibilityLabel: "Start recording" })+ .props.onPress();+ await Promise.resolve();+ });+ expect(uploadState.beginRecording).toHaveBeenCalledWith({+ fileName: expect.stringMatching(+ /^Cap Recording - \d{1,2} [A-Z][a-z]{2} \d{4} at \d{2}\.\d{2}\.mp4$/,+ ),+ width: 720,+ height: 1280,+ fps: 30,+ });+ expect(cameraState.startRecording).toHaveBeenCalledWith({+ recordingId: "cap_123",+ videoBitrate: 2_500_000,+ segmentDurationSeconds: 2,+ });+ expect(renderer.root.findByType(TeleprompterOverlay).props.playing).toBe(+ true,+ );++ await act(async () => {+ renderer.root.findByType(CapRecorderView).props.onRecordingSegment({+ nativeEvent: {+ track: "video",+ type: "media",+ index: 1,+ uri: "file:///recording/segment_001.m4s",+ durationSeconds: 2,+ byteLength: 650_000,+ },+ });+ });+ expect(uploadState.addSegment).toHaveBeenCalledWith(+ "cap_123",+ expect.objectContaining({ index: 1, track: "video", type: "media" }),+ );++ await act(async () => {+ renderer.root+ .findByProps({ accessibilityLabel: "Stop recording" })+ .props.onPress();+ });+ expect(cameraState.stopRecording).toHaveBeenCalledTimes(1);+ expect(uploadState.finishRecording).toHaveBeenCalledWith("cap_123", {+ durationSeconds: 4.2,+ segmentCount: 2,+ totalBytes: 1_400_000,+ });+ expect(+ renderer.root.findByProps({ accessibilityLabel: "Start recording" }),+ ).toBeTruthy();+ expect(routerState.back).not.toHaveBeenCalled();+ });++ it("cleans up a failed native recording without disabling the preview", async () => {+ const renderer = await renderComponent(React.createElement(RecordScreen));++ await act(async () => {+ renderer.root+ .findByProps({ accessibilityLabel: "Start recording" })+ .props.onPress();+ await Promise.resolve();+ });+ await act(async () => {+ renderer.root.findByType(CapRecorderView).props.onRecordingError({+ nativeEvent: { message: "The camera encoder stopped." },+ });+ await Promise.resolve();+ });++ expect(uploadState.discardRecording).toHaveBeenCalledWith("cap_123");+ expect(+ renderer.root.findByProps({ accessibilityLabel: "Start recording" }),+ ).toBeTruthy();+ expect(+ renderer.root.findByProps({ accessibilityRole: "alert" }),+ ).toBeTruthy();+ });+});