From d01834ec55676255621b9d1789b36cd3a7af5e20 Mon Sep 17 00:00:00 2001 From: KleiKodesh <211884665+KleiKodesh@users.noreply.github.com> Date: Mon, 23 Nov 2026 13:00:01 +0200 Subject: [PATCH 1/2] perf(reader): read a book as its lines, one format for every source A seforim.db book was read as rows, joined with newlines into one string and split again on newlines for the viewer. Each source now hands over a list of lines: database rows directly (BookTextReader.lines, worker method bookLines, getBookLinesFromDb; official, user-books and attached databases), a text file split once (BookLinesProvider, LibraryProviderManager.getBookLines, TextBookRepository.getBookLines). getBookText / getBookContent are joins of the lines, built on demand for consumers that need one string. Lines equal the old text split on newlines in every path (BOM per row, rows holding a newline, UTF-16, empty books). --- lib/data/data_providers/book_text_reader.dart | 65 ++++++++ .../database_library_provider.dart | 52 ++++-- lib/data/data_providers/db_read_worker.dart | 12 +- .../file_system_library_provider.dart | 20 ++- lib/data/data_providers/library_provider.dart | 45 +++++ .../library_provider_manager.dart | 70 ++++++-- .../data_providers/sqlite_data_provider.dart | 36 ++++ lib/data/repository/text_book_repository.dart | 33 ++-- test/data_providers/book_lines_test.dart | 157 ++++++++++++++++++ .../data_providers/book_text_reader_test.dart | 50 ++++++ 10 files changed, 498 insertions(+), 42 deletions(-) create mode 100644 test/data_providers/book_lines_test.dart diff --git a/lib/data/data_providers/book_text_reader.dart b/lib/data/data_providers/book_text_reader.dart index 6a330f11e2..9406781ae0 100644 --- a/lib/data/data_providers/book_text_reader.dart +++ b/lib/data/data_providers/book_text_reader.dart @@ -67,6 +67,18 @@ class BookTextReader { ); /// כמו [text], כבייטים גולמיים (UTF-8 כפי שמאוחסן) — מסלול האינדוקס. + /// The book's lines, one per row (see [readBookContentLines]). + static Future?> lines( + SeforimRepository repository, + db_models.Book book, + ) => _load( + repository, + book, + workerMethod: 'bookLines', + fromWorker: (result) => (result as List?)?.cast(), + read: readBookContentLines, + ); + static Future bytes( SeforimRepository repository, db_models.Book book, @@ -291,3 +303,56 @@ Future readBookContentText( ); return read == null ? null : utf8.decode(_concat(read.chunks)); } + +/// Reads a book as its list of lines: one entry per row, in line order. +/// +/// Gives the same lines as splitting [readBookContentText] on '\n' (a row +/// that itself holds '\n' becomes several lines, as the split would make it), +/// without building and re-splitting one joined string. +Future?> readBookContentLines( + sqlite3.Database db, + BookTextKey book, { + ReadCheckpoint? checkpoint, +}) async { + final codec = LineContentCodec.of(db); + final lines = []; + final encoding = db.select('PRAGMA encoding').first.values.first; + if (encoding != 'UTF-8') { + final rows = db.select(_contentSql(db, blob: false), [book.id, book.title]); + if (rows.isEmpty) return null; + for (final row in rows) { + _addRowLines(lines, codec.text(row.values.first) ?? ''); + } + return lines; + } + final statement = db.prepare(_contentSql(db, blob: true)); + try { + final raw = statement.raw + ..bindInt64(1, book.id) + ..bindText(2, book.title); + var rows = 0; + while (raw.step()) { + final bytes = raw.columnInt64(1) != 0 + ? codec.bytes(raw.columnBlob(0)) + : raw.columnBlob(0); + // utf8.decode drops one leading BOM, as the joined read drops it + // from every row. + _addRowLines(lines, utf8.decode(bytes)); + rows++; + if (checkpoint != null && rows % _rowsPerCheckpoint == 0) { + await checkpoint(); + } + } + return rows == 0 ? null : lines; + } finally { + statement.close(); + } +} + +void _addRowLines(List lines, String row) { + if (row.contains('\n')) { + lines.addAll(row.split('\n')); + } else { + lines.add(row); + } +} diff --git a/lib/data/data_providers/database_library_provider.dart b/lib/data/data_providers/database_library_provider.dart index 03a1cf0b5a..44063944a4 100644 --- a/lib/data/data_providers/database_library_provider.dart +++ b/lib/data/data_providers/database_library_provider.dart @@ -1956,7 +1956,8 @@ Future>> _runSelectableVersionKeysInIsolate({ } /// Library provider that loads books from the SQLite database. -class DatabaseLibraryProvider implements LibraryProvider, LinkLinesProvider { +class DatabaseLibraryProvider + implements LibraryProvider, LinkLinesProvider, BookLinesProvider { final SqliteDataProvider _sqliteProvider = SqliteDataProvider.instance; final _cachedKeys = BookKeySet(); final Map _categoryIdToPath = {}; @@ -3187,6 +3188,25 @@ class DatabaseLibraryProvider implements LibraryProvider, LinkLinesProvider { int categoryId, String fileType, { BookSource preferSource = BookSource.official, + }) async { + final lines = await getBookLines( + title, + categoryId, + fileType, + preferSource: preferSource, + ); + return lines?.join('\n'); + } + + static Future?> _fileLines(String? text) async => + text == null ? null : splitBookText(text); + + @override + Future?> getBookLines( + String title, + int categoryId, + String fileType, { + BookSource preferSource = BookSource.official, }) async { final source = _resolveSource( title: title, @@ -3195,7 +3215,7 @@ class DatabaseLibraryProvider implements LibraryProvider, LinkLinesProvider { preferSource: preferSource, ); if (source is AttachedBookSource) { - return _readAttachedBookText(title, categoryId, source); + return _readAttachedBookLines(title, categoryId, source); } // ספרים מתיקיות מותאמות אישית: לקרוא מ-user_books.db (תוכן מהקובץ // עצמו אם isFileBacked, אחרת משורות ה-line). @@ -3213,11 +3233,13 @@ class DatabaseLibraryProvider implements LibraryProvider, LinkLinesProvider { if (await file.exists()) { // PDF מוחזר null — הקוראים שלו עוברים בצנרת נפרדת. פורמט בינארי // (ZIP/OLE) לעולם אינו נקרא כטקסט אלא מומר. - return await readFileBackedBookText(file, book.fileType, title); + return await _fileLines( + await readFileBackedBookText(file, book.fileType, title), + ); } } // נופל לטעינה מתוך ה-DB עצמו (טבלת `line`). - return await _readBookTextFromUserBooksDb( + return await _readBookLinesFromUserBooksDb( repo, book.id, book.totalLines, @@ -3236,12 +3258,14 @@ class DatabaseLibraryProvider implements LibraryProvider, LinkLinesProvider { if (book != null && book.isFileBacked && book.filePath != null) { final file = File(book.filePath!); if (await file.exists()) { - return await readFileBackedBookText(file, book.fileType, title); + return await _fileLines( + await readFileBackedBookText(file, book.fileType, title), + ); } } // If not external or file not found, try DB text if (book != null) { - return await _sqliteProvider.getBookTextFromDb( + return await _sqliteProvider.getBookLinesFromDb( title, categoryId, fileType, @@ -3259,7 +3283,7 @@ class DatabaseLibraryProvider implements LibraryProvider, LinkLinesProvider { /// ספר ממסד מצורף נקרא תמיד משורות ה-`line` שלו — נתיב קובץ שבמסד אינו /// נפתח (מסד שאינו בשליטת התוכנה). - Future _readAttachedBookText( + Future?> _readAttachedBookLines( String title, int categoryId, AttachedBookSource source, @@ -3271,15 +3295,13 @@ class DatabaseLibraryProvider implements LibraryProvider, LinkLinesProvider { preferSource: source, ); if (record == null || record.source != source) return null; - final text = await BookTextReader.text(record.repository, record.book); - if (text != null) return text; + final lines = await BookTextReader.lines(record.repository, record.book); + if (lines != null) return lines; // ספר מבוסס-קובץ: filePath כבר נפתר בתוך תיקיית המסד (או null). final file = record.book.filePath; if (file == null || !await File(file).exists()) return null; - return await readFileBackedBookText( - File(file), - record.book.fileType, - title, + return await _fileLines( + await readFileBackedBookText(File(file), record.book.fileType, title), ); } catch (e) { debugPrint('⚠️ Error reading attached book text: $e'); @@ -3310,7 +3332,7 @@ class DatabaseLibraryProvider implements LibraryProvider, LinkLinesProvider { } /// קורא את תוכן הספר משורות ה-`line` של `user_books.db` ומחזיר טקסט מאוחד. - Future _readBookTextFromUserBooksDb( + Future?> _readBookLinesFromUserBooksDb( SeforimRepository repo, int bookId, int totalLines, @@ -3319,7 +3341,7 @@ class DatabaseLibraryProvider implements LibraryProvider, LinkLinesProvider { if (totalLines <= 0) return null; final lines = await repo.getLines(bookId, 0, totalLines - 1); if (lines.isEmpty) return null; - return lines.map((l) => l.content).join('\n'); + return rowsToBookLines(lines.map((l) => l.content)); } catch (e) { debugPrint('⚠️ Error loading user_books lines for book $bookId: $e'); return null; diff --git a/lib/data/data_providers/db_read_worker.dart b/lib/data/data_providers/db_read_worker.dart index 37089d87f2..5246c2f83e 100644 --- a/lib/data/data_providers/db_read_worker.dart +++ b/lib/data/data_providers/db_read_worker.dart @@ -483,7 +483,10 @@ class DbReadWorker { if (_suspendedForExternalWrite || _closedUntilReopen) { throw const DbReadWorkerSuspended(); } - final slot = method == 'bookText' || method == 'bookTextBytes' + final slot = + method == 'bookText' || + method == 'bookLines' || + method == 'bookTextBytes' ? _books : _ranges; if (slot.stalled) throw const DbReadWorkerUnavailable('stalled'); @@ -1495,6 +1498,13 @@ void _workerMain(_Bootstrap bootstrap) { _bookTextKey(args), checkpoint: bookReadCheckpoint, ); + case 'bookLines': + final repo = await ensureRepo(args['dbPath'] as String); + return readBookContentLines( + await repo.database.database, + _bookTextKey(args), + checkpoint: bookReadCheckpoint, + ); case 'bookTextBytes': final repo = await ensureRepo(args['dbPath'] as String); return readBookContentTransferable( diff --git a/lib/data/data_providers/file_system_library_provider.dart b/lib/data/data_providers/file_system_library_provider.dart index 924a473a39..532a78b671 100644 --- a/lib/data/data_providers/file_system_library_provider.dart +++ b/lib/data/data_providers/file_system_library_provider.dart @@ -35,7 +35,8 @@ typedef _BookFileScan = ({ }); /// Library provider that loads books from the file system. -class FileSystemLibraryProvider implements LibraryProvider, LinkLinesProvider { +class FileSystemLibraryProvider + implements LibraryProvider, LinkLinesProvider, BookLinesProvider { late String _libraryPath; Map? _keyToPath; Map? _catalogKeyToPath; @@ -357,6 +358,23 @@ class FileSystemLibraryProvider implements LibraryProvider, LinkLinesProvider { return readFileBackedBookText(file, fileType, title); } + /// A text file is split into its lines once, when read. + @override + Future?> getBookLines( + String title, + int categoryId, + String fileType, { + BookSource preferSource = BookSource.official, + }) async { + final text = await getBookText( + title, + categoryId, + fileType, + preferSource: preferSource, + ); + return text == null ? null : splitBookText(text); + } + @override Future?> getBookToc( String title, diff --git a/lib/data/data_providers/library_provider.dart b/lib/data/data_providers/library_provider.dart index 58f1df6015..1947b3005a 100644 --- a/lib/data/data_providers/library_provider.dart +++ b/lib/data/data_providers/library_provider.dart @@ -1,3 +1,5 @@ +import 'dart:isolate'; + import 'package:otzaria/models/book_source.dart'; import 'package:otzaria/models/books.dart'; import 'package:otzaria/models/links.dart'; @@ -85,3 +87,46 @@ abstract class LibraryProvider { abstract interface class LinkLinesProvider { Future> getLinkContentLines(Link link); } + +/// A provider that reads a book straight into its lines. +/// +/// One format for every source: a database book hands its rows over as lines, +/// a text file is split once when read. The viewer and every consumer take +/// the list; a joined string is built only where one is genuinely needed. +abstract interface class BookLinesProvider { + /// The book's lines, or null when this provider has no text for it. + /// Equals splitting [LibraryProvider.getBookText] on '\n'. + Future?> getBookLines( + String title, + int categoryId, + String fileType, { + BookSource preferSource = BookSource.official, + }); +} + +/// Splits a whole book text into its lines once (off the UI isolate). +/// An empty text has no lines. +Future> splitBookText(String content) async { + if (content.isEmpty) return const []; + return Isolate.run(() => content.split('\n')); +} + +/// Whether [lines] hold any text (the lines of an empty string do not). +bool bookLinesHaveText(List? lines) => + lines != null && + lines.isNotEmpty && + (lines.length > 1 || lines.first.isNotEmpty); + +/// The lines of a book stored as rows: one line per row; a row that holds +/// '\n' gives several lines, as splitting the joined rows would. +List rowsToBookLines(Iterable rows) { + final lines = []; + for (final row in rows) { + if (row.contains('\n')) { + lines.addAll(row.split('\n')); + } else { + lines.add(row); + } + } + return lines; +} diff --git a/lib/data/data_providers/library_provider_manager.dart b/lib/data/data_providers/library_provider_manager.dart index 9e2036f928..c011470a69 100644 --- a/lib/data/data_providers/library_provider_manager.dart +++ b/lib/data/data_providers/library_provider_manager.dart @@ -341,6 +341,58 @@ class LibraryProviderManager { int? categoryId, String? fileType, BookSource preferSource = BookSource.official, + }) => _readFromProvider( + title, + categoryId: categoryId, + fileType: fileType, + preferSource: preferSource, + read: (provider, key) => provider.getBookText( + title, + key.categoryId, + key.fileType, + preferSource: preferSource, + ), + ); + + /// Gets a book's lines from the appropriate provider: straight from a + /// [BookLinesProvider] (a database book's rows), otherwise its text split + /// once. Equals splitting [getBookText] on '\n'. + Future?> getBookLines( + String title, { + int? categoryId, + String? fileType, + BookSource preferSource = BookSource.official, + }) => _readFromProvider( + title, + categoryId: categoryId, + fileType: fileType, + preferSource: preferSource, + read: (provider, key) async { + if (provider is BookLinesProvider) { + return (provider as BookLinesProvider).getBookLines( + title, + key.categoryId, + key.fileType, + preferSource: preferSource, + ); + } + final text = await provider.getBookText( + title, + key.categoryId, + key.fileType, + preferSource: preferSource, + ); + return text == null ? null : splitBookText(text); + }, + ); + + Future _readFromProvider( + String title, { + required int? categoryId, + required String? fileType, + required BookSource preferSource, + required Future Function(LibraryProvider provider, BookCompositeKey key) + read, }) async { if (!_isInitialized) await initialize(); @@ -353,12 +405,7 @@ class LibraryProviderManager { if (mappedKey != null) { final provider = _bookToProvider[mappedKey]; if (provider != null) { - return await provider.getBookText( - title, - mappedKey.categoryId, - mappedKey.fileType, - preferSource: preferSource, - ); + return await read(provider, mappedKey); } } @@ -374,16 +421,11 @@ class LibraryProviderManager { return null; } - final text = await located.provider.getBookText( - title, - located.key.categoryId, - located.key.fileType, - preferSource: preferSource, - ); - if (text != null) { + final result = await read(located.provider, located.key); + if (result != null) { _mapBook(located.key, located.provider); } - return text; + return result; } /// Gets the table of contents for a book from the appropriate provider diff --git a/lib/data/data_providers/sqlite_data_provider.dart b/lib/data/data_providers/sqlite_data_provider.dart index 70d54059c1..ccc778f26a 100644 --- a/lib/data/data_providers/sqlite_data_provider.dart +++ b/lib/data/data_providers/sqlite_data_provider.dart @@ -458,6 +458,42 @@ class SqliteDataProvider { } /// טקסט הספר מהמסד; [emptyWhenNoLines] מבדיל ספר קיים שהתרוקן מכשל קריאה. + /// The book's lines straight from its rows (no joined string). + /// Same lines as splitting [getBookTextFromDb] on '\n'. + Future?> getBookLinesFromDb( + String title, [ + int? categoryId, + String? fileType, + BookSource preferSource = BookSource.official, + ]) async { + if (!_isInitialized) { + await initialize(); + } + if (!_isInitialized) return null; + + try { + final resolvedBook = await _resolveBookRecord( + title, + categoryId: categoryId, + fileType: fileType, + preferSource: preferSource, + ); + if (resolvedBook == null) return null; + return await BookTextReader.lines( + resolvedBook.repository, + resolvedBook.book, + ); + } on DbReadWorkerBusy { + rethrow; + } catch (e, st) { + debugPrint( + '[SqliteDataProvider] getBookLinesFromDb failed for ' + '"$title": $e\n$st', + ); + return null; + } + } + Future getBookTextFromDb( String title, [ int? categoryId, diff --git a/lib/data/repository/text_book_repository.dart b/lib/data/repository/text_book_repository.dart index 5350480e00..f3e99124dd 100644 --- a/lib/data/repository/text_book_repository.dart +++ b/lib/data/repository/text_book_repository.dart @@ -6,6 +6,7 @@ import 'package:otzaria/data/data_providers/book_database_resolver.dart'; import 'package:otzaria/data/data_providers/db_read_worker.dart'; import 'package:otzaria/data/data_providers/file_system_data_provider.dart'; import 'package:otzaria/data/data_providers/sqlite_data_provider.dart'; +import 'package:otzaria/data/data_providers/library_provider.dart'; import 'package:otzaria/data/data_providers/library_provider_manager.dart'; import 'package:otzaria/data/data_providers/database_library_provider.dart'; import 'package:otzaria/migration/database/repository/seforim_repository.dart'; @@ -67,7 +68,15 @@ class TextBookRepository { fileType: book.fileType, ); - Future getBookContent(TextBook book) async { + /// The whole book as one string, built on demand from [getBookLines] + /// (for consumers that genuinely need one string: export, print, plugins). + Future getBookContent(TextBook book) async => + (await getBookLines(book)).join('\n'); + + /// The book's lines, one format for every source: a database book's rows + /// as they are, a text file split once. Equals splitting the book's text on + /// '\n'; an empty book has no lines. + Future> getBookLines(TextBook book) async { // מהדורה חלופית: נטענת אך ורק משאילתת ה-overlay של version_line — בלי // ליפול בשקט לנוסח הממוזג (שהיה מוצג בשם המהדורה). כישלון = טקסט ריק. if (book.versionTitle != null) { @@ -76,7 +85,8 @@ class TextBookRepository { startLine: 0, endLine: 1 << 30, ); - return range?.lines.join('\n') ?? ''; + final lines = range == null ? null : rowsToBookLines(range.lines); + return bookLinesHaveText(lines) ? lines! : const []; } // Primary path: go through the provider manager (handles file system + DB). @@ -85,14 +95,14 @@ class TextBookRepository { final categoryId = book.categoryId; final fileType = book.fileType ?? 'txt'; - final providerText = await LibraryProviderManager.instance.getBookText( + final providerLines = await LibraryProviderManager.instance.getBookLines( title, categoryId: categoryId, fileType: fileType, preferSource: book.source, ); - if (providerText != null && providerText.isNotEmpty) { - return providerText; + if (bookLinesHaveText(providerLines)) { + return providerLines!; } // Fallback: read directly from the database (doesn't require provider caches). @@ -112,24 +122,25 @@ class TextBookRepository { final file = File(dbBook.filePath!); if (await file.exists()) { // PDF מוחזר '' — אין ממנו טקסט, והקוראים שלו בצנרת נפרדת. - return await readFileBackedBookText(file, dbBook.fileType, title) ?? - ''; + return splitBookText( + await readFileBackedBookText(file, dbBook.fileType, title) ?? '', + ); } } - final dbText = await _sqliteProvider.getBookTextFromDb( + final dbLines = await _sqliteProvider.getBookLinesFromDb( title, dbBook.categoryId, dbBook.fileType, book.source, ); - if (dbText != null && dbText.isNotEmpty) { - return dbText; + if (bookLinesHaveText(dbLines)) { + return dbLines!; } } // Last resort: keep existing behavior. - return ''; + return const []; } /// [maxChars] קוטע את הטווח אחרי השורה שממלאת אותו, מההתחלה או מהסוף לפי diff --git a/test/data_providers/book_lines_test.dart b/test/data_providers/book_lines_test.dart new file mode 100644 index 0000000000..98c799c6c8 --- /dev/null +++ b/test/data_providers/book_lines_test.dart @@ -0,0 +1,157 @@ +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:otzaria/data/data_providers/book_composite_key.dart'; +import 'package:otzaria/data/data_providers/file_system_library_provider.dart'; +import 'package:otzaria/data/data_providers/library_provider.dart'; +import 'package:otzaria/data/data_providers/library_provider_manager.dart'; +import 'package:otzaria/models/book_source.dart'; +import 'package:path/path.dart' as path; + +/// A provider that only knows a book as one text (the old shape). +class _TextProvider extends Fake implements LibraryProvider { + _TextProvider(this.text); + + final String text; + + @override + Future getBookText( + String title, + int categoryId, + String fileType, { + BookSource preferSource = BookSource.official, + }) async => text; +} + +/// A provider that reads a book straight into lines (a database source). +class _LinesProvider extends Fake + implements LibraryProvider, BookLinesProvider { + _LinesProvider(this.lines); + + final List lines; + var textReads = 0; + + @override + Future getBookText( + String title, + int categoryId, + String fileType, { + BookSource preferSource = BookSource.official, + }) async { + textReads++; + return lines.join('\n'); + } + + @override + Future?> getBookLines( + String title, + int categoryId, + String fileType, { + BookSource preferSource = BookSource.official, + }) async => lines; +} + +void main() { + const text = '

title

\nfirst line\n\nlast line'; + + group('splitBookText', () { + test('an empty text has no lines', () async { + expect(await splitBookText(''), isEmpty); + }); + + test('splits once on newline', () async { + expect(await splitBookText(text), text.split('\n')); + }); + }); + + test('rowsToBookLines: one line per row, a row with a newline splits ' + 'as the joined rows would', () { + final rows = ['a', '', 'b\nc', 'd']; + expect(rowsToBookLines(rows), rows.join('\n').split('\n')); + expect(rowsToBookLines(const []), isEmpty); + }); + + test('bookLinesHaveText matches a non-empty joined text', () { + expect(bookLinesHaveText(null), isFalse); + expect(bookLinesHaveText(const []), isFalse); + expect(bookLinesHaveText(const ['']), isFalse); + expect(bookLinesHaveText(const ['', '']), isTrue); + expect(bookLinesHaveText(const ['a']), isTrue); + }); + + group('LibraryProviderManager.getBookLines', () { + final manager = LibraryProviderManager.instance; + final key = BookCompositeKey.create( + title: 'book', + categoryId: 1, + fileType: 'txt', + ); + + tearDown(manager.resetForTesting); + + test('a text-only provider: its text split once', () async { + manager.seedMappingsForTesting( + mapping: {key: _TextProvider(text)}, + providers: [], + ); + final lines = await manager.getBookLines( + 'book', + categoryId: 1, + fileType: 'txt', + ); + expect(lines, text.split('\n')); + }); + + test('a lines provider: its lines as they are, no text read', () async { + final provider = _LinesProvider(['row 0', '', 'row 2']); + manager.seedMappingsForTesting(mapping: {key: provider}, providers: []); + final lines = await manager.getBookLines( + 'book', + categoryId: 1, + fileType: 'txt', + ); + expect(lines, same(provider.lines)); + expect(provider.textReads, 0); + expect( + await manager.getBookText('book', categoryId: 1, fileType: 'txt'), + lines!.join('\n'), + ); + }); + }); + + group('FileSystemLibraryProvider text-file book', () { + late Directory tempDir; + final provider = FileSystemLibraryProvider.instance; + + setUp(() async { + tempDir = await Directory.systemTemp.createTemp('otzaria_book_lines'); + }); + + tearDown(() async { + provider.resetForTesting(); + await tempDir.delete(recursive: true); + }); + + test('its lines equal its text split on newline', () async { + final file = File(path.join(tempDir.path, 'book.txt')); + await file.writeAsString(text); + provider.seedKeyToPathForTesting( + keyToPath: { + BookCompositeKey.create( + title: 'book', + categoryId: 1, + fileType: 'txt', + ).toStorageKey(): file.path, + }, + ); + + final bookText = await provider.getBookText('book', 1, 'txt'); + expect(bookText, text); + expect( + await provider.getBookLines('book', 1, 'txt'), + bookText!.split('\n'), + ); + expect(await provider.getBookLines('missing', 1, 'txt'), isNull); + }); + }); +} diff --git a/test/data_providers/book_text_reader_test.dart b/test/data_providers/book_text_reader_test.dart index 011486a59c..5ed4051043 100644 --- a/test/data_providers/book_text_reader_test.dart +++ b/test/data_providers/book_text_reader_test.dart @@ -158,6 +158,12 @@ void main() { legacy.text, reason: '$bookId', ); + // Lines = rows, equal to the joined text split on '\n'. + expect( + await readBookContentLines(db, k), + legacy.text?.split('\n'), + reason: '$bookId', + ); expect((await readBookContentBytes(db, k))?.bytes, legacy.bytes); expect( (await readBookContentTransferable( @@ -184,6 +190,10 @@ void main() { final legacy = legacyRead(dbPath, 1); expect(legacy.text, ['א', '$bomב', 'ג$bom', '', 'ד'].join('\n')); expect(await readBookContentText(db, key(1)), legacy.text); + expect( + await readBookContentLines(db, key(1)), + legacy.text?.split('\n'), + ); // הבייטים נשארים כפי שמאוחסנים, כולל ה-BOM. expect( (await readBookContentBytes(db, key(1)))?.bytes, @@ -217,6 +227,10 @@ void main() { final legacy = legacyRead(dbPath, 1); expect(await readBookContentText(db, key(1)), legacy.text); + expect( + await readBookContentLines(db, key(1)), + legacy.text?.split('\n'), + ); expect((await readBookContentBytes(db, key(1)))?.bytes, legacy.bytes); expect( (await readBookContentTransferable( @@ -242,6 +256,15 @@ void main() { final legacy = legacyRead(dbPath, 1); expect(legacy.text, contains('\n\nאחרי NULL')); expect(await readBookContentText(db, key(1)), legacy.text); + expect( + await readBookContentLines(db, key(1)), + legacy.text?.split('\n'), + ); + expect(await readBookContentLines(db, key(2)), isNull); + expect( + await readBookContentLines(db, key(1)), + legacy.text?.split('\n'), + ); expect(await readBookContentText(db, key(2)), isNull); if (encoding == 'UTF-8') { expect((await readBookContentBytes(db, key(1)))?.bytes, legacy.bytes); @@ -256,6 +279,7 @@ void main() { final BookTextKey stale = (id: 1, title: 'שמות'); expect(await readBookContentText(db, stale), isNull); + expect(await readBookContentLines(db, stale), isNull); expect(await readBookContentBytes(db, stale), isNull); expect(await readBookContentTransferable(db, stale), isNull); }); @@ -331,6 +355,11 @@ void main() { (await BookTextReader.bytes(repository, book(1)))?.bytes, legacyRead(dbPath, 1).bytes, ); + expect( + await BookTextReader.lines(repository, book(1)), + legacyRead(dbPath, 1).text?.split('\n'), + ); + expect(await BookTextReader.lines(repository, book(2)), isNull); expect(await BookTextReader.text(repository, book(2)), isNull); expect(BookTextReader.mainConnectionReads, 0); expect(DbReadWorker.sentMessageCount, sentBefore); @@ -348,6 +377,11 @@ void main() { legacyRead(dbPath, 1).text, ); expect(BookTextReader.mainConnectionReads, 1); + expect( + await BookTextReader.lines(repository, book(1)), + legacyRead(dbPath, 1).text?.split('\n'), + ); + expect(BookTextReader.mainConnectionReads, 2); }); group('seforim.db דרך SqliteDataProvider', () { @@ -371,6 +405,22 @@ void main() { AppPaths.debugOverrideDataRootPath(null); }); + test('getBookLinesFromDb: rows as lines through the worker', () async { + final provider = SqliteDataProvider.instance; + final sentBefore = DbReadWorker.sentMessageCount; + for (final id in [1, 2, 3]) { + final lines = await provider.getBookLinesFromDb(titles[id]!, 7, 'txt'); + final text = await provider.getBookTextFromDb(titles[id]!, 7, 'txt'); + expect(lines, text?.split('\n'), reason: '$id'); + } + expect( + await provider.getBookLinesFromDb(titles[1]!, 7, 'txt'), + legacyRead(dbPath, 1).text!.split('\n'), + ); + expect(BookTextReader.mainConnectionReads, 0); + expect(DbReadWorker.sentMessageCount, greaterThan(sentBefore)); + }); + test('טקסט ובייטים מלאים נקראים ב-worker וזהים למסלול הקודם', () async { final provider = SqliteDataProvider.instance; final legacy = legacyRead(dbPath, 1); From b595a39c74dcf98b967b02703d971db90ae6e6fe Mon Sep 17 00:00:00 2001 From: KleiKodesh <211884665+KleiKodesh@users.noreply.github.com> Date: Mon, 23 Nov 2026 13:00:06 +0200 Subject: [PATCH 2/2] perf(reader): viewer takes the book's lines without re-splitting The text book bloc (initial load, background full load) and the screen (in-book search loader, context-menu line lookup) take TextBookRepository.getBookLines instead of splitting getBookContent. splitContentLines is gone (splitBookText replaces it). Test repositories that override getBookContent also serve getBookLines from it. --- lib/text_book/bloc/text_book_bloc.dart | 14 ++++------- lib/text_book/utils/link_processing.dart | 8 ------- lib/text_book/view/text_book_screen.dart | 14 ++++------- .../view/book_preview_warming_test.dart | 6 +++++ .../bridge/plugin_bridge_adapter_test.dart | 12 ++++++++++ ...plugin_bridge_reader_print_range_test.dart | 6 +++++ test/tabs/bloc/tabs_bloc_test.dart | 6 +++++ test/tabs/models/reading_snapshot_test.dart | 6 +++++ ...reading_screen_background_resize_test.dart | 6 +++++ ...ing_screen_commentary_open_reuse_test.dart | 6 +++++ ...ding_screen_scrollbar_tab_switch_test.dart | 6 +++++ .../reading_screen_text_input_focus_test.dart | 6 +++++ ...mentary_links_no_not_found_flash_test.dart | 6 +++++ .../text_book/bloc/deep_link_select_test.dart | 6 +++++ .../hidden_commentator_visibility_test.dart | 6 +++++ .../bloc/inline_notes_commentator_test.dart | 6 +++++ .../bloc/links_load_failure_retry_test.dart | 6 +++++ test/text_book/bloc/links_retention_test.dart | 6 +++++ .../bloc/navigation_links_refresh_test.dart | 6 +++++ ...per_book_continuous_initial_load_test.dart | 6 +++++ .../bloc/search_highlight_lifecycle_test.dart | 6 +++++ .../selected_link_types_persistence_test.dart | 6 +++++ .../bloc/smart_highlight_restore_test.dart | 6 +++++ .../bloc/split_pane_warming_test.dart | 6 +++++ test/text_book/bloc/text_book_bloc_test.dart | 24 +++++++++++++++++++ .../visible_indices_after_scroll_test.dart | 6 +++++ .../bloc/visible_indices_stale_emit_test.dart | 6 +++++ .../bloc/visible_indices_throttle_test.dart | 6 +++++ .../commentators_closed_library_test.dart | 6 +++++ .../per_book_reading_metrics_test.dart | 6 +++++ .../text_book/utils/link_processing_test.dart | 8 ++++--- .../text_book_left_pane_focus_width_test.dart | 6 +++++ .../text_book_open_from_search_tab_test.dart | 6 +++++ .../view/text_book_screen_actions_test.dart | 6 +++++ 34 files changed, 217 insertions(+), 31 deletions(-) diff --git a/lib/text_book/bloc/text_book_bloc.dart b/lib/text_book/bloc/text_book_bloc.dart index 55762c7f6a..7ad1c4cec9 100644 --- a/lib/text_book/bloc/text_book_bloc.dart +++ b/lib/text_book/bloc/text_book_bloc.dart @@ -926,8 +926,7 @@ class TextBookBloc extends Bloc { contentLines = (state as TextBookLoaded).content; loadedLineFlags = _loadedContentFlags; } else if (requiresRenderedToc) { - final content = await repository.getBookContent(book); - contentLines = await splitContentLines(content); + contentLines = await repository.getBookLines(book); loadedLineFlags = List.filled(contentLines.length, true); _setLoadedContentFlags(book, loadedLineFlags); _markLoadedContentRange( @@ -987,8 +986,7 @@ class TextBookBloc extends Bloc { } if (contentLines == null) { - final content = await repository.getBookContent(book); - contentLines = await splitContentLines(content); + contentLines = await repository.getBookLines(book); loadedLineFlags = List.filled(contentLines.length, true); _setLoadedContentFlags(book, loadedLineFlags); _markLoadedContentRange( @@ -3033,9 +3031,9 @@ class TextBookBloc extends Bloc { Future _loadFullBookInBackground(TextBook book) async { try { - final fullContent = await repository.getBookContent(book); + final lines = await repository.getBookLines(book); - if (fullContent.isEmpty) { + if (lines.isEmpty) { return; } @@ -3048,10 +3046,6 @@ class TextBookBloc extends Bloc { return; } - final lines = await splitContentLines(fullContent); - if (isClosed) { - return; - } add( ApplyFullBookContent( bookTitle: book.title, diff --git a/lib/text_book/utils/link_processing.dart b/lib/text_book/utils/link_processing.dart index 9cdf3233bb..845165d77d 100644 --- a/lib/text_book/utils/link_processing.dart +++ b/lib/text_book/utils/link_processing.dart @@ -377,11 +377,3 @@ List buildPreviewLines(String previewContent, int previewStartLine) { return List.filled(previewStartLine, '', growable: true) ..addAll(previewLines); } - -Future> splitContentLines(String content) async { - if (content.isEmpty) { - return const []; - } - - return Isolate.run(() => content.split('\n')); -} diff --git a/lib/text_book/view/text_book_screen.dart b/lib/text_book/view/text_book_screen.dart index 170df55a81..c24e666dba 100644 --- a/lib/text_book/view/text_book_screen.dart +++ b/lib/text_book/view/text_book_screen.dart @@ -112,8 +112,6 @@ import 'package:otzaria/widgets/misc/rtl_icon.dart'; import 'package:otzaria/widgets/misc/touch_pinch_scale_recognizer.dart'; import 'package:otzaria/settings/services/nikud_display_service.dart'; import 'package:otzaria/utils/link_helpers.dart'; -import 'package:otzaria/text_book/utils/link_processing.dart' - show splitContentLines; import 'package:otzaria/widgets/feedback/loading_indicator.dart'; import 'package:otzaria/widgets/widgets_exports.dart'; import 'package:otzaria/widgets/navigation/nav_panel_search.dart'; @@ -2589,8 +2587,7 @@ class _TextBookViewerBlocState extends State _searchViewInputs = inputs; return _searchView = TextBookSearchView( focusNode: textSearchFocusNode, - contentLoader: () async => - splitContentLines(await repository.getBookContent(book)), + contentLoader: () => repository.getBookLines(book), lines: widget.tab.lines, // הוא מעביר את טקסט החיפוש מה-state הנוכחי אל תוך רכיב החיפוש initialQuery: state.searchText, @@ -3075,10 +3072,7 @@ Future _dispatchContextMenuShortcut({ final selectedLineCount = text.split('\n').length; List? fullContentLines; if (selectedLineCount > 1) { - final fullContent = await textBookBloc.repository.getBookContent( - state.book, - ); - fullContentLines = await splitContentLines(fullContent); + fullContentLines = await textBookBloc.repository.getBookLines(state.book); } // השורה המסומנת נטענה בהכרח, אבל ברשימה חלקית (חימום/שחרור) עשויים @@ -3087,8 +3081,8 @@ Future _dispatchContextMenuShortcut({ ? state.content[sectionIndex] : ''; if (rawText.isEmpty) { - fullContentLines ??= await splitContentLines( - await textBookBloc.repository.getBookContent(state.book), + fullContentLines ??= await textBookBloc.repository.getBookLines( + state.book, ); if (sectionIndex < fullContentLines.length) { rawText = fullContentLines[sectionIndex]; diff --git a/test/library/view/book_preview_warming_test.dart b/test/library/view/book_preview_warming_test.dart index cf90b9cc12..16c5ba89a7 100644 --- a/test/library/view/book_preview_warming_test.dart +++ b/test/library/view/book_preview_warming_test.dart @@ -20,6 +20,12 @@ class _CountingRepository extends TextBookRepository { final _lines = List.generate(_totalLines, (i) => 'שורה $i'); final requestedEnds = []; + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => _lines.join('\n'); diff --git a/test/plugins/bridge/plugin_bridge_adapter_test.dart b/test/plugins/bridge/plugin_bridge_adapter_test.dart index 4e0d052208..1e5b727d42 100644 --- a/test/plugins/bridge/plugin_bridge_adapter_test.dart +++ b/test/plugins/bridge/plugin_bridge_adapter_test.dart @@ -8895,6 +8895,12 @@ class _CountingTextBookRepository extends Fake implements TextBookRepository { int loads = 0; int failures = 0; + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async { loads++; @@ -8914,6 +8920,12 @@ class _EditionTextBookRepository extends Fake implements TextBookRepository { final Map contents; + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => contents[book]!; } diff --git a/test/plugins/bridge/plugin_bridge_reader_print_range_test.dart b/test/plugins/bridge/plugin_bridge_reader_print_range_test.dart index c08d5940fa..4c59cd6af7 100644 --- a/test/plugins/bridge/plugin_bridge_reader_print_range_test.dart +++ b/test/plugins/bridge/plugin_bridge_reader_print_range_test.dart @@ -51,6 +51,12 @@ class _PrintRepository extends Mock implements TextBookRepository { return pending?.future ?? available; } + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async { reads++; diff --git a/test/tabs/bloc/tabs_bloc_test.dart b/test/tabs/bloc/tabs_bloc_test.dart index 60e7412acb..3a67de4489 100644 --- a/test/tabs/bloc/tabs_bloc_test.dart +++ b/test/tabs/bloc/tabs_bloc_test.dart @@ -3192,6 +3192,12 @@ class _PinpointFakeTextBookRepository extends TextBookRepository { final String? content; final Future? pendingContent; + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async { return pendingContent ?? diff --git a/test/tabs/models/reading_snapshot_test.dart b/test/tabs/models/reading_snapshot_test.dart index 45a6cdb5c4..f9a7d2903d 100644 --- a/test/tabs/models/reading_snapshot_test.dart +++ b/test/tabs/models/reading_snapshot_test.dart @@ -801,6 +801,12 @@ class _Repository extends TextBookRepository { static final _lines = List.generate(20, (i) => 'שורה $i'); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => _lines.join('\n'); diff --git a/test/tabs/reading_screen_background_resize_test.dart b/test/tabs/reading_screen_background_resize_test.dart index 7126f5b049..4364ce2324 100644 --- a/test/tabs/reading_screen_background_resize_test.dart +++ b/test/tabs/reading_screen_background_resize_test.dart @@ -520,6 +520,12 @@ class _TestTextBookBloc extends Bloc class _FakeTextBookRepository extends TextBookRepository { _FakeTextBookRepository() : super(fileSystem: FileSystemData.instance); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => 'שורה א\nשורה ב'; } diff --git a/test/tabs/reading_screen_commentary_open_reuse_test.dart b/test/tabs/reading_screen_commentary_open_reuse_test.dart index 875925f512..af47d38087 100644 --- a/test/tabs/reading_screen_commentary_open_reuse_test.dart +++ b/test/tabs/reading_screen_commentary_open_reuse_test.dart @@ -188,6 +188,12 @@ class _TestTextBookBloc extends Bloc class _FakeTextBookRepository extends TextBookRepository { _FakeTextBookRepository() : super(fileSystem: FileSystemData.instance); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => 'שורה א\nשורה ב'; } diff --git a/test/tabs/reading_screen_scrollbar_tab_switch_test.dart b/test/tabs/reading_screen_scrollbar_tab_switch_test.dart index 92500180c3..014fb9ced1 100644 --- a/test/tabs/reading_screen_scrollbar_tab_switch_test.dart +++ b/test/tabs/reading_screen_scrollbar_tab_switch_test.dart @@ -275,6 +275,12 @@ class _FakeTextBookRepository extends TextBookRepository { ); } + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => [for (var i = 0; i < _lineCount; i++) _line(i)].join('\n'); diff --git a/test/tabs/reading_screen_text_input_focus_test.dart b/test/tabs/reading_screen_text_input_focus_test.dart index 40914a9eff..1e513d3c6e 100644 --- a/test/tabs/reading_screen_text_input_focus_test.dart +++ b/test/tabs/reading_screen_text_input_focus_test.dart @@ -386,6 +386,12 @@ class _FakeTextBookRepository extends TextBookRepository { ); } + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => [for (var i = 0; i < _lineCount; i++) _line(i)].join('\n'); diff --git a/test/text_book/bloc/commentary_links_no_not_found_flash_test.dart b/test/text_book/bloc/commentary_links_no_not_found_flash_test.dart index ea07e04abf..cef4110095 100644 --- a/test/text_book/bloc/commentary_links_no_not_found_flash_test.dart +++ b/test/text_book/bloc/commentary_links_no_not_found_flash_test.dart @@ -20,6 +20,12 @@ const _commentator = 'רש"י על ספר בדיקה'; class _Repository extends TextBookRepository { _Repository() : super(fileSystem: FileSystemData.instance); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => List.generate(40, (i) => 'שורה $i').join('\n'); diff --git a/test/text_book/bloc/deep_link_select_test.dart b/test/text_book/bloc/deep_link_select_test.dart index 687671c6c9..13003c4430 100644 --- a/test/text_book/bloc/deep_link_select_test.dart +++ b/test/text_book/bloc/deep_link_select_test.dart @@ -328,6 +328,12 @@ class _FortyLineRepository extends TextBookRepository { static final _lines = List.generate(40, (i) => 'שורה $i'); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => _lines.join('\n'); diff --git a/test/text_book/bloc/hidden_commentator_visibility_test.dart b/test/text_book/bloc/hidden_commentator_visibility_test.dart index dddf5f9861..d19e1dacb5 100644 --- a/test/text_book/bloc/hidden_commentator_visibility_test.dart +++ b/test/text_book/bloc/hidden_commentator_visibility_test.dart @@ -34,6 +34,12 @@ class _Repository extends TextBookRepository { TextBook book, ) async => const {}; + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => 'תוכן'; diff --git a/test/text_book/bloc/inline_notes_commentator_test.dart b/test/text_book/bloc/inline_notes_commentator_test.dart index 6a0792d78c..2ebd2073e8 100644 --- a/test/text_book/bloc/inline_notes_commentator_test.dart +++ b/test/text_book/bloc/inline_notes_commentator_test.dart @@ -614,6 +614,12 @@ class _ContentRepository extends TextBookRepository { final List lines; + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => lines.join('\n'); diff --git a/test/text_book/bloc/links_load_failure_retry_test.dart b/test/text_book/bloc/links_load_failure_retry_test.dart index 5eb164bdbb..38c6676fd8 100644 --- a/test/text_book/bloc/links_load_failure_retry_test.dart +++ b/test/text_book/bloc/links_load_failure_retry_test.dart @@ -27,6 +27,12 @@ class _Repository extends TextBookRepository { final Completer? firstCallGate; final Completer firstCallStarted = Completer(); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => List.generate(40, (i) => 'שורה $i').join('\n'); diff --git a/test/text_book/bloc/links_retention_test.dart b/test/text_book/bloc/links_retention_test.dart index 79625171bd..55be3f16b4 100644 --- a/test/text_book/bloc/links_retention_test.dart +++ b/test/text_book/bloc/links_retention_test.dart @@ -24,6 +24,12 @@ class _WindowLinksRepository extends TextBookRepository { static final _lines = List.generate(_lineCount, (i) => 'שורה $i'); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => _lines.join('\n'); @override diff --git a/test/text_book/bloc/navigation_links_refresh_test.dart b/test/text_book/bloc/navigation_links_refresh_test.dart index 470aefbd7b..6ead0ffe2d 100644 --- a/test/text_book/bloc/navigation_links_refresh_test.dart +++ b/test/text_book/bloc/navigation_links_refresh_test.dart @@ -13,6 +13,12 @@ import '../../test_helpers/memory_cache_provider.dart'; class _LinksRepository extends TextBookRepository { _LinksRepository() : super(fileSystem: FileSystemData.instance); int loads = 0; + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => List.generate(100, (i) => 'שורה $i').join('\n'); diff --git a/test/text_book/bloc/per_book_continuous_initial_load_test.dart b/test/text_book/bloc/per_book_continuous_initial_load_test.dart index 3546bf88a8..39b44073bc 100644 --- a/test/text_book/bloc/per_book_continuous_initial_load_test.dart +++ b/test/text_book/bloc/per_book_continuous_initial_load_test.dart @@ -85,6 +85,12 @@ class _Repository extends TextBookRepository { @override Future supportsContinuousReadingMode(TextBook book) async => true; + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => _lines.join('\n'); diff --git a/test/text_book/bloc/search_highlight_lifecycle_test.dart b/test/text_book/bloc/search_highlight_lifecycle_test.dart index df0affac6b..2402da203a 100644 --- a/test/text_book/bloc/search_highlight_lifecycle_test.dart +++ b/test/text_book/bloc/search_highlight_lifecycle_test.dart @@ -95,6 +95,12 @@ class _Repository extends TextBookRepository { lines: [_text], ); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => _text; diff --git a/test/text_book/bloc/selected_link_types_persistence_test.dart b/test/text_book/bloc/selected_link_types_persistence_test.dart index b14c8ddad9..f2c6e3a31f 100644 --- a/test/text_book/bloc/selected_link_types_persistence_test.dart +++ b/test/text_book/bloc/selected_link_types_persistence_test.dart @@ -130,6 +130,12 @@ void main() { class _FakeTextBookRepository extends TextBookRepository { _FakeTextBookRepository() : super(fileSystem: FileSystemData.instance); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => List.generate(10, (index) => 'שורה $index').join('\n'); diff --git a/test/text_book/bloc/smart_highlight_restore_test.dart b/test/text_book/bloc/smart_highlight_restore_test.dart index d2d46647a8..6c5717a4cd 100644 --- a/test/text_book/bloc/smart_highlight_restore_test.dart +++ b/test/text_book/bloc/smart_highlight_restore_test.dart @@ -158,6 +158,12 @@ class _Repository extends TextBookRepository { @override Future supportsContinuousReadingMode(TextBook book) async => false; + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => _line; diff --git a/test/text_book/bloc/split_pane_warming_test.dart b/test/text_book/bloc/split_pane_warming_test.dart index 844942c05d..d0fcf4f24e 100644 --- a/test/text_book/bloc/split_pane_warming_test.dart +++ b/test/text_book/bloc/split_pane_warming_test.dart @@ -15,6 +15,12 @@ class _CountingRepository extends TextBookRepository { int rangeRequests = 0; + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => List.generate(6000, (i) => 'שורה $i').join('\n'); diff --git a/test/text_book/bloc/text_book_bloc_test.dart b/test/text_book/bloc/text_book_bloc_test.dart index b9523d1228..670ef51172 100644 --- a/test/text_book/bloc/text_book_bloc_test.dart +++ b/test/text_book/bloc/text_book_bloc_test.dart @@ -2430,6 +2430,12 @@ class _FakeTextBookRepository extends TextBookRepository { int getBookContentCalls = 0; int getBookContentRangeCalls = 0; + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async { getBookContentCalls++; @@ -2485,6 +2491,12 @@ class _FakeTextBookRepository extends TextBookRepository { } class _EmptyContentTextBookRepository extends _FakeTextBookRepository { + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async { return ''; @@ -2503,6 +2515,12 @@ class _EmptyContentTextBookRepository extends _FakeTextBookRepository { } class _MarkdownTocRepository extends _FakeTextBookRepository { + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async { getBookContentCalls++; @@ -2526,6 +2544,12 @@ class _MarkdownTocRepository extends _FakeTextBookRepository { class _DelayedContentTextBookRepository extends _FakeTextBookRepository { final Completer _fullContentCompleter = Completer(); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async { getBookContentCalls++; diff --git a/test/text_book/bloc/visible_indices_after_scroll_test.dart b/test/text_book/bloc/visible_indices_after_scroll_test.dart index b365b0bcb0..6942a0d31f 100644 --- a/test/text_book/bloc/visible_indices_after_scroll_test.dart +++ b/test/text_book/bloc/visible_indices_after_scroll_test.dart @@ -318,6 +318,12 @@ Future _waitFor( class _TwoSectionRepository extends TextBookRepository { _TwoSectionRepository() : super(fileSystem: FileSystemData.instance); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async { return List.generate(40, (i) => 'שורה $i').join('\n'); diff --git a/test/text_book/bloc/visible_indices_stale_emit_test.dart b/test/text_book/bloc/visible_indices_stale_emit_test.dart index 060975124d..bf91325c3f 100644 --- a/test/text_book/bloc/visible_indices_stale_emit_test.dart +++ b/test/text_book/bloc/visible_indices_stale_emit_test.dart @@ -152,6 +152,12 @@ Future _waitFor( class _TwoSectionRepository extends TextBookRepository { _TwoSectionRepository() : super(fileSystem: FileSystemData.instance); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async { return List.generate(40, (i) => 'שורה $i').join('\n'); diff --git a/test/text_book/bloc/visible_indices_throttle_test.dart b/test/text_book/bloc/visible_indices_throttle_test.dart index 020e7d2744..0cc972f31a 100644 --- a/test/text_book/bloc/visible_indices_throttle_test.dart +++ b/test/text_book/bloc/visible_indices_throttle_test.dart @@ -498,6 +498,12 @@ Future _waitFor( class _TwoSectionRepository extends TextBookRepository { _TwoSectionRepository() : super(fileSystem: FileSystemData.instance); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async { return List.generate(40, (i) => 'שורה $i').join('\n'); diff --git a/test/text_book/commentators_closed_library_test.dart b/test/text_book/commentators_closed_library_test.dart index c3cce839b5..e764fe7c0d 100644 --- a/test/text_book/commentators_closed_library_test.dart +++ b/test/text_book/commentators_closed_library_test.dart @@ -29,6 +29,12 @@ class _FailingCommentatorsRepository extends TextBookRepository { int commentatorCalls = 0; + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => List.generate(10, (i) => 'שורה $i').join('\n'); diff --git a/test/text_book/per_book_reading_metrics_test.dart b/test/text_book/per_book_reading_metrics_test.dart index a2d1bd02e1..b57877f93d 100644 --- a/test/text_book/per_book_reading_metrics_test.dart +++ b/test/text_book/per_book_reading_metrics_test.dart @@ -330,6 +330,12 @@ void main() { class _Repository extends TextBookRepository { _Repository() : super(fileSystem: FileSystemData.instance); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => 'שורה א\nשורה ב'; diff --git a/test/text_book/utils/link_processing_test.dart b/test/text_book/utils/link_processing_test.dart index ea3884881d..d7c3617e3e 100644 --- a/test/text_book/utils/link_processing_test.dart +++ b/test/text_book/utils/link_processing_test.dart @@ -1,6 +1,8 @@ import 'dart:math'; import 'package:flutter_test/flutter_test.dart'; +import 'package:otzaria/data/data_providers/library_provider.dart' + show splitBookText; import 'package:otzaria/models/book_source.dart'; import 'package:otzaria/models/link_types.dart'; import 'package:otzaria/models/links.dart'; @@ -683,13 +685,13 @@ void main() { }); }); - group('splitContentLines', () { + group('splitBookText', () { test('תוכן ריק מחזיר רשימה ריקה', () async { - expect(await splitContentLines(''), isEmpty); + expect(await splitBookText(''), isEmpty); }); test('מפצל תוכן לפי שורות ב-isolate', () async { - expect(await splitContentLines('א\nב\nג'), ['א', 'ב', 'ג']); + expect(await splitBookText('א\nב\nג'), ['א', 'ב', 'ג']); }); }); } diff --git a/test/text_book/view/text_book_left_pane_focus_width_test.dart b/test/text_book/view/text_book_left_pane_focus_width_test.dart index 1268ba770e..29cfa3316d 100644 --- a/test/text_book/view/text_book_left_pane_focus_width_test.dart +++ b/test/text_book/view/text_book_left_pane_focus_width_test.dart @@ -284,6 +284,12 @@ class _TestTextBookBloc extends Bloc class _FakeTextBookRepository extends TextBookRepository { _FakeTextBookRepository() : super(fileSystem: FileSystemData.instance); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => 'שורה א\nשורה ב'; } diff --git a/test/text_book/view/text_book_open_from_search_tab_test.dart b/test/text_book/view/text_book_open_from_search_tab_test.dart index e1e3381a46..9b69409b1b 100644 --- a/test/text_book/view/text_book_open_from_search_tab_test.dart +++ b/test/text_book/view/text_book_open_from_search_tab_test.dart @@ -191,6 +191,12 @@ class _TestTextBookBloc extends Bloc class _FakeTextBookRepository extends TextBookRepository { _FakeTextBookRepository() : super(fileSystem: FileSystemData.instance); + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => 'שורה א\nשורה ב'; } diff --git a/test/text_book/view/text_book_screen_actions_test.dart b/test/text_book/view/text_book_screen_actions_test.dart index cacbe58d01..cb6a7cce17 100644 --- a/test/text_book/view/text_book_screen_actions_test.dart +++ b/test/text_book/view/text_book_screen_actions_test.dart @@ -2884,6 +2884,12 @@ class _FakeTextBookRepository extends Fake implements TextBookRepository { final String content; + @override + Future> getBookLines(TextBook book) async { + final content = await getBookContent(book); + return content.isEmpty ? const [] : content.split('\n'); + } + @override Future getBookContent(TextBook book) async => content; }