From 93062292d5e56aa391e9bb29a4a13cdd18c3f6eb Mon Sep 17 00:00:00 2001 From: sedataskan Date: Thu, 13 Mar 2025 20:39:05 +0300 Subject: [PATCH] Add current page localization and update book components #54 #55 #57 --- src/components/CurrentlyReadingBook.tsx | 11 +- src/components/book.tsx | 19 +-- src/i18n/locales/en.json | 3 +- src/i18n/locales/tr.json | 3 +- src/screens/AddBookScreen.tsx | 153 +++++++++++++----------- src/screens/BookDetailScreen.tsx | 2 - src/screens/BookPreviewScreen.tsx | 129 ++++++++++++++++---- src/screens/HomeMainScreen.tsx | 10 +- src/screens/LibraryScreen.tsx | 9 +- src/screens/ProfileScreen.tsx | 3 +- 10 files changed, 229 insertions(+), 113 deletions(-) diff --git a/src/components/CurrentlyReadingBook.tsx b/src/components/CurrentlyReadingBook.tsx index bf36256..5d5ff84 100644 --- a/src/components/CurrentlyReadingBook.tsx +++ b/src/components/CurrentlyReadingBook.tsx @@ -5,6 +5,7 @@ import { sizes } from "../constants/sizes"; import { Ionicons } from "@expo/vector-icons"; import { useNavigation } from "@react-navigation/native"; import { StackNavigationProp } from "@react-navigation/stack"; +import { useTranslation } from "react-i18next"; type RootStackParamList = { BookPreview: { @@ -20,6 +21,7 @@ type RootStackParamList = { status: string; favPage?: number; favPageImage?: string; + currentPage?: number; }; }; }; @@ -40,6 +42,7 @@ type CurrentlyReadingBookProps = { status: string; favPage?: number; favPageImage?: string; + currentPage?: number; }; const getSecureImageUrl = (url: string | undefined) => { @@ -58,9 +61,10 @@ export const CurrentlyReadingBook = ({ status, favPage, favPageImage, + currentPage, }: CurrentlyReadingBookProps) => { const navigation = useNavigation(); - + const { t } = useTranslation(); const handlePress = () => { navigation.navigate("BookPreview", { book: { @@ -75,6 +79,7 @@ export const CurrentlyReadingBook = ({ status, favPage, favPageImage, + currentPage: currentPage !== undefined ? currentPage : 0, }, }); }; @@ -100,7 +105,9 @@ export const CurrentlyReadingBook = ({ - {pages} pages + + {t("current_page")}: {currentPage} + diff --git a/src/components/book.tsx b/src/components/book.tsx index ed4ff41..68c06f8 100644 --- a/src/components/book.tsx +++ b/src/components/book.tsx @@ -21,6 +21,7 @@ type RootStackParamList = { status: string; favPage?: number; favPageImage?: string; + currentPage?: number; }; }; }; @@ -31,15 +32,12 @@ type BookScreenNavigationProp = StackNavigationProp< >; const getStatusIcon = (status: string) => { - switch (status.toLowerCase()) { + switch (status) { case "read": - case "okudum": return "checkmark-circle"; - case "to read": - case "okunacak": + case "to_read": return "time"; - case "currently reading": - case "şu an okuyorum": + case "currently_reading": return "book"; default: return "help-circle"; @@ -57,6 +55,7 @@ export const Book = ({ status, favPage, favPageImage, + currentPage, style, }: { title: string; @@ -69,6 +68,7 @@ export const Book = ({ status: string; favPage?: number; favPageImage?: string; + currentPage?: number; style?: object; }) => { const navigation = useNavigation(); @@ -94,6 +94,7 @@ export const Book = ({ status, favPage, favPageImage, + currentPage: currentPage ?? 0, }, }); }; @@ -113,7 +114,7 @@ export const Book = ({ style={styles.image} /> - + {title} @@ -164,16 +165,18 @@ const styles = StyleSheet.create({ borderRadius: sizes.borderRadius, }, title: { - fontSize: sizes.fontSizeMedium, + fontSize: sizes.fontSizeSmall, fontWeight: "bold", color: colors.textPrimary, marginTop: 8, marginBottom: 4, maxWidth: 105, + overflow: "hidden", }, author: { fontSize: sizes.fontSizeSmall, color: colors.textSecondary, maxWidth: 105, + overflow: "hidden", }, }); diff --git a/src/i18n/locales/en.json b/src/i18n/locales/en.json index bd61273..e9bcb22 100644 --- a/src/i18n/locales/en.json +++ b/src/i18n/locales/en.json @@ -82,5 +82,6 @@ "turkish": "Turkish", "tap_to_change_fav_page_image": "Tap to change favorite page image", "favPage": "Favorite Page", - "favPageNumber": "Favorite Page Number" + "favPageNumber": "Favorite Page Number", + "current_page": "Current Page" } diff --git a/src/i18n/locales/tr.json b/src/i18n/locales/tr.json index ae59d2e..bda481c 100644 --- a/src/i18n/locales/tr.json +++ b/src/i18n/locales/tr.json @@ -83,5 +83,6 @@ "turkish": "Türkçe", "tap_to_change_fav_page_image": "Favori sayfa resmini değiştirmek için dokunun", "favPage": "Favori Sayfa", - "favPageNumber": "Favori Sayfa Numarası" + "favPageNumber": "Favori Sayfa Numarası", + "current_page": "Şu Anki Sayfa" } diff --git a/src/screens/AddBookScreen.tsx b/src/screens/AddBookScreen.tsx index 13da6e1..71ec457 100644 --- a/src/screens/AddBookScreen.tsx +++ b/src/screens/AddBookScreen.tsx @@ -7,7 +7,6 @@ import { Button, Image, TouchableOpacity, - Modal, } from "react-native"; import { Layout } from "../layout/layout"; import AsyncStorage from "@react-native-async-storage/async-storage"; @@ -17,11 +16,11 @@ import { KeyboardAvoidingScrollView } from "react-native-keyboard-avoiding-scrol import { ScrollView } from "react-native-gesture-handler"; import { colors } from "../constants/colors"; import { sizes } from "../constants/sizes"; -import { Picker } from "@react-native-picker/picker"; import * as ImagePicker from "expo-image-picker"; import { Ionicons } from "@expo/vector-icons"; import { StarRating } from "../components/StarRating"; import { useTranslation } from "react-i18next"; +import DropDownPicker from "react-native-dropdown-picker"; type AddBookScreenRouteProp = RouteProp< { @@ -50,8 +49,9 @@ export default function AddBookScreen({ const [error, setError] = useState(""); const [errorFields, setErrorFields] = useState([]); const [status, setStatus] = useState(""); - const [isPickerVisible, setPickerVisible] = useState(false); const [showAdditionalFields, setShowAdditionalFields] = useState(false); + const [open, setOpen] = useState(false); + const [currentPage, setCurrentPage] = useState(0); useEffect(() => { if (isEdit && book) { @@ -65,6 +65,7 @@ export default function AddBookScreen({ setStatus(book.status); setFavPageImage(book.favPageImage); setFavPage(book.favPage); + setCurrentPage(book.currentPage); } }, [isEdit, book]); @@ -91,11 +92,17 @@ export default function AddBookScreen({ favPage, saveDate: new Date(), status, + currentPage: currentPage ?? 0, }; try { const existingBooks = await AsyncStorage.getItem("books"); - const books = existingBooks ? JSON.parse(existingBooks) : []; + let books = existingBooks ? JSON.parse(existingBooks) : []; + + books = books.map((b: any) => ({ + ...b, + currentPage: Number(b.currentPage), + })); if (isEdit) { const updatedBooks = books.map((b: any) => b.title === book.title ? newBook : b @@ -103,6 +110,7 @@ export default function AddBookScreen({ await AsyncStorage.setItem("books", JSON.stringify(updatedBooks)); alert(t("book_updated_successfully")); } else { + newBook.currentPage = Number(newBook.currentPage); books.push(newBook); await AsyncStorage.setItem("books", JSON.stringify(books)); alert(t("book_saved_successfully")); @@ -119,6 +127,7 @@ export default function AddBookScreen({ setStatus(""); setFavPageImage(""); setFavPage(0); + setCurrentPage(0); onBookAdded(newBook); navigation.navigate("BookPreview" as never, { book: newBook, @@ -180,6 +189,39 @@ export default function AddBookScreen({ } }; + const getStatusLabel = (statusKey: string) => { + switch (statusKey) { + case "read": + return t("read"); + case "to_read": + return t("to_read"); + case "currently_reading": + return t("currently_reading"); + } + }; + + const handleStatusChange = async (newStatusKey: string) => { + setStatus(newStatusKey); + if (book) { + try { + const existingBooks = await AsyncStorage.getItem("books"); + const books = existingBooks ? JSON.parse(existingBooks) : []; + const updatedBooks = books.map((b: any) => + b.title === book.title ? { ...b, status: newStatusKey } : b + ); + await AsyncStorage.setItem("books", JSON.stringify(updatedBooks)); + } catch (error) { + console.error("Error updating book status", error); + } + } + }; + + const items = [ + { label: t("read"), value: "read" }, + { label: t("to_read"), value: "to_read" }, + { label: t("currently_reading"), value: "currently_reading" }, + ]; + return ( @@ -225,17 +267,29 @@ export default function AddBookScreen({ {t("status")} * - setPickerVisible(true)} - > - - {status || t("select_status")} - - + { + const newStatusKey = callback(status); + setStatus(newStatusKey); + handleStatusChange(newStatusKey); + }} + setItems={() => {}} + style={styles.statusDropdown} + dropDownContainerStyle={{ + backgroundColor: colors.background, + borderColor: colors.secondary, + }} + selectedItemContainerStyle={{ + backgroundColor: colors.backgroundSecondary, + }} + placeholder={getStatusLabel(status)} + /> - - {status === t("read") && ( + {status === "read" && ( )} + {status === "currently_reading" && ( + + {t("current_page")} + { + setCurrentPage(text); + }} + keyboardType="numeric" + /> + + )} @@ -352,34 +419,6 @@ export default function AddBookScreen({ - - setPickerVisible(false)} - > - - -