From 76f311b53fa4e64f12f456697eeaecdfcdd42d8a Mon Sep 17 00:00:00 2001 From: dudua99 <99@9900.co.il> Date: Thu, 19 Nov 2026 15:32:29 +0000 Subject: [PATCH 1/3] =?UTF-8?q?=D7=91=D7=93=D7=99=D7=A7=D7=AA=20=D7=90?= =?UTF-8?q?=D7=99=D7=9E=D7=95=D7=AA:=20=D7=9E=D7=A2=D7=91=D7=A8=20=D7=91?= =?UTF-8?q?=D7=99=D7=9F=20=D7=98=D7=90=D7=91=D7=99=D7=9D=20=D7=91=D7=9E?= =?UTF-8?q?=D7=A1=D7=9A=20=D7=94=D7=A2=D7=99=D7=95=D7=9F=20=D7=9E=D7=96?= =?UTF-8?q?=D7=99=D7=96=20=D7=90=D7=AA=20=D7=A1=D7=9E=D7=9F=20=D7=A4=D7=A1?= =?UTF-8?q?=20=D7=94=D7=92=D7=9C=D7=99=D7=9C=D7=94=20(issue=20#4173)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ding_screen_scrollbar_tab_switch_test.dart | 320 ++++++++++++++++++ .../utils/reading_extent_estimates_test.dart | 31 ++ 2 files changed, 351 insertions(+) create mode 100644 test/tabs/reading_screen_scrollbar_tab_switch_test.dart diff --git a/test/tabs/reading_screen_scrollbar_tab_switch_test.dart b/test/tabs/reading_screen_scrollbar_tab_switch_test.dart new file mode 100644 index 0000000000..92500180c3 --- /dev/null +++ b/test/tabs/reading_screen_scrollbar_tab_switch_test.dart @@ -0,0 +1,320 @@ +// ignore_for_file: invalid_use_of_protected_member, invalid_use_of_visible_for_testing_member +import 'package:flutter/material.dart'; +import 'package:otzaria/tabs/bloc/tabs_event.dart'; +import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:otzaria/settings/engine/app_settings_store.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:otzaria/bookmarks/bloc/bookmark_bloc.dart'; +import 'package:otzaria/bookmarks/bloc/bookmark_state.dart'; +import 'package:otzaria/core/focus_repository.dart'; +import 'package:otzaria/data/data_providers/file_system_data_provider.dart'; +import 'package:otzaria/data/repository/data_repository.dart'; +import 'package:otzaria/library/models/library.dart'; +import 'package:otzaria/models/books.dart'; +import 'package:otzaria/models/links.dart'; +import 'package:otzaria/personal_notes/personal_notes_system.dart'; +import 'package:otzaria/search/models/search_configuration.dart'; +import 'package:otzaria/settings/engine/settings_bloc.dart'; +import 'package:otzaria/settings/engine/settings_state.dart'; +import 'package:otzaria/tabs/bloc/tabs_bloc.dart'; +import 'package:otzaria/tabs/bloc/tabs_state.dart'; +import 'package:otzaria/tabs/models/text_tab.dart'; +import 'package:otzaria/tabs/reading_screen.dart'; +import 'package:otzaria/history/bloc/history_bloc.dart'; +import 'package:otzaria/history/bloc/history_event.dart'; +import 'package:otzaria/history/bloc/history_state.dart'; +import 'package:otzaria/text_book/bloc/text_book_bloc.dart'; +import 'package:otzaria/text_book/bloc/text_book_event.dart'; +import 'package:otzaria/text_book/bloc/text_book_state.dart'; +import 'package:otzaria/data/repository/text_book_repository.dart'; +import 'package:otzaria/text_book/view/text_book_screen.dart'; +import 'package:otzaria/tools/shamor_zachor/providers/shamor_zachor_data_provider.dart'; +import 'package:otzaria/tools/shamor_zachor/providers/shamor_zachor_progress_provider.dart'; +import 'package:otzaria/tour/bloc/tour_cubit.dart'; +import 'package:otzaria/widgets/feedback/scrollable_positioned_list_scrollbar.dart'; +import 'package:provider/provider.dart'; + +import '../test_helpers/memory_cache_provider.dart'; +import '../test_helpers/stub_settings_bloc.dart'; + +/// issue #4173: מעבר בין טאבים במסך העיון הזיז את סמן פס הגלילה, כי טאב רקע +/// משחרר את תוכנו והערכות הגובה של השורות ששוחררו השתנו. +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + + final themeMode = ValueNotifier(ThemeMode.light); + + late List tabs; + late _TestTabsBloc tabsBloc; + + Future pumpReadingScreen( + WidgetTester tester, { + int tabCount = 3, + TextDirection direction = TextDirection.rtl, + }) async { + await Settings.init(cacheProvider: MemoryCacheProvider()); + DataRepository.instance.library = Future.value( + Library(categories: const []), + ); + final focusRepository = FocusRepository()..resetForTesting(); + final bookmarkBloc = _TestBookmarkBloc(); + final personalNotesBloc = PersonalNotesBloc(); + final tourCubit = TourCubit(); + final historyBloc = _FakeHistoryBloc(); + final settingsBloc = StubSettingsBloc(SettingsState.initial()); + tabs = [ + for (var i = 0; i < tabCount; i++) + () { + final book = TextBook(title: 'ספר $i'); + return TextBookTab( + book: book, + index: 0, + blocOverride: TextBookBloc( + repository: _FakeTextBookRepository(), + initialState: TextBookInitial.named(book, 0, true, const []), + )..emit(_loadedState(book)), + ); + }(), + ]; + tabsBloc = _TestTabsBloc(TabsState(tabs: tabs, currentTabIndex: 0)); + + tester.view.physicalSize = const Size(1600, 900); + tester.view.devicePixelRatio = 1.0; + addTearDown(tester.view.reset); + addTearDown(() => themeMode.value = ThemeMode.light); + addTearDown(() async { + await tester.pumpWidget(const SizedBox.shrink()); + await tester.pump(); + for (final tab in tabs) { + await tab.bloc.close(); + tab.dispose(); + } + await tabsBloc.close(); + await settingsBloc.close(); + await historyBloc.close(); + await bookmarkBloc.close(); + await personalNotesBloc.close(); + await tourCubit.close(); + focusRepository.resetForTesting(); + }); + + await tester.pumpWidget( + MultiProvider( + providers: [ + Provider.value(value: focusRepository), + ChangeNotifierProvider.value( + value: _FakeShamorZachorDataProvider(), + ), + ChangeNotifierProvider.value( + value: _FakeShamorZachorProgressProvider(), + ), + ], + child: MultiBlocProvider( + providers: [ + BlocProvider.value(value: tabsBloc), + BlocProvider.value(value: settingsBloc), + BlocProvider.value(value: bookmarkBloc), + BlocProvider.value(value: personalNotesBloc), + BlocProvider.value(value: tourCubit), + BlocProvider.value(value: historyBloc), + ], + child: ValueListenableBuilder( + valueListenable: themeMode, + builder: (context, mode, _) => MaterialApp( + theme: ThemeData(), + darkTheme: ThemeData.dark(), + themeMode: mode, + home: Directionality( + textDirection: direction, + child: const ReadingScreen(), + ), + ), + ), + ), + ), + ); + await tester.pump(const Duration(milliseconds: 500)); + // טווח שהוחל מוכיח שהספר נטען בטווחים, תנאי לשחרור התוכן בטאב רקע. + for (final tab in tabs) { + tab.bloc.add( + ApplyBookContentRanges( + bookTitle: tab.book.title, + ranges: [ + (startLine: 1500, totalLines: _lineCount, lines: [_line(1500)]), + ], + ), + ); + } + await tester.pump(); + // ביקור בכל טאב כדי שייבנה ויישמר חי ברקע. + for (var i = tabCount - 1; i >= 0; i--) { + await showTab(tester, i); + } + } + + testWidgets('חזרה לטאב שתוכנו שוחרר ברקע אינה מזיזה את סמן פס הגלילה', ( + tester, + ) async { + await pumpReadingScreen(tester, tabCount: 2); + final position = scrollPosition(tester, tabs[0]); + position.jumpTo(position.maxScrollExtent / 2); + await tester.pumpAndSettle(); + // ה-bloc שהוזרק אינו מאזין למיקום הקריאה של הטאב; מעבירים אותו כמו המאזין. + tabs[0].bloc.add( + UpdateVisibleIndecies(tabs[0].lines.position.value!.visibleLines), + ); + await tester.pumpAndSettle(); + final before = tester.getRect(thumbOf(tabs[0])); + + await showTab(tester, 1); + expect( + (tabs[0].bloc.state as TextBookLoaded).content.first, + isEmpty, + reason: 'טאב הרקע משחרר את התוכן שמחוץ לחלון', + ); + await showTab(tester, 0); + expect(tester.getRect(thumbOf(tabs[0])), before); + await tester.pumpAndSettle(); + expect(tester.getRect(thumbOf(tabs[0])), before); + }); +} + +Finder thumbOf(TextBookTab tab) => find.descendant( + of: find.byWidgetPredicate( + (w) => w is TextBookViewerBloc && identical(w.tab, tab), + skipOffstage: false, + ), + matching: find.byKey( + ScrollablePositionedListScrollbar.thumbKey, + skipOffstage: false, + ), +); + +ScrollPosition scrollPosition(WidgetTester tester, TextBookTab tab) => tester + .widget( + find + .descendant( + of: find.byWidgetPredicate( + (w) => w is TextBookViewerBloc && identical(w.tab, tab), + ), + matching: find.byType(ScrollablePositionedListScrollbar), + ) + .first, + ) + .scrollController + .scrollController! + .position; + +Future showTab(WidgetTester tester, int index) async { + final controller = tester + .widget(find.byKey(const ValueKey('normal_tab_view'))) + .controller!; + final bloc = + tester.element(find.byType(ReadingScreen)).read() + as _TestTabsBloc; + bloc.emit(TabsState(tabs: bloc.state.tabs, currentTabIndex: index)); + await tester.pump(); + controller.jumpToPage(index); + await tester.pump(); + await tester.pump(const Duration(milliseconds: 100)); +} + +class _FakeHistoryBloc extends Bloc + implements HistoryBloc { + _FakeHistoryBloc() : super(HistoryInitial()) { + on((event, emit) {}); + } + + @override + dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); +} + +TextBookLoaded _loadedState(TextBook book) { + return TextBookLoaded( + book: book, + showLeftPane: true, + content: [for (var i = 0; i < _lineCount; i++) _line(i)], + fontSize: 18, + showSplitView: false, + activeCommentators: const [], + commentatorGroups: const [], + availableCommentators: const [], + links: const [], + visibleLinks: const [], + linksByLine: const {}, + tableOfContents: const [], + removeNikud: false, + removePunctuation: false, + visibleIndices: const [0], + selectedIndex: 0, + pinLeftPane: true, + searchText: '', + currentTitle: 'סימן א', + searchMode: SearchMode.exact, + ); +} + +class _FakeTextBookRepository extends TextBookRepository { + _FakeTextBookRepository() : super(fileSystem: FileSystemData.instance); + + @override + Future getBookContentRange( + TextBook book, { + required int startLine, + required int endLine, + int? maxChars, + bool budgetFromEnd = false, + }) async { + final start = startLine.clamp(0, _lineCount - 1); + final end = endLine.clamp(start, _lineCount - 1); + return BookContentRange( + startLine: start, + endLine: end, + totalLines: _lineCount, + lines: [for (var i = start; i <= end; i++) _line(i)], + ); + } + + @override + Future getBookContent(TextBook book) async => + [for (var i = 0; i < _lineCount; i++) _line(i)].join('\n'); +} + +const _lineCount = 3000; + +/// שורות באורכים שונים, כמו בספר אמיתי. +String _line(int i) => 'שורה $i ' * (1 + i % 7 * 9); + +class _TestTabsBloc extends Cubit implements TabsBloc { + _TestTabsBloc(super.initialState); + + @override + void add(TabsEvent event) { + if (event is SetCurrentTab) { + emit(state.copyWith(currentTabIndex: event.index)); + } + } + + @override + dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); +} + +class _TestBookmarkBloc extends Cubit implements BookmarkBloc { + _TestBookmarkBloc() : super(BookmarkState.initial()); + + @override + dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); +} + +class _FakeShamorZachorDataProvider extends ShamorZachorDataProvider { + @override + bool get hasData => false; + + @override + Future ensureLoaded() async {} +} + +class _FakeShamorZachorProgressProvider extends ShamorZachorProgressProvider { + @override + Future ensureLoaded() async {} +} diff --git a/test/text_book/utils/reading_extent_estimates_test.dart b/test/text_book/utils/reading_extent_estimates_test.dart index a7fddc2649..8d2979c4d5 100644 --- a/test/text_book/utils/reading_extent_estimates_test.dart +++ b/test/text_book/utils/reading_extent_estimates_test.dart @@ -73,6 +73,37 @@ void main() { expect(data.needsRefresh, isFalse); }); + test('שחרור תוכן בטאב רקע אינו משנה את הערכות הגובה (issue #4173)', () async { + final identity = Object(); + final content = [for (var i = 0; i < 3000; i++) 'א' * (1 + i % 7 * 90)]; + // ספר שחומם בחלקו: 2000 השורות הראשונות נספרו, השאר טרם נטענו. + final data = ReadingExtentEstimates( + _segments( + content, + identity: identity, + version: 1, + flags: [for (var i = 0; i < 3000; i++) i < 2000], + ), + ); + addTearDown(data.dispose); + await data.refresh(); + const metrics = ReadingExtentMetrics(lineExtent: 20, charWidth: 8); + final items = [100, 1000, 2500]; + final before = [for (final i in items) data.estimate(i, 400, metrics)]; + + bool kept(int i) => (i - 1000).abs() <= 50; + data.update( + _segments( + [for (var i = 0; i < 3000; i++) kept(i) ? content[i] : ''], + identity: identity, + version: 2, + flags: [for (var i = 0; i < 3000; i++) kept(i)], + ), + ); + await data.refresh(); + expect([for (final i in items) data.estimate(i, 400, metrics)], before); + }); + test('מסיר רק סימנים משולבים ומשאיר פיסוק עברי', () { expect(visibleReadingCharCount('אַ֑ב־ג׀ד׃ה׆ו'), 'אב־ג׀ד׃ה׆ו'.length); expect(visibleReadingCharCount('e\u0301'), 1); From d0122e5e7e5d5376e976a16cb0a1c9d5ce2edcad Mon Sep 17 00:00:00 2001 From: dudua99 <99@9900.co.il> Date: Thu, 19 Nov 2026 15:36:02 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=D7=AA=D7=99=D7=A7=D7=95=D7=9F=20issue=20#4?= =?UTF-8?q?173=20=E2=80=94=20=D7=A1=D7=9E=D7=9F=20=D7=A4=D7=A1=20=D7=94?= =?UTF-8?q?=D7=92=D7=9C=D7=99=D7=9C=D7=94=20=D7=96=D7=96=20=D7=91=D7=9E?= =?UTF-8?q?=D7=A2=D7=91=D7=A8=20=D7=91=D7=99=D7=9F=20=D7=98=D7=90=D7=91?= =?UTF-8?q?=D7=99=D7=9D=20=D7=91=D7=9E=D7=A1=D7=9A=20=D7=94=D7=A2=D7=99?= =?UTF-8?q?=D7=95=D7=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit הסיבה: טאב רקע משחרר את תוכן הספר שמחוץ לחלון הקריאה (55fbd0aa5). מאז 43ff01191 הערכות הגובה של הרשימה נשענות על ספירת התווים של כל שורה, והשחרור מגיע כשינוי מלא בתוכן. הספירה מחדש סימנה כל שורה ששוחררה כ"לא נטענה" ומחקה את הספירה הידועה שלה, וגם הממוצע לשורות שלא נטענו חושב מחדש מחלון הקריאה בלבד. בחזרה לטאב כל ההערכות הוחלפו, אורך הרשימה המשוער השתנה והסמן קפץ; כשהתוכן נטען שוב הסמן חזר למקומו. התיקון: שורה ששוחררה שומרת את הספירה הידועה שלה, והממוצע לשורות שטרם נספרו מחושב מכל הספירות הידועות. ספירה חדשה של שורה טעונה מחליפה כרגיל, וספר אחר או שינוי באורך הספר מאפסים את הספירות כמו קודם. --- .../utils/reading_extent_estimates.dart | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/text_book/utils/reading_extent_estimates.dart b/lib/text_book/utils/reading_extent_estimates.dart index f6e26534e1..cb5125aea8 100644 --- a/lib/text_book/utils/reading_extent_estimates.dart +++ b/lib/text_book/utils/reading_extent_estimates.dart @@ -125,16 +125,23 @@ class ReadingExtentEstimates { if (_fullRefresh) continue; var cursor = 0; for (final range in ranges) { - final length = range.last - range.first + 1; - _counts.setRange(range.first, range.last + 1, result.counts, cursor); - cursor += length; + for (var line = range.first; line <= range.last; line++) { + final count = result.counts[cursor++]; + // שורה ששוחררה מהזיכרון (טאב רקע) לא השתנתה: הספירה הידועה נשמרת. + if (count >= 0) _counts[line] = count; + } } debugCountedLines += cursor; if (full) { replacedAll = true; - _unloadedLineChars = result.loaded == 0 - ? 200 - : result.chars / result.loaded; + var chars = 0; + var known = 0; + for (final count in _counts) { + if (count < 0) continue; + chars += count; + known++; + } + _unloadedLineChars = known == 0 ? 200 : chars / known; } applied.addAll(ranges); } From 13ab90273abe5011269de7a66fe9873127393dfa Mon Sep 17 00:00:00 2001 From: ypl <7353755@gmail.com> Date: Thu, 19 Nov 2026 22:03:36 +0200 Subject: [PATCH 3/3] =?UTF-8?q?=D7=92=D7=9C=D7=99=D7=9C=D7=94:=20=D7=A9?= =?UTF-8?q?=D7=99=D7=9E=D7=95=D7=A8=20=D7=94=D7=A2=D7=A8=D7=9B=D7=95=D7=AA?= =?UTF-8?q?=20=D7=91=D7=A4=D7=99=D7=A0=D7=95=D7=99=20=D7=95=D7=97=D7=99?= =?UTF-8?q?=D7=A9=D7=95=D7=91=20=D7=99=D7=A2=D7=99=D7=9C=20=D7=A9=D7=9C=20?= =?UTF-8?q?=D7=A1=D7=A4=D7=99=D7=A8=D7=95=D7=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/reading_extent_estimates.dart | 45 +++++++++++++------ .../utils/reading_extent_estimates_test.dart | 28 ++++++++++-- 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/lib/text_book/utils/reading_extent_estimates.dart b/lib/text_book/utils/reading_extent_estimates.dart index cb5125aea8..3089fd45a6 100644 --- a/lib/text_book/utils/reading_extent_estimates.dart +++ b/lib/text_book/utils/reading_extent_estimates.dart @@ -62,14 +62,19 @@ class ReadingExtentEstimates { } void update(ReadingSegments next) { - final changes = next.changedLinesSince(segments); + final changedReader = !identical( + next.readerIdentity, + segments.readerIdentity, + ); + final changes = changedReader ? null : next.changedLinesSince(segments); segments = next; if (changes == null) { _fullRefresh = true; _pendingRanges.clear(); - if (_counts.length != next.content.length) { + if (changedReader || _counts.length != next.content.length) { _counts = Int32List(next.content.length); _counts.fillRange(0, _counts.length, -1); + _unloadedLineChars = 200; } } else if (!_fullRefresh) { _pendingRanges.addAll(changes); @@ -123,25 +128,39 @@ class ReadingExtentEstimates { return (full: false, ranges: []); } if (_fullRefresh) continue; + final hasUnloaded = result.loaded != result.counts.length; + var meanMayChange = !hasUnloaded; + var known = result.loaded; + var chars = result.chars; var cursor = 0; for (final range in ranges) { - for (var line = range.first; line <= range.last; line++) { - final count = result.counts[cursor++]; - // שורה ששוחררה מהזיכרון (טאב רקע) לא השתנתה: הספירה הידועה נשמרת. - if (count >= 0) _counts[line] = count; + final length = range.last - range.first + 1; + if (hasUnloaded) { + for (var i = 0; i < length; i++) { + final previous = _counts[range.first + i]; + final count = result.counts[cursor + i]; + if (count < 0 && previous >= 0) { + result.counts[cursor + i] = previous; + known++; + chars += previous; + } else if (count >= 0 && count != previous) { + meanMayChange = true; + } + } + } + if (!full) { + _counts.setRange(range.first, range.last + 1, result.counts, cursor); } + cursor += length; } debugCountedLines += cursor; if (full) { replacedAll = true; - var chars = 0; - var known = 0; - for (final count in _counts) { - if (count < 0) continue; - chars += count; - known++; + _counts = result.counts; + // שחרור תוכן בלבד לא משנה את ברירת המחדל שנקבעה לפני החימום. + if (meanMayChange) { + _unloadedLineChars = known == 0 ? 200 : chars / known; } - _unloadedLineChars = known == 0 ? 200 : chars / known; } applied.addAll(ranges); } diff --git a/test/text_book/utils/reading_extent_estimates_test.dart b/test/text_book/utils/reading_extent_estimates_test.dart index 8d2979c4d5..14921167b5 100644 --- a/test/text_book/utils/reading_extent_estimates_test.dart +++ b/test/text_book/utils/reading_extent_estimates_test.dart @@ -87,16 +87,31 @@ void main() { ); addTearDown(data.dispose); await data.refresh(); + // חימום הדרגתי משמר את הערכת השורות שטרם נטענו גם כשהממוצע השתנה. + final warmed = List.of(content)..fillRange(2000, 2700, 'א' * 1000); + data.update( + _segments( + warmed, + identity: identity, + version: 2, + flags: [for (var i = 0; i < 3000; i++) i < 2700], + changes: ContentLineChanges( + fromVersion: 1, + ranges: [(first: 2000, last: 2699)], + ), + ), + ); + expect((await data.refresh()).full, isFalse); const metrics = ReadingExtentMetrics(lineExtent: 20, charWidth: 8); - final items = [100, 1000, 2500]; + final items = [100, 1000, 2500, 2800]; final before = [for (final i in items) data.estimate(i, 400, metrics)]; bool kept(int i) => (i - 1000).abs() <= 50; data.update( _segments( - [for (var i = 0; i < 3000; i++) kept(i) ? content[i] : ''], + [for (var i = 0; i < 3000; i++) kept(i) ? warmed[i] : ''], identity: identity, - version: 2, + version: 3, flags: [for (var i = 0; i < 3000; i++) kept(i)], ), ); @@ -292,7 +307,8 @@ void main() { expect((await data.refresh()).full, isTrue); expect(data.debugCountedLines, 40); final other = _segments( - after.content, + ['', for (var i = 0; i < 19; i++) 'א' * 100], + flags: [false, ...List.filled(19, true)], identity: Object(), version: 4, changes: ContentLineChanges( @@ -301,6 +317,10 @@ void main() { ), ); expect(other.changedLinesSince(after), isNull); + data.update(other); + expect((await data.refresh()).full, isTrue); + const metrics = ReadingExtentMetrics(lineExtent: 20, charWidth: 10); + expect(data.estimate(0, 100, metrics), 204); }); for (final textWidth in [null, (w) => w / 2]) {