From ea3f5ac7ee9ecf148186ae5e70dc7433a15cfc7e Mon Sep 17 00:00:00 2001 From: Rajarshee Chatterjee Date: Sat, 3 Jul 2021 11:24:04 +0530 Subject: [PATCH] Add jump to chapter --- src/screens/Novel/Novel.js | 23 +++ .../Novel/components/JumpToChapterModal.js | 148 ++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 src/screens/Novel/components/JumpToChapterModal.js diff --git a/src/screens/Novel/Novel.js b/src/screens/Novel/Novel.js index bb5794db..7c9ea138 100644 --- a/src/screens/Novel/Novel.js +++ b/src/screens/Novel/Novel.js @@ -38,6 +38,7 @@ import ChaptersSettingsSheet from "./components/ChaptersSettingsSheet"; import TrackSheet from "./components/Tracker/TrackSheet"; import ChapterActionsSheet from "./components/ChapterActionsSheet"; import { Row } from "../../components/Common"; +import JumpToChapterModal from "./components/JumpToChapterModal"; const Novel = ({ route, navigation }) => { const item = route.params; @@ -51,6 +52,7 @@ const Novel = ({ route, navigation }) => { const [selected, setSelected] = useState([]); const [downloadMenu, showDownloadMenu] = useState(false); + let flatlistRef = useRef(null); let chaptersSettingsSheetRef = useRef(null); let trackerSheetRef = useRef(null); let chapterActionsSheetRef = useRef(null); @@ -86,6 +88,8 @@ const Novel = ({ route, navigation }) => { /> ); + const [jumpToChapterModal, showJumpToChapterModal] = useState(false); + const renderItem = ({ item, index }) => ( { }) } /> + showJumpToChapterModal(true)} + /> )} (flatlistRef.current = ref)} data={!loading && chapters} keyExtractor={(item) => item.chapterId.toString()} removeClippedSubviews={true} @@ -362,6 +377,14 @@ const Novel = ({ route, navigation }) => { )} {!loading && ( + showJumpToChapterModal(false)} + theme={theme} + chapters={chapters} + novel={novel} + navigation={navigation} + /> { + const [mode, setMode] = useState(0); + + const [text, setText] = useState(); + const [error, setError] = useState(); + + const onDismiss = () => { + hideModal(); + setText(); + setError(); + }; + + const onSubmit = () => { + if (!mode) { + if (text <= chapters.length) { + navigation.navigate("Chapter", { + sourceId: novel.sourceId, + novelUrl: novel.novelUrl, + novelId: novel.novelId, + chapterId: chapters[text - 1].chapterId, + chapterUrl: chapters[text - 1].chapterUrl, + chapterName: chapters[text - 1].chapterName, + novelName: novel.novelName, + bookmark: chapters[text - 1].bookmark, + }); + hideModal(); + } else { + setError( + `Enter a valid chapter number (<= ${chapters.length})` + ); + } + } else { + const chapter = chapters.find((chap) => + chap.chapterName.includes(text) + ); + + if (chapter) { + navigation.navigate("Chapter", { + sourceId: novel.sourceId, + novelUrl: novel.novelUrl, + novelId: chapter.novelId, + chapterId: chapter.chapterId, + chapterUrl: chapter.chapterUrl, + chapterName: chapter.chapterName, + novelName: novel.novelName, + bookmark: chapter.bookmark, + }); + hideModal(); + } else { + setError(`Enter a valid chapter name`); + } + } + }; + + const onChangeText = (text) => setText(text); + + const textInputTheme = { + colors: { + primary: theme.colorAccent, + placeholder: theme.textColorHint, + text: theme.textColorPrimary, + background: theme.colorPrimary, + }, + }; + + return ( + + + + Jump to Chapter + + + {error} + + + Chapter Name + + setMode(!mode)} + color={theme.colorAccent} + /> + + + + ); +}; + +export default JumpToChapterModal; + +const styles = StyleSheet.create({ + modalContainer: { + margin: 30, + padding: 20, + borderRadius: 8, + }, + modalTitle: { + fontSize: 18, + marginBottom: 16, + }, + errorText: { + color: "#FF0033", + paddingTop: 8, + }, +});