diff --git a/src/redux/actions/novel.js b/src/redux/actions/novel.js index ca9dba36..649cc3c4 100644 --- a/src/redux/actions/novel.js +++ b/src/redux/actions/novel.js @@ -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); +}; diff --git a/src/redux/reducers/novel.js b/src/redux/reducers/novel.js index d8442999..94fcbaf9 100644 --- a/src/redux/reducers/novel.js +++ b/src/redux/reducers/novel.js @@ -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( diff --git a/src/screens/Novel/Novel.js b/src/screens/Novel/Novel.js index 85426a0b..30ee5d66 100644 --- a/src/screens/Novel/Novel.js +++ b/src/screens/Novel/Novel.js @@ -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} /> ); diff --git a/src/screens/Novel/components/ChapterCard.js b/src/screens/Novel/components/ChapterCard.js index 8c35e0c3..19a9465e 100644 --- a/src/screens/Novel/components/ChapterCard.js +++ b/src/screens/Novel/components/ChapterCard.js @@ -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 ( - {downloading.downloading && - downloading.chapterUrl === chapter.chapterUrl ? ( + {isDownloading() && !chapter.downloaded ? ( { 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: {