Skip to content

Commit

Permalink
fixed daf yomi openning
Browse files Browse the repository at this point in the history
  • Loading branch information
Sivan22 committed Aug 13, 2024
1 parent b388606 commit d2f423c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 50 deletions.
6 changes: 3 additions & 3 deletions lib/models/app_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -171,19 +171,19 @@ class AppModel with ChangeNotifier {
currentView.value = Screens.reading;
}

Future<TextBook?> findBookByTitle(String title) async {
Future<Book?> findBookByTitle(String title) async {
final books = await findBooks(title, null);

if (books.isEmpty) {
return null;
}

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.
Expand Down
47 changes: 2 additions & 45 deletions lib/screens/library_browser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -85,7 +86,7 @@ class _LibraryBrowserState extends State<LibraryBrowser>
),
DafYomi(
onDafYomiTap: (tractate, daf) {
_openDafYomiBook(tractate, daf);
openDafYomiBook(context, tractate, daf);
},
)
],
Expand Down Expand Up @@ -258,50 +259,6 @@ class _LibraryBrowserState extends State<LibraryBrowser>
);
}

void _openDafYomiBook(String tractate, String daf) async {
final appModel = Provider.of<AppModel>(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<TocEntry?> _findDafInToc(TextBook book, String daf) async {
final toc = await book.tableOfContents;

TocEntry? findDafInEntries(List<TocEntry> 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,
Expand Down
5 changes: 4 additions & 1 deletion lib/utils/calendar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ String getHebrewTimeStamp() {
}

String formatAmud(int amud) {
return HebrewDateFormatter().formatHebrewNumber(amud).replaceAll('״', '');
return HebrewDateFormatter()
.formatHebrewNumber(amud)
.replaceAll('״', '')
.replaceAll('׳', '');
}
68 changes: 68 additions & 0 deletions lib/utils/daf_yomi_helper.dart
Original file line number Diff line number Diff line change
@@ -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<AppModel>(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<TocEntry?> _findDafInToc(TextBook book, String daf) async {
final toc = await book.tableOfContents;
TocEntry? findDafInEntries(List<TocEntry> 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<PdfOutlineNode?> getDafYomiOutline(PdfBook book, String daf) async {
final outlines = await PdfDocument.openFile(book.path)
.then((value) => value.loadOutline());

PdfOutlineNode? findDafInEntries(List<PdfOutlineNode> 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);
}
2 changes: 1 addition & 1 deletion lib/widgets/daf_yomi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit d2f423c

Please sign in to comment.