refactor: move image sync service to mobile, bring hydration back

This commit is contained in:
Stephen Zhou
2025-06-16 16:25:43 +08:00
parent 53957468c9
commit 95b23c3be7
8 changed files with 39 additions and 32 deletions
@@ -1,4 +1,3 @@
import { usePrefetchImageColors } from "@follow/store/image/hooks"
import { cn } from "@follow/utils"
import { useAtomValue } from "jotai"
import { use, useEffect } from "react"
@@ -15,6 +14,7 @@ import { BottomTabContext } from "@/src/lib/navigation/bottom-tab/BottomTabConte
import { useNavigation } from "@/src/lib/navigation/hooks"
import { useActiveTrack } from "@/src/lib/player"
import { PlayerScreen } from "@/src/screens/PlayerScreen"
import { usePrefetchImageColors } from "@/src/store/image/hooks"
import { PlayPauseButton, SeekButton } from "./control"
@@ -1,4 +1,4 @@
import { useImageColors, usePrefetchImageColors } from "@follow/store/image/hooks"
import { useImageColors } from "@follow/store/image/hooks"
import { useUserById } from "@follow/store/user/hooks"
import { cn, getLuminance } from "@follow/utils"
import { LinearGradient } from "expo-linear-gradient"
@@ -21,6 +21,7 @@ import { WebCuteReIcon } from "@/src/icons/web_cute_re"
import { YoutubeCuteFiIcon } from "@/src/icons/youtube_cute_fi"
import { useNavigation } from "@/src/lib/navigation/hooks"
import { LoginScreen } from "@/src/screens/(modal)/LoginScreen"
import { usePrefetchImageColors } from "@/src/store/image/hooks"
const defaultGradientColors = ["#000", "#100", "#200"]
+1 -1
View File
@@ -1,4 +1,3 @@
import { usePrefetchImageColors } from "@follow/store/image/hooks"
import { cn } from "@follow/utils"
import { LinearGradient } from "expo-linear-gradient"
import { useEffect, useMemo } from "react"
@@ -20,6 +19,7 @@ import { useActiveTrack, useIsPlaying } from "../lib/player"
import { PlayerScreenContext, usePlayerScreenContext } from "../modules/player/context"
import { ControlGroup, ProgressBar, VolumeBar } from "../modules/player/control"
import { useCoverGradient } from "../modules/player/hooks"
import { usePrefetchImageColors } from "../store/image/hooks"
function CoverArt({ cover }: { cover?: string }) {
const scale = useSharedValue(1)
+11
View File
@@ -0,0 +1,11 @@
import { useQuery } from "@tanstack/react-query"
import { imageSyncService } from "./store"
export const usePrefetchImageColors = (url?: string | null) => {
useQuery({
queryKey: ["image", "colors", url],
queryFn: () => imageSyncService.getColors(url),
enabled: !!url,
})
}
+21
View File
@@ -0,0 +1,21 @@
import { getImageInfo } from "@follow/store/image/getters"
import { imageActions } from "@follow/store/image/store"
import ImageColors from "react-native-image-colors"
class ImageSyncService {
async getColors(url?: string | null) {
if (!url) {
return
}
const existing = getImageInfo(url)?.colors
if (existing) {
return existing
}
const result = await ImageColors.getColors(url, { cache: true })
await imageActions.upsertMany([{ url, colors: result }])
return result
}
}
export const imageSyncService = new ImageSyncService()
+2
View File
@@ -3,6 +3,7 @@ import { initializeDB, migrateDB } from "@follow/database/db"
import { collectionActions } from "./collection/store"
import { entryActions } from "./entry/store"
import { feedActions } from "./feed/store"
import { imageActions } from "./image/store"
import { inboxActions } from "./inbox/store"
import type { Hydratable } from "./internal/base"
import { listActions } from "./list/store"
@@ -23,6 +24,7 @@ const hydrates: Hydratable[] = [
collectionActions,
summaryActions,
translationActions,
imageActions,
]
export const hydrateDatabaseToStore = async (options?: { migrateDatabase?: boolean }) => {
+1 -10
View File
@@ -1,15 +1,6 @@
import { useQuery } from "@tanstack/react-query"
import { useCallback } from "react"
import { imageSyncService, useImagesStore } from "./store"
export const usePrefetchImageColors = (url?: string | null) => {
useQuery({
queryKey: ["image", "colors", url],
queryFn: () => imageSyncService.getColors(url),
enabled: !!url,
})
}
import { useImagesStore } from "./store"
export const useImageColors = (url?: string | null) => {
return useImagesStore(
@@ -1,10 +1,8 @@
import type { ImageSchema } from "@follow/database/schemas/types"
import { ImagesService } from "@follow/database/services/image"
import ImageColors from "react-native-image-colors"
import type { Hydratable } from "../internal/base"
import { createImmerSetter, createTransaction, createZustandStore } from "../internal/helper"
import { getImageInfo } from "./getters"
export type ImageModel = ImageSchema
type ImageStore = {
@@ -17,22 +15,6 @@ export const useImagesStore = createZustandStore<ImageStore>("images")(() => ({
const immerSet = createImmerSetter(useImagesStore)
class ImageSyncService {
async getColors(url?: string | null) {
if (!url) {
return
}
const existing = getImageInfo(url)?.colors
if (existing) {
return existing
}
const result = await ImageColors.getColors(url, { cache: true })
await imageActions.upsertMany([{ url, colors: result }])
return result
}
}
class ImageActions implements Hydratable {
async hydrate() {
const images = await ImagesService.getImageAll()
@@ -55,5 +37,4 @@ class ImageActions implements Hydratable {
}
}
export const imageSyncService = new ImageSyncService()
export const imageActions = new ImageActions()