Skip to content

Commit

Permalink
fix: fixed bug in ExternalBooks loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Sivan22 committed Nov 30, 2024
1 parent 5f5be2e commit 98a064a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
79 changes: 42 additions & 37 deletions lib/data/data_providers/file_system_data_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:io';
import 'dart:isolate';
import 'dart:convert';
import 'package:csv/csv.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:otzaria/data/data_providers/cache_provider.dart';
import 'package:otzaria/utils/docx_to_otzaria.dart';
Expand All @@ -23,12 +24,15 @@ class FileSystemData {
/// Future that resolves to a mapping of book titles to their file system paths
late Future<Map<String, String>> titleToPath;

late String libraryPath;

/// Future that resolves to metadata for all books and categories
late Future<Map<String, Map<String, dynamic>>> metadata;

/// Creates a new instance of [FileSystemData] and initializes the title to path mapping
/// and metadata
FileSystemData() {
libraryPath = Settings.getValue<String>('key-library-path') ?? '.';
titleToPath = _getTitleToPath();
metadata = _getMetadata();
}
Expand All @@ -44,8 +48,7 @@ class FileSystemData {
titleToPath = _getTitleToPath();
metadata = _getMetadata();
return _getLibraryFromDirectory(
'${Settings.getValue<String>('key-library-path') ?? '.'}${Platform.pathSeparator}אוצריא',
await metadata);
'${libraryPath}${Platform.pathSeparator}אוצריא', await metadata);
}

/// Recursively builds the library structure from a directory.
Expand Down Expand Up @@ -143,22 +146,22 @@ class FileSystemData {
}

/// Retrieves the list of books from Otzar HaChochma
Future<List<ExternalBook>> getOtzarBooks() {
static Future<List<ExternalBook>> getOtzarBooks() {
return _getOtzarBooks();
}

/// Retrieves the list of books from HebrewBooks
Future<List<ExternalBook>> getHebrewBooks() {
static Future<List<ExternalBook>> getHebrewBooks() {
return _getHebrewBooks();
}

/// Internal implementation for loading Otzar HaChochma books from CSV
Future<List<ExternalBook>> _getOtzarBooks() async {
static Future<List<ExternalBook>> _getOtzarBooks() async {
try {
print('Loading Otzar HaChochma books from CSV');
final csvData = await rootBundle.loadString('assets/otzar_books.csv');

return Isolate.run(() {
final table = await Isolate.run(() {
// Normalize line endings for cross-platform compatibility
final normalizedCsvData =
csvData.replaceAll('\r\n', '\n').replaceAll('\r', '\n');
Expand All @@ -173,31 +176,32 @@ class FileSystemData {

print('Loaded ${csvTable.length} rows');

return csvTable.skip(1).map((row) {
return ExternalBook(
title: row[1],
id: int.tryParse(row[0]) ?? -1,
author: row[2],
pubPlace: row[3],
pubDate: row[4],
topics: row[5],
link: row[7],
);
}).toList();
return csvTable;
});
return table.skip(1).map((row) {
return ExternalBook(
title: row[1],
id: int.tryParse(row[0]) ?? -1,
author: row[2],
pubPlace: row[3],
pubDate: row[4],
topics: row[5],
link: row[7],
);
}).toList();
} catch (e) {
print('Error loading Otzar HaChochma books: $e');
return [];
}
}

/// Internal implementation for loading HebrewBooks from CSV
Future<List<ExternalBook>> _getHebrewBooks() async {
static Future<List<ExternalBook>> _getHebrewBooks() async {
try {
print('Loading hebrewbooks from CSV');
final csvData = await rootBundle.loadString('assets/hebrew_books.csv');

return Isolate.run(() {
final table = await Isolate.run(() {
// Normalize line endings for cross-platform compatibility
final normalizedCsvData =
csvData.replaceAll('\r\n', '\n').replaceAll('\r', '\n');
Expand All @@ -212,24 +216,26 @@ class FileSystemData {

print('Loaded ${csvTable.length} rows');

return csvTable.skip(1).map((row) {
try {
return ExternalBook(
title: row[1].toString(),
id: -1,
author: row[2].toString(),
pubPlace: row[3].toString(),
pubDate: row[4].toString(),
topics: row[15].toString().replaceAll(';', ', '),
heShortDesc: row[13].toString(),
link: 'https://beta.hebrewbooks.org/${row[0]}',
);
} catch (e) {
print('Error loading book: $e');
return ExternalBook(title: 'error', id: 0, link: '');
}
}).toList();
return csvTable;
});

return table.skip(1).map((row) {
try {
return ExternalBook(
title: row[1].toString(),
id: -1,
author: row[2].toString(),
pubPlace: row[3].toString(),
pubDate: row[4].toString(),
topics: row[15].toString().replaceAll(';', ', '),
heShortDesc: row[13].toString(),
link: 'https://beta.hebrewbooks.org/${row[0]}',
);
} catch (e) {
print('Error loading book: $e');
return ExternalBook(title: 'error', id: 0, link: '');
}
}).toList();
} catch (e) {
print('Error loading hebrewbooks: $e');
return [];
Expand Down Expand Up @@ -322,7 +328,6 @@ class FileSystemData {
/// file system paths, excluding PDF files.
Future<Map<String, String>> _getTitleToPath() async {
Map<String, String> titleToPath = {};
final libraryPath = Settings.getValue('key-library-path') ?? 'C:\\אוצריא';
List<String> paths = await getAllBooksPathsFromDirecctory(libraryPath);
for (var path in paths) {
if (path.toLowerCase().endsWith('.pdf')) continue;
Expand Down
4 changes: 2 additions & 2 deletions lib/data/repository/data_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ class DataRepository {
/// Returns a [Future] that completes with a list of [ExternalBook] objects
/// representing books from the Otzar HaHochma collection
Future<List<ExternalBook>> getOtzarBooks() {
return _fileSystemData.getOtzarBooks();
return FileSystemData.getOtzarBooks();
}

/// Retrieves the list of books from the Hebrew Books project
///
/// Returns a [Future] that completes with a list of [ExternalBook] objects
/// representing books from the Hebrew Books collection
Future<List<ExternalBook>> getHebrewBooks() {
return _fileSystemData.getHebrewBooks();
return FileSystemData.getHebrewBooks();
}

/// Retrieves the full text content of a specific book
Expand Down

0 comments on commit 98a064a

Please sign in to comment.