chapter prev/next UI+Backend

This commit is contained in:
Aria Moradi 2021-03-23 03:50:55 +04:30
parent f41c5c9428
commit bf908c4d17
10 changed files with 261 additions and 57 deletions

View File

@ -172,10 +172,10 @@ class Main {
ctx.json(getChapterList(mangaId)) ctx.json(getChapterList(mangaId))
} }
app.get("/api/v1/manga/:mangaId/chapter/:chapterId") { ctx -> app.get("/api/v1/manga/:mangaId/chapter/:chapterIndex") { ctx ->
val chapterId = ctx.pathParam("chapterId").toInt() val chapterIndex = ctx.pathParam("chapterIndex").toInt()
val mangaId = ctx.pathParam("mangaId").toInt() val mangaId = ctx.pathParam("mangaId").toInt()
ctx.json(getChapter(chapterId, mangaId)) ctx.json(getChapter(chapterIndex, mangaId))
} }
app.get("/api/v1/manga/:mangaId/chapter/:chapterId/page/:index") { ctx -> app.get("/api/v1/manga/:mangaId/chapter/:chapterId/page/:index") { ctx ->

View File

@ -12,5 +12,7 @@ data class ChapterDataClass(
val chapter_number: Float, val chapter_number: Float,
val scanlator: String?, val scanlator: String?,
val mangaId: Int, val mangaId: Int,
val chapterIndex: Int,
val chapterCount: Int,
val pageCount: Int? = null, val pageCount: Int? = null,
) )

View File

@ -13,5 +13,7 @@ object ChapterTable : IntIdTable() {
val chapter_number = float("chapter_number").default(-1f) val chapter_number = float("chapter_number").default(-1f)
val scanlator = varchar("scanlator", 128).nullable() val scanlator = varchar("scanlator", 128).nullable()
val chapterIndex = integer("number_in_list")
val manga = reference("manga", MangaTable) val manga = reference("manga", MangaTable)
} }

View File

@ -14,7 +14,9 @@ import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.insert import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.insertAndGetId import org.jetbrains.exposed.sql.insertAndGetId
import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update
fun getChapterList(mangaId: Int): List<ChapterDataClass> { fun getChapterList(mangaId: Int): List<ChapterDataClass> {
val mangaDetails = getManga(mangaId) val mangaDetails = getManga(mangaId)
@ -27,8 +29,10 @@ fun getChapterList(mangaId: Int): List<ChapterDataClass> {
} }
).toBlocking().first() ).toBlocking().first()
val chapterCount = chapterList.count()
return transaction { return transaction {
chapterList.forEach { fetchedChapter -> chapterList.reversed().forEachIndexed { index, fetchedChapter ->
val chapterEntry = ChapterTable.select { ChapterTable.url eq fetchedChapter.url }.firstOrNull() val chapterEntry = ChapterTable.select { ChapterTable.url eq fetchedChapter.url }.firstOrNull()
if (chapterEntry == null) { if (chapterEntry == null) {
ChapterTable.insertAndGetId { ChapterTable.insertAndGetId {
@ -38,12 +42,29 @@ fun getChapterList(mangaId: Int): List<ChapterDataClass> {
it[chapter_number] = fetchedChapter.chapter_number it[chapter_number] = fetchedChapter.chapter_number
it[scanlator] = fetchedChapter.scanlator it[scanlator] = fetchedChapter.scanlator
it[chapterIndex] = index + 1
it[manga] = mangaId
}
} else {
ChapterTable.update({ ChapterTable.url eq fetchedChapter.url }) {
it[name] = fetchedChapter.name
it[date_upload] = fetchedChapter.date_upload
it[chapter_number] = fetchedChapter.chapter_number
it[scanlator] = fetchedChapter.scanlator
it[chapterIndex] = index + 1
it[manga] = mangaId it[manga] = mangaId
} }
} }
} }
return@transaction chapterList.map { // clear any orphaned chapters
val dbChapterCount = transaction { ChapterTable.selectAll().count() }
if (dbChapterCount > chapterCount) { // we got some clean up due
// TODO
}
return@transaction chapterList.mapIndexed { index, it ->
ChapterDataClass( ChapterDataClass(
ChapterTable.select { ChapterTable.url eq it.url }.firstOrNull()!![ChapterTable.id].value, ChapterTable.select { ChapterTable.url eq it.url }.firstOrNull()!![ChapterTable.id].value,
it.url, it.url,
@ -51,16 +72,19 @@ fun getChapterList(mangaId: Int): List<ChapterDataClass> {
it.date_upload, it.date_upload,
it.chapter_number, it.chapter_number,
it.scanlator, it.scanlator,
mangaId mangaId,
chapterCount - index,
chapterCount
) )
} }
} }
} }
fun getChapter(chapterId: Int, mangaId: Int): ChapterDataClass { fun getChapter(chapterIndex: Int, mangaId: Int): ChapterDataClass {
return transaction { return transaction {
val chapterEntry = ChapterTable.select { ChapterTable.id eq chapterId }.firstOrNull()!! val chapterEntry = ChapterTable.select {
assert(mangaId == chapterEntry[ChapterTable.manga].value) // sanity check ChapterTable.chapterIndex eq chapterIndex and (ChapterTable.manga eq mangaId)
}.firstOrNull()!!
val mangaEntry = MangaTable.select { MangaTable.id eq mangaId }.firstOrNull()!! val mangaEntry = MangaTable.select { MangaTable.id eq mangaId }.firstOrNull()!!
val source = getHttpSource(mangaEntry[MangaTable.sourceReference]) val source = getHttpSource(mangaEntry[MangaTable.sourceReference])
@ -71,14 +95,20 @@ fun getChapter(chapterId: Int, mangaId: Int): ChapterDataClass {
} }
).toBlocking().first() ).toBlocking().first()
val chapterId = chapterEntry[ChapterTable.id].value
val chapterCount = transaction { ChapterTable.selectAll().count() }
val chapter = ChapterDataClass( val chapter = ChapterDataClass(
chapterEntry[ChapterTable.id].value, chapterId,
chapterEntry[ChapterTable.url], chapterEntry[ChapterTable.url],
chapterEntry[ChapterTable.name], chapterEntry[ChapterTable.name],
chapterEntry[ChapterTable.date_upload], chapterEntry[ChapterTable.date_upload],
chapterEntry[ChapterTable.chapter_number], chapterEntry[ChapterTable.chapter_number],
chapterEntry[ChapterTable.scanlator], chapterEntry[ChapterTable.scanlator],
mangaId, mangaId,
chapterEntry[ChapterTable.chapterIndex],
chapterCount.toInt(),
pageList.count() pageList.count()
) )

View File

@ -88,7 +88,7 @@ export default function App() {
<Route path="/sources"> <Route path="/sources">
<Sources /> <Sources />
</Route> </Route>
<Route path="/manga/:mangaId/chapter/:chapterId"> <Route path="/manga/:mangaId/chapter/:chapterNum">
<></> <></>
</Route> </Route>
<Route path="/manga/:id"> <Route path="/manga/:id">
@ -115,7 +115,7 @@ export default function App() {
</Switch> </Switch>
</Container> </Container>
<Switch> <Switch>
<Route path="/manga/:mangaId/chapter/:chapterId"> <Route path="/manga/:mangaId/chapter/:chapterIndex">
<Reader /> <Reader />
</Route> </Route>
</Switch> </Switch>

View File

@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* This Source Code Form is subject to the terms of the Mozilla Public /* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
@ -8,7 +9,7 @@ import Card from '@material-ui/core/Card';
import CardContent from '@material-ui/core/CardContent'; import CardContent from '@material-ui/core/CardContent';
import Button from '@material-ui/core/Button'; import Button from '@material-ui/core/Button';
import Typography from '@material-ui/core/Typography'; import Typography from '@material-ui/core/Typography';
import { useHistory } from 'react-router-dom'; import { Link, useHistory } from 'react-router-dom';
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
root: { root: {
@ -65,9 +66,19 @@ export default function ChapterCard(props: IProps) {
</Typography> </Typography>
</div> </div>
</div> </div>
<div style={{ display: 'flex' }}> <Link
<Button variant="outlined" style={{ marginLeft: 20 }} onClick={() => { history.push(`/manga/${chapter.mangaId}/chapter/${chapter.id}`); }}>open</Button> to={`/manga/${chapter.mangaId}/chapter/${chapter.chapterIndex}`}
</div> style={{ textDecoration: 'none' }}
>
<Button
variant="outlined"
style={{ marginLeft: 20 }}
>
open
</Button>
</Link>
</CardContent> </CardContent>
</Card> </Card>
</li> </li>

View File

@ -8,16 +8,17 @@ import CircularProgress from '@material-ui/core/CircularProgress';
import { makeStyles } from '@material-ui/core/styles'; import { makeStyles } from '@material-ui/core/styles';
import React, { useEffect, useRef, useState } from 'react'; import React, { useEffect, useRef, useState } from 'react';
import LazyLoad from 'react-lazyload'; import LazyLoad from 'react-lazyload';
import { IReaderSettings } from './ReaderNavBar';
const useStyles = makeStyles({ const useStyles = (settings: IReaderSettings) => makeStyles({
loading: { loading: {
margin: '100px auto', margin: '100px auto',
height: '100vh', height: '100vh',
}, },
loadingImage: { loadingImage: {
padding: 'calc(50vh - 40px) calc(50vw - 40px)', padding: settings.staticNav ? 'calc(50vh - 40px) calc(50vw - 340px)' : 'calc(50vh - 40px) calc(50vw - 40px)',
height: '100vh', height: '100vh',
width: '100vw', width: '200px',
backgroundColor: '#525252', backgroundColor: '#525252',
marginBottom: 10, marginBottom: 10,
}, },
@ -27,11 +28,15 @@ interface IProps {
src: string src: string
index: number index: number
setCurPage: React.Dispatch<React.SetStateAction<number>> setCurPage: React.Dispatch<React.SetStateAction<number>>
settings: IReaderSettings
} }
function LazyImage(props: IProps) { function LazyImage(props: IProps) {
const classes = useStyles(); const {
const { src, index, setCurPage } = props; src, index, setCurPage, settings,
} = props;
const classes = useStyles(settings)();
const [imageSrc, setImagsrc] = useState<string>(''); const [imageSrc, setImagsrc] = useState<string>('');
const ref = useRef<HTMLImageElement>(null); const ref = useRef<HTMLImageElement>(null);
@ -57,7 +62,7 @@ function LazyImage(props: IProps) {
img.src = src; img.src = src;
img.onload = () => setImagsrc(src); img.onload = () => setImagsrc(src);
}, []); }, [src]);
if (imageSrc.length === 0) { if (imageSrc.length === 0) {
return ( return (
@ -72,27 +77,33 @@ function LazyImage(props: IProps) {
ref={ref} ref={ref}
src={imageSrc} src={imageSrc}
alt={`Page #${index}`} alt={`Page #${index}`}
style={{ maxWidth: '100%' }} style={{ width: '100%' }}
/> />
); );
} }
export default function Page(props: IProps) { export default function Page(props: IProps) {
const { src, index, setCurPage } = props; const {
const classes = useStyles(); src, index, setCurPage, settings,
} = props;
const classes = useStyles(settings)();
return ( return (
<div style={{ margin: '0 auto' }}> <div style={{ margin: '0 auto' }}>
<LazyLoad <LazyLoad
offset={window.innerHeight} offset={window.innerHeight}
once
placeholder={( placeholder={(
<div className={classes.loading}> <div className={classes.loading}>
<CircularProgress thickness={5} /> <CircularProgress thickness={5} />
</div> </div>
)} )}
> >
<LazyImage src={src} index={index} setCurPage={setCurPage} /> <LazyImage
src={src}
index={index}
setCurPage={setCurPage}
settings={settings}
/>
</LazyLoad> </LazyLoad>
</div> </div>
); );

View File

@ -7,16 +7,25 @@ import IconButton from '@material-ui/core/IconButton';
import CloseIcon from '@material-ui/icons/Close'; import CloseIcon from '@material-ui/icons/Close';
import KeyboardArrowLeftIcon from '@material-ui/icons/KeyboardArrowLeft'; import KeyboardArrowLeftIcon from '@material-ui/icons/KeyboardArrowLeft';
import KeyboardArrowRightIcon from '@material-ui/icons/KeyboardArrowRight'; import KeyboardArrowRightIcon from '@material-ui/icons/KeyboardArrowRight';
import KeyboardArrowDownIcon from '@material-ui/icons/KeyboardArrowDown';
import KeyboardArrowUpIcon from '@material-ui/icons/KeyboardArrowUp';
import { makeStyles, Theme, useTheme } from '@material-ui/core/styles'; import { makeStyles, Theme, useTheme } from '@material-ui/core/styles';
import React, { useContext, useEffect, useState } from 'react'; import React, { useContext, useEffect, useState } from 'react';
import Typography from '@material-ui/core/Typography'; import Typography from '@material-ui/core/Typography';
import { useHistory } from 'react-router-dom'; import { useHistory, Link } from 'react-router-dom';
import Slide from '@material-ui/core/Slide'; import Slide from '@material-ui/core/Slide';
import Fade from '@material-ui/core/Fade'; import Fade from '@material-ui/core/Fade';
import Zoom from '@material-ui/core/Zoom'; import Zoom from '@material-ui/core/Zoom';
import { Switch } from '@material-ui/core'; import { Switch } from '@material-ui/core';
import NavBarContext from '../context/NavbarContext'; import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemIcon from '@material-ui/core/ListItemIcon';
import ListItemText from '@material-ui/core/ListItemText';
import ListItemSecondaryAction from '@material-ui/core/ListItemSecondaryAction';
import Collapse from '@material-ui/core/Collapse';
import Button from '@material-ui/core/Button';
import DarkTheme from '../context/DarkTheme'; import DarkTheme from '../context/DarkTheme';
import NavBarContext from '../context/NavbarContext';
const useStyles = (settings: IReaderSettings) => makeStyles((theme: Theme) => ({ const useStyles = (settings: IReaderSettings) => makeStyles((theme: Theme) => ({
// main container and root div need to change classes... // main container and root div need to change classes...
@ -64,6 +73,49 @@ const useStyles = (settings: IReaderSettings) => makeStyles((theme: Theme) => ({
flexGrow: 1, flexGrow: 1,
}, },
}, },
'& hr': {
margin: '0 16px',
height: '1px',
border: '0',
backgroundColor: 'rgb(38, 41, 43)',
},
},
navigation: {
margin: '0 16px',
'& > span:nth-child(1)': {
textAlign: 'center',
display: 'block',
marginTop: '16px',
},
'& $navigationChapters': {
display: 'grid',
gridTemplateColumns: '1fr 1fr',
gridTemplateAreas: '"prev next"',
gridColumnGap: '5px',
margin: '10px 0',
'& a': {
flexGrow: 1,
textDecoration: 'none',
'& button': {
width: '100%',
padding: '5px 8px',
textTransform: 'none',
},
},
},
},
navigationChapters: {}, // dummy rule
settingsCollapsseHeader: {
'& span': {
fontWeight: 'bold',
},
}, },
openDrawerButton: { openDrawerButton: {
@ -94,7 +146,9 @@ export const defaultReaderSettings = () => ({
interface IProps { interface IProps {
settings: IReaderSettings settings: IReaderSettings
setSettings: React.Dispatch<React.SetStateAction<IReaderSettings>> setSettings: React.Dispatch<React.SetStateAction<IReaderSettings>>
manga: IMangaCard | IManga manga: IManga | IMangaCard
chapter: IChapter | IPartialChpter
curPage: number
} }
export default function ReaderNavBar(props: IProps) { export default function ReaderNavBar(props: IProps) {
@ -103,11 +157,14 @@ export default function ReaderNavBar(props: IProps) {
const history = useHistory(); const history = useHistory();
const { settings, setSettings, manga } = props; const {
settings, setSettings, manga, chapter, curPage,
} = props;
const [drawerOpen, setDrawerOpen] = useState(false || settings.staticNav); const [drawerOpen, setDrawerOpen] = useState(false || settings.staticNav);
const [hideOpenButton, setHideOpenButton] = useState(false); const [hideOpenButton, setHideOpenButton] = useState(false);
const [prevScrollPos, setPrevScrollPos] = useState(0); const [prevScrollPos, setPrevScrollPos] = useState(0);
const [settingsCollapseOpen, setSettingsCollapseOpen] = useState(false);
const theme = useTheme(); const theme = useTheme();
const classes = useStyles(settings)(); const classes = useStyles(settings)();
@ -176,16 +233,92 @@ export default function ReaderNavBar(props: IProps) {
</IconButton> </IconButton>
) } ) }
</header> </header>
<h3>Static Navigation</h3> <ListItem ContainerComponent="div" className={classes.settingsCollapsseHeader}>
<Switch <ListItemText primary="Reader Settings" />
checked={settings.staticNav} <ListItemSecondaryAction>
onChange={(e) => setSettingValue('staticNav', e.target.checked)} <IconButton
/> edge="start"
<h3>Show page number</h3> color="inherit"
<Switch aria-label="menu"
checked={settings.showPageNumber} disableRipple
onChange={(e) => setSettingValue('showPageNumber', e.target.checked)} disableFocusRipple
/> onClick={() => setSettingsCollapseOpen(!settingsCollapseOpen)}
>
{settingsCollapseOpen && <KeyboardArrowUpIcon />}
{!settingsCollapseOpen && <KeyboardArrowDownIcon />}
</IconButton>
</ListItemSecondaryAction>
</ListItem>
<Collapse in={settingsCollapseOpen} timeout="auto" unmountOnExit>
<List>
<ListItem>
<ListItemText primary="Static Navigation" />
<ListItemSecondaryAction>
<Switch
edge="end"
checked={settings.staticNav}
onChange={(e) => setSettingValue('staticNav', e.target.checked)}
/>
</ListItemSecondaryAction>
</ListItem>
<ListItem>
<ListItemText primary="Show page number" />
<ListItemSecondaryAction>
<Switch
edge="end"
checked={settings.showPageNumber}
onChange={(e) => setSettingValue('showPageNumber', e.target.checked)}
/>
</ListItemSecondaryAction>
</ListItem>
</List>
</Collapse>
<hr />
<div className={classes.navigation}>
<span>
Currently on page
{' '}
{curPage + 1}
{' '}
of
{' '}
{chapter.pageCount}
</span>
<div className={classes.navigationChapters}>
{chapter.chapterIndex > 1
&& (
<Link
style={{ gridArea: 'prev' }}
to={`/manga/${manga.id}/chapter/${chapter.chapterIndex - 1}`}
>
<Button
variant="outlined"
startIcon={<KeyboardArrowLeftIcon />}
>
Chapter
{' '}
{chapter.chapterIndex - 1}
</Button>
</Link>
)}
{chapter.chapterIndex < chapter.chapterCount
&& (
<Link
style={{ gridArea: 'next' }}
to={`/manga/${manga.id}/chapter/${chapter.chapterIndex + 1}`}
>
<Button
variant="outlined"
endIcon={<KeyboardArrowRightIcon />}
>
Chapter
{' '}
{chapter.chapterIndex + 1}
</Button>
</Link>
)}
</div>
</div>
</div> </div>
</Slide> </Slide>
<Zoom in={!drawerOpen}> <Zoom in={!drawerOpen}>

View File

@ -38,6 +38,7 @@ const useStyles = (settings: IReaderSettings) => makeStyles({
}); });
const range = (n:number) => Array.from({ length: n }, (value, key) => key); const range = (n:number) => Array.from({ length: n }, (value, key) => key);
const initialChapter = () => ({ pageCount: -1, chapterIndex: -1, chapterCount: 0 });
export default function Reader() { export default function Reader() {
const [settings, setSettings] = useLocalStorage<IReaderSettings>('readerSettings', defaultReaderSettings); const [settings, setSettings] = useLocalStorage<IReaderSettings>('readerSettings', defaultReaderSettings);
@ -46,9 +47,9 @@ export default function Reader() {
const [serverAddress] = useLocalStorage<String>('serverBaseURL', ''); const [serverAddress] = useLocalStorage<String>('serverBaseURL', '');
const { chapterId, mangaId } = useParams<{chapterId: string, mangaId: string}>(); const { chapterIndex, mangaId } = useParams<{chapterIndex: string, mangaId: string}>();
const [manga, setManga] = useState<IMangaCard | IManga>({ id: +mangaId, title: '', thumbnailUrl: '' }); const [manga, setManga] = useState<IMangaCard | IManga>({ id: +mangaId, title: '', thumbnailUrl: '' });
const [pageCount, setPageCount] = useState<number>(-1); const [chapter, setChapter] = useState<IChapter | IPartialChpter>(initialChapter());
const [curPage, setCurPage] = useState<number>(0); const [curPage, setCurPage] = useState<number>(0);
const { setOverride, setTitle } = useContext(NavbarContext); const { setOverride, setTitle } = useContext(NavbarContext);
@ -56,17 +57,21 @@ export default function Reader() {
setOverride( setOverride(
{ {
status: true, status: true,
value: <ReaderNavBar value: (
settings={settings} <ReaderNavBar
setSettings={setSettings} settings={settings}
manga={manga} setSettings={setSettings}
/>, manga={manga}
chapter={chapter}
curPage={curPage}
/>
),
}, },
); );
// clean up for when we leave the reader // clean up for when we leave the reader
return () => setOverride({ status: false, value: <div /> }); return () => setOverride({ status: false, value: <div /> });
}, [manga, settings]); }, [manga, chapter, settings, curPage, chapterIndex]);
useEffect(() => { useEffect(() => {
setTitle('Reader'); setTitle('Reader');
@ -76,17 +81,18 @@ export default function Reader() {
setManga(data); setManga(data);
setTitle(data.title); setTitle(data.title);
}); });
}, []); }, [chapterIndex]);
useEffect(() => { useEffect(() => {
client.get(`/api/v1/manga/${mangaId}/chapter/${chapterId}`) setChapter(initialChapter);
client.get(`/api/v1/manga/${mangaId}/chapter/${chapterIndex}`)
.then((response) => response.data) .then((response) => response.data)
.then((data:IChapter) => { .then((data:IChapter) => {
setPageCount(data.pageCount); setChapter(data);
}); });
}, []); }, [chapterIndex]);
if (pageCount === -1) { if (chapter.pageCount === -1) {
return ( return (
<div className={classes.loading}> <div className={classes.loading}>
<CircularProgress thickness={5} /> <CircularProgress thickness={5} />
@ -96,14 +102,15 @@ export default function Reader() {
return ( return (
<div className={classes.reader}> <div className={classes.reader}>
<div className={classes.pageNumber}> <div className={classes.pageNumber}>
{`${curPage + 1} / ${pageCount}`} {`${curPage + 1} / ${chapter.pageCount}`}
</div> </div>
{range(pageCount).map((index) => ( {range(chapter.pageCount).map((index) => (
<Page <Page
key={index} key={index}
index={index} index={index}
src={`${serverAddress}/api/v1/manga/${mangaId}/chapter/${chapterId}/page/${index}`} src={`${serverAddress}/api/v1/manga/${mangaId}/chapter/${chapterIndex}/page/${index}`}
setCurPage={setCurPage} setCurPage={setCurPage}
settings={settings}
/> />
))} ))}
</div> </div>

View File

@ -53,9 +53,17 @@ interface IChapter {
chapter_number: number chapter_number: number
scanlator: String scanlator: String
mangaId: number mangaId: number
chapterIndex: number
chapterCount: number
pageCount: number pageCount: number
} }
interface IPartialChpter {
pageCount: number
chapterIndex: number
chapterCount: number
}
interface ICategory { interface ICategory {
id: number id: number
order: number order: number