feat(mobile): toggle unread only (#2873)

This commit is contained in:
Stephen Zhou
2025-02-25 11:33:27 +08:00
committed by GitHub
parent e40f8a9bbd
commit 8d76c35d69
10 changed files with 50 additions and 21 deletions
@@ -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 && <ButtonOverlay />}
{selected && !noOverlay && <ButtonOverlay />}
{!hasDifferentIcon ? (
normalIcon
@@ -31,7 +31,7 @@ export const EntryListContentArticle = forwardRef<
[isFetching],
)
const onViewableItemsChanged = useOnViewableItemsChanged()
const onViewableItemsChanged = useOnViewableItemsChanged({ isLoading: isRefetching })
return (
<TimelineSelectorList
@@ -18,7 +18,7 @@ export const EntryListContentGrid = forwardRef<
} & Omit<MasonryFlashListProps<string>, "data" | "renderItem">
>(({ entryIds, ...rest }, ref) => {
const { fetchNextPage, refetch, isRefetching, hasNextPage } = useFetchEntriesControls()
const onViewableItemsChanged = useOnViewableItemsChanged()
const onViewableItemsChanged = useOnViewableItemsChanged({ isLoading: isRefetching })
return (
<TimelineSelectorMasonryList
@@ -25,7 +25,7 @@ export const EntryListContentSocial = forwardRef<
[isFetching],
)
const onViewableItemsChanged = useOnViewableItemsChanged()
const onViewableItemsChanged = useOnViewableItemsChanged({ isLoading: isRefetching })
return (
<TimelineSelectorList
+10 -4
View File
@@ -6,9 +6,13 @@ import { debouncedFetchEntryContentByStream } from "@/src/store/entry/store"
import { unreadSyncService } from "@/src/store/unread/store"
const defaultIdExtractor = (item: ViewToken) => 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],
)
}
+14
View File
@@ -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 (
<ActionGroup className="-mr-2">
{props.children}
<UIBarButton
label={unreadOnly ? "Show All" : "Show Unread Only"}
normalIcon={<RoundCuteReIcon height={20} width={20} color={accentColor} />}
selectedIcon={<RoundCuteFiIcon height={20} width={20} color={accentColor} />}
onPress={() => {
setGeneralSetting("unreadOnly", !unreadOnly)
}}
selected={unreadOnly}
noOverlay
/>
<UIBarButton
label="Mark All as Read"
normalIcon={<CheckCircleCuteReIcon height={20} width={20} color={accentColor} />}
-6
View File
@@ -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
}
+6 -1
View File
@@ -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 },
)
}
}
+8 -4
View File
@@ -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<FetchEntriesProps, "pageParam"> | null) => {
const { feedId, inboxId, listId, view, read, limit } = props || {}
export const usePrefetchEntries = (props: Omit<FetchEntriesProps, "pageParam" | "read"> | 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
+5 -2
View File
@@ -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,