Skip to content

Commit f3e00ea

Browse files
committed
fix(print): snapshot source geometry before pending book data
1 parent 5005410 commit f3e00ea

2 files changed

Lines changed: 176 additions & 7 deletions

File tree

‎lib/printing/view/printing_screen.dart‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -831,11 +831,14 @@ class _PrintingScreenState extends State<PrintingScreen> {
831831

832832
/// חתימת כל הפרמטרים שמשפיעים על תוכן ה-base PDF. מספר העמודים בגיליון
833833
/// נכלל רק דרך גודל עמוד הטקסט, וטווח עמודים ב-PDF חיצוני בכוונה אינו נכלל.
834-
String _baseCacheKey(PdfPageFormat format) {
834+
String _baseCacheKey(
835+
PdfPageFormat format, {
836+
PdfPageFormat? sourceFormat,
837+
}) {
835838
if (_isPdfSource) {
836839
return 'override|${identityHashCode(widget.createPdfOverride)}|${orientation.name}|${format.width}x${format.height}';
837840
}
838-
final source = _sourcePageFormat(format);
841+
final source = sourceFormat ?? _sourcePageFormat(format);
839842
final holyNames = _shouldReplaceHolyNames;
840843
return [
841844
identityHashCode(_dataFuture),
@@ -865,8 +868,10 @@ class _PrintingScreenState extends State<PrintingScreen> {
865868
Future<PrintPdfSource> _createBasePdf(PdfPageFormat format) async {
866869
final path = widget.pdfFilePath;
867870
if (path != null) return PrintPdfSource.file(path);
871+
// כיוון עמוד המקור חייב להתאים למשימה גם כשההגדרות משתנות בזמן הטעינה.
872+
final sourceFormat = _sourcePageFormat(format);
868873
await _divineNamesReady;
869-
final key = _baseCacheKey(format);
874+
final key = _baseCacheKey(format, sourceFormat: sourceFormat);
870875
final cached = _cachedBasePdf;
871876
if (cached != null && _cachedBaseKey == key) {
872877
return PrintPdfSource.data(cached);
@@ -879,7 +884,7 @@ class _PrintingScreenState extends State<PrintingScreen> {
879884
: format;
880885
bytes = await override(effectiveFormat);
881886
} else {
882-
bytes = await createPdf(format);
887+
bytes = await createPdf(format, sourceFormat: sourceFormat);
883888
}
884889
if (mounted && !_preview.isStopped && _baseCacheKey(format) == key) {
885890
_cachedBasePdf = bytes;
@@ -979,12 +984,15 @@ class _PrintingScreenState extends State<PrintingScreen> {
979984
}
980985
}
981986

982-
Future<Uint8List> createPdf(PdfPageFormat format) async {
987+
Future<Uint8List> createPdf(
988+
PdfPageFormat format, {
989+
PdfPageFormat? sourceFormat,
990+
}) async {
991+
format = sourceFormat ?? _sourcePageFormat(format);
983992
if (widget.prebuiltBlocks != null) {
984993
return _createPrebuiltPdf(format);
985994
}
986995
String dataString = await _dataFuture;
987-
format = _sourcePageFormat(format);
988996

989997
final shouldReplaceHolyNames = _shouldReplaceHolyNames;
990998

@@ -1026,7 +1034,6 @@ class _PrintingScreenState extends State<PrintingScreen> {
10261034

10271035
/// יוצר PDF מבלוקים מוכנים מראש (כרטיסיית מפרשים).
10281036
Future<Uint8List> _createPrebuiltPdf(PdfPageFormat format) async {
1029-
format = _sourcePageFormat(format);
10301037
final bookName = widget.documentTitle ?? widget.bookId;
10311038
final blocks = _mapPrebuiltBlocks(widget.prebuiltBlocks ?? const []);
10321039
return _renderBlocksToPdf(

‎test/printing/printing_screen_output_test.dart‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import 'dart:ffi' hide Size;
33
import 'dart:io';
44

55
import 'package:bloc_test/bloc_test.dart';
6+
import 'package:otzaria/book_protection/models/book_protection.dart';
7+
import 'package:otzaria/divine_names/models/divine_name_spans.dart';
8+
import 'package:otzaria/divine_names/repository/divine_names_repository.dart';
9+
import 'package:otzaria/models/books.dart';
10+
import 'package:otzaria/text_book/models/book_notes.dart';
11+
import 'package:otzaria/text_book/repository/book_notes_repository.dart';
612
import 'package:ffi/ffi.dart' show using;
713
import 'package:flutter/material.dart';
814
import 'package:flutter/services.dart';
@@ -508,4 +514,160 @@ void main() {
508514
timeout: const Timeout(Duration(minutes: 2)),
509515
);
510516
}
517+
for (final prebuilt in [false, true]) {
518+
for (final initialPages in [1, 2]) {
519+
testWidgets(
520+
'שינוי מ-$initialPages ל-${3 - initialPages} עמודים בזמן טעינת נתוני ${prebuilt ? 'מפרשים' : 'טקסט'} שומר את כיוון המשימה',
521+
(tester) async {
522+
tester.view.physicalSize = const Size(1600, 1200);
523+
tester.view.devicePixelRatio = 1;
524+
addTearDown(tester.view.reset);
525+
final pendingNames = Completer<DivineNameSpans>();
526+
addTearDown(() {
527+
if (!pendingNames.isCompleted) {
528+
pendingNames.complete(DivineNameSpans.empty);
529+
}
530+
});
531+
final lines = ['ספר', for (var i = 1; i <= 100; i++) 'שורת בדיקה $i'];
532+
DivineNamesRepository.instance.clear();
533+
BookNotesRepository.instance.clear();
534+
DivineNamesRepository.instance.debugLoader =
535+
(
536+
title, {
537+
categoryId,
538+
required source,
539+
}) => pendingNames.future;
540+
BookNotesRepository.instance.debugLoader =
541+
(
542+
title, {
543+
categoryId,
544+
required source,
545+
}) async => BookNotes.empty;
546+
addTearDown(() {
547+
DivineNamesRepository.instance
548+
..debugLoader = null
549+
..clear();
550+
BookNotesRepository.instance
551+
..debugLoader = null
552+
..clear();
553+
});
554+
final platform = _CapturingPrintingPlatform();
555+
PrintingPlatform.instance = platform;
556+
addTearDown(() async {
557+
if (!platform.job.isCompleted) platform.job.complete(false);
558+
await tester.pumpWidget(const SizedBox());
559+
await drainPrintingRasterQueue(tester);
560+
});
561+
final settingsBloc = _MockSettingsBloc();
562+
whenListen(
563+
settingsBloc,
564+
const Stream<SettingsState>.empty(),
565+
initialState: SettingsState.initial(),
566+
);
567+
await tester.runAsync(pdfrxFlutterInitialize);
568+
await tester.pumpWidget(
569+
RepositoryProvider<SettingsRepository>.value(
570+
value: _FakeSettingsRepository(),
571+
child: BlocProvider<SettingsBloc>.value(
572+
value: settingsBloc,
573+
child: MaterialApp(
574+
builder: (context, child) => MediaQuery(
575+
data: MediaQuery.of(
576+
context,
577+
).copyWith(textScaler: const TextScaler.linear(0.5)),
578+
child: child!,
579+
),
580+
home: PrintingScreen(
581+
data: Future.value(lines.join('\n')),
582+
prebuiltBlocks: prebuilt
583+
? [
584+
for (final line in lines)
585+
PrintBlock(
586+
kind: PrintBlockKind.commentary,
587+
text: line,
588+
),
589+
]
590+
: null,
591+
startLine: 1,
592+
endLine: 101,
593+
bookId: 'ספר בדיקה',
594+
book: TextBook(title: 'ספר בדיקה'),
595+
protection: BookProtection.none,
596+
),
597+
),
598+
),
599+
),
600+
);
601+
await tester.pump();
602+
tester
603+
.widget<PrintingOrientationDropdownRow>(
604+
find.byType(PrintingOrientationDropdownRow),
605+
)
606+
.onChanged(pw.PageOrientation.landscape);
607+
void setPages(int value) => tester
608+
.widget<PrintingPagesPerSheetDropdownRow>(
609+
find.byType(PrintingPagesPerSheetDropdownRow),
610+
)
611+
.onChanged(value);
612+
setPages(initialPages);
613+
await tester.pump(const Duration(milliseconds: 300));
614+
await tester.tap(find.text('הדפסה'));
615+
await tester.pump();
616+
expect(platform.onLayout, isNotNull);
617+
var outputReady = false;
618+
final pendingOutput = Future.value(
619+
platform.onLayout!(PdfPageFormat.a4),
620+
)..then((_) => outputReady = true);
621+
await tester.pump(const Duration(milliseconds: 50));
622+
expect(
623+
outputReady,
624+
isFalse,
625+
reason: 'נתוני הספר עדיין ממתינים בזמן שינוי ההגדרות.',
626+
);
627+
setPages(3 - initialPages);
628+
await tester.pump(const Duration(milliseconds: 300));
629+
expect(outputReady, isFalse);
630+
pendingNames.complete(DivineNameSpans.empty);
631+
final output = await _settle(tester, pendingOutput);
632+
platform.job.complete(false);
633+
await tester.pump();
634+
Future<void> expectGeometry(Uint8List bytes, int pages) async {
635+
final document = await _settle(tester, PdfDocument.openData(bytes));
636+
final sheet = document.pages.first.size;
637+
await _settle(tester, document.dispose());
638+
expect(sheet.width, closeTo(PdfPageFormat.a4.landscape.width, 0.1));
639+
expect(
640+
sheet.height,
641+
closeTo(PdfPageFormat.a4.landscape.height, 0.1),
642+
);
643+
if (pages == 2) {
644+
final placements = await _settle(
645+
tester,
646+
_pdfFirstSheet(bytes, 1, 2),
647+
);
648+
expect(placements, hasLength(2));
649+
for (final placement in placements) {
650+
expect(
651+
placement.page.width,
652+
closeTo(placement.cell.width, 0.1),
653+
);
654+
expect(
655+
placement.page.height,
656+
closeTo(placement.cell.height, 0.1),
657+
);
658+
}
659+
}
660+
}
661+
662+
await expectGeometry(output, initialPages);
663+
final nextOutput = await _settle(
664+
tester,
665+
Future.value(platform.onLayout!(PdfPageFormat.a4)),
666+
);
667+
await expectGeometry(nextOutput, 3 - initialPages);
668+
},
669+
timeout: const Timeout(Duration(minutes: 2)),
670+
);
671+
}
672+
}
511673
}

0 commit comments

Comments
 (0)