Crash fixes

This commit is contained in:
Rajarshee Chatterjee
2020-12-30 21:03:19 +05:30
parent 40dfebe964
commit 547773f0f6
12 changed files with 238 additions and 222 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ const ChapterCard = ({
]}
numberOfLines={1}
>
{chapter.chapterName.substring(0, 50) + "..."}
{chapter.chapterName.substring(0, 50)}
</Text>
<Text
style={[
+1
View File
@@ -15,6 +15,7 @@ const HistoryCard = ({ item, deleteHistory, navigation }) => (
novelCover: item.novelCover,
extensionId: item.extensionId,
novelUrl: item.novelUrl,
navigatingFrom: 0,
})
}
>
+6 -7
View File
@@ -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"}
</Button>
{novel.novelSummary.length > 0 && (
{novel.novelSummary !== "" && (
<View
style={{
paddingHorizontal: 15,
@@ -288,10 +288,9 @@ const NovelInfoHeader = ({
backgroundColor: theme.colorAccentDark,
marginHorizontal: 15,
},
item.novelSummary &&
item.novelSummary.length === 0 && {
marginBottom: 20,
},
item.novelSummary === "" && {
marginBottom: 20,
},
]}
icon={
item.libraryStatus === 0
@@ -307,7 +306,7 @@ const NovelInfoHeader = ({
: "In Library"}
</Button>
)}
{item.novelSummary && item.novelSummary.length > 0 && (
{item.novelSummary !== "" && (
<View
style={{
paddingHorizontal: 15,
+2
View File
@@ -14,6 +14,7 @@ import Browse from "../screens/Browse";
import More from "../screens/More/More";
import About from "../screens/More/About";
import Settings from "../screens/More/Settings";
import NovelItem from "../screens/NovelItem";
import ChapterItem from "../screens/ChapterItem";
@@ -41,6 +42,7 @@ const MoreStack = () => {
<Stack.Navigator screenOptions={stackNavigatorConfig}>
<Stack.Screen name="More" component={More} />
<Stack.Screen name="About" component={About} />
<Stack.Screen name="Settings" component={Settings} />
</Stack.Navigator>
);
};
+9 -5
View File
@@ -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 }) => {
>
<Appbar.BackAction
onPress={() => {
// 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();
}
}}
-90
View File
@@ -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 (
<View style={styles.container}>
{loading ? (
<View style={{ flex: 1, justifyContent: "center" }}>
<ActivityIndicator
size="large"
color={theme.colorAccentDark}
/>
</View>
) : (
<FlatList
contentContainerStyle={styles.list}
numColumns={3}
data={novels}
showsVerticalScrollIndicator={false}
keyExtractor={(item) => item.novelUrl}
renderItem={({ item }) => (
<NovelCover
item={item}
onPress={() =>
navigation.navigate("NovelItem", item)
}
/>
)}
refreshControl={
<RefreshControl
refreshing={refreshing}
onRefresh={onRefresh}
colors={["white"]}
progressBackgroundColor={theme.colorAccentDark}
/>
}
/>
)}
</View>
);
};
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,
},
});
+4 -4
View File
@@ -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 }) => {
<NovelCover
item={item}
onPress={() =>
navigation.navigate("NovelItem", item)
navigation.navigate("NovelItem", {
...item,
navigatingFrom: 1,
})
}
/>
)}
+1 -1
View File
@@ -25,7 +25,7 @@ const MoreScreen = ({ navigation }) => {
icon="code-tags"
/>
)}
onPress={() => navigation.navigate("About")}
onPress={() => navigation.navigate("Settings")}
/>
<List.Item
titleStyle={{ color: theme.textColorPrimaryDark }}
+137
View File
@@ -0,0 +1,137 @@
import React from "react";
import { View } from "react-native";
import { List, Divider } from "react-native-paper";
import { CustomAppbar } from "../../components/Appbar";
import { theme } from "../../theming/theme";
import * as SQLite from "expo-sqlite";
const db = SQLite.openDatabase("lnreader.db");
const AboutScreen = ({ navigation }) => {
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 (
<>
<CustomAppbar
title="Settings"
onBackAction={() => navigation.goBack()}
/>
<View
style={{ flex: 1, backgroundColor: theme.colorDarkPrimaryDark }}
>
<List.Section
style={{
flex: 1,
backgroundColor: theme.colorDarkPrimaryDark,
}}
>
<List.Subheader
style={{
color: theme.colorAccentDark,
paddingBottom: 5,
}}
>
Data
</List.Subheader>
<List.Item
titleStyle={titleStyles}
title="Clear database"
descriptionStyle={desciptionStyles}
description="Delete history for novels not in your library"
onPress={() => deleteNovelsNotInLib()}
rippleColor={theme.rippleColorDark}
/>
<List.Item
titleStyle={titleStyles}
title="Clear history"
descriptionStyle={desciptionStyles}
description="Delete reading history for all novels"
onPress={() => deleteHistory()}
rippleColor={theme.rippleColorDark}
/>
<List.Subheader
style={{
color: theme.colorAccentDark,
paddingBottom: 5,
}}
>
Devloper options
</List.Subheader>
<List.Item
titleStyle={titleStyles}
title="Delete database"
descriptionStyle={desciptionStyles}
description="Delete entire database"
rippleColor={theme.rippleColorDark}
onPress={() => deleteDatabase()}
/>
</List.Section>
</View>
</>
);
};
export default AboutScreen;
+69 -112
View File
@@ -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 (
<>
<Appbar.Header style={{ backgroundColor: theme.colorDarkPrimary }}>
@@ -329,12 +323,13 @@ const NovelItem = ({ route, navigation }) => {
/>
<FlatList
data={chapters}
extraData={chapters}
showsVerticalScrollIndicator={false}
keyExtractor={(item) => item.chapterUrl}
removeClippedSubviews={true}
maxToRenderPerBatch={10}
initialNumToRender={10}
windowSize={15}
getItemLayout={getItemLayout}
initialNumToRender={5}
renderItem={renderChapterCard}
ListHeaderComponent={() => (
<NovelInfoHeader
@@ -356,7 +351,7 @@ const NovelItem = ({ route, navigation }) => {
/>
}
/>
{!loading && readingStatus && (
{readingStatus && (
<FAB
style={styles.fab}
icon="play"
@@ -386,44 +381,6 @@ const styles = StyleSheet.create({
// backgroundColor: "#202125",
backgroundColor: "#000000",
},
nameContainer: {
flex: 1,
width: "100%",
marginHorizontal: 15,
// justifyContent: "center",
},
background: {
height: 240,
},
linearGradient: {
height: "100%",
backgroundColor: "rgba(256, 256, 256, 0.5)",
},
detailsContainer: {
flex: 1,
flexDirection: "row",
margin: 15,
},
logo: {
height: 180,
width: 120,
margin: 3.2,
borderRadius: 6,
},
genre: {
borderRadius: 24,
borderWidth: 1,
paddingHorizontal: 10,
marginHorizontal: 2,
fontSize: 13,
paddingVertical: 2,
justifyContent: "center",
flex: 1,
},
name: {
fontWeight: "bold",
fontSize: 20,
},
fab: {
backgroundColor: theme.colorAccentDark,
position: "absolute",
+4 -1
View File
@@ -123,7 +123,10 @@ const AllNovels = ({ navigation }) => {
<NovelCover
item={item}
onPress={() =>
navigation.navigate("NovelItem", item)
navigation.navigate("NovelItem", {
...item,
navigatingFrom: 0,
})
}
/>
)}
@@ -124,7 +124,10 @@ const AllNovels = ({ navigation }) => {
<NovelCover
item={item}
onPress={() =>
navigation.navigate("NovelItem", item)
navigation.navigate("NovelItem", {
...item,
navigatingFrom: 0,
})
}
/>
)}