Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions lib/text_book/utils/split_book_parts.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/// ספרי המקרא שחלוקתם לשניים אינה מן המסורה — שמואל, מלכים, דברי הימים,
/// ועזרא ונחמיה (#2786, שלב 2): הקריאה ממשיכה ברצף מסוף החלק הראשון לתחילת
/// השני, ומתחילת השני לסוף הראשון. כל חלק נשאר ספר בפני עצמו במסד, ולכן
/// המפרשים, ההערות והסימניות אינם משתנים.
library;

import 'dart:math' as math;

/// החלק הראשון ← החלק השני.
const Map<String, String> kSplitBookNextPart = {
'שמואל א': 'שמואל ב',
'מלכים א': 'מלכים ב',
'דברי הימים א': 'דברי הימים ב',
'עזרא': 'נחמיה',
};

/// החלק שאחרי [title], או null.
String? splitBookNextPart(String title) => kSplitBookNextPart[title];

/// החלק שלפני [title], או null.
String? splitBookPreviousPart(String title) {
for (final entry in kSplitBookNextPart.entries) {
if (entry.value == title) return entry.key;
}
return null;
}

enum SplitBookStep { next, previous }

/// מזהה דחיפה מכוונת אל מעבר לקצה הספר: גלילה (גלגלת או גרירה) בכיוון הקצה
/// כשהרשימה כבר בו, שמצטברת עד [threshold] פיקסלים. גלילה לכיוון אחר, או
/// כשהרשימה אינה בקצה, מאפסת את הצבירה — כדי שגלילה מקרית לא תעביר ספר.
class SplitBookEdgeTracker {
SplitBookEdgeTracker({this.threshold = 160});

final double threshold;
double _pushed = 0;

/// [delta] חיובי — למטה (אל סוף הספר), שלילי — למעלה.
SplitBookStep? push(
double delta, {
required bool atStart,
required bool atEnd,
}) {
if (delta > 0 && atEnd) {
_pushed = math.max(0, _pushed) + delta;
if (_pushed >= threshold) {
_pushed = 0;
return SplitBookStep.next;
}
} else if (delta < 0 && atStart) {
_pushed = math.min(0, _pushed) + delta;
if (_pushed <= -threshold) {
_pushed = 0;
return SplitBookStep.previous;
}
} else {
_pushed = 0;
}
return null;
}

void reset() => _pushed = 0;
}
8 changes: 7 additions & 1 deletion lib/text_book/view/combined_view/combined_book_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:flutter/gestures.dart' show kPrimaryMouseButton;
import 'package:flutter/material.dart';
import 'package:otzaria/text_book/view/reading_viewport/line_scroll_controller.dart';
import 'package:otzaria/text_book/view/reading_viewport/reading_scrollbar_label.dart';
import 'package:otzaria/text_book/view/split_book_edge_scroll.dart';

import 'package:flutter/services.dart';
import 'package:flutter/rendering.dart';
Expand Down Expand Up @@ -2742,7 +2743,12 @@ class _CombinedViewState extends State<CombinedView> {
if (notification.depth == 0) _cancelCommentaryReveal();
return false;
},
child: widget.tab.lines.trackUserScroll(child),
child: SplitBookEdgeScroll(
tab: widget.tab,
content: widget.data,
enabled: !widget.isPreviewMode,
child: widget.tab.lines.trackUserScroll(child),
),
),
);

Expand Down
70 changes: 40 additions & 30 deletions lib/text_book/view/page_shape/page_shape_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:collection';
import 'dart:math' as math;

import 'package:otzaria/text_book/view/split_book_edge_scroll.dart';
import 'package:otzaria/text_book/view/page_shape/utils/page_shape_default_commentators.dart';
import 'package:otzaria/book_common/utils/commentator_name_matching.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -1342,36 +1343,45 @@ class _PageShapeScreenState extends State<PageShapeScreen>
},
);

/// הטקסט הראשי; לחיצה בו ממקדת את הערות הספר.
Widget _buildMainText(TextBookLoaded state) => Listener(
onPointerDown: (_) => _focusMainBookNotes(),
child: SimpleTextViewer(
key: _mainTextViewerKey,
content: state.content,
fontSize: state.fontSize,
selectionSyncController: _selectionSyncController,
openBookCallback: widget.openBookCallback,
lines: widget.tab?.lines,
isMainText: true,
isPersonalNotesTabActive:
_isLeftSidebarOpen && _leftSidebarTabIndex == kNotesTabIndex,
isCommentatorsTabActive:
_isLeftSidebarOpen && _leftSidebarTabIndex == kCommentaryTabIndex,
isLinksTabActive:
_isLeftSidebarOpen && _leftSidebarTabIndex == kLinksTabIndex,
onOpenCommentatorsPane: _openCommentatorsPane,
onOpenCommentatorsPaneWithFilter: _openCommentatorsPaneWithFilter,
onCommentatorPicked: _requestCommentaryScroll,
tab: widget.tab,
labelForIndex: state.tableOfContents.isEmpty
? null
: (index) => readingScrollbarLabel(
state,
index,
),
onOpenSidebarTab: _openLeftSidebarTab,
onOpenSearch: widget.onOpenSearch,
),
/// הטקסט הראשי; לחיצה בו ממקדת את הערות הספר. בספר מקרא מחולק — רצף
/// הגלילה אל החלק הסמוך, כבמסך העיון.
Widget _buildMainText(TextBookLoaded state) {
final tab = widget.tab;
final text = _buildMainTextViewer(state);
return Listener(
onPointerDown: (_) => _focusMainBookNotes(),
child: tab == null
? text
: SplitBookEdgeScroll(tab: tab, content: state.content, child: text),
);
}

Widget _buildMainTextViewer(TextBookLoaded state) => SimpleTextViewer(
key: _mainTextViewerKey,
content: state.content,
fontSize: state.fontSize,
selectionSyncController: _selectionSyncController,
openBookCallback: widget.openBookCallback,
lines: widget.tab?.lines,
isMainText: true,
isPersonalNotesTabActive:
_isLeftSidebarOpen && _leftSidebarTabIndex == kNotesTabIndex,
isCommentatorsTabActive:
_isLeftSidebarOpen && _leftSidebarTabIndex == kCommentaryTabIndex,
isLinksTabActive:
_isLeftSidebarOpen && _leftSidebarTabIndex == kLinksTabIndex,
onOpenCommentatorsPane: _openCommentatorsPane,
onOpenCommentatorsPaneWithFilter: _openCommentatorsPaneWithFilter,
onCommentatorPicked: _requestCommentaryScroll,
tab: widget.tab,
labelForIndex: state.tableOfContents.isEmpty
? null
: (index) => readingScrollbarLabel(
state,
index,
),
onOpenSidebarTab: _openLeftSidebarTab,
onOpenSearch: widget.onOpenSearch,
);

/// פאנל הגדרות צורת הדף — overlay צף בצד הימני הויזואלי.
Expand Down
148 changes: 148 additions & 0 deletions lib/text_book/view/split_book_edge_scroll.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import 'package:flutter/gestures.dart' show PointerScrollEvent;
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:otzaria/data/data_providers/file_system_data_provider.dart';
import 'package:otzaria/data/repository/data_repository.dart';
import 'package:otzaria/data/repository/text_book_repository.dart';
import 'package:otzaria/models/books.dart';
import 'package:otzaria/tabs/bloc/tabs_bloc.dart';
import 'package:otzaria/tabs/bloc/tabs_event.dart';
import 'package:otzaria/tabs/models/text_tab.dart';
import 'package:otzaria/text_book/bloc/text_book_bloc.dart';
import 'package:otzaria/text_book/bloc/text_book_state.dart';
import 'package:otzaria/text_book/utils/reading_position.dart';
import 'package:otzaria/text_book/utils/split_book_parts.dart';
import 'package:otzaria/utils/navigation/talmud_bavli_open_format.dart';

/// ספר מקרא מחולק (שמואל, מלכים, דברי הימים, עזרא ונחמיה): דחיפה אל מעבר
/// לסוף החלק הראשון ממשיכה לתחילת השני באותו טאב, ומעבר לתחילת השני — לסוף
/// הראשון. עוטף את רשימת הטקסט הראשי — במסך העיון ובצורת הדף.
class SplitBookEdgeScroll extends StatefulWidget {
const SplitBookEdgeScroll({
super.key,
required this.tab,
required this.content,
required this.child,
this.enabled = true,
});

final TextBookTab tab;

/// התוכן שהרשימה מציגה; כשאינו תוכן הספר שבמצב — אין מעבר.
final List<String> content;
final bool enabled;
final Widget child;

@override
State<SplitBookEdgeScroll> createState() => _SplitBookEdgeScrollState();
}

/// החלק [title] שבקטגוריה של [current], ומספר שורותיו; null — אין.
typedef SplitBookPartLoader =
Future<({TextBook book, int totalLines})?> Function(
String title,
TextBook current,
);

/// מאתר את החלק בספרייה ומונה את שורותיו מן המסד. מבחנים מחליפים אותו.
@visibleForTesting
SplitBookPartLoader splitBookPartLoader = _loadPart;

Future<({TextBook book, int totalLines})?> _loadPart(
String title,
TextBook current,
) async {
final library = await DataRepository.instance.library;
final book = findTextBookByExactCategory(library, title, current.categoryId);
if (book == null) return null;
final range = await TextBookRepository(
fileSystem: FileSystemData.instance,
).getBookContentRange(book, startLine: 0, endLine: 0);
return (book: book, totalLines: range?.totalLines ?? 0);
}

class _SplitBookEdgeScrollState extends State<SplitBookEdgeScroll> {
final SplitBookEdgeTracker _edge = SplitBookEdgeTracker();
bool _opening = false;

@override
Widget build(BuildContext context) => Listener(
onPointerSignal: (event) {
if (event is PointerScrollEvent) _onEdgeScroll(event.scrollDelta.dy);
},
child: NotificationListener<OverscrollNotification>(
onNotification: (notification) {
// גרירה (מגע) אל מעבר לקצה; הגלגלת מטופלת ב-onPointerSignal.
if (notification.dragDetails != null &&
notification.metrics.axis == Axis.vertical) {
_onEdgeScroll(notification.overscroll);
}
return false;
},
child: widget.child,
),
);

void _onEdgeScroll(double delta) {
if (_opening || !widget.enabled) return;
final state = context.read<TextBookBloc>().state;
if (state is! TextBookLoaded || !state.isTanach) return;
if (!identical(widget.content, state.content)) return;
final title = state.book.title;
final next = splitBookNextPart(title);
final previous = splitBookPreviousPart(title);
if (next == null && previous == null) return;
final scroll = widget.tab.lines.itemController.scrollController;
if (scroll == null || !scroll.hasClients) return;
final position = scroll.position;
final step = _edge.push(
delta,
atStart: previous != null && position.extentBefore <= 0.5,
atEnd: next != null && position.extentAfter <= 0.5,
);
if (step == null) return;
_openPart(
state,
step == SplitBookStep.next ? next! : previous!,
toEnd: step == SplitBookStep.previous,
);
}

Future<void> _openPart(
TextBookLoaded state,
String title, {
required bool toEnd,
}) async {
_opening = true;
try {
final part = await splitBookPartLoader(title, state.book);
if (part == null || !mounted) return;
final book = part.book;
var line = 0;
if (toEnd) {
if (part.totalLines <= 0) return;
line = part.totalLines - 1;
}
final tab = TextBookTab(
book: book,
index: line,
// אחורה — בסוף החלק הראשון, השורה האחרונה בתחתית המסך.
initialReadingAnchor: toEnd
? ReadingAnchor(line: line, alignment: 1)
: null,
commentators: List.of(state.activeCommentators),
openLeftPane: state.showLeftPane,
splitedView: state.showSplitView,
// צורת הדף נשמרת במעבר — החלק הבא נפתח כמו שהיה הקודם.
showPageShapeView: state.showPageShapeView,
isPinned: widget.tab.isPinned,
);
if (!mounted) return;
context.read<TabsBloc>().add(
ReplaceTab(oldTab: widget.tab, newTab: tab),
);
} finally {
_opening = false;
}
}
}
46 changes: 46 additions & 0 deletions test/text_book/utils/split_book_parts_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/// רצף הגלילה בספרי המקרא המחולקים (#2786, שלב 2).
library;

import 'package:flutter_test/flutter_test.dart';
import 'package:otzaria/text_book/utils/split_book_parts.dart';

void main() {
test('החלק הבא והקודם — רק בארבעת הספרים המחולקים', () {
expect(splitBookNextPart('שמואל א'), 'שמואל ב');
expect(splitBookNextPart('מלכים א'), 'מלכים ב');
expect(splitBookNextPart('דברי הימים א'), 'דברי הימים ב');
expect(splitBookNextPart('עזרא'), 'נחמיה');
expect(splitBookPreviousPart('נחמיה'), 'עזרא');
expect(splitBookPreviousPart('שמואל ב'), 'שמואל א');
expect(splitBookNextPart('שמואל ב'), isNull);
expect(splitBookPreviousPart('שמואל א'), isNull);
expect(splitBookNextPart('בראשית'), isNull);
expect(splitBookNextPart('ישעיהו'), isNull);
});

group('SplitBookEdgeTracker', () {
test('דחיפה מצטברת בקצה — מעבר אחד, ואחריו מתחילים מחדש', () {
final edge = SplitBookEdgeTracker(threshold: 100);
expect(edge.push(60, atStart: false, atEnd: true), isNull);
expect(edge.push(60, atStart: false, atEnd: true), SplitBookStep.next);
expect(edge.push(60, atStart: false, atEnd: true), isNull);
});

test('למעלה בתחילת הספר — החלק הקודם', () {
final edge = SplitBookEdgeTracker(threshold: 100);
expect(
edge.push(-120, atStart: true, atEnd: false),
SplitBookStep.previous,
);
});

test('לא בקצה, או כיוון הפוך — מאפס', () {
final edge = SplitBookEdgeTracker(threshold: 100);
expect(edge.push(90, atStart: false, atEnd: true), isNull);
expect(edge.push(-5, atStart: false, atEnd: true), isNull);
expect(edge.push(90, atStart: false, atEnd: true), isNull);
expect(edge.push(90, atStart: false, atEnd: false), isNull);
expect(edge.push(90, atStart: false, atEnd: true), isNull);
});
});
}
Loading
Loading