Move download chapter logic into redux

This commit is contained in:
Rajarshee Chatterjee
2021-03-22 20:52:39 +05:30
parent 65a3e8fd2d
commit 97f2f84fa5
4 changed files with 104 additions and 97 deletions
+34 -34
View File
@@ -130,43 +130,43 @@ export const updateChapterRead = (chapterUrl, novelUrl) => async (dispatch) => {
dispatch({ type: CHAPTER_READ, payload: { chapterUrl, novelUrl } });
};
// export const downloadChapter = (
// extensionId,
// novelUrl,
// chapterUrl,
// chapterName
// ) => async (dispatch) => {
// dispatch({
// type: CHAPTER_DOWNLOADING,
// payload: {
// extensionId,
// novelUrl,
// chapterUrl,
// },
// });
export const downloadChapter = (
extensionId,
novelUrl,
chapterUrl,
chapterName
) => async (dispatch) => {
dispatch({
type: CHAPTER_DOWNLOADING,
payload: {
extensionId,
novelUrl,
chapterUrl,
},
});
// await downloadChapterFromSource(extensionId, novelUrl, chapterUrl);
await downloadChapterFromSource(extensionId, novelUrl, chapterUrl);
// dispatch({
// type: CHAPTER_DOWNLOADED,
// payload: { extensionId, novelUrl, chapterUrl },
// });
dispatch({
type: CHAPTER_DOWNLOADED,
payload: { extensionId, novelUrl, chapterUrl },
});
// ToastAndroid.show(`Downloaded ${chapterName}`, ToastAndroid.SHORT);
// };
ToastAndroid.show(`Downloaded ${chapterName}`, ToastAndroid.SHORT);
};
// export const deleteChapter = (
// extensionId,
// novelUrl,
// chapterUrl,
// chapterName
// ) => (dispatch) => {
// deleteChapterFromDb(extensionId, novelUrl, chapterUrl);
export const deleteChapter = (
extensionId,
novelUrl,
chapterUrl,
chapterName
) => (dispatch) => {
deleteChapterFromDb(extensionId, novelUrl, chapterUrl);
// dispatch({
// type: CHAPTER_DELETED,
// payload: { extensionId, novelUrl, chapterUrl },
// });
dispatch({
type: CHAPTER_DELETED,
payload: { extensionId, novelUrl, chapterUrl },
});
// ToastAndroid.show(`Deleted ${chapterName}`, ToastAndroid.SHORT);
// };
ToastAndroid.show(`Deleted ${chapterName}`, ToastAndroid.SHORT);
};
+21 -22
View File
@@ -17,6 +17,7 @@ const initialState = {
novel: null,
chapters: [],
chapter: null,
downloading: [],
chapterLoading: true,
loading: true,
fetching: false,
@@ -70,28 +71,26 @@ const novelReducer = (state = initialState, action) => {
};
case GET_CHAPTER:
return { ...state, chapter: payload, chapterLoading: false };
// case CHAPTER_DOWNLOADING:
// return {
// ...state,
// chapters: [
// state.chapters.map((chap) =>
// chap.novelUrl === payload.novelUrl &&
// chap.chapterUrl === payload.novelUrl
// ? { ...chap, downloading: true, downloaded: false }
// : chap
// ),
// ],
// };
// case CHAPTER_DOWNLOADED:
// return {
// ...state,
// downloads: state.downloads.map((chap) =>
// chap.chapterUrl === payload.chapterUrl &&
// chap.novelUrl === payload.novelUrl
// ? { ...chap, downloading: false, downloaded: true }
// : chap
// ),
// };
case CHAPTER_DOWNLOADING:
return {
...state,
downloading: [...state.downloading, payload],
};
case CHAPTER_DOWNLOADED:
return {
...state,
downloading: state.downloading.filter(
(chapter) =>
chapter.chapterUrl === payload.chapterUrl &&
chapter.novelUrl === payload.novelUrl
),
chapters: state.chapters.map((chapter) =>
chapter.chapterUrl === payload.chapterUrl &&
chapter.novelUrl === payload.novelUrl
? { ...chapter, downloaded: 1 }
: chapter
),
};
// case CHAPTER_DELETED:
// return {
// downloads: state.downloads.filter(
+29 -29
View File
@@ -41,10 +41,10 @@ const Novel = ({
libraryStatus,
} = route.params;
const [downloading, setDownloading] = useState({
downloading: false,
chapterUrl: "",
});
// const [downloading, setDownloading] = useState({
// downloading: false,
// chapterUrl: "",
// });
let _panel = useRef(null); // Bottomsheet ref
@@ -54,29 +54,29 @@ const Novel = ({
* If downloaded then delete from db
*/
const downloadChapter = (
downloadStatus,
extensionId,
novelUrl,
chapterUrl,
chapterName
) => {
setDownloading({ downloading: true, chapterUrl: chapterUrl });
downloadOrDeleteChapter(
downloadStatus,
extensionId,
novelUrl,
chapterUrl
).then((res) => {
setDownloading({ downloading: false });
ToastAndroid.show(
!downloadStatus
? `Downloaded ${chapterName}`
: `Deleted ${chapterName}`,
ToastAndroid.SHORT
);
});
};
// const downloadChapter = (
// downloadStatus,
// extensionId,
// novelUrl,
// chapterUrl,
// chapterName
// ) => {
// setDownloading({ downloading: true, chapterUrl: chapterUrl });
// downloadOrDeleteChapter(
// downloadStatus,
// extensionId,
// novelUrl,
// chapterUrl
// ).then((res) => {
// setDownloading({ downloading: false });
// ToastAndroid.show(
// !downloadStatus
// ? `Downloaded ${chapterName}`
// : `Deleted ${chapterName}`,
// ToastAndroid.SHORT
// );
// });
// };
useEffect(() => {
getNovel(libraryStatus, extensionId, novelUrl);
@@ -87,8 +87,8 @@ const Novel = ({
novelUrl={novelUrl}
extensionId={extensionId}
chapter={item}
downloadChapter={downloadChapter}
downloading={downloading}
// downloadChapter={downloadChapter}
// downloading={downloading}
/>
);
+20 -12
View File
@@ -4,20 +4,26 @@ import { TouchableRipple, IconButton } from "react-native-paper";
import { useNavigation } from "@react-navigation/native";
import { useSelector } from "react-redux";
import { downloadChapter } from "../../../redux/actions/novel";
import { connect } from "react-redux";
const ChapterCard = ({
novelUrl,
chapter,
downloadChapter,
extensionId,
theme,
downloading,
downloadChapter,
}) => {
const [downloaded, setDownloaded] = useState(chapter.downloaded);
const theme = useSelector((state) => state.themeReducer.theme);
const navigation = useNavigation();
const isDownloading = () =>
downloading.some(
(obj) =>
obj.chapterUrl === chapter.chapterUrl &&
obj.novelUrl === novelUrl
);
return (
<TouchableRipple
style={styles.chapterCardContainer}
@@ -65,8 +71,7 @@ const ChapterCard = ({
)}
</View>
<View>
{downloading.downloading &&
downloading.chapterUrl === chapter.chapterUrl ? (
{isDownloading() && !chapter.downloaded ? (
<ActivityIndicator
color={theme.textColorHintDark}
size={25}
@@ -75,26 +80,24 @@ const ChapterCard = ({
) : (
<IconButton
icon={
downloaded
chapter.downloaded
? "check-circle"
: "arrow-down-circle-outline"
}
animated
color={
downloaded
chapter.downloaded
? theme.textColorPrimary
: theme.textColorHintDark
}
size={25}
onPress={() => {
downloadChapter(
downloaded ? 1 : 0,
extensionId,
novelUrl,
chapter.chapterUrl,
chapter.chapterName
);
setDownloaded(!downloaded);
}}
style={{ margin: 2 }}
/>
@@ -105,7 +108,12 @@ const ChapterCard = ({
);
};
export default ChapterCard;
const mapStateToProps = (state) => ({
theme: state.themeReducer.theme,
downloading: state.novelReducer.downloading,
});
export default connect(mapStateToProps, { downloadChapter })(ChapterCard);
const styles = StyleSheet.create({
chapterCardContainer: {