diff --git a/lib/models/app_model.dart b/lib/models/app_model.dart index acf55a81..891d1c63 100644 --- a/lib/models/app_model.dart +++ b/lib/models/app_model.dart @@ -171,7 +171,7 @@ class AppModel with ChangeNotifier { currentView.value = Screens.reading; } - Future findBookByTitle(String title) async { + Future findBookByTitle(String title) async { final books = await findBooks(title, null); if (books.isEmpty) { @@ -179,11 +179,11 @@ class AppModel with ChangeNotifier { } final exactMatch = books.firstWhere( - (book) => book.title.toLowerCase() == title.toLowerCase(), + (book) => book.title == title, orElse: () => books.first, ); - return exactMatch is TextBook ? exactMatch : null; + return exactMatch; } /// Opens a new search tab. diff --git a/lib/screens/library_browser.dart b/lib/screens/library_browser.dart index 750ddc4a..4ef23ace 100644 --- a/lib/screens/library_browser.dart +++ b/lib/screens/library_browser.dart @@ -6,6 +6,7 @@ import 'package:flutter_settings_screens/flutter_settings_screens.dart'; import 'package:otzaria/models/app_model.dart'; import 'package:otzaria/models/books.dart'; import 'package:otzaria/models/library.dart'; +import 'package:otzaria/utils/daf_yomi_helper.dart'; import 'package:otzaria/utils/extraction.dart'; import 'package:otzaria/widgets/daf_yomi.dart'; import 'package:otzaria/widgets/grid_items.dart'; @@ -85,7 +86,7 @@ class _LibraryBrowserState extends State ), DafYomi( onDafYomiTap: (tractate, daf) { - _openDafYomiBook(tractate, daf); + openDafYomiBook(context, tractate, daf); }, ) ], @@ -258,50 +259,6 @@ class _LibraryBrowserState extends State ); } - void _openDafYomiBook(String tractate, String daf) async { - final appModel = Provider.of(context, listen: false); - final book = await appModel.findBookByTitle(tractate); - if (book != null) { - final tocEntry = await _findDafInToc(book, daf); - if (tocEntry != null) { - appModel.openBook(book, tocEntry.index, openLeftPane: true); - } else { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('לא נמצא דף $daf בספר $tractate'), - ), - ); - } - } else { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - content: Text('לא נמצא הספר $tractate'), - ), - ); - } - } - - Future _findDafInToc(TextBook book, String daf) async { - final toc = await book.tableOfContents; - - TocEntry? findDafInEntries(List entries) { - for (var entry in entries) { - String ref = entry.text; - if (ref.contains('דף $daf')) { - return entry; - } - // Recursively search in children - TocEntry? result = findDafInEntries(entry.children); - if (result != null) { - return result; - } - } - return null; - } - - return findDafInEntries(toc); - } - void _showFilterDialog() { showDialog( context: context, diff --git a/lib/utils/calendar.dart b/lib/utils/calendar.dart index 937775ed..262015c1 100644 --- a/lib/utils/calendar.dart +++ b/lib/utils/calendar.dart @@ -17,5 +17,8 @@ String getHebrewTimeStamp() { } String formatAmud(int amud) { - return HebrewDateFormatter().formatHebrewNumber(amud).replaceAll('״', ''); + return HebrewDateFormatter() + .formatHebrewNumber(amud) + .replaceAll('״', '') + .replaceAll('׳', ''); } diff --git a/lib/utils/daf_yomi_helper.dart b/lib/utils/daf_yomi_helper.dart new file mode 100644 index 00000000..655bd07e --- /dev/null +++ b/lib/utils/daf_yomi_helper.dart @@ -0,0 +1,68 @@ +import 'package:flutter/material.dart'; +import 'package:otzaria/models/app_model.dart'; +import 'package:otzaria/models/books.dart'; +import 'package:pdfrx/pdfrx.dart'; +import 'package:provider/provider.dart'; + +void openDafYomiBook(BuildContext context, String tractate, String daf) async { + final appModel = Provider.of(context, listen: false); + final book = await appModel.findBookByTitle(tractate); + var index = 0; + if (book != null) { + if (book is TextBook) { + final tocEntry = await _findDafInToc(book, daf); + index = tocEntry?.index ?? 0; + } else if (book is PdfBook) { + final outline = await getDafYomiOutline(book, daf); + index = outline?.dest?.pageNumber ?? 0; + } + appModel.openBook(book, index, openLeftPane: true); + } else { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text('לא נמצא דף $daf בספר $tractate'), + ), + ); + } +} + +Future _findDafInToc(TextBook book, String daf) async { + final toc = await book.tableOfContents; + TocEntry? findDafInEntries(List entries) { + for (var entry in entries) { + String ref = entry.text; + if (ref.contains('דף $daf.')) { + return entry; + } + // Recursively search in children + TocEntry? result = findDafInEntries(entry.children); + if (result != null) { + return result; + } + } + return null; + } + + return findDafInEntries(toc); +} + +Future getDafYomiOutline(PdfBook book, String daf) async { + final outlines = await PdfDocument.openFile(book.path) + .then((value) => value.loadOutline()); + + PdfOutlineNode? findDafInEntries(List entries) { + for (var entry in entries) { + String ref = entry.title; + if (ref.contains(' $daf, א')) { + return entry; + } + // Recursively search in children + PdfOutlineNode? result = findDafInEntries(entry.children); + if (result != null) { + return result; + } + } + } + + return findDafInEntries(outlines); +} diff --git a/lib/widgets/daf_yomi.dart b/lib/widgets/daf_yomi.dart index c6d26c2a..fb3ceba8 100644 --- a/lib/widgets/daf_yomi.dart +++ b/lib/widgets/daf_yomi.dart @@ -20,7 +20,7 @@ class DafYomi extends StatelessWidget { final tractate = dafYomi.getMasechta(); final dafAmud = dafYomi.getDaf(); - return GestureDetector( + return InkWell( onTap: () => onDafYomiTap( tractate, formatAmud(dafAmud),