fix(mobile): remove splash screen and stabilize navigation (#45)
This commit was merged in pull request #45.
This commit is contained in:
1 parent
7c27f0102b
commit
da954d985b
14 files changed
+128
-164
No files matched your search
+1
-18
@@ -44,24 +44,7 @@
|
||||
[
|
||||
"expo-splash-screen",
|
||||
{
|
||||
"ios": {
|
||||
"backgroundColor": "#ffffff",
|
||||
"image": "./assets/ios-icon.png",
|
||||
"dark": {
|
||||
"image": "./assets/ios-icon-dark.png",
|
||||
"backgroundColor": "#000000"
|
||||
},
|
||||
"imageWidth": 200
|
||||
},
|
||||
"android": {
|
||||
"backgroundColor": "#ffffff",
|
||||
"image": "./assets/icon.png",
|
||||
"dark": {
|
||||
"image": "./assets/icon-dark.png",
|
||||
"backgroundColor": "#000000"
|
||||
},
|
||||
"imageWidth": 200
|
||||
}
|
||||
"backgroundColor": "#000000"
|
||||
}
|
||||
],
|
||||
"expo-image",
|
||||
|
||||
@@ -7,6 +7,8 @@ import { TabBottomAccessory } from "@/components/mini-player"
|
||||
import { usePlaybackQueue } from "@/contexts/playback"
|
||||
import { appleMusic, colors } from "@/lib/theme"
|
||||
|
||||
export const unstable_settings = { anchor: "library" }
|
||||
|
||||
function useRouterTheme() {
|
||||
const isDark = useColorScheme() === "dark"
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ import { Ionicons } from "@expo/vector-icons"
|
||||
import { useAction, useMutation, useQuery } from "convex/react"
|
||||
import { Image } from "expo-image"
|
||||
import * as ImagePicker from "expo-image-picker"
|
||||
import { useRouter } from "expo-router"
|
||||
import { useState } from "react"
|
||||
import {
|
||||
Pressable,
|
||||
@@ -20,7 +19,7 @@ import { useInvalidateAuthSession } from "@/contexts/convex-auth-session"
|
||||
import { useServerConfig } from "@/contexts/server-config"
|
||||
import { initialsFromName } from "@/lib/format"
|
||||
import { useCachedQuery } from "@/hooks/use-cached-query"
|
||||
import { clearLocalData, signOutToSignIn } from "@/lib/session"
|
||||
import { changeServer, signOut } from "@/lib/session"
|
||||
import { radius, spacing, useThemeColors } from "@/lib/theme"
|
||||
import { showError } from "@/lib/toast"
|
||||
import type { PlaybackQuality } from "@/lib/urls"
|
||||
@@ -73,7 +72,6 @@ const SECTIONS: SectionConfig[] = [
|
||||
|
||||
export default function SettingsScreen() {
|
||||
const colors = useThemeColors()
|
||||
const router = useRouter()
|
||||
const user = useCachedQuery(api.auth.currentUser)
|
||||
const { invalidateSession } = useInvalidateAuthSession()
|
||||
const { clearServer } = useServerConfig()
|
||||
@@ -91,14 +89,19 @@ export default function SettingsScreen() {
|
||||
const visibleSections = SECTIONS.filter((item) => !item.admin || isAdmin)
|
||||
|
||||
async function handleSignOut() {
|
||||
await signOutToSignIn(router, invalidateSession)
|
||||
try {
|
||||
await signOut(invalidateSession)
|
||||
} catch {
|
||||
showError("Could not clear all local data.")
|
||||
}
|
||||
}
|
||||
|
||||
async function handleChangeServer() {
|
||||
await invalidateSession()
|
||||
await clearLocalData()
|
||||
await clearServer()
|
||||
router.replace("/server")
|
||||
try {
|
||||
await changeServer(invalidateSession, clearServer)
|
||||
} catch {
|
||||
showError("Could not clear all local data.")
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -61,12 +61,14 @@ export default function LibraryScreen() {
|
||||
{links.map((link) => (
|
||||
<Pressable
|
||||
key={link.href}
|
||||
style={[styles.row, { borderBottomColor: colors.border }]}
|
||||
style={styles.row}
|
||||
onPress={() => push(link.href)}
|
||||
>
|
||||
<LibraryIcon name={link.icon} />
|
||||
<Text style={[styles.rowTitle, { color: colors.foreground }]}>{link.title}</Text>
|
||||
<Ionicons name="chevron-forward" size={16} color={colors.mutedForeground} />
|
||||
<View style={[styles.rowContent, { borderBottomColor: colors.border }]}>
|
||||
<Text style={[styles.rowTitle, { color: colors.foreground }]}>{link.title}</Text>
|
||||
<Ionicons name="chevron-forward" size={16} color={colors.mutedForeground} />
|
||||
</View>
|
||||
</Pressable>
|
||||
))}
|
||||
|
||||
@@ -111,6 +113,12 @@ const styles = StyleSheet.create({
|
||||
alignItems: "center",
|
||||
gap: spacing.md,
|
||||
paddingHorizontal: spacing.lg,
|
||||
},
|
||||
rowContent: {
|
||||
flex: 1,
|
||||
flexDirection: "row",
|
||||
alignItems: "center",
|
||||
gap: spacing.md,
|
||||
paddingVertical: 14,
|
||||
borderBottomWidth: StyleSheet.hairlineWidth,
|
||||
},
|
||||
|
||||
@@ -3,25 +3,22 @@ import {
|
||||
useConvexAuth as useServerConvexAuth,
|
||||
useConvexConnectionState,
|
||||
} from "convex/react"
|
||||
import { Redirect, Stack } from "expo-router"
|
||||
import { Stack } from "expo-router"
|
||||
import { Platform } from "react-native"
|
||||
import { useEffect } from "react"
|
||||
|
||||
import { AppIntentBridge } from "@/components/app-intent-bridge"
|
||||
import { ConvexAuthRecovery } from "@/components/convex-auth-recovery"
|
||||
import { LoadingView } from "@/components/ui/primitives"
|
||||
import { AddToPlaylistProvider } from "@/contexts/add-to-playlist"
|
||||
import { useInvalidateAuthSession } from "@/contexts/convex-auth-session"
|
||||
import { FavoritesProvider } from "@/contexts/favorites"
|
||||
import { PlaybackProvider } from "@/contexts/playback"
|
||||
import { useServerConfig } from "@/contexts/server-config"
|
||||
import { useStackScreenOptions } from "@/lib/navigation"
|
||||
import { AppNavigationSync } from "@/lib/app-navigation"
|
||||
|
||||
const AUTH_HANDSHAKE_TIMEOUT_MS = 15_000
|
||||
|
||||
export default function AppLayout() {
|
||||
const { config } = useServerConfig()
|
||||
const localAuth = useLocalConvexAuth()
|
||||
const serverAuth = useServerConvexAuth()
|
||||
const { isWebSocketConnected } = useConvexConnectionState()
|
||||
@@ -51,18 +48,6 @@ export default function AppLayout() {
|
||||
serverAuth?.isAuthenticated,
|
||||
])
|
||||
|
||||
if (!config) {
|
||||
return <Redirect href="/server" />
|
||||
}
|
||||
|
||||
if (!localAuth || localAuth.isLoading) {
|
||||
return <LoadingView />
|
||||
}
|
||||
|
||||
if (!localAuth.isAuthenticated) {
|
||||
return <Redirect href="/signin" />
|
||||
}
|
||||
|
||||
// Convex holds queries until the stored token is confirmed, so the app can
|
||||
// render cached data while the server handshake is still in progress.
|
||||
|
||||
@@ -74,7 +59,10 @@ export default function AppLayout() {
|
||||
<AddToPlaylistProvider>
|
||||
<FavoritesProvider>
|
||||
<Stack screenOptions={stackOptions}>
|
||||
<Stack.Screen name="(tabs)" options={{ headerShown: false }} />
|
||||
<Stack.Screen
|
||||
name="(tabs)"
|
||||
options={{ headerShown: false, animation: "none" }}
|
||||
/>
|
||||
<Stack.Screen
|
||||
name="settings"
|
||||
options={{
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
import { Redirect } from "expo-router"
|
||||
|
||||
export default function AppIndex() {
|
||||
return <Redirect href="/(app)/(tabs)/home" />
|
||||
}
|
||||
@@ -1,28 +1,7 @@
|
||||
import { useConvexAuth } from "@convex-dev/auth/react"
|
||||
import { Redirect, Stack } from "expo-router"
|
||||
import { Stack } from "expo-router"
|
||||
|
||||
import { LoadingView } from "@/components/ui/primitives"
|
||||
import { useServerConfig } from "@/contexts/server-config"
|
||||
export const unstable_settings = { anchor: "signin" }
|
||||
|
||||
export default function AuthLayout() {
|
||||
const { config, isLoading: serverLoading } = useServerConfig()
|
||||
const auth = useConvexAuth()
|
||||
|
||||
if (serverLoading) {
|
||||
return <LoadingView />
|
||||
}
|
||||
|
||||
if (!config) {
|
||||
return <Redirect href="/server" />
|
||||
}
|
||||
|
||||
if (!auth || auth.isLoading) {
|
||||
return <LoadingView />
|
||||
}
|
||||
|
||||
if (auth.isAuthenticated) {
|
||||
return <Redirect href="/(app)/(tabs)/home" />
|
||||
}
|
||||
|
||||
return <Stack screenOptions={{ headerShown: false }} />
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Stack } from "expo-router"
|
||||
import { useConvexAuth } from "@convex-dev/auth/react"
|
||||
import { DarkTheme, Stack, ThemeProvider } from "expo-router"
|
||||
import { StatusBar } from "expo-status-bar"
|
||||
import { useColorScheme } from "react-native"
|
||||
import { GestureHandlerRootView } from "react-native-gesture-handler"
|
||||
@@ -6,23 +7,57 @@ import { SafeAreaProvider } from "react-native-safe-area-context"
|
||||
|
||||
import { Toaster } from "@/components/ui/toaster"
|
||||
import { ConvexAppProvider } from "@/contexts/convex-app"
|
||||
import { ServerConfigProvider } from "@/contexts/server-config"
|
||||
import { ServerConfigProvider, useServerConfig } from "@/contexts/server-config"
|
||||
|
||||
const navigationTheme = {
|
||||
...DarkTheme,
|
||||
colors: { ...DarkTheme.colors, background: "#000", card: "#000" },
|
||||
}
|
||||
|
||||
function RootNavigator() {
|
||||
const { config, isLoading } = useServerConfig()
|
||||
const auth = useConvexAuth()
|
||||
|
||||
if (isLoading || (config && (!auth || auth.isLoading))) {
|
||||
return null
|
||||
}
|
||||
|
||||
const isAuthenticated = Boolean(config && auth?.isAuthenticated)
|
||||
|
||||
return (
|
||||
<Stack
|
||||
screenOptions={{
|
||||
headerShown: false,
|
||||
animation: "none",
|
||||
contentStyle: { backgroundColor: "#000" },
|
||||
}}
|
||||
>
|
||||
<Stack.Screen name="index" />
|
||||
<Stack.Protected guard={isAuthenticated}>
|
||||
<Stack.Screen name="(app)" />
|
||||
</Stack.Protected>
|
||||
<Stack.Protected guard={Boolean(config) && !isAuthenticated}>
|
||||
<Stack.Screen name="(auth)" />
|
||||
</Stack.Protected>
|
||||
<Stack.Protected guard={!config}>
|
||||
<Stack.Screen name="server" />
|
||||
</Stack.Protected>
|
||||
</Stack>
|
||||
)
|
||||
}
|
||||
|
||||
export default function RootLayout() {
|
||||
const colorScheme = useColorScheme()
|
||||
|
||||
return (
|
||||
<GestureHandlerRootView style={{ flex: 1 }}>
|
||||
<GestureHandlerRootView style={{ flex: 1, backgroundColor: "#000" }}>
|
||||
<SafeAreaProvider>
|
||||
<ServerConfigProvider>
|
||||
<ConvexAppProvider>
|
||||
<StatusBar style={colorScheme === "dark" ? "light" : "dark"} />
|
||||
<Stack screenOptions={{ headerShown: false }}>
|
||||
<Stack.Screen name="index" />
|
||||
<Stack.Screen name="server" />
|
||||
<Stack.Screen name="(auth)" />
|
||||
<Stack.Screen name="(app)" />
|
||||
</Stack>
|
||||
<ThemeProvider value={navigationTheme}>
|
||||
<RootNavigator />
|
||||
</ThemeProvider>
|
||||
<Toaster />
|
||||
</ConvexAppProvider>
|
||||
</ServerConfigProvider>
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
import { useConvexAuth } from "@convex-dev/auth/react"
|
||||
import { Redirect } from "expo-router"
|
||||
|
||||
import { LoadingView } from "@/components/ui/primitives"
|
||||
import { useServerConfig } from "@/contexts/server-config"
|
||||
import { AuthenticatedRedirect } from "@/components/authenticated-redirect"
|
||||
|
||||
export default function Index() {
|
||||
const { config, isLoading } = useServerConfig()
|
||||
|
||||
if (isLoading) {
|
||||
return <LoadingView />
|
||||
}
|
||||
const { config } = useServerConfig()
|
||||
const auth = useConvexAuth()
|
||||
|
||||
if (!config) {
|
||||
return <Redirect href="/server" />
|
||||
}
|
||||
|
||||
return <AuthenticatedRedirect />
|
||||
return (
|
||||
<Redirect
|
||||
href={auth?.isAuthenticated ? "/(app)/(tabs)/library" : "/signin"}
|
||||
/>
|
||||
)
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { useRouter } from "expo-router"
|
||||
import { useEffect, useState } from "react"
|
||||
import { useState } from "react"
|
||||
import {
|
||||
KeyboardAvoidingView,
|
||||
Platform,
|
||||
@@ -14,19 +13,12 @@ import { useServerConfig } from "@/contexts/server-config"
|
||||
import { spacing, useThemeColors } from "@/lib/theme"
|
||||
|
||||
export default function ServerScreen() {
|
||||
const router = useRouter()
|
||||
const colors = useThemeColors()
|
||||
const { config, isLoading, setServerHostname } = useServerConfig()
|
||||
const { setServerHostname } = useServerConfig()
|
||||
const [hostname, setHostname] = useState("")
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [submitting, setSubmitting] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
if (config?.hostname) {
|
||||
setHostname(config.hostname)
|
||||
}
|
||||
}, [config?.hostname])
|
||||
|
||||
async function handleContinue() {
|
||||
setError(null)
|
||||
setSubmitting(true)
|
||||
@@ -38,7 +30,6 @@ export default function ServerScreen() {
|
||||
)
|
||||
return
|
||||
}
|
||||
router.replace("/signin")
|
||||
} catch {
|
||||
setError(
|
||||
"Could not save the server address. Check device storage and try again."
|
||||
@@ -48,10 +39,6 @@ export default function ServerScreen() {
|
||||
}
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return null
|
||||
}
|
||||
|
||||
return (
|
||||
<SafeAreaView style={[styles.safe, { backgroundColor: colors.background }]}>
|
||||
<KeyboardAvoidingView
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useAuthActions, useConvexAuth } from "@convex-dev/auth/react"
|
||||
import { Link, useRouter } from "expo-router"
|
||||
import { useAuthActions } from "@convex-dev/auth/react"
|
||||
import { Link } from "expo-router"
|
||||
import { useQuery } from "convex/react"
|
||||
import { useEffect, useState } from "react"
|
||||
import { useState } from "react"
|
||||
import {
|
||||
KeyboardAvoidingView,
|
||||
Platform,
|
||||
@@ -16,6 +16,7 @@ import { Button, Card, Input } from "@/components/ui/primitives"
|
||||
import { useServerConfig } from "@/contexts/server-config"
|
||||
import { api } from "@otunes/convex/_generated/api"
|
||||
import { spacing, useThemeColors } from "@/lib/theme"
|
||||
import { showError } from "@/lib/toast"
|
||||
|
||||
type AuthFlow = "signIn" | "signUp"
|
||||
|
||||
@@ -50,10 +51,8 @@ const copy: Record<
|
||||
|
||||
export function AuthForm({ flow }: { flow: AuthFlow }) {
|
||||
const { signIn } = useAuthActions()
|
||||
const auth = useConvexAuth()
|
||||
const router = useRouter()
|
||||
const colors = useThemeColors()
|
||||
const { config } = useServerConfig()
|
||||
const { config, clearServer } = useServerConfig()
|
||||
const appSettings = useQuery(api.users.getAppSettings)
|
||||
const [name, setName] = useState("")
|
||||
const [email, setEmail] = useState("")
|
||||
@@ -61,12 +60,6 @@ export function AuthForm({ flow }: { flow: AuthFlow }) {
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
const [submitting, setSubmitting] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
if (auth.isAuthenticated) {
|
||||
router.replace("/(app)/(tabs)/home")
|
||||
}
|
||||
}, [auth.isAuthenticated, router])
|
||||
|
||||
const text = copy[flow]
|
||||
const accountCreationDisabled = appSettings?.disableAccountCreation ?? false
|
||||
const signupsDisabled = flow === "signUp" && accountCreationDisabled
|
||||
@@ -164,11 +157,16 @@ export function AuthForm({ flow }: { flow: AuthFlow }) {
|
||||
)}
|
||||
</Card>
|
||||
|
||||
<Link href="/server" asChild>
|
||||
<Pressable style={styles.changeServer}>
|
||||
<Text style={{ color: colors.mutedForeground }}>Change server</Text>
|
||||
</Pressable>
|
||||
</Link>
|
||||
<Pressable
|
||||
style={styles.changeServer}
|
||||
onPress={() =>
|
||||
void clearServer().catch(() =>
|
||||
showError("Could not clear the server address.")
|
||||
)
|
||||
}
|
||||
>
|
||||
<Text style={{ color: colors.mutedForeground }}>Change server</Text>
|
||||
</Pressable>
|
||||
</KeyboardAvoidingView>
|
||||
</SafeAreaView>
|
||||
)
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
import { useConvexAuth } from "@convex-dev/auth/react"
|
||||
import { Redirect } from "expo-router"
|
||||
|
||||
import { LoadingView } from "@/components/ui/primitives"
|
||||
|
||||
export function AuthenticatedRedirect() {
|
||||
const auth = useConvexAuth()
|
||||
|
||||
if (!auth) {
|
||||
return <LoadingView />
|
||||
}
|
||||
|
||||
const { isLoading, isAuthenticated } = auth
|
||||
|
||||
if (isLoading) {
|
||||
return <LoadingView />
|
||||
}
|
||||
|
||||
if (!isAuthenticated) {
|
||||
return <Redirect href="/signin" />
|
||||
}
|
||||
|
||||
return <Redirect href="/(app)/(tabs)/home" />
|
||||
}
|
||||
@@ -22,11 +22,11 @@ export function ConvexAppProvider({ children }: { children: ReactNode }) {
|
||||
return
|
||||
}
|
||||
await clearStoredAuthTokens(config.convexUrl)
|
||||
try {
|
||||
await clearAppIntentCatalogs()
|
||||
} finally {
|
||||
setAuthEpoch((epoch) => epoch + 1)
|
||||
}
|
||||
setAuthEpoch((epoch) => epoch + 1)
|
||||
// Native catalog cleanup can be slow; it must not hold the signed-in UI open.
|
||||
void clearAppIntentCatalogs().catch((error) => {
|
||||
console.warn("Could not clear app intent catalogs.", error)
|
||||
})
|
||||
}, [config])
|
||||
|
||||
if (!client || !config) {
|
||||
|
||||
+22
-12
@@ -1,22 +1,32 @@
|
||||
import type { useRouter } from "expo-router"
|
||||
|
||||
import { clearAppIntentCatalogs } from "@/lib/app-intent-handled"
|
||||
import { resetPlaybackStores } from "@otunes/convex/client/playback-store"
|
||||
import { clearPersistentCache } from "@/lib/persistent-cache"
|
||||
|
||||
type AppRouter = ReturnType<typeof useRouter>
|
||||
|
||||
/** Forgets this device's cached library and playback state. */
|
||||
export async function clearLocalData() {
|
||||
resetPlaybackStores()
|
||||
await Promise.all([clearPersistentCache(), clearAppIntentCatalogs()])
|
||||
await clearPersistentCache()
|
||||
}
|
||||
|
||||
export async function signOutToSignIn(
|
||||
router: Pick<AppRouter, "replace">,
|
||||
invalidateSession: () => Promise<void>
|
||||
/** Root route guards leave the app when the session or server is cleared. */
|
||||
export async function signOut(invalidateSession: () => Promise<void>) {
|
||||
try {
|
||||
await invalidateSession()
|
||||
} finally {
|
||||
await clearLocalData()
|
||||
}
|
||||
}
|
||||
|
||||
export async function changeServer(
|
||||
invalidateSession: () => Promise<void>,
|
||||
clearServer: () => Promise<void>
|
||||
) {
|
||||
await invalidateSession()
|
||||
await clearLocalData()
|
||||
router.replace("/signin")
|
||||
try {
|
||||
await invalidateSession()
|
||||
} finally {
|
||||
try {
|
||||
await clearServer()
|
||||
} finally {
|
||||
await clearLocalData()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user