-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathonboarding.tsx
More file actions
254 lines (231 loc) · 7.03 KB
/
Copy pathonboarding.tsx
File metadata and controls
254 lines (231 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import React, { useLayoutEffect, useRef, useState, useEffect } from 'react';
import {
Dimensions,
Image,
ImageSourcePropType,
StyleSheet,
View,
BackHandler,
} from 'react-native';
import { useThemedStyles } from '../../hooks/useThemedStyles';
import { Theme } from '../../styles/colors';
import OnboardingIntroPanel from '../../components/onboarding/OnboardingIntroPanel';
import Animated, {
FadeIn,
FadeInDown,
interpolate,
useAnimatedStyle,
useDerivedValue,
useSharedValue,
withTiming,
} from 'react-native-reanimated';
import CloseButton from '../../components/onboarding/CloseButton';
import OnboardingStepPanel, {
OnboardingStepPanelProps,
} from '../../components/onboarding/OnboardingStepPanel';
import { useRouter } from 'expo-router';
import { markOnboardingComplete } from '../../utils/onboardingStatus';
import { Feedback } from '../../utils/Feedback';
const SCREEN_HEIGHT = Dimensions.get('screen').height;
type PanelStepProps = Omit<
OnboardingStepPanelProps,
'onBackPress' | 'onNextPress' | 'buttonPrimary'
>;
const STEPS: (PanelStepProps & {
image: ImageSourcePropType;
alignment: 'center' | 'bottom';
})[] = [
{
label: 'Offline AI access',
title: 'Chat with AI models offline',
description:
'Interact with AI models securely and offline on your mobile device.',
buttonLabel: 'Got it, next',
image: require('../../assets/onboarding/step_chat.png'),
alignment: 'center',
},
{
label: 'AI RAG',
title: 'Add source documents',
description: 'Use extra files to extend models knowledge and responses.',
buttonLabel: 'Great, next',
image: require('../../assets/onboarding/step_sources.png'),
alignment: 'bottom',
},
{
label: 'Speech to text',
title: 'Use voice instead of chat',
description: 'Use voice messages that automatically transcript into text.',
buttonLabel: 'Awesome, next',
image: require('../../assets/onboarding/step_voice.png'),
alignment: 'bottom',
},
{
label: 'Custom models',
title: 'Upload extra models',
description: 'Add your own custom models compatible with ExecuTorch.',
buttonLabel: 'Start chatting',
image: require('../../assets/onboarding/step_models.png'),
alignment: 'center',
},
];
function OnboardingScreen() {
const router = useRouter();
const closeOnboarding = () => {
markOnboardingComplete();
router.replace('/(modals)/select-starting-model');
};
const { styles, theme } = useThemedStyles(createStyles);
const bgSpillProgress = useSharedValue(0);
const [stepNumber, setStepNumber] = useState(0);
const step = stepNumber > 0 ? STEPS[stepNumber - 1] : null;
// Handle Android back button
useEffect(() => {
const backHandler = BackHandler.addEventListener(
'hardwareBackPress',
() => {
if (stepNumber > 1) {
// Go to previous onboarding step
setStepNumber(stepNumber - 1);
return true; // Prevent default back action
} else if (stepNumber === 1) {
// Go back to intro panel
bgSpillProgress.set(withTiming(0, { duration: 500 }));
setTimeout(() => setStepNumber(0), 500);
return true; // Prevent default back action
}
// stepNumber === 0 (intro panel) - allow default back action
return false;
}
);
return () => backHandler.remove();
}, [stepNumber, bgSpillProgress]);
const introPanel = useMeasureHeight();
const stepPanel = useMeasureHeight();
const initialBgBottom = introPanel.height + 16;
const bgDistance = useDerivedValue(() =>
interpolate(bgSpillProgress.get(), [0, 1], [0, initialBgBottom])
);
const divider = useDerivedValue(
() => SCREEN_HEIGHT - initialBgBottom + bgDistance.get()
);
const colorBgAnimation = useAnimatedStyle(() => {
const topEdge = Math.max(theme.insets.top + 16 - bgDistance.get(), 0);
const sideEdge = Math.max(16 - bgDistance.get(), 0);
return {
position: 'absolute',
top: topEdge,
left: sideEdge,
right: sideEdge,
bottom: SCREEN_HEIGHT - divider.get(),
borderRadius: Math.max(10 - bgDistance.get(), 0),
};
});
const imageAnimation = useAnimatedStyle(() => ({
height: divider.get(),
}));
return (
<View style={styles.container}>
<View ref={introPanel.ref} style={styles.bottomPanel}>
<OnboardingIntroPanel
onPressStart={() => {
bgSpillProgress.set(withTiming(1, { duration: 500 }));
setTimeout(() => setStepNumber(1), 500);
}}
/>
</View>
<Animated.View style={[styles.colorBg, colorBgAnimation]} />
<Animated.View style={[styles.imageWrapper, imageAnimation]}>
<View
style={[
styles.innerImageWrapper,
step?.alignment === 'bottom'
? {
justifyContent: 'flex-end',
bottom: stepPanel.height + 8,
}
: { justifyContent: 'center' },
]}
>
<Image source={step?.image ?? STEPS[0].image} />
</View>
</Animated.View>
{step && (
<>
<Animated.View entering={FadeIn} style={styles.close}>
<CloseButton onPress={closeOnboarding} />
</Animated.View>
<Animated.View
ref={stepPanel.ref}
entering={FadeInDown}
style={styles.bottomPanel}
>
<OnboardingStepPanel
label={step.label}
title={step.title}
description={step.description}
buttonLabel={step.buttonLabel}
onBackPress={
stepNumber > 1 ? () => setStepNumber(stepNumber - 1) : undefined
}
onNextPress={() => {
if (stepNumber < STEPS.length) {
setStepNumber(stepNumber + 1);
} else {
Feedback.onboardingComplete();
closeOnboarding();
}
}}
buttonPrimary={stepNumber === STEPS.length}
/>
</Animated.View>
</>
)}
</View>
);
}
export default OnboardingScreen;
const createStyles = (theme: Theme) =>
StyleSheet.create({
container: {
flex: 1,
backgroundColor: theme.bg.softPrimary,
},
colorBg: {
backgroundColor: theme.bg.main,
},
imageWrapper: {
position: 'absolute',
top: 0,
left: 0,
right: 0,
overflow: 'hidden',
},
innerImageWrapper: {
alignItems: 'center',
height: SCREEN_HEIGHT,
},
bottomPanel: {
padding: 16,
paddingBottom: 16 + theme.insets.bottom,
position: 'absolute',
bottom: 0,
left: 0,
right: 0,
},
close: {
position: 'absolute',
top: theme.insets.top + 16,
right: 16,
},
});
function useMeasureHeight() {
const ref = useRef<View>(null);
const [height, setHeight] = useState(0);
useLayoutEffect(() => {
ref.current?.measure((x, y, width, height, pageX, pageY) => {
setHeight(height);
});
});
return { ref, height };
}