diff --git a/apps/mobile/src/atoms/app.ts b/apps/mobile/src/atoms/app.ts new file mode 100644 index 000000000..74da5bd6a --- /dev/null +++ b/apps/mobile/src/atoms/app.ts @@ -0,0 +1,13 @@ +import { atom } from "jotai" + +export const loadingVisibleAtom = atom(false) + +export const loadingAtom = atom<{ + finish: null | (() => any) + cancel: null | (() => any) + thenable: null | Promise +}>({ + finish: null, + cancel: null, + thenable: null, +}) diff --git a/apps/mobile/src/components/common/LoadingContainer.tsx b/apps/mobile/src/components/common/LoadingContainer.tsx new file mode 100644 index 000000000..ea5f866dd --- /dev/null +++ b/apps/mobile/src/components/common/LoadingContainer.tsx @@ -0,0 +1,100 @@ +import { useAtom } from "jotai" +import { useCallback, useEffect, useRef, useState } from "react" +import { Modal, Text, TouchableOpacity, View } from "react-native" +import Animated, { + Easing, + useAnimatedStyle, + useSharedValue, + withRepeat, + withTiming, +} from "react-native-reanimated" + +import { loadingAtom, loadingVisibleAtom } from "@/src/atoms/app" +import { Loading3CuteReIcon } from "@/src/icons/loading_3_cute_re" + +export const LoadingContainer = () => { + const rotate = useSharedValue(0) + + const [visible, setVisible] = useAtom(loadingVisibleAtom) + const [showCancelButton, setShowCancelButton] = useState(false) + + const [loadingCaller, setLoadingCaller] = useAtom(loadingAtom) + + const resetLoadingCaller = useCallback(() => { + setLoadingCaller({ + finish: null, + cancel: null, + thenable: null, + }) + }, [setLoadingCaller]) + + useEffect(() => { + rotate.value = withRepeat( + withTiming(360, { duration: 1000, easing: Easing.linear }), + Infinity, + false, + ) + return () => { + rotate.value = 0 + } + }, [rotate]) + + useEffect(() => { + if (loadingCaller.thenable) { + loadingCaller.thenable.finally(() => { + setVisible(false) + setShowCancelButton(false) + + resetLoadingCaller() + + loadingCaller.finish?.() + }) + } + }, [loadingCaller.thenable]) + + const cancelTimerRef = useRef(null) + useEffect(() => { + cancelTimerRef.current = setTimeout(() => { + setShowCancelButton(true) + }, 3000) + return () => { + if (cancelTimerRef.current) { + clearTimeout(cancelTimerRef.current) + } + } + }, []) + + const rotateStyle = useAnimatedStyle(() => ({ + transform: [{ rotate: `${rotate.value}deg` }], + })) + + const cancel = () => { + setVisible(false) + setShowCancelButton(false) + + if (loadingCaller.cancel) { + loadingCaller.cancel() + } + resetLoadingCaller() + } + + return ( + + + + + + + + + {showCancelButton && ( + + + Cancel + + + )} + + + ) +} diff --git a/apps/mobile/src/hooks/useLoadingCallback.tsx b/apps/mobile/src/hooks/useLoadingCallback.tsx new file mode 100644 index 000000000..f4980622e --- /dev/null +++ b/apps/mobile/src/hooks/useLoadingCallback.tsx @@ -0,0 +1,21 @@ +import { useSetAtom } from "jotai" +import { useCallback } from "react" + +import { loadingAtom, loadingVisibleAtom } from "../atoms/app" + +export const useLoadingCallback = () => { + const setLoadingCaller = useSetAtom(loadingAtom) + const setVisible = useSetAtom(loadingVisibleAtom) + + return useCallback( + (thenable: Promise, options: { finish: () => any; cancel: () => any }) => { + setLoadingCaller({ + thenable, + finish: options.finish, + cancel: options.cancel, + }) + setVisible(true) + }, + [setLoadingCaller, setVisible], + ) +} diff --git a/apps/mobile/src/screens/(headless)/debug.tsx b/apps/mobile/src/screens/(headless)/debug.tsx index 41da66a11..76195ffe8 100644 --- a/apps/mobile/src/screens/(headless)/debug.tsx +++ b/apps/mobile/src/screens/(headless)/debug.tsx @@ -19,27 +19,11 @@ import { clearSessionToken, getSessionToken, setSessionToken } from "@/src/lib/c export default function DebugPanel() { const insets = useSafeAreaInsets() - // const isExpanded = useSharedValue(false) + return ( Users - {/*