Skip to content

Commit

Permalink
Merge pull request #384 from project-violet/fix-two-page-viewer-2
Browse files Browse the repository at this point in the history
Fix two page viewer
  • Loading branch information
violet-dev authored Feb 3, 2024
2 parents 2418696 + ee290f6 commit 1ada1f1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
86 changes: 57 additions & 29 deletions lib/pages/viewer/horizontal_viewer_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,29 +146,38 @@ class _HorizontalViewerPageState extends State<HorizontalViewerPage> {

if (c.provider.useFileSystem) {
if (onTwoPageMode()) {
var firstIndex = index * 2;
var secondIndex = index * 2 + 1;

if (c.rightToLeft.value) {
firstIndex += 1;
secondIndex -= 1;
}

viewWidget = Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image(
image: FileImage(File(c.provider.uris[index * 2]))
..resolve(ImageConfiguration.empty)
.addListener(ImageStreamListener((imageInfo, _) {
final width = sizes[index ~/ 2].value.width +
imageInfo.image.width / imageInfo.image.height * height;
sizes[index].value = Size(
width,
height,
);
})),
),
if (c.maxPage > index * 2 + 1)
if (c.maxPage > firstIndex)
Image(
image: FileImage(File(c.provider.uris[firstIndex]))
..resolve(ImageConfiguration.empty)
.addListener(ImageStreamListener((imageInfo, _) {
final width = sizes[firstIndex].value.width +
imageInfo.image.width / imageInfo.image.height * height;
sizes[firstIndex].value = Size(
width,
height,
);
})),
),
if (c.maxPage > secondIndex)
Image(
image: FileImage(File(c.provider.uris[index * 2 + 1]))
image: FileImage(File(c.provider.uris[secondIndex]))
..resolve(ImageConfiguration.empty)
.addListener(ImageStreamListener((imageInfo, _) {
final width = sizes[index].value.width +
final width = sizes[secondIndex].value.width +
imageInfo.image.width / imageInfo.image.height * height;
sizes[index].value = Size(
sizes[secondIndex].value = Size(
width,
height,
);
Expand All @@ -188,28 +197,47 @@ class _HorizontalViewerPageState extends State<HorizontalViewerPage> {
}
} else if (c.provider.useProvider) {
viewWidget = FutureBuilder(
future: c.load(index),
future: onTwoPageMode()
? Future.wait([c.load(index * 2), c.load(index * 2 + 1)])
: c.load(index),
builder: (context, snapshot) {
if (c.urlCache[index] != null && c.headerCache[index] != null) {
final checkLoad = onTwoPageMode()
? c.urlCache[index * 2] != null &&
c.headerCache[index * 2] != null &&
(c.maxPage <= index * 2 + 1 ||
(c.urlCache[index * 2 + 1] != null &&
c.headerCache[index * 2 + 1] != null))
: c.urlCache[index] != null && c.headerCache[index] != null;

if (checkLoad) {
if (onTwoPageMode()) {
var firstIndex = index * 2;
var secondIndex = index * 2 + 1;

if (c.rightToLeft.value) {
firstIndex += 1;
secondIndex -= 1;
}

return Obx(
() => Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image(
image: ExtendedNetworkImageProvider(
c.urlCache[index * 2]!.value,
headers: c.headerCache[index * 2],
cache: true,
retries: 10,
timeRetry: const Duration(milliseconds: 300),
if (c.maxPage > firstIndex)
Image(
image: ExtendedNetworkImageProvider(
c.urlCache[firstIndex]!.value,
headers: c.headerCache[firstIndex],
cache: true,
retries: 10,
timeRetry: const Duration(milliseconds: 300),
),
),
),
if (c.maxPage > index * 2 + 1)
if (c.maxPage > secondIndex)
Image(
image: ExtendedNetworkImageProvider(
c.urlCache[index * 2 + 1]!.value,
headers: c.headerCache[index * 2 + 1],
c.urlCache[secondIndex]!.value,
headers: c.headerCache[secondIndex],
cache: true,
retries: 10,
timeRetry: const Duration(milliseconds: 300),
Expand Down
3 changes: 2 additions & 1 deletion lib/pages/viewer/overlay/viewer_overlay.dart
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,8 @@ class _ViewerOverlayState extends State<ViewerOverlay> {
alignment: 0.12,
);
} else {
c.horizontalPageController.jumpToPage(value.toInt() - 1);
c.horizontalPageController.jumpToPage(
c.onTwoPage ? value.toInt() ~/ 2 : value.toInt() - 1);
}
}
},
Expand Down
6 changes: 5 additions & 1 deletion lib/pages/viewer/viewer_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,12 @@ class ViewerController extends GetxController {
prev() => move(page.value - (onTwoPage ? 2 : 1));
next() => move(page.value + (onTwoPage ? 2 : 1));

load(int index) async {
Future<void> load(int index) async {
if (provider.useProvider) {
if (index >= maxPage) {
return;
}

if (headerCache[index] == null) {
var header = await provider.provider!.getHeader(index);
headerCache[index] = header;
Expand Down

0 comments on commit 1ada1f1

Please sign in to comment.