Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 36 additions & 34 deletions lib/pdf_book/view/pdf_commentators_tab_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import 'package:otzaria/book_common/utils/commentary_search_utils.dart';
import 'package:otzaria/book_common/utils/commentary_type_filter.dart';
import 'package:otzaria/models/books.dart';
import 'package:otzaria/data/repository/data_repository.dart';
import 'package:otzaria/utils/text/ref_helper.dart';
import 'package:otzaria/utils/text/text_manipulation.dart' as utils;
import 'package:otzaria/utils/navigation/open_book.dart';
import 'package:otzaria/utils/file/page_converter.dart';
Expand Down Expand Up @@ -113,8 +114,9 @@ class _PdfCommentatorsTabScreenState extends State<PdfCommentatorsTabScreen>
final headings = _sortedHeadings;
if (headings == null || idx < 0 || idx >= headings.length) return;
final source = widget.tab.sourceTab;
source.currentTitle.value = headings[idx].key;
_pdfPageForHeading(idx).then((page) {
final heading = headings[idx];
source.currentTitle.value = heading.key;
_pdfPageForHeading(source.book, heading.value).then((page) {
if (page != null && mounted && version == _headingSelectionVersion) {
source.pageNumber = page;
}
Expand Down Expand Up @@ -907,10 +909,15 @@ class _PdfCommentatorsTabScreenState extends State<PdfCommentatorsTabScreen>
});
}

/// עמוד ה-PDF של הכותרת [idx]; null כשאין מיפוי אמין.
Future<int?> _pdfPageForHeading(int idx) async {
final book = widget.tab.sourceTab.book;
final line = _sortedHeadings![idx].value;
MapEntry<String, int>? get _selectedHeading {
final headings = _sortedHeadings;
final index = _selectedHeadingIdx;
if (headings == null || index < 0 || index >= headings.length) return null;
return headings[index];
}

/// עמוד ה-PDF של שורת הכותרת, אם נמצא לה מיפוי.
Future<int?> _pdfPageForHeading(PdfBook book, int line) async {
try {
final library = await DataRepository.instance.library;
final textBook = library.getCompanionBook(book, TextBook) as TextBook?;
Expand All @@ -923,38 +930,27 @@ class _PdfCommentatorsTabScreenState extends State<PdfCommentatorsTabScreen>
}

Future<void> _addBookmark(BuildContext context) async {
final sourceTab = widget.tab.sourceTab;
final source = widget.tab.sourceTab;
final bookmarkBloc = context.read<BookmarkBloc>();
final headings = _sortedHeadings;
final hasSelectedHeading =
headings != null &&
_selectedHeadingIdx >= 0 &&
_selectedHeadingIdx < headings.length;

// הכותרת הנבחרת בכרטיסייה (כפי שמוצג בסרגל), ולא מצב הספר המקורי
final heading = hasSelectedHeading
? headings[_selectedHeadingIdx].key.trim()
: sourceTab.currentTitle.value.trim();

// עמוד ברירת מחדל — מצב הספר המקורי אם אין כותרת נבחרת
int page = sourceTab.pdfViewerController.isReady
? (sourceTab.pdfViewerController.pageNumber ?? sourceTab.pageNumber)
: sourceTab.pageNumber;

// המרת שורת הכותרת הנבחרת לעמוד PDF מדויק; אם לא ניתן — נשארים על ברירת המחדל
if (hasSelectedHeading) {
page = await _pdfPageForHeading(_selectedHeadingIdx) ?? page;
final book = source.book;
final selected = _selectedHeading;
final heading = (selected?.key ?? source.currentTitle.value).trim();
final commentators = source.activeCommentators.toList();
int page = source.pdfViewerController.isReady
? (source.pdfViewerController.pageNumber ?? source.pageNumber)
: source.pageNumber;
if (selected != null) {
page = await _pdfPageForHeading(book, selected.value) ?? page;
}

final ref = heading.isNotEmpty
? '${sourceTab.book.title} $heading'
: '${sourceTab.book.title} עמוד $page';

final ref = addBookTitleToRef(
heading.isEmpty ? 'עמוד $page' : heading,
book.title,
);
final added = bookmarkBloc.addPdfCommentatorsBookmark(
ref: 'מפרשים | $ref',
book: sourceTab.book,
book: book,
index: page,
commentatorsToShow: sourceTab.activeCommentators.toList(),
commentatorsToShow: commentators,
pdfCommentatorsHeading: heading.isEmpty ? null : heading,
);
UiSnack.show(
Expand All @@ -964,8 +960,14 @@ class _PdfCommentatorsTabScreenState extends State<PdfCommentatorsTabScreen>

Widget _buildAppTopBar(BuildContext context) {
final isCompact = AppDensity.of(context).isCompact;
final source = widget.tab.sourceTab;
final heading = (_selectedHeading?.key ?? source.currentTitle.value).trim();
final ref = addBookTitleToRef(
heading.isEmpty ? 'עמוד ${source.pageNumber}' : heading,
source.book.title,
);
return CommentatorsTabTopBar(
title: 'מפרשים על ${widget.tab.sourceTab.book.title}',
title: 'מפרשים על $ref',
titleMaxLines: 1,
prevMajorTooltip: 'הכותרת הקודמת',
nextMajorTooltip: 'הכותרת הבאה',
Expand Down
16 changes: 15 additions & 1 deletion lib/text_book/view/commentators_tab_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,20 @@ class _CommentatorsTabScreenState extends State<CommentatorsTabScreen>

// ── AppBar ─────────────────────────────────────────────────────────────────

String _currentTitleRef(TextBookLoaded state, List<TocEntry> chapters) {
final chapter = _selectedChapter;
final String ref;
if (chapter != null && chapters.contains(chapter)) {
final child = _selectedVerseIdx >= 0
? chapter.children.elementAtOrNull(_selectedVerseIdx)
: null;
ref = (child ?? chapter).fullText;
} else {
ref = refFromTocList(widget.tab.currentIndex, state.tableOfContents);
}
return addBookTitleToRef(refUpToDaf(ref), state.book.title);
}

Widget _buildAppBar(
BuildContext context,
TextBookLoaded state,
Expand All @@ -1100,7 +1114,7 @@ class _CommentatorsTabScreenState extends State<CommentatorsTabScreen>
),
);
return CommentatorsTabTopBar(
title: 'מפרשים על ${state.book.title}',
title: 'מפרשים על ${_currentTitleRef(state, chapters)}',
prevMajorTooltip: 'הפרק הקודם',
nextMajorTooltip: 'הפרק הבא',
navPaneOpen: _navPaneOpen,
Expand Down
16 changes: 12 additions & 4 deletions test/pdf_book/pdf_commentators_tab_screen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,28 @@ void main() {
addTearDown(sourceTab.dispose);
sourceTab.pdfHeadings = PdfHeadings(
bookTitle: 'PDF בדיקה',
headingsMap: {'פרק א': 1, 'פרק ב': 10},
headingsMap: {'PDF בדיקה': 1, 'פרק ב': 10},
);
sourceTab.currentTitle.value = 'פרק א';
sourceTab.currentTitle.value = 'PDF בדיקה';

final tab = PdfCommentatorsTab(sourceTab: sourceTab);
await tester.pumpWidget(_wrap(PdfCommentatorsTabScreen(tab: tab)));
await tester.pump();

expect(find.text('מפרשים על PDF בדיקה פרק א'), findsOneWidget);
expect(find.text('מפרשים על PDF בדיקה'), findsOneWidget);

sourceTab.currentTitle.value = 'פרק ב';
await tester.pump();

expect(find.text('מפרשים על PDF בדיקה פרק ב'), findsOneWidget);
expect(find.text('מפרשים על PDF בדיקה, פרק ב'), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
sourceTab.pdfHeadings = null;
sourceTab.currentTitle.value = '';
sourceTab.pageNumber = 7;
await tester.pumpWidget(_wrap(PdfCommentatorsTabScreen(tab: tab)));
await tester.pump();

expect(find.text('מפרשים על PDF בדיקה, עמוד 7'), findsOneWidget);
});

testWidgets('כרטסיית מפרשי PDF מסתנכרנת עם currentTitle של sourceTab', (
Expand Down
7 changes: 7 additions & 0 deletions test/text_book/view/commentators_tab_position_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class _LoadingTextBookBloc extends Bloc<TextBookEvent, TextBookState>
});
}

void replaceLoaded(TextBookLoaded loaded) => emit(loaded);

@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
Expand Down Expand Up @@ -163,5 +165,10 @@ void main() {
await tester.pump();

expect(find.text('מפרשים על כתובות, דף ב'), findsOneWidget);

tabBloc.replaceLoaded(_loaded(book, [TocEntry(text: 'דף ג', index: 0)], 0));
await tester.pump();

expect(find.text('מפרשים על כתובות, דף ג'), findsOneWidget);
});
}
Loading