From 8d76c35d69fa375f288c9f398cf1dcb5483c5aa9 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Tue, 25 Feb 2025 11:33:27 +0800 Subject: [PATCH] feat(mobile): toggle unread only (#2873) --- .../src/components/ui/button/UIBarButton.tsx | 5 ++++- .../modules/entry-list/EntryListContentArticle.tsx | 2 +- .../modules/entry-list/EntryListContentGrid.tsx | 2 +- .../modules/entry-list/EntryListContentSocial.tsx | 2 +- apps/mobile/src/modules/entry-list/hooks.ts | 14 ++++++++++---- apps/mobile/src/modules/screen/action.tsx | 14 ++++++++++++++ apps/mobile/src/modules/screen/atoms.ts | 6 ------ apps/mobile/src/services/entry.ts | 7 ++++++- apps/mobile/src/store/entry/hooks.ts | 12 ++++++++---- apps/mobile/src/store/entry/store.ts | 7 +++++-- 10 files changed, 50 insertions(+), 21 deletions(-) diff --git a/apps/mobile/src/components/ui/button/UIBarButton.tsx b/apps/mobile/src/components/ui/button/UIBarButton.tsx index 9d20d385a..f28c7238d 100644 --- a/apps/mobile/src/components/ui/button/UIBarButton.tsx +++ b/apps/mobile/src/components/ui/button/UIBarButton.tsx @@ -9,6 +9,8 @@ interface UIBarButtonProps extends PressableProps { normalIcon: React.ReactNode selectedIcon?: React.ReactNode + + noOverlay?: boolean } export const UIBarButton = ({ normalIcon, @@ -16,6 +18,7 @@ export const UIBarButton = ({ onPress, selected, label, + noOverlay, ...props }: UIBarButtonProps) => { @@ -30,7 +33,7 @@ export const UIBarButton = ({ aria-label={label} {...props} > - {selected && } + {selected && !noOverlay && } {!hasDifferentIcon ? ( normalIcon diff --git a/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx b/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx index 6ca45a11d..c681bb2f0 100644 --- a/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx +++ b/apps/mobile/src/modules/entry-list/EntryListContentArticle.tsx @@ -31,7 +31,7 @@ export const EntryListContentArticle = forwardRef< [isFetching], ) - const onViewableItemsChanged = useOnViewableItemsChanged() + const onViewableItemsChanged = useOnViewableItemsChanged({ isLoading: isRefetching }) return ( , "data" | "renderItem"> >(({ entryIds, ...rest }, ref) => { const { fetchNextPage, refetch, isRefetching, hasNextPage } = useFetchEntriesControls() - const onViewableItemsChanged = useOnViewableItemsChanged() + const onViewableItemsChanged = useOnViewableItemsChanged({ isLoading: isRefetching }) return ( item.key -export function useOnViewableItemsChanged( - idExtractor: (item: ViewToken) => string = defaultIdExtractor, -): (info: { viewableItems: ViewToken[]; changed: ViewToken[] }) => void { +export function useOnViewableItemsChanged({ + idExtractor = defaultIdExtractor, + isLoading, +}: { + isLoading?: boolean + idExtractor?: (item: ViewToken) => string +} = {}): (info: { viewableItems: ViewToken[]; changed: ViewToken[] }) => void { const markAsReadWhenScrolling = useGeneralSettingKey("scrollMarkUnread") const markAsReadWhenRendering = useGeneralSettingKey("renderMarkUnread") @@ -16,6 +20,8 @@ export function useOnViewableItemsChanged( return useCallback( ({ viewableItems, changed }) => { + if (isLoading) return + debouncedFetchEntryContentByStream(viewableItems.map((item) => stableIdExtractor(item))) if (markAsReadWhenScrolling) { changed @@ -33,6 +39,6 @@ export function useOnViewableItemsChanged( }) } }, - [markAsReadWhenRendering, markAsReadWhenScrolling, stableIdExtractor], + [markAsReadWhenRendering, markAsReadWhenScrolling, stableIdExtractor, isLoading], ) } diff --git a/apps/mobile/src/modules/screen/action.tsx b/apps/mobile/src/modules/screen/action.tsx index 495b31727..c66ae90b9 100644 --- a/apps/mobile/src/modules/screen/action.tsx +++ b/apps/mobile/src/modules/screen/action.tsx @@ -3,6 +3,7 @@ import { router } from "expo-router" import type { PropsWithChildren } from "react" import { TouchableOpacity, View } from "react-native" +import { setGeneralSetting, useGeneralSettingKey } from "@/src/atoms/settings/general" import { setUISetting, useUISettingKey } from "@/src/atoms/settings/ui" import { UserAvatar } from "@/src/components/ui/avatar/UserAvatar" import { UIBarButton } from "@/src/components/ui/button/UIBarButton" @@ -10,6 +11,8 @@ import { AddCuteReIcon } from "@/src/icons/add_cute_re" import { CheckCircleCuteReIcon } from "@/src/icons/check_circle_cute_re" import { PhotoAlbumCuteFiIcon } from "@/src/icons/photo_album_cute_fi" import { PhotoAlbumCuteReIcon } from "@/src/icons/photo_album_cute_re" +import { RoundCuteFiIcon } from "@/src/icons/round_cute_fi" +import { RoundCuteReIcon } from "@/src/icons/round_cute_re" import { Dialog } from "@/src/lib/dialog" import { useWhoami } from "@/src/store/user/hooks" import { accentColor, useColor } from "@/src/theme/colors" @@ -36,9 +39,20 @@ export function HomeLeftAction() { } export function HomeSharedRightAction(props: PropsWithChildren) { + const unreadOnly = useGeneralSettingKey("unreadOnly") return ( {props.children} + } + selectedIcon={} + onPress={() => { + setGeneralSetting("unreadOnly", !unreadOnly) + }} + selected={unreadOnly} + noOverlay + /> } diff --git a/apps/mobile/src/modules/screen/atoms.ts b/apps/mobile/src/modules/screen/atoms.ts index 4ed79c689..095131e25 100644 --- a/apps/mobile/src/modules/screen/atoms.ts +++ b/apps/mobile/src/modules/screen/atoms.ts @@ -173,12 +173,6 @@ export function useSelectedFeed() { const selectedTimeline = useAtomValue(selectedTimelineAtom) const selectedFeed = useAtomValue(selectedFeedAtom) - - const payload = getFetchEntryPayload( - entryListContext.type === "feed" ? selectedFeed : selectedTimeline, - ) - usePrefetchEntries(payload) - return entryListContext.type === "feed" ? selectedFeed : selectedTimeline } diff --git a/apps/mobile/src/services/entry.ts b/apps/mobile/src/services/entry.ts index dd15cad45..d1e49ec5a 100644 --- a/apps/mobile/src/services/entry.ts +++ b/apps/mobile/src/services/entry.ts @@ -1,5 +1,6 @@ import { eq, inArray, or } from "drizzle-orm" +import { getGeneralSettings } from "../atoms/settings/general" import { db } from "../database" import { entriesTable } from "../database/schemas" import type { EntrySchema } from "../database/schemas/types" @@ -52,7 +53,11 @@ class EntryServiceStatic implements Hydratable, Resetable { async hydrate() { const entries = await db.query.entriesTable.findMany() - entryActions.upsertManyInSession(entries.map((e) => dbStoreMorph.toEntryModel(e))) + const { unreadOnly } = getGeneralSettings() + entryActions.upsertManyInSession( + entries.map((e) => dbStoreMorph.toEntryModel(e)), + { unreadOnly }, + ) } } diff --git a/apps/mobile/src/store/entry/hooks.ts b/apps/mobile/src/store/entry/hooks.ts index 6635cfd1a..7eaa62032 100644 --- a/apps/mobile/src/store/entry/hooks.ts +++ b/apps/mobile/src/store/entry/hooks.ts @@ -2,15 +2,19 @@ import type { FeedViewType } from "@follow/constants" import { useInfiniteQuery, useMutation, useQuery } from "@tanstack/react-query" import { useCallback, useEffect } from "react" +import { useGeneralSettingKey } from "@/src/atoms/settings/general" + import { getEntry } from "./getter" import { entrySyncServices, useEntryStore } from "./store" import type { EntryModel, FetchEntriesProps } from "./types" -export const usePrefetchEntries = (props: Omit | null) => { - const { feedId, inboxId, listId, view, read, limit } = props || {} +export const usePrefetchEntries = (props: Omit | null) => { + const { feedId, inboxId, listId, view, limit } = props || {} + const unreadOnly = useGeneralSettingKey("unreadOnly") return useInfiniteQuery({ - queryKey: ["entries", feedId, inboxId, listId, view, read, limit], - queryFn: ({ pageParam }) => entrySyncServices.fetchEntries({ ...props, pageParam }), + queryKey: ["entries", feedId, inboxId, listId, view, unreadOnly, limit], + queryFn: ({ pageParam }) => + entrySyncServices.fetchEntries({ ...props, pageParam, read: unreadOnly ? false : undefined }), getNextPageParam: (lastPage) => listId ? lastPage.data?.at(-1)?.entries.insertedAt diff --git a/apps/mobile/src/store/entry/store.ts b/apps/mobile/src/store/entry/store.ts index cddd4b547..e3a5a93a7 100644 --- a/apps/mobile/src/store/entry/store.ts +++ b/apps/mobile/src/store/entry/store.ts @@ -124,15 +124,18 @@ class EntryActions { } } - upsertManyInSession(entries: EntryModel[]) { + upsertManyInSession(entries: EntryModel[], options?: { unreadOnly?: boolean }) { if (entries.length === 0) return + const { unreadOnly } = options ?? {} immerSet((draft) => { for (const entry of entries) { draft.entryIdSet.add(entry.id) draft.data[entry.id] = entry - const { feedId, inboxHandle } = entry + const { feedId, inboxHandle, read } = entry + if (unreadOnly && read) continue + this.addEntryIdToFeed({ draft, feedId,