diff --git a/lib/pages/comic_page.dart b/lib/pages/comic_page.dart index 62f5bc0..f39abcd 100644 --- a/lib/pages/comic_page.dart +++ b/lib/pages/comic_page.dart @@ -49,19 +49,19 @@ class ComicPage extends StatefulWidget { class _ComicPageState extends LoadingState with _ComicPageActions { + @override + History? history; + bool showAppbarTitle = false; var scrollController = ScrollController(); bool isDownloaded = false; - void updateHistory() async { - var newHistory = await HistoryManager() - .find(widget.id, ComicType(widget.sourceKey.hashCode)); - if (newHistory?.ep != history?.ep || newHistory?.page != history?.page) { - history = newHistory; - update(); - } + @override + void onReadEnd() { + // The history is passed by reference, so it will be updated automatically. + update(); } @override @@ -77,14 +77,12 @@ class _ComicPageState extends LoadingState @override void initState() { scrollController.addListener(onScroll); - HistoryManager().addListener(updateHistory); super.initState(); } @override void dispose() { scrollController.removeListener(onScroll); - HistoryManager().removeListener(updateHistory); super.dispose(); } @@ -552,7 +550,7 @@ class _ComicPageState extends LoadingState if (comic.chapters == null) { return const SliverPadding(padding: EdgeInsets.zero); } - return const _ComicChapters(); + return _ComicChapters(history); } Widget buildThumbnails() { @@ -594,7 +592,7 @@ abstract mixin class _ComicPageActions { ComicSource get comicSource => ComicSource.find(comic.sourceKey)!; - History? history; + History? get history; bool isLiking = false; @@ -688,11 +686,13 @@ abstract mixin class _ComicPageActions { chapters: comic.chapters, initialChapter: ep, initialPage: page, - history: History.fromModel(model: comic, ep: 0, page: 0), + history: history ?? History.fromModel(model: comic, ep: 0, page: 0), author: comic.findAuthor() ?? '', tags: comic.plainTags, ), - ); + ).then((_) { + onReadEnd(); + }); } void continueRead() { @@ -701,6 +701,8 @@ abstract mixin class _ComicPageActions { read(ep, page); } + void onReadEnd(); + void download() async { if (LocalManager().isDownloading(comic.id, comic.comicType)) { App.rootContext.showMessage(message: "The comic is downloading".tl); @@ -1083,7 +1085,9 @@ class _ActionButton extends StatelessWidget { } class _ComicChapters extends StatefulWidget { - const _ComicChapters(); + const _ComicChapters(this.history); + + final History? history; @override State<_ComicChapters> createState() => _ComicChaptersState(); @@ -1096,12 +1100,28 @@ class _ComicChaptersState extends State<_ComicChapters> { bool showAll = false; + late History? history; + + @override + void initState() { + super.initState(); + history = widget.history; + } + @override void didChangeDependencies() { state = context.findAncestorStateOfType<_ComicPageState>()!; super.didChangeDependencies(); } + @override + void didUpdateWidget(covariant _ComicChapters oldWidget) { + super.didUpdateWidget(oldWidget); + setState(() { + history = widget.history; + }); + } + @override Widget build(BuildContext context) { final eps = state.comic.chapters!; @@ -1151,8 +1171,7 @@ class _ComicChaptersState extends State<_ComicChapters> { } var key = eps.keys.elementAt(i); var value = eps[key]!; - bool visited = - (state.history?.readEpisode ?? const {}).contains(i + 1); + bool visited = (history?.readEpisode ?? {}).contains(i + 1); return Padding( padding: const EdgeInsets.fromLTRB(6, 4, 6, 4), child: Material( diff --git a/lib/pages/reader/reader.dart b/lib/pages/reader/reader.dart index 34db62a..7d4a686 100644 --- a/lib/pages/reader/reader.dart +++ b/lib/pages/reader/reader.dart @@ -237,6 +237,7 @@ class _ReaderState extends State with _ReaderLocation, _ReaderWindow { history!.maxPage = maxPage; } history!.readEpisode.add(chapter); + print(history!.readEpisode); history!.time = DateTime.now(); HistoryManager().addHistory(history!); }