From 547773f0f6b0cff0344f187f00a49d52bd01bd96 Mon Sep 17 00:00:00 2001 From: Rajarshee Chatterjee Date: Wed, 30 Dec 2020 21:03:19 +0530 Subject: [PATCH] Crash fixes --- src/components/ChapterCard.js | 2 +- src/components/HistoryCard.js | 1 + src/components/NovelInfoHeader.js | 13 +- src/navigation/Router.js | 2 + src/screens/ChapterItem.js | 14 +- src/screens/Latest.js | 90 --------- src/screens/Library.js | 8 +- src/screens/More/More.js | 2 +- src/screens/More/Settings.js | 137 +++++++++++++ src/screens/NovelItem.js | 181 +++++++----------- src/screens/Sources/BoxNovel/BoxNovel.js | 5 +- .../Sources/ReadLightNovel/ReadLightNovel.js | 5 +- 12 files changed, 238 insertions(+), 222 deletions(-) delete mode 100644 src/screens/Latest.js create mode 100644 src/screens/More/Settings.js diff --git a/src/components/ChapterCard.js b/src/components/ChapterCard.js index 510a20f8..e9aa11cb 100644 --- a/src/components/ChapterCard.js +++ b/src/components/ChapterCard.js @@ -41,7 +41,7 @@ const ChapterCard = ({ ]} numberOfLines={1} > - {chapter.chapterName.substring(0, 50) + "..."} + {chapter.chapterName.substring(0, 50)} ( novelCover: item.novelCover, extensionId: item.extensionId, novelUrl: item.novelUrl, + navigatingFrom: 0, }) } > diff --git a/src/components/NovelInfoHeader.js b/src/components/NovelInfoHeader.js index 20d12be8..ae507ba7 100644 --- a/src/components/NovelInfoHeader.js +++ b/src/components/NovelInfoHeader.js @@ -178,7 +178,7 @@ const NovelInfoHeader = ({ backgroundColor: theme.colorAccentDark, marginHorizontal: 15, }, - novel.novelSummary.length === 0 && { + novel.novelSummary === "" && { marginBottom: 20, }, ]} @@ -193,7 +193,7 @@ const NovelInfoHeader = ({ > {libraryStatus === 0 ? "Add to library" : "In Library"} - {novel.novelSummary.length > 0 && ( + {novel.novelSummary !== "" && ( )} - {item.novelSummary && item.novelSummary.length > 0 && ( + {item.novelSummary !== "" && ( { + ); }; diff --git a/src/screens/ChapterItem.js b/src/screens/ChapterItem.js index cccb40d2..dd4ee243 100644 --- a/src/screens/ChapterItem.js +++ b/src/screens/ChapterItem.js @@ -89,10 +89,10 @@ const ChapterItem = ({ route, navigation }) => { (txObj, res) => { console.log(res.rows.length); if (res.rows.length === 0) { - console.log("Not Downloaded"); + // console.log("Not Downloaded"); getChapter(); } else { - console.log("Already Downloaded"); + // console.log("Already Downloaded"); getChapterFromDB(); } checkIfInHistory(); @@ -162,8 +162,12 @@ const ChapterItem = ({ route, navigation }) => { > { - // navigation.navigate("NovelItem", { novelUrl }); - navigation.goBack(); + navigation.navigate("NovelItem", { + novelUrl, + extensionId, + navigatingFrom: 0, + }); + // navigation.goBack(); }} color={"white"} size={26} @@ -231,7 +235,7 @@ const ChapterItem = ({ route, navigation }) => { onScroll={({ nativeEvent }) => { _panel.hide(); if (isCloseToBottom(nativeEvent)) { - console.log("Scroll End Reached"); + // console.log("Scroll End Reached"); setRead(); } }} diff --git a/src/screens/Latest.js b/src/screens/Latest.js deleted file mode 100644 index 240ae5be..00000000 --- a/src/screens/Latest.js +++ /dev/null @@ -1,90 +0,0 @@ -import React, { useEffect, useState } from "react"; -import { - StyleSheet, - View, - FlatList, - ActivityIndicator, - RefreshControl, -} from "react-native"; -import { TouchableRipple } from "react-native-paper"; -import NovelCover from "../components/NovelCover"; -import { theme } from "../theming/theme"; - -const LatestScreen = ({ navigation }) => { - const [loading, setLoading] = useState(true); - const [refreshing, setRefreshing] = useState(true); - const [novels, setNovels] = useState(); - - const getNovels = () => { - fetch("https://lnreader-extensions.herokuapp.com/api/1/latest") - .then((response) => response.json()) - .then((json) => setNovels(json)) - .catch((error) => console.error(error)) - .finally(() => { - setRefreshing(false); - setLoading(false); - }); - }; - - useEffect(() => { - getNovels(); - }, []); - - const onRefresh = () => { - setRefreshing(true); - getNovels(); - }; - - return ( - - {loading ? ( - - - - ) : ( - item.novelUrl} - renderItem={({ item }) => ( - - navigation.navigate("NovelItem", item) - } - /> - )} - refreshControl={ - - } - /> - )} - - ); -}; - -export default LatestScreen; - -const styles = StyleSheet.create({ - container: { - flex: 1, - // backgroundColor: "#202125", - backgroundColor: "#000000", - }, - opac: { - height: 190, - flex: 1 / 3, - marginHorizontal: 3.6, - marginVertical: 3.2, - }, -}); diff --git a/src/screens/Library.js b/src/screens/Library.js index 49ae69f3..d07345ea 100644 --- a/src/screens/Library.js +++ b/src/screens/Library.js @@ -20,7 +20,6 @@ import * as SQLite from "expo-sqlite"; const db = SQLite.openDatabase("lnreader.db"); const UpdatesScreen = ({ navigation }) => { - const [loading, setLoading] = useState(true); const [refreshing, setRefreshing] = useState(false); const [novels, setNovels] = useState(); @@ -31,8 +30,6 @@ const UpdatesScreen = ({ navigation }) => { null, (txObj, { rows: { _array } }) => { setNovels(_array); - // console.log(_array); - setLoading(false); setRefreshing(false); }, (txObj, error) => console.log("Error ", error) @@ -104,7 +101,10 @@ const UpdatesScreen = ({ navigation }) => { - navigation.navigate("NovelItem", item) + navigation.navigate("NovelItem", { + ...item, + navigatingFrom: 1, + }) } /> )} diff --git a/src/screens/More/More.js b/src/screens/More/More.js index cd56bcbb..c1fd42b6 100644 --- a/src/screens/More/More.js +++ b/src/screens/More/More.js @@ -25,7 +25,7 @@ const MoreScreen = ({ navigation }) => { icon="code-tags" /> )} - onPress={() => navigation.navigate("About")} + onPress={() => navigation.navigate("Settings")} /> { + const desciptionStyles = { + color: theme.textColorSecondaryDark, + }; + + const titleStyles = { color: theme.textColorPrimaryDark }; + + const deleteHistory = () => { + db.transaction((tx) => { + tx.executeSql("DELETE FROM HistoryTable"); + }); + }; + + const deleteNovelsNotInLib = () => { + db.transaction((tx) => { + tx.executeSql( + "DELETE FROM LibraryTable WHERE libraryStatus=0", + null, + (txObj, res) => { + console.log("Deleted"); + }, + (txObj, error) => console.log("Error ", error) + ); + }); + }; + + const deleteDatabase = () => { + db.transaction((tx) => { + tx.executeSql( + "DROP TABLE LibraryTable", + null, + (txObj, { rows: { _array } }) => { + console.log("DELETED LIB TABLE"); + }, + (txObj, error) => console.log("Error ", error) + ); + tx.executeSql( + "DROP TABLE ChapterTable", + null, + (txObj, { rows: { _array } }) => { + console.log("DELETED CHAP TABLE"); + }, + (txObj, error) => console.log("Error ", error) + ); + tx.executeSql( + "DROP TABLE HistoryTable", + null, + (txObj, { rows: { _array } }) => { + console.log("DELETED History TABLE"); + }, + (txObj, error) => console.log("Error ", error) + ); + tx.executeSql( + "DROP TABLE DownloadsTable", + null, + (txObj, { rows: { _array } }) => { + console.log("DELETED Downloads TABLE"); + }, + (txObj, error) => console.log("Error ", error) + ); + }); + setNovels([]); + }; + + return ( + <> + navigation.goBack()} + /> + + + + Data + + deleteNovelsNotInLib()} + rippleColor={theme.rippleColorDark} + /> + deleteHistory()} + rippleColor={theme.rippleColorDark} + /> + + Devloper options + + deleteDatabase()} + /> + + + + ); +}; + +export default AboutScreen; diff --git a/src/screens/NovelItem.js b/src/screens/NovelItem.js index 6b69881a..59ac6bed 100644 --- a/src/screens/NovelItem.js +++ b/src/screens/NovelItem.js @@ -1,13 +1,11 @@ import React, { useState, useCallback } from "react"; import { useFocusEffect } from "@react-navigation/native"; - import { StyleSheet, View, FlatList, RefreshControl, ToastAndroid, - ActivityIndicator, } from "react-native"; import { Appbar, FAB, ProgressBar } from "react-native-paper"; @@ -22,7 +20,7 @@ const db = SQLite.openDatabase("lnreader.db"); const NovelItem = ({ route, navigation }) => { const item = route.params; - const { extensionId, novelUrl } = route.params; + const { extensionId, novelUrl, navigatingFrom } = route.params; const [loading, setLoading] = useState(true); const [refreshing, setRefreshing] = useState(false); @@ -34,7 +32,6 @@ const NovelItem = ({ route, navigation }) => { const [sort, setSort] = useState("ASC"); const [readingStatus, setReadingStatus] = useState(); - const [downloading, setDownloading] = useState(false); const getNovel = () => { @@ -45,9 +42,13 @@ const NovelItem = ({ route, navigation }) => { .then((json) => { setNovel(json); setChapters(json.novelChapters); - insertIntoDb(json, json.novelChapters); let chaps = json.novelChapters; - resumeReading(chaps[0].chapterName, chaps[0].chapterUrl); + insertIntoDb(json, json.novelChapters); + setReadingStatus({ + chapterName: chaps[0].chapterName, + chapterUrl: chaps[0].chapterUrl, + fabLabel: "Start", + }); }) .catch((error) => console.error(error)) .finally(() => { @@ -60,12 +61,13 @@ const NovelItem = ({ route, navigation }) => { db.transaction((tx) => { tx.executeSql( `SELECT * FROM ChapterTable WHERE novelUrl=? ORDER BY chapterId ${sort}`, - [item.novelUrl], + [novelUrl], (txObj, { rows: { _array } }) => { setChapters(_array); - resumeReading(_array[0].chapterName, _array[0].chapterUrl); - setRefreshing(false); + resumeReading(_array[0]); + setLoading(false); + setRefreshing(false); }, (txObj, error) => console.log("Error ", error) ); @@ -81,14 +83,13 @@ const NovelItem = ({ route, navigation }) => { } }; - const resumeReading = (chapName, chapUrl) => { + const resumeReading = (chap) => { db.transaction((tx) => { tx.executeSql( "SELECT * FROM HistoryTable WHERE novelUrl = ?", [item.novelUrl], (txObj, results) => { - let len = results.rows.length; - if (len > 0) { + if (results.rows.length > 0) { let row = results.rows.item(0); setReadingStatus({ chapterUrl: row.chapterUrl, @@ -97,8 +98,8 @@ const NovelItem = ({ route, navigation }) => { }); } else { setReadingStatus({ - chapterUrl: chapUrl, - chapterName: chapName, + chapterUrl: chap.chapterUrl, + chapterName: chap.chapterName, fabLabel: "Start", }); } @@ -109,11 +110,12 @@ const NovelItem = ({ route, navigation }) => { }; const insertIntoDb = (nov, chaps) => { + // Insert into database db.transaction((tx) => { tx.executeSql( - "INSERT OR REPLACE INTO LibraryTable (novelUrl, novelName, novelCover, novelSummary, Alternative, `Author(s)`, `Genre(s)`, Type, `Release`, Status, extensionId, libraryStatus) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + "INSERT INTO LibraryTable (novelUrl, novelName, novelCover, novelSummary, Alternative, `Author(s)`, `Genre(s)`, Type, `Release`, Status, extensionId, libraryStatus) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [ - item.novelUrl, + novelUrl, nov.novelName, nov.novelCover, nov.novelSummary, @@ -125,13 +127,10 @@ const NovelItem = ({ route, navigation }) => { nov.Status, extensionId, 0, - ], - (tx, res) => { - // console.log("Inserted into DB"); - }, - (txObj, error) => console.log("Error ", error) + ] ); + // Insert chapters into database chaps.map((chap) => tx.executeSql( "INSERT INTO ChapterTable (chapterUrl, chapterName, releaseDate, novelUrl) values (?, ?, ?, ?)", @@ -139,16 +138,19 @@ const NovelItem = ({ route, navigation }) => { chap.chapterUrl, chap.chapterName, chap.releaseDate, - item.novelUrl, + novelUrl, ] ) ); }); }; + /** + * Download th chapter if not downloaded else delete + */ const downloadChapter = (downloadStatus, cdUrl) => { - setDownloading(true); if (downloadStatus === 0) { + setDownloading(true); fetch( `https://lnreader-extensions.herokuapp.com/api/${extensionId}/${cdUrl}` ) @@ -157,13 +159,7 @@ const NovelItem = ({ route, navigation }) => { db.transaction((tx) => { tx.executeSql( `UPDATE ChapterTable SET downloaded = 1 WHERE chapterUrl = ?`, - [cdUrl], - (tx, res) => { - // console.log( - // "Updated Download Status to downloaded" - // ); - }, - (txObj, error) => console.log("Error ", error) + [cdUrl] ); tx.executeSql( `INSERT INTO DownloadsTable (chapterUrl, novelUrl, chapterName, chapterText, prevChapter, nextChapter) VALUES (?, ?, ?, ?, ?, ?)`, @@ -180,7 +176,6 @@ const NovelItem = ({ route, navigation }) => { `Downloaded ${json.chapterName}`, ToastAndroid.SHORT ); - // console.log("Inserted into Downloads Table"); }, (txObj, error) => console.log("Error ", error) ); @@ -188,24 +183,20 @@ const NovelItem = ({ route, navigation }) => { }) .catch((error) => console.error(error)) .finally(() => { - checkIfExistsInDB(); + setDownloading(false); + getChaptersFromDb(); }); } else { db.transaction((tx) => { tx.executeSql( `UPDATE ChapterTable SET downloaded = 0 WHERE chapterUrl = ?`, - [cdUrl], - (tx, res) => { - // console.log("Updated Download Status to downloaded"); - }, - (txObj, error) => console.log("Error ", error) + [cdUrl] ); tx.executeSql( `DELETE FROM DownloadsTable WHERE chapterUrl = ?`, [cdUrl], (tx, res) => { - // console.log("Deleted Download"); - checkIfExistsInDB(); + getChaptersFromDb(); ToastAndroid.show( `Chapter deleted`, ToastAndroid.SHORT @@ -219,6 +210,7 @@ const NovelItem = ({ route, navigation }) => { const insertToLibrary = () => { if (libraryStatus === 0) { + // Insert into library db.transaction((tx) => { tx.executeSql( "UPDATE LibraryTable SET libraryStatus = 1 WHERE novelUrl=?", @@ -228,14 +220,13 @@ const NovelItem = ({ route, navigation }) => { "Added to library", ToastAndroid.SHORT ); - // console.log("Inserted into Library"); - setlibraryStatus(1); }, (txObj, error) => console.log("Error ", error) ); }); } else { + // Delete from library db.transaction((tx) => { tx.executeSql( "UPDATE LibraryTable SET libraryStatus = 0 WHERE novelUrl=?", @@ -245,7 +236,6 @@ const NovelItem = ({ route, navigation }) => { "Removed from library", ToastAndroid.SHORT ); - // console.log("Removed From Library"); setlibraryStatus(0); }, (txObj, error) => console.log("Error ", error) @@ -254,44 +244,42 @@ const NovelItem = ({ route, navigation }) => { } }; - const checkIfExistsInDB = () => { - db.transaction((tx) => { - tx.executeSql( - "SELECT * FROM LibraryTable WHERE novelUrl=?", - [novelUrl], - (txObj, res) => { - if (res.rows.length === 0) { - setRefreshing(true); - setlibraryStatus(0); - // console.log("Not In Database"); - getNovel(); - } else { - // setRefreshing(true); - // console.log("In Database"); - setlibraryStatus(res.rows.item(0).libraryStatus); - setNovel(res.rows.item(0)); - getChaptersFromDb(); - } - }, - (txObj, error) => console.log("Error ", error) - ); - }); - setDownloading(false); + const checkIfExistsInDb = () => { + setRefreshing(true); + if (navigatingFrom === 1) { + getChaptersFromDb(); + } else { + db.transaction((tx) => { + tx.executeSql( + "SELECT * FROM LibraryTable WHERE novelUrl=? LIMIT 1", + [novelUrl], + (txObj, res) => { + if (res.rows.length === 0) { + // Not in database + setlibraryStatus(0); + getNovel(); + } else { + // In database + setNovel(res.rows.item(0)); + setlibraryStatus(res.rows.item(0).libraryStatus); + getChaptersFromDb(); + } + }, + (txObj, error) => console.log("Error ", error) + ); + }); + } }; - // useEffect(() => { - // checkIfExistsInDB(item.novelUrl); - // }, [sort]); - useFocusEffect( useCallback(() => { - checkIfExistsInDB(); + checkIfExistsInDb(); }, [sort]) ); const onRefresh = async () => { setRefreshing(true); - checkIfExistsInDB(); + checkIfExistsInDb(); }; const renderChapterCard = ({ item }) => ( @@ -304,6 +292,12 @@ const NovelItem = ({ route, navigation }) => { /> ); + const getItemLayout = (data, index) => ({ + length: 72, + offset: 72 * index, + index, + }); + return ( <> @@ -329,12 +323,13 @@ const NovelItem = ({ route, navigation }) => { /> item.chapterUrl} removeClippedSubviews={true} maxToRenderPerBatch={10} - initialNumToRender={10} + windowSize={15} + getItemLayout={getItemLayout} + initialNumToRender={5} renderItem={renderChapterCard} ListHeaderComponent={() => ( { /> } /> - {!loading && readingStatus && ( + {readingStatus && ( { - navigation.navigate("NovelItem", item) + navigation.navigate("NovelItem", { + ...item, + navigatingFrom: 0, + }) } /> )} diff --git a/src/screens/Sources/ReadLightNovel/ReadLightNovel.js b/src/screens/Sources/ReadLightNovel/ReadLightNovel.js index bba673cb..a3e70ef7 100644 --- a/src/screens/Sources/ReadLightNovel/ReadLightNovel.js +++ b/src/screens/Sources/ReadLightNovel/ReadLightNovel.js @@ -124,7 +124,10 @@ const AllNovels = ({ navigation }) => { - navigation.navigate("NovelItem", item) + navigation.navigate("NovelItem", { + ...item, + navigatingFrom: 0, + }) } /> )}