From 98a064aa63aeb94b13b3e6f2e2dac3241fbcdee0 Mon Sep 17 00:00:00 2001 From: Sivan Ratson <89018301+Sivan22@users.noreply.github.com> Date: Sun, 1 Dec 2024 00:10:22 +0200 Subject: [PATCH] fix: fixed bug in ExternalBooks loading --- .../file_system_data_provider.dart | 79 ++++++++++--------- lib/data/repository/data_repository.dart | 4 +- 2 files changed, 44 insertions(+), 39 deletions(-) diff --git a/lib/data/data_providers/file_system_data_provider.dart b/lib/data/data_providers/file_system_data_provider.dart index da63ec3b..7b107764 100644 --- a/lib/data/data_providers/file_system_data_provider.dart +++ b/lib/data/data_providers/file_system_data_provider.dart @@ -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'; @@ -23,12 +24,15 @@ class FileSystemData { /// Future that resolves to a mapping of book titles to their file system paths late Future> titleToPath; + late String libraryPath; + /// Future that resolves to metadata for all books and categories late Future>> metadata; /// Creates a new instance of [FileSystemData] and initializes the title to path mapping /// and metadata FileSystemData() { + libraryPath = Settings.getValue('key-library-path') ?? '.'; titleToPath = _getTitleToPath(); metadata = _getMetadata(); } @@ -44,8 +48,7 @@ class FileSystemData { titleToPath = _getTitleToPath(); metadata = _getMetadata(); return _getLibraryFromDirectory( - '${Settings.getValue('key-library-path') ?? '.'}${Platform.pathSeparator}אוצריא', - await metadata); + '${libraryPath}${Platform.pathSeparator}אוצריא', await metadata); } /// Recursively builds the library structure from a directory. @@ -143,22 +146,22 @@ class FileSystemData { } /// Retrieves the list of books from Otzar HaChochma - Future> getOtzarBooks() { + static Future> getOtzarBooks() { return _getOtzarBooks(); } /// Retrieves the list of books from HebrewBooks - Future> getHebrewBooks() { + static Future> getHebrewBooks() { return _getHebrewBooks(); } /// Internal implementation for loading Otzar HaChochma books from CSV - Future> _getOtzarBooks() async { + static Future> _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'); @@ -173,18 +176,19 @@ 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 []; @@ -192,12 +196,12 @@ class FileSystemData { } /// Internal implementation for loading HebrewBooks from CSV - Future> _getHebrewBooks() async { + static Future> _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'); @@ -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 []; @@ -322,7 +328,6 @@ class FileSystemData { /// file system paths, excluding PDF files. Future> _getTitleToPath() async { Map titleToPath = {}; - final libraryPath = Settings.getValue('key-library-path') ?? 'C:\\אוצריא'; List paths = await getAllBooksPathsFromDirecctory(libraryPath); for (var path in paths) { if (path.toLowerCase().endsWith('.pdf')) continue; diff --git a/lib/data/repository/data_repository.dart b/lib/data/repository/data_repository.dart index 723e92d6..221665ba 100644 --- a/lib/data/repository/data_repository.dart +++ b/lib/data/repository/data_repository.dart @@ -40,7 +40,7 @@ class DataRepository { /// Returns a [Future] that completes with a list of [ExternalBook] objects /// representing books from the Otzar HaHochma collection Future> getOtzarBooks() { - return _fileSystemData.getOtzarBooks(); + return FileSystemData.getOtzarBooks(); } /// Retrieves the list of books from the Hebrew Books project @@ -48,7 +48,7 @@ class DataRepository { /// Returns a [Future] that completes with a list of [ExternalBook] objects /// representing books from the Hebrew Books collection Future> getHebrewBooks() { - return _fileSystemData.getHebrewBooks(); + return FileSystemData.getHebrewBooks(); } /// Retrieves the full text content of a specific book