fix(mobile): handle warm links and show artwork in Android sharing
This commit is contained in:
4 files changed
+37
-12
No files matched your search
@@ -1,6 +1,10 @@
|
||||
import { router, usePathname, type Href } from "expo-router"
|
||||
import { router, type Href } from "expo-router"
|
||||
import { useServerConfig } from "@/contexts/server-config"
|
||||
import { takeIncomingMediaLink } from "@/lib/incoming-media-link"
|
||||
import {
|
||||
incomingMediaLink,
|
||||
subscribeIncomingMediaLink,
|
||||
takeIncomingMediaLink,
|
||||
} from "@/lib/incoming-media-link"
|
||||
import { useConvexAuth as useLocalConvexAuth } from "@convex-dev/auth/react"
|
||||
import {
|
||||
useConvexAuth as useServerConvexAuth,
|
||||
@@ -8,7 +12,7 @@ import {
|
||||
} from "convex/react"
|
||||
import { Stack } from "expo-router"
|
||||
import { Platform } from "react-native"
|
||||
import { useEffect } from "react"
|
||||
import { useEffect, useSyncExternalStore } from "react"
|
||||
|
||||
import { AppIntentBridge } from "@/components/app-intent-bridge"
|
||||
import { ConvexAuthRecovery } from "@/components/convex-auth-recovery"
|
||||
@@ -28,13 +32,17 @@ export default function AppLayout() {
|
||||
const { invalidateSession } = useInvalidateAuthSession()
|
||||
const stackOptions = useStackScreenOptions()
|
||||
const { config } = useServerConfig()
|
||||
const pathname = usePathname()
|
||||
const pendingDestination = useSyncExternalStore(
|
||||
subscribeIncomingMediaLink,
|
||||
() => (config ? incomingMediaLink(config.serverUrl) : null),
|
||||
() => null
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (!config) return
|
||||
if (!config || !pendingDestination) return
|
||||
const destination = takeIncomingMediaLink(config.serverUrl)
|
||||
if (destination) router.replace(destination as Href)
|
||||
}, [config, pathname])
|
||||
}, [config, pendingDestination])
|
||||
|
||||
useEffect(() => {
|
||||
if (
|
||||
|
||||
@@ -43,12 +43,10 @@ export async function redirectSystemPath({
|
||||
const songId = target.searchParams.get("s")
|
||||
if (target.pathname.startsWith("/album/") && songId) query.set("s", songId)
|
||||
const suffix = query.size ? `?${query}` : ""
|
||||
rememberIncomingMediaLink(
|
||||
`/(app)/(tabs)/home${target.pathname}${suffix}`,
|
||||
target.origin
|
||||
)
|
||||
const destination = `/(app)/(tabs)/home${target.pathname}${suffix}`
|
||||
rememberIncomingMediaLink(destination, target.origin)
|
||||
return config
|
||||
? "/"
|
||||
? destination
|
||||
: `/server?hostname=${encodeURIComponent(server.hostname)}`
|
||||
} catch {
|
||||
return "/"
|
||||
|
||||
@@ -1,4 +1,13 @@
|
||||
// Keep the destination while server setup or sign-in is on screen.
|
||||
const listeners = new Set<() => void>()
|
||||
|
||||
export function subscribeIncomingMediaLink(listener: () => void) {
|
||||
listeners.add(listener)
|
||||
return () => {
|
||||
listeners.delete(listener)
|
||||
}
|
||||
}
|
||||
|
||||
let pendingLink: { path: string; serverOrigin: string } | null = null
|
||||
|
||||
export function incomingMediaLink(serverUrl: string) {
|
||||
@@ -11,9 +20,11 @@ export function incomingMediaLink(serverUrl: string) {
|
||||
export function takeIncomingMediaLink(serverUrl: string) {
|
||||
const path = incomingMediaLink(serverUrl)
|
||||
pendingLink = null
|
||||
for (const listener of listeners) listener()
|
||||
return path
|
||||
}
|
||||
|
||||
export function rememberIncomingMediaLink(path: string, serverOrigin: string) {
|
||||
pendingLink = { path, serverOrigin }
|
||||
for (const listener of listeners) listener()
|
||||
}
|
||||
@@ -16,6 +16,7 @@ class MediaLinkShare : Module() {
|
||||
val intent = Intent(Intent.ACTION_SEND).apply {
|
||||
type = "text/plain"
|
||||
putExtra(Intent.EXTRA_TEXT, link)
|
||||
putExtra(Intent.EXTRA_TITLE, "oTunes")
|
||||
}
|
||||
// ClipData supplies the chooser thumbnail while the shared item stays a URL.
|
||||
try {
|
||||
@@ -26,7 +27,14 @@ class MediaLinkShare : Module() {
|
||||
context.cacheDir.listFiles()?.filter {
|
||||
it.name.startsWith("media-link-cover-") && it.lastModified() < System.currentTimeMillis() - 86400000
|
||||
}?.forEach { it.delete() }
|
||||
val file = File(context.cacheDir, "media-link-cover-${java.util.UUID.randomUUID()}.img")
|
||||
val extension = when (connection.contentType?.substringBefore(";")) {
|
||||
"image/webp" -> "webp"
|
||||
"image/png" -> "png"
|
||||
"image/jpeg" -> "jpg"
|
||||
"image/gif" -> "gif"
|
||||
else -> error("Artwork format is not supported")
|
||||
}
|
||||
val file = File(context.cacheDir, "media-link-cover-${java.util.UUID.randomUUID()}.${extension}")
|
||||
connection.getInputStream().use { input ->
|
||||
file.outputStream().use { output -> input.copyTo(output) }
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user