|
| 1 | +import React, { |
| 2 | + useRef, |
| 3 | + useState, |
| 4 | +} from 'react'; |
| 5 | +import { |
| 6 | + Dimensions, |
| 7 | + FlatList, |
| 8 | + NativeScrollEvent, |
| 9 | + NativeSyntheticEvent, |
| 10 | + StyleSheet, |
| 11 | + View, |
| 12 | +} from 'react-native'; |
| 13 | +import { Image } from 'expo-image'; |
| 14 | +import { useRouter } from 'expo-router'; |
| 15 | +import AsyncStorage from '@react-native-async-storage/async-storage'; |
| 16 | + |
| 17 | +import welcome1 from '@/assets/images/custom/welcome1.png'; |
| 18 | +import welcome2 from '@/assets/images/custom/welcome2.png'; |
| 19 | +import welcome3 from '@/assets/images/custom/welcome3.png'; |
| 20 | +import welcome4 from '@/assets/images/custom/welcome4.png'; |
| 21 | +import welcome5 from '@/assets/images/custom/welcome5.png'; |
| 22 | +import BlockListView from '@/components/BlockListView'; |
| 23 | +import Button from '@/components/Button'; |
| 24 | +import Text from '@/components/Text'; |
| 25 | +import { |
| 26 | + FONT_SIZE_3XL, |
| 27 | + FONT_SIZE_XL, |
| 28 | + SCREEN_HEIGHT, |
| 29 | + SCREEN_WIDTH, |
| 30 | +} from '@/constants/dimensions'; |
| 31 | +import { AppTheme } from '@/constants/theme'; |
| 32 | +import useTheme from '@/hooks/useTheme'; |
| 33 | +import useThemedStyles from '@/hooks/useThemedStyles'; |
| 34 | + |
| 35 | +const { width, height } = Dimensions.get('window'); |
| 36 | + |
| 37 | +const slides = [ |
| 38 | + { |
| 39 | + id: '1', |
| 40 | + title: 'Welcome to MapSwipe', |
| 41 | + description: 'Help improve humanitarian responses from the comfort of your phone', |
| 42 | + imageUrl: welcome1, |
| 43 | + }, |
| 44 | + { |
| 45 | + id: '2', |
| 46 | + title: 'Part of Missing Maps', |
| 47 | + description: 'With Missing Maps, we aim to put the world\'s vulnerable communities on the map', |
| 48 | + imageUrl: welcome2, |
| 49 | + }, |
| 50 | + { |
| 51 | + id: '3', |
| 52 | + title: 'Swipe', |
| 53 | + description: 'Complete tasks by swiping through satellite imagery of areas that need mapping', |
| 54 | + imageUrl: welcome3, |
| 55 | + }, |
| 56 | + { |
| 57 | + id: '4', |
| 58 | + title: 'Create meaningful data', |
| 59 | + description: 'The data is used to focus the efforts of Missing Maps volunteers to add detail to OpenStreetMap', |
| 60 | + imageUrl: welcome4, |
| 61 | + }, |
| 62 | + { |
| 63 | + id: '5', |
| 64 | + title: 'Save lives', |
| 65 | + description: 'The map helps organisations coordinate humanitarian efforts and save lives', |
| 66 | + imageUrl: welcome5, |
| 67 | + }, |
| 68 | +]; |
| 69 | + |
| 70 | +const createStyles = (theme: AppTheme) => StyleSheet.create({ |
| 71 | + mainContent: { |
| 72 | + width, |
| 73 | + height, |
| 74 | + justifyContent: 'center', |
| 75 | + alignItems: 'center', |
| 76 | + }, |
| 77 | + icon: { |
| 78 | + resizeMode: 'contain', |
| 79 | + height: SCREEN_HEIGHT * 0.3, |
| 80 | + width: SCREEN_WIDTH * 0.8, |
| 81 | + }, |
| 82 | + heading: { |
| 83 | + color: theme.primaryBlue, |
| 84 | + textAlign: 'center', |
| 85 | + fontSize: FONT_SIZE_3XL, |
| 86 | + fontWeight: 'bold', |
| 87 | + width: SCREEN_WIDTH * 0.75, |
| 88 | + }, |
| 89 | + text: { |
| 90 | + color: theme.primaryBlue, |
| 91 | + width: SCREEN_WIDTH * 0.8, |
| 92 | + textAlign: 'center', |
| 93 | + fontSize: FONT_SIZE_XL, |
| 94 | + }, |
| 95 | + dotContainer: { |
| 96 | + flexDirection: 'row', |
| 97 | + justifyContent: 'center', |
| 98 | + position: 'absolute', |
| 99 | + bottom: 40, |
| 100 | + width: '100%', |
| 101 | + }, |
| 102 | + dotBase: { |
| 103 | + height: 8, |
| 104 | + width: 8, |
| 105 | + borderRadius: 4, |
| 106 | + marginHorizontal: 6, |
| 107 | + }, |
| 108 | + |
| 109 | +}); |
| 110 | +export default function Onboarding() { |
| 111 | + const [index, setIndex] = useState(0); |
| 112 | + const flatListRef = useRef<FlatList>(null); |
| 113 | + const styles = useThemedStyles(createStyles); |
| 114 | + const theme = useTheme(); |
| 115 | + const router = useRouter(); |
| 116 | + |
| 117 | + const handleScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => { |
| 118 | + const slideIndex = Math.round( |
| 119 | + event.nativeEvent.contentOffset.x / width, |
| 120 | + ); |
| 121 | + setIndex(slideIndex); |
| 122 | + }; |
| 123 | + |
| 124 | + const handleSignUp = async () => { |
| 125 | + try { |
| 126 | + await AsyncStorage.setItem('@hasSeenOnboarding', 'true'); |
| 127 | + router.replace('/register'); // Navigate to Sign Up screen |
| 128 | + } catch (error) { |
| 129 | + console.error('Error saving onboarding state:', error); |
| 130 | + } |
| 131 | + }; |
| 132 | + |
| 133 | + return ( |
| 134 | + <View> |
| 135 | + <FlatList |
| 136 | + ref={flatListRef} |
| 137 | + data={slides} |
| 138 | + horizontal |
| 139 | + pagingEnabled |
| 140 | + showsHorizontalScrollIndicator={false} |
| 141 | + onScroll={handleScroll} |
| 142 | + scrollEventThrottle={16} |
| 143 | + keyExtractor={(item) => item.id} |
| 144 | + renderItem={({ item, index: itemIndex }) => ( |
| 145 | + <BlockListView |
| 146 | + style={styles.mainContent} |
| 147 | + withCenteredContent |
| 148 | + withPadding |
| 149 | + > |
| 150 | + <Image |
| 151 | + style={styles.icon} |
| 152 | + source={item.imageUrl} |
| 153 | + /> |
| 154 | + <BlockListView withCenteredContent withPadding> |
| 155 | + <Text style={styles.heading}> |
| 156 | + {item.title} |
| 157 | + </Text> |
| 158 | + <Text style={styles.text}> |
| 159 | + {item.description} |
| 160 | + </Text> |
| 161 | + </BlockListView> |
| 162 | + |
| 163 | + {/* Only render button on the last slide */} |
| 164 | + {itemIndex === slides.length - 1 && ( |
| 165 | + <Button |
| 166 | + name="Sign Up" |
| 167 | + title="Sign Up" |
| 168 | + colorVariant="primaryRed" |
| 169 | + styleVariant="filled" |
| 170 | + onPress={handleSignUp} |
| 171 | + /> |
| 172 | + )} |
| 173 | + </BlockListView> |
| 174 | + )} |
| 175 | + /> |
| 176 | + <View style={styles.dotContainer}> |
| 177 | + {slides.map((item, i) => ( |
| 178 | + <View |
| 179 | + key={item.id} |
| 180 | + style={[ |
| 181 | + styles.dotBase, |
| 182 | + // eslint-disable-next-line react-native/no-inline-styles |
| 183 | + { backgroundColor: i === index ? theme.backgroundBrand : '#ccc' }, |
| 184 | + ]} |
| 185 | + /> |
| 186 | + ))} |
| 187 | + </View> |
| 188 | + </View> |
| 189 | + ); |
| 190 | +} |
0 commit comments