Fix page calculation logic && trigger recalculation on orientation change (#428)

This commit is contained in:
角砂糖
2025-06-26 19:55:21 +08:00
committed by GitHub
parent 5ebb554e54
commit b9817ec030

View File

@@ -177,10 +177,18 @@ class _ReaderState extends State<Reader>
super.initState(); super.initState();
} }
bool _isInitialized = false;
@override @override
void didChangeDependencies() { void didChangeDependencies() {
super.didChangeDependencies(); super.didChangeDependencies();
if (!_isInitialized) {
initImagesPerPage(widget.initialPage ?? 1); initImagesPerPage(widget.initialPage ?? 1);
_isInitialized = true;
} else {
// For orientation changed
_checkImagesPerPageChange();
}
initReaderWindow(); initReaderWindow();
} }
@@ -345,6 +353,8 @@ class _ReaderState extends State<Reader>
abstract mixin class _ImagePerPageHandler { abstract mixin class _ImagePerPageHandler {
late int _lastImagesPerPage; late int _lastImagesPerPage;
late bool _lastOrientation;
bool get isPortrait; bool get isPortrait;
int get page; int get page;
@@ -355,6 +365,7 @@ abstract mixin class _ImagePerPageHandler {
void initImagesPerPage(int initialPage) { void initImagesPerPage(int initialPage) {
_lastImagesPerPage = imagesPerPage; _lastImagesPerPage = imagesPerPage;
_lastOrientation = isPortrait;
if (imagesPerPage != 1) { if (imagesPerPage != 1) {
if (showSingleImageOnFirstPage) { if (showSingleImageOnFirstPage) {
page = ((initialPage - 1) / imagesPerPage).ceil() + 1; page = ((initialPage - 1) / imagesPerPage).ceil() + 1;
@@ -380,19 +391,42 @@ abstract mixin class _ImagePerPageHandler {
/// Check if the number of images per page has changed /// Check if the number of images per page has changed
void _checkImagesPerPageChange() { void _checkImagesPerPageChange() {
int currentImagesPerPage = imagesPerPage; int currentImagesPerPage = imagesPerPage;
if (_lastImagesPerPage != currentImagesPerPage) { bool currentOrientation = isPortrait;
if (_lastImagesPerPage != currentImagesPerPage || _lastOrientation != currentOrientation) {
_adjustPageForImagesPerPageChange( _adjustPageForImagesPerPageChange(
_lastImagesPerPage, currentImagesPerPage); _lastImagesPerPage, currentImagesPerPage);
_lastImagesPerPage = currentImagesPerPage; _lastImagesPerPage = currentImagesPerPage;
_lastOrientation = currentOrientation;
} }
} }
/// Adjust the page number when the number of images per page changes /// Adjust the page number when the number of images per page changes
void _adjustPageForImagesPerPageChange( void _adjustPageForImagesPerPageChange(
int oldImagesPerPage, int newImagesPerPage) { int oldImagesPerPage, int newImagesPerPage) {
int previousImageIndex = (page - 1) * oldImagesPerPage; int previousImageIndex = 1;
int newPage = (previousImageIndex ~/ newImagesPerPage) + 1; if (!showSingleImageOnFirstPage || oldImagesPerPage == 1) {
page = newPage; previousImageIndex = (page - 1) * oldImagesPerPage + 1;
} else {
if (page == 1) {
previousImageIndex = 1;
} else {
previousImageIndex = (page - 2) * oldImagesPerPage + 2;
}
}
int newPage;
if (newImagesPerPage != 1) {
if (showSingleImageOnFirstPage) {
newPage = ((previousImageIndex - 1) / newImagesPerPage).ceil() + 1;
} else {
newPage = (previousImageIndex / newImagesPerPage).ceil();
}
} else {
newPage = previousImageIndex;
}
page = newPage>0 ? newPage : 1;
} }
} }