Skip to content

Commit 48e6d73

Browse files
juliusmarmingecodex
andcommitted
feat(mobile): animate thread settings changes
Co-authored-by: codex <[email protected]>
1 parent 1755f01 commit 48e6d73

1 file changed

Lines changed: 116 additions & 47 deletions

File tree

‎apps/mobile/src/features/threads/ThreadSettingsSheet.tsx‎

Lines changed: 116 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import type {
44
ProviderOptionSelection,
55
RuntimeMode,
66
} from "@t3tools/contracts";
7-
import { LegendList, type LegendListRenderItemProps } from "@legendapp/list/react-native";
7+
import type { LegendListRenderItemProps } from "@legendapp/list/react-native";
8+
import { AnimatedLegendList } from "@legendapp/list/reanimated";
89
import { HeaderHeightContext } from "@react-navigation/elements";
910
import {
1011
getProviderOptionCurrentLabel,
@@ -27,6 +28,7 @@ import {
2728
type ReactNode,
2829
} from "react";
2930
import { Platform, Pressable, ScrollView, Switch, TextInput, View } from "react-native";
31+
import Animated, { FadeIn, FadeOut, LinearTransition } from "react-native-reanimated";
3032
import { useSafeAreaInsets } from "react-native-safe-area-context";
3133

3234
import { SymbolView } from "../../components/AppSymbol";
@@ -76,6 +78,12 @@ const THREAD_SETTINGS_MAINTAIN_VISIBLE_CONTENT_POSITION = {
7678
data: false,
7779
size: true,
7880
} as const;
81+
const THREAD_SETTINGS_CATALOG_LAYOUT_TRANSITION = LinearTransition.duration(180);
82+
const THREAD_SETTINGS_CATALOG_ENTER_TRANSITION = FadeIn.duration(140);
83+
const THREAD_SETTINGS_CATALOG_EXIT_TRANSITION = FadeOut.duration(120);
84+
const THREAD_SETTINGS_OPTIONS_LAYOUT_TRANSITION = LinearTransition.duration(180);
85+
const THREAD_SETTINGS_OPTION_ENTER_TRANSITION = FadeIn.duration(140);
86+
const THREAD_SETTINGS_OPTION_EXIT_TRANSITION = FadeOut.duration(100);
7987
const THREAD_SETTINGS_HEADER_SCROLL_EDGE_EFFECTS = nativeHeaderScrollEdgeEffects(
8088
Platform.OS,
8189
Platform.Version,
@@ -525,6 +533,14 @@ type ThreadSettingsCatalogItem =
525533
readonly option: ModelOption;
526534
readonly isFirst: boolean;
527535
readonly isLast: boolean;
536+
}
537+
| {
538+
readonly kind: "empty";
539+
readonly key: "empty";
540+
}
541+
| {
542+
readonly kind: "options";
543+
readonly key: "options";
528544
};
529545

530546
function ThreadSettingsModelListRow(props: {
@@ -641,23 +657,8 @@ function useThreadSettingsCatalogItems(
641657
);
642658
}
643659

644-
function renderThreadSettingsCatalogItem(
645-
props: LegendListRenderItemProps<ThreadSettingsCatalogItem>,
646-
) {
647-
if (props.item.kind === "provider") {
648-
return <ThreadSettingsProviderListHeader provider={props.item.provider} />;
649-
}
650-
651-
return (
652-
<ThreadSettingsModelListRow
653-
isFirst={props.item.isFirst}
654-
isLast={props.item.isLast}
655-
option={props.item.option}
656-
/>
657-
);
658-
}
659-
660-
function ThreadSettingsOptionsFooter(props: {
660+
function ThreadSettingsOptionsItem(props: {
661+
readonly animationsReady: boolean;
661662
readonly onOpenSubmenu: (submenu: ThreadSettingsSubmenuPage) => void;
662663
}) {
663664
const insets = useSafeAreaInsets();
@@ -670,34 +671,55 @@ function ThreadSettingsOptionsFooter(props: {
670671
return (
671672
<View style={{ paddingBottom: insets.bottom + bottomToolbarInset + 12 }}>
672673
<Text className="px-5 pb-2 pt-2 text-sm font-t3-medium text-foreground-muted">Options</Text>
673-
<View className="mx-4 overflow-hidden rounded-2xl bg-card">
674+
<Animated.View
675+
className="mx-4 overflow-hidden rounded-2xl bg-card"
676+
layout={THREAD_SETTINGS_OPTIONS_LAYOUT_TRANSITION}
677+
>
674678
{session.displayedDescriptors.map((descriptor) => {
675679
if (descriptor.type === "select") {
676680
return (
677-
<DisclosureRow
681+
<Animated.View
678682
key={descriptor.id}
679-
label={descriptor.label}
680-
value={getProviderOptionCurrentLabel(descriptor)}
681-
onPress={() => props.onOpenSubmenu({ kind: "descriptor", id: descriptor.id })}
682-
/>
683+
entering={
684+
props.animationsReady ? THREAD_SETTINGS_OPTION_ENTER_TRANSITION : undefined
685+
}
686+
exiting={props.animationsReady ? THREAD_SETTINGS_OPTION_EXIT_TRANSITION : undefined}
687+
layout={THREAD_SETTINGS_OPTIONS_LAYOUT_TRANSITION}
688+
>
689+
<DisclosureRow
690+
label={descriptor.label}
691+
value={getProviderOptionCurrentLabel(descriptor)}
692+
onPress={() => props.onOpenSubmenu({ kind: "descriptor", id: descriptor.id })}
693+
/>
694+
</Animated.View>
683695
);
684696
}
685697
return (
686-
<SwitchRow
698+
<Animated.View
687699
key={descriptor.id}
688-
label={descriptor.label}
689-
value={descriptor.currentValue ?? false}
690-
onValueChange={(value) => session.applyOptionChange(descriptor.id, value)}
691-
/>
700+
entering={props.animationsReady ? THREAD_SETTINGS_OPTION_ENTER_TRANSITION : undefined}
701+
exiting={props.animationsReady ? THREAD_SETTINGS_OPTION_EXIT_TRANSITION : undefined}
702+
layout={THREAD_SETTINGS_OPTIONS_LAYOUT_TRANSITION}
703+
>
704+
<SwitchRow
705+
label={descriptor.label}
706+
value={descriptor.currentValue ?? false}
707+
onValueChange={(value) => session.applyOptionChange(descriptor.id, value)}
708+
/>
709+
</Animated.View>
692710
);
693711
})}
694-
<DisclosureRow
695-
isLast
696-
label="Runtime"
697-
value={RUNTIME_MODE_CHOICES.find((choice) => choice.mode === session.runtimeMode)?.label}
698-
onPress={() => props.onOpenSubmenu({ kind: "runtime" })}
699-
/>
700-
</View>
712+
<Animated.View layout={THREAD_SETTINGS_OPTIONS_LAYOUT_TRANSITION}>
713+
<DisclosureRow
714+
isLast
715+
label="Runtime"
716+
value={
717+
RUNTIME_MODE_CHOICES.find((choice) => choice.mode === session.runtimeMode)?.label
718+
}
719+
onPress={() => props.onOpenSubmenu({ kind: "runtime" })}
720+
/>
721+
</Animated.View>
722+
</Animated.View>
701723

702724
{Platform.OS !== "ios" && session.hasLegacyModels ? (
703725
<>
@@ -724,32 +746,78 @@ function ThreadSettingsMainContent(props: {
724746
}) {
725747
const session = useThreadSettingsSession();
726748
const catalogItems = useThreadSettingsCatalogItems(session);
749+
const [animationsReady, setAnimationsReady] = useState(false);
727750
const nativeHeaderHeight = use(HeaderHeightContext) ?? 0;
728751
const hasActiveCatalogFilter =
729752
session.providerFilter !== null || session.searchQuery.trim().length > 0;
730753
const usesTransparentNativeHeader = Platform.OS === "ios" && NATIVE_LIQUID_GLASS_SUPPORTED;
754+
const listItems = useMemo<ReadonlyArray<ThreadSettingsCatalogItem>>(
755+
() => [
756+
...(catalogItems.length === 0 && hasActiveCatalogFilter
757+
? ([{ kind: "empty", key: "empty" }] as const)
758+
: catalogItems),
759+
{ kind: "options", key: "options" },
760+
],
761+
[catalogItems, hasActiveCatalogFilter],
762+
);
763+
const renderCatalogItem = useCallback(
764+
(itemProps: LegendListRenderItemProps<ThreadSettingsCatalogItem>) => {
765+
const item = itemProps.item;
766+
let content: ReactNode;
767+
768+
if (item.kind === "provider") {
769+
content = <ThreadSettingsProviderListHeader provider={item.provider} />;
770+
} else if (item.kind === "model") {
771+
content = (
772+
<ThreadSettingsModelListRow
773+
isFirst={item.isFirst}
774+
isLast={item.isLast}
775+
option={item.option}
776+
/>
777+
);
778+
} else if (item.kind === "empty") {
779+
content = (
780+
<View className="items-center px-8 py-14">
781+
<Text className="text-center text-sm text-foreground-muted">No matching models</Text>
782+
</View>
783+
);
784+
} else {
785+
content = (
786+
<ThreadSettingsOptionsItem
787+
animationsReady={animationsReady}
788+
onOpenSubmenu={props.onOpenSubmenu}
789+
/>
790+
);
791+
}
792+
793+
return (
794+
<Animated.View
795+
key={item.key}
796+
entering={animationsReady ? THREAD_SETTINGS_CATALOG_ENTER_TRANSITION : undefined}
797+
exiting={animationsReady ? THREAD_SETTINGS_CATALOG_EXIT_TRANSITION : undefined}
798+
>
799+
{content}
800+
</Animated.View>
801+
);
802+
},
803+
[animationsReady, props.onOpenSubmenu],
804+
);
731805

732806
return (
733-
<LegendList
807+
<AnimatedLegendList
734808
automaticallyAdjustsScrollIndicatorInsets
735809
className="flex-1 bg-sheet"
736810
contentContainerStyle={{ paddingTop: 4 }}
737811
contentInsetAdjustmentBehavior={usesTransparentNativeHeader ? "never" : "automatic"}
738-
data={catalogItems}
812+
data={listItems}
739813
estimatedItemSize={48}
814+
extraData={animationsReady}
740815
getItemType={(item) => item.kind}
816+
itemLayoutAnimation={THREAD_SETTINGS_CATALOG_LAYOUT_TRANSITION}
741817
keyExtractor={(item) => item.key}
742818
keyboardDismissMode="on-drag"
743819
keyboardShouldPersistTaps="handled"
744820
maintainVisibleContentPosition={THREAD_SETTINGS_MAINTAIN_VISIBLE_CONTENT_POSITION}
745-
ListEmptyComponent={
746-
hasActiveCatalogFilter ? (
747-
<View className="items-center px-8 py-14">
748-
<Text className="text-center text-sm text-foreground-muted">No matching models</Text>
749-
</View>
750-
) : null
751-
}
752-
ListFooterComponent={<ThreadSettingsOptionsFooter onOpenSubmenu={props.onOpenSubmenu} />}
753821
ListHeaderComponent={
754822
<>
755823
{usesTransparentNativeHeader ? <View style={{ height: nativeHeaderHeight }} /> : null}
@@ -770,7 +838,8 @@ function ThreadSettingsMainContent(props: {
770838
</>
771839
}
772840
recycleItems
773-
renderItem={renderThreadSettingsCatalogItem}
841+
onLoad={() => setAnimationsReady(true)}
842+
renderItem={renderCatalogItem}
774843
showsVerticalScrollIndicator={false}
775844
/>
776845
);

0 commit comments

Comments
 (0)