fix: reset max page count for new documents in DocumentStateNotifier

This commit is contained in:
insleker 2025-12-15 14:19:05 +08:00
parent 1394341935
commit 5fe31a6947
3 changed files with 20 additions and 13 deletions

View File

@ -313,11 +313,16 @@ void main() {
expect(pagesSidebar, findsOneWidget);
// Scroll to make page 3 thumbnail visible
final page3Thumb = find.text('3');
await tester.ensureVisible(page3Thumb);
final page3Thumb = find.descendant(
of: pagesSidebar,
matching: find.text('3'),
);
// Scroll more aggressively to ensure page 3 is visible
await tester.drag(pagesSidebar, const Offset(0, -500));
await tester.pumpAndSettle();
expect(page3Thumb, findsOneWidget);
await tester.tap(page3Thumb);
await tester.tap(page3Thumb, warnIfMissed: false);
await tester.pumpAndSettle();
expect(container.read(pdfViewModelProvider).currentPage, 3);
});

View File

@ -81,21 +81,22 @@ void main() {
final loadedStart = DateTime.now();
while (container.read(documentRepositoryProvider).pageCount == 0) {
await tester.pump(const Duration(milliseconds: 40));
if (DateTime.now().difference(loadedStart) > const Duration(seconds: 8)) {
if (DateTime.now().difference(loadedStart) >
const Duration(seconds: 10)) {
fail('Document never loaded (pageCount still 0)');
}
}
// Wait a bit more for controller to become ready after document loads
await tester.pump(const Duration(milliseconds: 200));
await tester.pump(const Duration(milliseconds: 300));
await tester.pumpAndSettle();
final controller = container.read(pdfViewModelProvider).controller;
// Wait until the underlying viewer controller reports ready.
final readyStart = DateTime.now();
while (!controller.isReady) {
await tester.pump(const Duration(milliseconds: 40));
if (DateTime.now().difference(readyStart) > const Duration(seconds: 8)) {
while (controller != null && !controller.isReady) {
await tester.pump(const Duration(milliseconds: 50));
if (DateTime.now().difference(readyStart) > const Duration(seconds: 15)) {
fail('PdfViewerController never became ready');
}
}

View File

@ -49,8 +49,9 @@ class DocumentStateNotifier extends Notifier<Document> {
);
if (bytes == null) {
// No bytes: treat as synthetic document (tests) using provided pageCount or default 1
// Reset max page count for new document
final pc = pageCount ?? 1;
_maxPageCount = math.max(_maxPageCount, pc).toInt();
_maxPageCount = pc;
state = state.copyWith(
loaded: true,
pageCount: _maxPageCount,
@ -59,11 +60,11 @@ class DocumentStateNotifier extends Notifier<Document> {
);
return;
}
// Bytes provided
// Bytes provided - reset max page count for new document
if ((knownPageCount || pageCount != null) && pageCount != null) {
// Fast path: caller already determined count
final pc = pageCount.clamp(1, 9999) as int;
_maxPageCount = math.max(_maxPageCount, pc).toInt();
final pc = pageCount.clamp(1, 9999);
_maxPageCount = pc;
state = state.copyWith(
loaded: true,
pageCount: _maxPageCount,
@ -125,7 +126,7 @@ class DocumentStateNotifier extends Notifier<Document> {
debugPrint(
'[DocumentRepository] setPageCount called: $count (current: ${state.pageCount})',
);
final clamped = count.clamp(1, 9999) as int;
final clamped = count.clamp(1, 9999);
_maxPageCount = math.max(_maxPageCount, clamped).toInt();
state = state.copyWith(pageCount: _maxPageCount);
}