sana işlevsel şu anda saat gibi çalışan bir sayfa göndereceğim. bu sayfayı incelemeni ve…
- User
sana işlevsel şu anda saat gibi çalışan bir sayfa göndereceğim. bu sayfayı incelemeni ve kodları optimize etmeni istiyorum. ayrıca bu sayfa çok büyük olduğundan bazı işlevleri dış sayfaya taşıyıp import komutuyla o işlevi çağırabiliriz bunu yapacaksın. import React, { useEffect, useState, useRef, useCallback, useMemo } from 'react'; import { View, StyleSheet, Text, FlatList, Image, TouchableOpacity, ActivityIndicator, BackHandler } from 'react-native'; import { Ionicons } from '@expo/vector-icons'; import WordExplanationBox from './WordExplanationBox'; import AsyncStorage from '@react-native-async-storage/async-storage'; import * as Font from 'expo-font'; import { useThemeAndLanguage } from '../utils/ThemeAndLanguageContext'; import { BottomSheetModal, BottomSheetModalProvider, BottomSheetScrollView } from '@gorhom/bottom-sheet'; import QuizScreen from './QuizScreen'; import { doc, getDoc } from 'firebase/firestore'; import { db } from '../utils/firebase'; import { useFocusEffect } from '@react-navigation/native'; import { useVocabulary } from './VocabularyContext'; import PropTypes from 'prop-types'; import ToggleHeartButton from './heart';
const ARABIC_FONTS = { 'Scheherazade': require('../../assets/fonts/scheherazade.ttf'), 'Adobe Arabic': require('../../assets/fonts/adobearabic.ttf'), 'Amiri': require('../../assets/fonts/amiri.ttf'), 'Noto Naskh Arabic': require('../../assets/fonts/noto.ttf'), }; const STORAGE_KEY = '@user_words'; const Word = React.memo(({ word, onPress, style, isKnown }) => ( <TouchableOpacity onPress={() => onPress(word)}> <Text style={[style, isKnown && styles.knownWord]}>{word} )); Word.propTypes = { word: PropTypes.string.isRequired, onPress: PropTypes.func.isRequired, style: PropTypes.object, isKnown: PropTypes.bool, }; const Sentence = React.memo(({ sentenceObj, selectedSentence, handleSentenceButtonPress, handleWordPress, fontSize, theme, arabicFont, knownWords }) => { if (sentenceObj.type === 'image') { return ( <Image source={{ uri: sentenceObj.content }} style={[styles.imageStyle]} /> ); } const isHeaderOrSubHeader = sentenceObj.type === 'header' || sentenceObj.type === 'subheader'; return ( <View style={[ styles.sentenceContainer, selectedSentence === sentenceObj.content && styles.selectedSentence, isHeaderOrSubHeader && (sentenceObj.type === 'header' ? styles.headerContainer : styles.subHeaderContainer), { borderRightColor: sentenceObj.type === 'header' ? theme.primaryColor : theme.secondaryColor } ]}> <TouchableOpacity style={styles.explanationButton} onPress={() => handleSentenceButtonPress(sentenceObj.content)}> {sentenceObj.content.split(' ').map((word, wordIndex) => ( <Word key={
${sentenceObj.id}-${wordIndex}} word={word} onPress={handleWordPress} style={[ styles.descriptionArabic, { fontSize: isHeaderOrSubHeader ? fontSize * 1.6 : fontSize * 1.4, color: theme.textColor, fontFamily: arabicFont, } ]} isKnown={knownWords.has(word)} /> ))} ); }); Sentence.propTypes = { sentenceObj: PropTypes.shape({ type: PropTypes.string, content: PropTypes.string, id: PropTypes.string, }).isRequired, selectedSentence: PropTypes.string, handleSentenceButtonPress: PropTypes.func.isRequired, handleWordPress: PropTypes.func.isRequired, fontSize: PropTypes.number.isRequired, theme: PropTypes.object.isRequired, arabicFont: PropTypes.string.isRequired, knownWords: PropTypes.instanceOf(Set).isRequired, };export default function ReadStoryScreen({ route, navigation }) { const { story, storyId } = route.params; const [fontSize, setFontSize] = useState(18); const [selectedWord, setSelectedWord] = useState(null); const [selectedSentence, setSelectedSentence] = useState(null); const [loading, setLoading] = useState(true); const [cachedStory, setCachedStory] = useState(null); const [hasQuiz, setHasQuiz] = useState(false); const bottomSheetModalRef = useRef(null); const settingsBottomSheetModalRef = useRef(null); const quizBottomSheetModalRef = useRef(null); const { locale, theme, arabicFont, setFontFamily, t } = useThemeAndLanguage(); const [activeModal, setActiveModal] = useState(null); const [knownWords, setKnownWords] = useState(new Set()); const { vocabulary, isLoggedIn, userId} = useVocabulary();
const increaseFontSize = useCallback(() => setFontSize(prevSize => prevSize + 2), []); const decreaseFontSize = useCallback(() => setFontSize(prevSize => prevSize - 2), []);
const closeAllModals = useCallback(() => { bottomSheetModalRef.current?.dismiss(); settingsBottomSheetModalRef.current?.dismiss(); quizBottomSheetModalRef.current?.dismiss(); setActiveModal(null); }, []);
const openSettingsModal = useCallback(() => { settingsBottomSheetModalRef.current?.present(); setActiveModal('settings'); }, []);
const openQuizModal = useCallback(() => { quizBottomSheetModalRef.current?.present(); setActiveModal('quiz'); }, []);
const handleWordPress = useCallback((word) => { setSelectedWord(word); setSelectedSentence(null); bottomSheetModalRef.current?.present(); setActiveModal('word'); }, []);
const handleSentenceButtonPress = useCallback((sentence) => { setSelectedSentence(sentence); setSelectedWord(null); bottomSheetModalRef.current?.present(); setActiveModal('sentence'); }, []);
const loadFonts = useCallback(async () => { await Font.loadAsync(ARABIC_FONTS); }, []);
const fetchStoryFromFirestore = useCallback(async (id) => { const storyDocRef = doc(db, 'stories', id); const storyDoc = await getDoc(storyDocRef); return storyDoc.exists() ? { id: storyDoc.id, ...storyDoc.data() } : null; }, []);
const fetchStory = useCallback(async () => { try { let storyData; if (story) { storyData = story; } else if (storyId) { const cachedData = await AsyncStorage.getItem(
story-${storyId}); if (cachedData) { storyData = JSON.parse(cachedData); } else { storyData = await fetchStoryFromFirestore(storyId); if (storyData) { await AsyncStorage.setItem(story-${storyId}, JSON.stringify(storyData)); } } }if (storyData) { setCachedStory(storyData); setHasQuiz(storyData.test_questions && storyData.test_questions.length > 0); } } catch (error) { console.error('Error fetching story:', error); } finally { setLoading(false); }}, [story, storyId, fetchStoryFromFirestore]);
const fetchKnownWords = useCallback(async () => { if (!isLoggedIn) { setKnownWords(new Set()); return; }
try { const cachedWords = await AsyncStorage.getItem(STORAGE_KEY); if (cachedWords) { const wordsArray = JSON.parse(cachedWords); setKnownWords(new Set(wordsArray.map(word => word.word))); } } catch (error) { console.error('Error fetching known words:', error); }}, [isLoggedIn]);
useEffect(() => { const initialize = async () => { await loadFonts(); await fetchStory(); await fetchKnownWords(); };
initialize();}, [loadFonts, fetchStory, fetchKnownWords]);
useEffect(() => { if (cachedStory) { const storyTitle = locale === 'tr' ? cachedStory.storytitleturkish : cachedStory.storytitleenglish; const truncatedTitle = storyTitle.length > 23 ?
${storyTitle.substring(0, 23)}...: storyTitle;navigation.setOptions({ title: truncatedTitle, headerStyle: { backgroundColor: theme.lighterCardBackgroundColor }, headerTintColor: theme.textColor, headerTitleStyle: { color: theme.textColor, fontWeight: 'bold' }, headerRight: () => (<View style={{ flexDirection: 'row', alignItems: 'center' }}>
), }); }}, [navigation, cachedStory, theme, locale, openSettingsModal]);
useFocusEffect( useCallback(() => { const onBackPress = () => { if (activeModal) { closeAllModals(); return true; } return false; };
BackHandler.addEventListener('hardwareBackPress', onBackPress); return () => BackHandler.removeEventListener('hardwareBackPress', onBackPress); }, [activeModal, closeAllModals]));
const renderSentence = useCallback(({ item: sentenceObj }) => ( ), [selectedSentence, handleSentenceButtonPress, handleWordPress, fontSize, theme, arabicFont, knownWords]);
const memoizedData = useMemo(() => cachedStory?.sentences || [], [cachedStory]);
const keyExtractor = useCallback((item) => item.id.toString(), []);
useEffect(() => { const updatedKnownWords = new Set(vocabulary.map(word => word.word)); setKnownWords(updatedKnownWords); }, [vocabulary]);
if (loading) { return ( <View style={[styles.loadingContainer, { backgroundColor: theme.backgroundColorForWordBox }]}> ); }
if (!cachedStory) { return ( <View style={[styles.loadingContainer, { backgroundColor: theme.backgroundColorForWordBox }]}> <Text style={{ color: theme.textColor }}>{t('storyNotFound')} ); }
return ( <View style={[styles.container, { backgroundColor: theme.backgroundColorForWordBox }]}> <FlatList data={memoizedData} renderItem={renderSentence} keyExtractor={keyExtractor} ListHeaderComponent={() => ( <> <Image source={{ uri: cachedStory.thumbnail }} style={styles.thumbnail} /> <Text style={[styles.titleArabic, { color: theme.textColor, fontFamily: arabicFont }]}>{cachedStory.storytitlearabic} <Text style={[styles.titleTurkish, { color: theme.secondaryColor }]}> {locale === 'tr' ? cachedStory.storytitleturkish : cachedStory.storytitleenglish} </> )} initialNumToRender={10} maxToRenderPerBatch={5} windowSize={10} />
{hasQuiz && ( <TouchableOpacity style={[styles.quizButton, { backgroundColor: theme.primaryColor }]} onPress={openQuizModal} > <Text style={styles.quizButtonText}>{t('startQuiz')}</Text> </TouchableOpacity> )}<BottomSheetModal ref={bottomSheetModalRef} index={0} snapPoints={['45%', '90%']} handleStyle={{ backgroundColor: theme.backgroundColorForWordBox }} onDismiss={() => setActiveModal(null)} > <WordExplanationBox word={selectedWord || selectedSentence} storyId={cachedStory.id} onClose={() => bottomSheetModalRef.current?.dismiss()} />
<BottomSheetModal ref={settingsBottomSheetModalRef} index={0} snapPoints={['85%']} handleStyle={{ backgroundColor: theme.backgroundColorForWordBox }} onDismiss={() => setActiveModal(null)} > <BottomSheetScrollView contentContainerStyle={[ styles.settingsContent, { backgroundColor: theme.backgroundColorForWordBox } ]} > <View style={styles.exampleCard}> <Text style={[styles.exampleText, { fontSize: fontSize * 1.4, fontFamily: arabicFont }]}> (هذا مثال على الكتابة بالعربية) </Text> </View> <View style={styles.fontSizeControls}> <TouchableOpacity onPress={increaseFontSize} style={styles.fontSizeButton}> <Ionicons name="add-circle-outline" size={40} color={theme.primaryColor} /> <Text style={styles.fontSizeButtonText}>{t('increaseFontSize')}</Text> </TouchableOpacity> <TouchableOpacity onPress={decreaseFontSize} style={styles.fontSizeButton}> <Ionicons name="remove-circle-outline" size={40} color={theme.primaryColor} /> <Text style={styles.fontSizeButtonText}>{t('decreaseFontSize')}</Text> </TouchableOpacity> </View> <View style={styles.fontOptionsContainer}> {Object.keys(ARABIC_FONTS).map((font) => ( <TouchableOpacity key={font} style={styles.fontOption} onPress={() => setFontFamily(font)} > <Text style={styles.fontOptionText}>{font}</Text> <Text style={[styles.fontOptionSample, { fontFamily: font }]}> (اقرأ العربية) </Text> </TouchableOpacity> ))} </View> </BottomSheetScrollView> </BottomSheetModal> {hasQuiz && ( <BottomSheetModal ref={quizBottomSheetModalRef} index={0} snapPoints={['90%']} handleStyle={{ backgroundColor: theme.backgroundColorForWordBox }} onDismiss={() => setActiveModal(null)} > <QuizScreen story={cachedStory} onClose={() => { quizBottomSheetModalRef.current?.dismiss(); setActiveModal(null); }} /> </BottomSheetModal> )} </View> </BottomSheetModalProvider>); } const styles = StyleSheet.create({ container: { flex: 1, }, knownWord: { backgroundColor: 'rgba(0, 255, 0, 0.1)', borderRadius: 10, }, loadingContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', }, thumbnail: { width: '100%', aspectRatio:1/1, }, content: { padding: 16, }, header: { flexDirection: 'column', alignItems: 'center', justifyContent: 'center', marginBottom: 16, }, titleArabic: { fontSize: 26, }, titleTurkish: { fontSize: 20, marginTop: 4, textAlign: 'center', }, headerRight: { flexDirection: 'row', alignItems: 'center', marginRight: 10, }, fontSizeControl: { marginHorizontal: 5, }, sentenceContainer: { flexDirection: 'row', alignItems: 'center', marginBottom: 8, paddingLeft: 10, }, explanationButton: { marginRight: 2, }, arabicTextWrapper: { flex: 1, flexDirection: 'row-reverse', flexWrap: 'wrap', paddingRight: 15, }, descriptionArabic: { marginLeft: 2, marginBottom: 0, padding: 2, }, selectedSentence: { backgroundColor: 'rgba(255, 215, 0, 0.3)', }, settingsContent: { flexGrow: 1, paddingBottom: 20, }, fontExampleContainer: { alignItems: 'center', marginBottom: 20, }, exampleLabel: { fontSize: 18, fontWeight: 'bold', marginBottom: 10, }, exampleBox: { borderWidth: 1, borderColor: '#ccc', borderRadius: 10, padding: 10, width: '80%', alignItems: 'center', backgroundColor: 'white', elevation: 4, }, fontSizeControls: { flexDirection: 'row', justifyContent: 'space-between', marginVertical: 25, paddingHorizontal: 25, width: '80%', alignSelf: 'center', }, fontSizeControl: { alignItems: 'center', padding: 10, }, fontSizeControlText: { marginTop: 5, fontSize: 14, color: 'gray', }, selectFontLabel: { fontSize: 18, fontWeight: 'bold', textAlign: 'center', marginBottom: 10, }, fontGrid: { flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'space-around', marginTop: 20, width: '80%', alignSelf: 'center', }, fontOptionGridItem: { width: '45%', padding: 5, marginBottom: 20, borderWidth: 1, borderColor: '#ccc', borderRadius: 10, alignItems: 'center', backgroundColor: 'white', elevation: 4, }, fontOptionText: { fontSize: 16, marginBottom: 5, textAlign: 'center', }, quizButton: { position: 'absolute', bottom: 20, right: 20, padding: 10, borderRadius: 5, }, quizButtonText: { color: 'white', fontWeight: 'bold', }, bottomSheetContent: { flexGrow: 1, }, settingsContent: { flexGrow: 1, paddingHorizontal: 20, paddingBottom: 20, alignItems: 'center', }, exampleCard: { backgroundColor: 'white', borderRadius: 10, padding: 15, marginVertical: 20, width: '90%', alignItems: 'center', elevation: 4, shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.25, shadowRadius: 3.84, }, exampleText: { textAlign: 'center', }, fontSizeControls: { flexDirection: 'row', justifyContent: 'space-around', width: '100%', marginBottom: 20, }, fontSizeButton: { alignItems: 'center', }, fontSizeButtonText: { marginTop: 5, fontSize: 12, color: 'gray', textAlign: 'center', }, fontOptionsContainer: { flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'space-between', width: '100%', }, fontOption: { width: '48%', backgroundColor: 'white', borderRadius: 10, padding: 10, marginBottom: 15, alignItems: 'center', elevation: 2, shadowColor: '#000', shadowOffset: { width: 0, height: 1 }, shadowOpacity: 0.22, shadowRadius: 2.22, }, fontOptionText: { fontSize: 16, marginBottom: 5, }, fontOptionSample: { fontSize: 18, }, headerContainer: { marginVertical: 10, backgroundColor: 'rgba(0, 0, 0, 0.03)', borderRightWidth: 5, width: '97%', // Genişliği biraz küçülttük
}, subHeaderContainer: { marginVertical: 10, backgroundColor: 'rgba(0, 0, 0, 0.03)', borderRightWidth: 3, width: '97%', }, imageStyle: { width: "90%", alignSelf: "center", // Ortalamak için bunu ekledik marginBottom: 10, aspectRatio: 1 / 1, // Oranı sabit tutmak için ekledik elevation:1, borderRadius:15, }, });
- Assistant
Certainly! Let's optimize and refactor your
ReadStoryScreencomponent to improve maintainability, readability, and performance. We'll break down the code into smaller, reusable components and utility functions. Here's a step-by-step guide along with the code snippets for each part.1. Refactoring Overview
Objectives:
- Modularize Components: Extract
Word,Sentence, and modal components into separate files. - Create Utility Functions: Move story fetching and known words management into separate utility files or custom hooks.
- Optimize Imports: Ensure that each component only imports what it needs.
- Enhance Performance: Utilize
React.memo,useCallback, anduseMemoeffectively to prevent unnecessary re-renders. - Improve Code Readability: Organize code for better clarity and maintainability.
2. Project Structure
Organize your components and utilities as follows:
/components /Word Word.js Word.styles.js /Sentence Sentence.js Sentence.styles.js /Modals SettingsModal.js QuizModal.js WordSentenceModal.js Modals.styles.js /common LoadingIndicator.js NotFound.js /utils fetchStory.js useKnownWords.js fonts.js /screens ReadStoryScreen.js3. Creating Separate Components and Utilities
a. Word Component
File:
components/Word/Word.jsimport React from 'react'; import { TouchableOpacity, Text } from 'react-native'; import PropTypes from 'prop-types'; import styles from './Word.styles'; const Word = React.memo(({ word, onPress, style, isKnown }) => ( <TouchableOpacity onPress={() => onPress(word)}> <Text style={[style, isKnown && styles.knownWord]}>{word}</Text> </TouchableOpacity> )); Word.propTypes = { word: PropTypes.string.isRequired, onPress: PropTypes.func.isRequired, style: PropTypes.object, isKnown: PropTypes.bool, }; Word.defaultProps = { style: {}, isKnown: false, }; export default Word;File:
components/Word/Word.styles.jsimport { StyleSheet } from 'react-native'; export default StyleSheet.create({ knownWord: { backgroundColor: 'rgba(0, 255, 0, 0.1)', borderRadius: 10, }, });b. Sentence Component
File:
components/Sentence/Sentence.jsimport React from 'react'; import { View, Image, TouchableOpacity } from 'react-native'; import PropTypes from 'prop-types'; import { Ionicons } from '@expo/vector-icons'; import Word from '../Word/Word'; import styles from './Sentence.styles'; const Sentence = React.memo( ({ sentenceObj, selectedSentence, handleSentenceButtonPress, handleWordPress, fontSize, theme, arabicFont, knownWords, }) => { if (sentenceObj.type === 'image') { return <Image source={{ uri: sentenceObj.content }} style={styles.imageStyle} />; } const isHeaderOrSubHeader = sentenceObj.type === 'header' || sentenceObj.type === 'subheader'; return ( <View style={[ styles.sentenceContainer, selectedSentence === sentenceObj.content && styles.selectedSentence, isHeaderOrSubHeader && (sentenceObj.type === 'header' ? styles.headerContainer : styles.subHeaderContainer), { borderRightColor: sentenceObj.type === 'header' ? theme.primaryColor : theme.secondaryColor, }, ]} > <TouchableOpacity style={styles.explanationButton} onPress={() => handleSentenceButtonPress(sentenceObj.content)} > <Ionicons name="open" size={24} color={theme.primaryColor} /> </TouchableOpacity> <View style={styles.arabicTextWrapper}> {sentenceObj.content.split(' ').map((word, wordIndex) => ( <Word key={`${sentenceObj.id}-${wordIndex}`} word={word} onPress={handleWordPress} style={[ styles.descriptionArabic, { fontSize: isHeaderOrSubHeader ? fontSize * 1.6 : fontSize * 1.4, color: theme.textColor, fontFamily: arabicFont, }, ]} isKnown={knownWords.has(word)} /> ))} </View> </View> ); } ); Sentence.propTypes = { sentenceObj: PropTypes.shape({ type: PropTypes.string, content: PropTypes.string, id: PropTypes.string, }).isRequired, selectedSentence: PropTypes.string, handleSentenceButtonPress: PropTypes.func.isRequired, handleWordPress: PropTypes.func.isRequired, fontSize: PropTypes.number.isRequired, theme: PropTypes.object.isRequired, arabicFont: PropTypes.string.isRequired, knownWords: PropTypes.instanceOf(Set).isRequired, }; Sentence.defaultProps = { selectedSentence: null, }; export default Sentence;File:
components/Sentence/Sentence.styles.jsimport { StyleSheet } from 'react-native'; export default StyleSheet.create({ sentenceContainer: { flexDirection: 'row', alignItems: 'center', marginBottom: 8, paddingLeft: 10, }, explanationButton: { marginRight: 2, }, arabicTextWrapper: { flex: 1, flexDirection: 'row-reverse', flexWrap: 'wrap', paddingRight: 15, }, descriptionArabic: { marginLeft: 2, marginBottom: 0, padding: 2, }, selectedSentence: { backgroundColor: 'rgba(255, 215, 0, 0.3)', }, headerContainer: { marginVertical: 10, backgroundColor: 'rgba(0, 0, 0, 0.03)', borderRightWidth: 5, width: '97%', }, subHeaderContainer: { marginVertical: 10, backgroundColor: 'rgba(0, 0, 0, 0.03)', borderRightWidth: 3, width: '97%', }, imageStyle: { width: '90%', alignSelf: 'center', marginBottom: 10, aspectRatio: 1 / 1, elevation: 1, borderRadius: 15, }, });c. Modals Components
We'll create three modal components:
WordSentenceModal,SettingsModal, andQuizModal.i. WordSentenceModal
File:
components/Modals/WordSentenceModal.jsimport React from 'react'; import { BottomSheetScrollView } from '@gorhom/bottom-sheet'; import PropTypes from 'prop-types'; import WordExplanationBox from '../WordExplanationBox'; import styles from './Modals.styles'; const WordSentenceModal = ({ wordOrSentence, storyId, onClose, theme }) => ( <BottomSheetScrollView contentContainerStyle={styles.bottomSheetContent}> <WordExplanationBox word={wordOrSentence} storyId={storyId} onClose={onClose} /> </BottomSheetScrollView> ); WordSentenceModal.propTypes = { wordOrSentence: PropTypes.string, storyId: PropTypes.string.isRequired, onClose: PropTypes.func.isRequired, theme: PropTypes.object.isRequired, }; WordSentenceModal.defaultProps = { wordOrSentence: '', }; export default WordSentenceModal;ii. SettingsModal
File:
components/Modals/SettingsModal.jsimport React from 'react'; import { View, Text, TouchableOpacity, Image } from 'react-native'; import { Ionicons } from '@expo/vector-icons'; import PropTypes from 'prop-types'; import styles from './Modals.styles'; const SettingsModal = ({ fontSize, increaseFontSize, decreaseFontSize, fonts, setFontFamily, theme, arabicFont, t, }) => ( <BottomSheetScrollView contentContainerStyle={[styles.settingsContent, { backgroundColor: theme.backgroundColorForWordBox }]}> <View style={styles.exampleCard}> <Text style={[styles.exampleText, { fontSize: fontSize * 1.4, fontFamily: arabicFont }]}> (هذا مثال على الكتابة بالعربية) </Text> </View> <View style={styles.fontSizeControls}> <TouchableOpacity onPress={increaseFontSize} style={styles.fontSizeButton}> <Ionicons name="add-circle-outline" size={40} color={theme.primaryColor} /> <Text style={styles.fontSizeButtonText}>{t('increaseFontSize')}</Text> </TouchableOpacity> <TouchableOpacity onPress={decreaseFontSize} style={styles.fontSizeButton}> <Ionicons name="remove-circle-outline" size={40} color={theme.primaryColor} /> <Text style={styles.fontSizeButtonText}>{t('decreaseFontSize')}</Text> </TouchableOpacity> </View> <View style={styles.fontOptionsContainer}> {Object.keys(fonts).map((font) => ( <TouchableOpacity key={font} style={styles.fontOption} onPress={() => setFontFamily(font)} > <Text style={styles.fontOptionText}>{font}</Text> <Text style={[styles.fontOptionSample, { fontFamily: font }]}> (اقرأ العربية) </Text> </TouchableOpacity> ))} </View> </BottomSheetScrollView> ); SettingsModal.propTypes = { fontSize: PropTypes.number.isRequired, increaseFontSize: PropTypes.func.isRequired, decreaseFontSize: PropTypes.func.isRequired, fonts: PropTypes.object.isRequired, setFontFamily: PropTypes.func.isRequired, theme: PropTypes.object.isRequired, arabicFont: PropTypes.string.isRequired, t: PropTypes.func.isRequired, }; export default SettingsModal;iii. QuizModal
File:
components/Modals/QuizModal.jsimport React from 'react'; import PropTypes from 'prop-types'; import { QuizScreen } from './QuizScreen'; // Assuming QuizScreen is in the same Modals folder import styles from './Modals.styles'; const QuizModal = ({ story, onClose, theme }) => ( <BottomSheetScrollView contentContainerStyle={styles.bottomSheetContent}> <QuizScreen story={story} onClose={onClose} /> </BottomSheetScrollView> ); QuizModal.propTypes = { story: PropTypes.object.isRequired, onClose: PropTypes.func.isRequired, theme: PropTypes.object.isRequired, }; export default QuizModal;iv. Modals.styles.js
File:
components/Modals/Modals.styles.jsimport { StyleSheet } from 'react-native'; export default StyleSheet.create({ bottomSheetContent: { flexGrow: 1, padding: 20, }, settingsContent: { flexGrow: 1, paddingHorizontal: 20, paddingBottom: 20, alignItems: 'center', }, exampleCard: { backgroundColor: 'white', borderRadius: 10, padding: 15, marginVertical: 20, width: '90%', alignItems: 'center', elevation: 4, shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.25, shadowRadius: 3.84, }, exampleText: { textAlign: 'center', }, fontSizeControls: { flexDirection: 'row', justifyContent: 'space-around', width: '100%', marginBottom: 20, }, fontSizeButton: { alignItems: 'center', }, fontSizeButtonText: { marginTop: 5, fontSize: 12, color: 'gray', textAlign: 'center', }, fontOptionsContainer: { flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'space-between', width: '100%', }, fontOption: { width: '48%', backgroundColor: 'white', borderRadius: 10, padding: 10, marginBottom: 15, alignItems: 'center', elevation: 2, shadowColor: '#000', shadowOffset: { width: 0, height: 1 }, shadowOpacity: 0.22, shadowRadius: 2.22, }, fontOptionText: { fontSize: 16, marginBottom: 5, }, fontOptionSample: { fontSize: 18, }, });d. Utility Functions and Hooks
i. fetchStory Utility
File:
utils/fetchStory.jsimport { doc, getDoc } from 'firebase/firestore'; import { db } from './firebase'; export const fetchStoryFromFirestore = async (id) => { const storyDocRef = doc(db, 'stories', id); const storyDoc = await getDoc(storyDocRef); return storyDoc.exists() ? { id: storyDoc.id, ...storyDoc.data() } : null; };ii. useKnownWords Custom Hook
File:
utils/useKnownWords.jsimport { useState, useEffect, useCallback } from 'react'; import AsyncStorage from '@react-native-async-storage/async-storage'; const STORAGE_KEY = '@user_words'; export const useKnownWords = (isLoggedIn) => { const [knownWords, setKnownWords] = useState(new Set()); const fetchKnownWords = useCallback(async () => { if (!isLoggedIn) { setKnownWords(new Set()); return; } try { const cachedWords = await AsyncStorage.getItem(STORAGE_KEY); if (cachedWords) { const wordsArray = JSON.parse(cachedWords); setKnownWords(new Set(wordsArray.map((word) => word.word))); } } catch (error) { console.error('Error fetching known words:', error); } }, [isLoggedIn]); useEffect(() => { fetchKnownWords(); }, [fetchKnownWords]); return { knownWords, setKnownWords }; };iii. Fonts Utility
File:
utils/fonts.jsexport const ARABIC_FONTS = { Scheherazade: require('../assets/fonts/scheherazade.ttf'), 'Adobe Arabic': require('../assets/fonts/adobearabic.ttf'), Amiri: require('../assets/fonts/amiri.ttf'), 'Noto Naskh Arabic': require('../assets/fonts/noto.ttf'), };e. Common Components
i. LoadingIndicator
File:
components/common/LoadingIndicator.jsimport React from 'react'; import { View, ActivityIndicator } from 'react-native'; import PropTypes from 'prop-types'; import styles from './Common.styles'; const LoadingIndicator = ({ theme }) => ( <View style={[styles.loadingContainer, { backgroundColor: theme.backgroundColorForWordBox }]}> <ActivityIndicator size="large" color={theme.primaryColor} /> </View> ); LoadingIndicator.propTypes = { theme: PropTypes.object.isRequired, }; export default LoadingIndicator;ii. NotFound Component
File:
components/common/NotFound.jsimport React from 'react'; import { View, Text } from 'react-native'; import PropTypes from 'prop-types'; import styles from './Common.styles'; const NotFound = ({ theme, message }) => ( <View style={[styles.loadingContainer, { backgroundColor: theme.backgroundColorForWordBox }]}> <Text style={{ color: theme.textColor }}>{message}</Text> </View> ); NotFound.propTypes = { theme: PropTypes.object.isRequired, message: PropTypes.string.isRequired, }; export default NotFound;File:
components/common/Common.styles.jsimport { StyleSheet } from 'react-native'; export default StyleSheet.create({ loadingContainer: { flex: 1, justifyContent: 'center', alignItems: 'center', }, });4. Updated ReadStoryScreen Component
Now, let's update the main
ReadStoryScreencomponent to utilize the newly created components and utilities.File:
screens/ReadStoryScreen.jsimport React, { useEffect, useState, useRef, useCallback, useMemo } from 'react'; import { View, StyleSheet, Text, FlatList, Image, TouchableOpacity, BackHandler } from 'react-native'; import { Ionicons } from '@expo/vector-icons'; import AsyncStorage from '@react-native-async-storage/async-storage'; import * as Font from 'expo-font'; import { useThemeAndLanguage } from '../utils/ThemeAndLanguageContext'; import { BottomSheetModal, BottomSheetModalProvider } from '@gorhom/bottom-sheet'; import { useFocusEffect } from '@react-navigation/native'; import PropTypes from 'prop-types'; import Word from '../components/Word/Word'; import Sentence from '../components/Sentence/Sentence'; import WordSentenceModal from '../components/Modals/WordSentenceModal'; import SettingsModal from '../components/Modals/SettingsModal'; import QuizModal from '../components/Modals/QuizModal'; import LoadingIndicator from '../components/common/LoadingIndicator'; import NotFound from '../components/common/NotFound'; import ToggleHeartButton from '../components/heart'; import QuizScreen from '../components/QuizScreen'; import WordExplanationBox from '../components/WordExplanationBox'; import { fetchStoryFromFirestore } from '../utils/fetchStory'; import { useKnownWords } from '../utils/useKnownWords'; import { ARABIC_FONTS } from '../utils/fonts'; export default function ReadStoryScreen({ route, navigation }) { const { story, storyId } = route.params; const [fontSize, setFontSize] = useState(18); const [selectedWord, setSelectedWord] = useState(null); const [selectedSentence, setSelectedSentence] = useState(null); const [loading, setLoading] = useState(true); const [cachedStory, setCachedStory] = useState(null); const [hasQuiz, setHasQuiz] = useState(false); const bottomSheetModalRef = useRef(null); const settingsBottomSheetModalRef = useRef(null); const quizBottomSheetModalRef = useRef(null); const { locale, theme, arabicFont, setFontFamily, t } = useThemeAndLanguage(); const [activeModal, setActiveModal] = useState(null); const { knownWords, setKnownWords } = useKnownWords(true); // Pass isLoggedIn from your context const { vocabulary, isLoggedIn, userId } = useVocabulary(); const increaseFontSize = useCallback(() => setFontSize((prevSize) => prevSize + 2), []); const decreaseFontSize = useCallback(() => setFontSize((prevSize) => prevSize - 2), []); const closeAllModals = useCallback(() => { bottomSheetModalRef.current?.dismiss(); settingsBottomSheetModalRef.current?.dismiss(); quizBottomSheetModalRef.current?.dismiss(); setActiveModal(null); }, []); const openSettingsModal = useCallback(() => { settingsBottomSheetModalRef.current?.present(); setActiveModal('settings'); }, []); const openQuizModal = useCallback(() => { quizBottomSheetModalRef.current?.present(); setActiveModal('quiz'); }, []); const handleWordPress = useCallback((word) => { setSelectedWord(word); setSelectedSentence(null); bottomSheetModalRef.current?.present(); setActiveModal('word'); }, []); const handleSentenceButtonPress = useCallback((sentence) => { setSelectedSentence(sentence); setSelectedWord(null); bottomSheetModalRef.current?.present(); setActiveModal('sentence'); }, []); const loadFonts = useCallback(async () => { await Font.loadAsync(ARABIC_FONTS); }, []); const fetchStory = useCallback(async () => { try { let storyData; if (story) { storyData = story; } else if (storyId) { const cachedData = await AsyncStorage.getItem(`story-${storyId}`); if (cachedData) { storyData = JSON.parse(cachedData); } else { storyData = await fetchStoryFromFirestore(storyId); if (storyData) { await AsyncStorage.setItem(`story-${storyId}`, JSON.stringify(storyData)); } } } if (storyData) { setCachedStory(storyData); setHasQuiz(storyData.test_questions && storyData.test_questions.length > 0); } } catch (error) { console.error('Error fetching story:', error); } finally { setLoading(false); } }, [story, storyId]); useEffect(() => { const initialize = async () => { await loadFonts(); await fetchStory(); // Known words are handled in the custom hook }; initialize(); }, [loadFonts, fetchStory]); useEffect(() => { if (cachedStory) { const storyTitle = locale === 'tr' ? cachedStory.storytitleturkish : cachedStory.storytitleenglish; const truncatedTitle = storyTitle.length > 23 ? `${storyTitle.substring(0, 23)}...` : storyTitle; navigation.setOptions({ title: truncatedTitle, headerStyle: { backgroundColor: theme.lighterCardBackgroundColor }, headerTintColor: theme.textColor, headerTitleStyle: { color: theme.textColor, fontWeight: 'bold' }, headerRight: () => ( <View style={{ flexDirection: 'row', alignItems: 'center' }}> <ToggleHeartButton storyId={cachedStory.id} initialHeartCount={cachedStory.hearts} /> <TouchableOpacity onPress={openSettingsModal} style={styles.fontSizeControl}> <Ionicons name="settings-outline" size={24} color={theme.textColor} /> </TouchableOpacity> </View> ), }); } }, [navigation, cachedStory, theme, locale, openSettingsModal]); useFocusEffect( useCallback(() => { const onBackPress = () => { if (activeModal) { closeAllModals(); return true; } return false; }; BackHandler.addEventListener('hardwareBackPress', onBackPress); return () => BackHandler.removeEventListener('hardwareBackPress', onBackPress); }, [activeModal, closeAllModals]) ); const renderSentence = useCallback( ({ item: sentenceObj }) => ( <Sentence sentenceObj={sentenceObj} selectedSentence={selectedSentence} handleSentenceButtonPress={handleSentenceButtonPress} handleWordPress={handleWordPress} fontSize={fontSize} theme={theme} arabicFont={arabicFont} knownWords={knownWords} /> ), [ selectedSentence, handleSentenceButtonPress, handleWordPress, fontSize, theme, arabicFont, knownWords, ] ); const memoizedData = useMemo(() => cachedStory?.sentences || [], [cachedStory]); const keyExtractor = useCallback((item) => item.id.toString(), []); useEffect(() => { const updatedKnownWords = new Set(vocabulary.map((word) => word.word)); setKnownWords(updatedKnownWords); }, [vocabulary, setKnownWords]); if (loading) { return <LoadingIndicator theme={theme} />; } if (!cachedStory) { return <NotFound theme={theme} message={t('storyNotFound')} />; } return ( <BottomSheetModalProvider> <View style={[styles.container, { backgroundColor: theme.backgroundColorForWordBox }]}> <FlatList data={memoizedData} renderItem={renderSentence} keyExtractor={keyExtractor} ListHeaderComponent={() => ( <> <Image source={{ uri: cachedStory.thumbnail }} style={styles.thumbnail} /> <View style={styles.content}> <View style={styles.header}> <Text style={[styles.titleArabic, { color: theme.textColor, fontFamily: arabicFont }]} > {cachedStory.storytitlearabic} </Text> <Text style={[styles.titleTurkish, { color: theme.secondaryColor }]}> {locale === 'tr' ? cachedStory.storytitleturkish : cachedStory.storytitleenglish} </Text> </View> </View> </> )} initialNumToRender={10} maxToRenderPerBatch={5} windowSize={10} /> {hasQuiz && ( <TouchableOpacity style={[styles.quizButton, { backgroundColor: theme.primaryColor }]} onPress={openQuizModal} > <Text style={styles.quizButtonText}>{t('startQuiz')}</Text> </TouchableOpacity> )} {/* Word/Sentence Explanation Modal */} <BottomSheetModal ref={bottomSheetModalRef} index={0} snapPoints={['45%', '90%']} handleStyle={{ backgroundColor: theme.backgroundColorForWordBox }} onDismiss={() => setActiveModal(null)} > <WordSentenceModal wordOrSentence={selectedWord || selectedSentence} storyId={cachedStory.id} onClose={() => bottomSheetModalRef.current?.dismiss()} theme={theme} /> </BottomSheetModal> {/* Settings Modal */} <BottomSheetModal ref={settingsBottomSheetModalRef} index={0} snapPoints={['85%']} handleStyle={{ backgroundColor: theme.backgroundColorForWordBox }} onDismiss={() => setActiveModal(null)} > <SettingsModal fontSize={fontSize} increaseFontSize={increaseFontSize} decreaseFontSize={decreaseFontSize} fonts={ARABIC_FONTS} setFontFamily={setFontFamily} theme={theme} arabicFont={arabicFont} t={t} /> </BottomSheetModal> {/* Quiz Modal */} {hasQuiz && ( <BottomSheetModal ref={quizBottomSheetModalRef} index={0} snapPoints={['90%']} handleStyle={{ backgroundColor: theme.backgroundColorForWordBox }} onDismiss={() => setActiveModal(null)} > <QuizModal story={cachedStory} onClose={() => { quizBottomSheetModalRef.current?.dismiss(); setActiveModal(null); }} theme={theme} /> </BottomSheetModal> )} </View> </BottomSheetModalProvider> ); } ReadStoryScreen.propTypes = { route: PropTypes.shape({ params: PropTypes.shape({ story: PropTypes.object, storyId: PropTypes.string, }), }).isRequired, navigation: PropTypes.object.isRequired, }; const styles = StyleSheet.create({ container: { flex: 1, }, thumbnail: { width: '100%', aspectRatio: 1 / 1, }, content: { padding: 16, }, header: { flexDirection: 'column', alignItems: 'center', justifyContent: 'center', marginBottom: 16, }, titleArabic: { fontSize: 26, }, titleTurkish: { fontSize: 20, marginTop: 4, textAlign: 'center', }, fontSizeControl: { marginHorizontal: 5, }, quizButton: { position: 'absolute', bottom: 20, right: 20, padding: 10, borderRadius: 5, }, quizButtonText: { color: 'white', fontWeight: 'bold', }, });5. Explanation of Optimizations
-
Modular Components: By extracting
Word,Sentence, and various modals into separate files, theReadStoryScreenbecomes cleaner and easier to manage. Each component handles its specific functionality, making the codebase more organized. -
Custom Hooks: The
useKnownWordshook encapsulates the logic for fetching and managing known words. This separation of concerns enhances reusability and readability. -
Utility Functions: Moving the story fetching logic to a utility file (
fetchStory.js) keeps API-related code separate from the UI logic. -
Performance Enhancements:
- Memoization: Components like
WordandSentenceare wrapped withReact.memoto prevent unnecessary re-renders when their props haven't changed. - useCallback & useMemo: Functions and computed values are memoized to maintain referential integrity across renders, optimizing performance, especially within lists.
- Memoization: Components like
-
Styles Separation: Styles are moved to separate
.styles.jsfiles corresponding to their components. This approach keeps styles organized and component-specific. -
Common Components:
LoadingIndicatorandNotFoundare reusable components that can be used across different screens, promoting DRY (Don't Repeat Yourself) principles. -
PropTypes Validation: PropTypes are defined for each component to ensure that components receive the correct props, aiding in debugging and maintaining type safety.
-
BottomSheetModals: The modals are abstracted into their own components, making it easier to manage their content and styles separately from the main screen logic.
6. Additional Recommendations
-
TypeScript Integration: Consider migrating to TypeScript for enhanced type safety and better developer experience.
-
Error Handling: Improve error handling by providing user feedback in case of failures during data fetching or other asynchronous operations.
-
Accessibility: Ensure that all touchable elements are accessible, with proper labels and hints for users relying on assistive technologies.
-
Testing: Implement unit and integration tests for components and hooks to ensure reliability and prevent regressions.
-
Performance Monitoring: Use tools like React Native's Performance Monitor or third-party services to track and optimize performance further.
7. Final Thoughts
By modularizing your code and separating concerns, you not only enhance the maintainability and scalability of your application but also set a foundation for easier collaboration and testing. The refactored structure promotes reusability and cleaner code, making future enhancements more straightforward.
Feel free to adjust the paths and names based on your project's specific structure and conventions. If you have any further questions or need assistance with specific parts of the implementation, feel free to ask!
- Modularize Components: Extract