Skip to content
Merged
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
35 changes: 25 additions & 10 deletions lib/search/utils/scope_tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,33 @@ class ScopeTree {

bool isAllSelected(Set<String> selection) => selection.contains('/');

/// מחזיר true/false/null (tristate) למצב הסימון של קטגוריה לפי נתיבה.
bool? categoryCheckState(String categoryPath, Set<String> selection) {
if (isFacetCovered(categoryPath, selection)) return true;
if (_hasSelectedDescendant(categoryPath, selection)) return null;
return false;
}

bool _hasSelectedDescendant(String categoryPath, Set<String> selection) {
/// נתיבי קטגוריות עם צאצא נבחר, לשימוש חוזר באותה בחירה.
static Set<String> ancestorsOfSelectedFacets(Set<String> selection) {
final ancestors = <String>{};
for (final facet in selection) {
if (facet.startsWith('$categoryPath/')) return true;
var separator = facet.lastIndexOf('/');
while (separator >= 0) {
if (!ancestors.add(facet.substring(0, separator)) || separator == 0) {
break;
}
separator = facet.lastIndexOf('/', separator - 1);
}
}
return false;
return ancestors;
}

/// מחזיר true/false/null (tristate) למצב הסימון של קטגוריה לפי נתיבה.
/// בסדרת בדיקות אפשר לספק [selectedAncestors] שחושב פעם אחת מהבחירה.
bool? categoryCheckState(
String categoryPath,
Set<String> selection, {
Set<String>? selectedAncestors,
}) {
if (isFacetCovered(categoryPath, selection)) return true;
final hasSelectedDescendant =
selectedAncestors?.contains(categoryPath) ??
selection.any((facet) => facet.startsWith('$categoryPath/'));
return hasSelectedDescendant ? null : false;
}

/// האם [facet] מכוסה ע"י הבחירה (נבחר ישירות, או אב שלו נבחר, או הכל).
Expand Down
32 changes: 25 additions & 7 deletions lib/search/view/search_scope_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,8 @@ class _ScopeMenuPanelState extends State<_ScopeMenuPanel> {
final ScrollController _scrollController = ScrollController();

late Set<String> _selection;
Set<String>? _cachedCategoryPart;
Set<String>? _cachedCategoryAncestors;
_View _view = _View.root;
final List<ScopeNode> _stack = [];
int _highlight = 0;
Expand Down Expand Up @@ -761,7 +763,7 @@ class _ScopeMenuPanelState extends State<_ScopeMenuPanel> {
final incoming = widget.selected;
if (incoming.length != _selection.length ||
!incoming.containsAll(_selection)) {
_selection = Set<String>.from(incoming);
_replaceSelection(Set<String>.from(incoming));
}
if (_hasActiveSearch) {
widget.onRequireTree();
Expand Down Expand Up @@ -792,8 +794,19 @@ class _ScopeMenuPanelState extends State<_ScopeMenuPanel> {
normalizeFindQuery(widget.searchController.text);
bool get _hasActiveSearch => _normalizedQuery.length >= _minSearchQueryLength;

Set<String> get _categoryPart =>
FacetHelper.categoryFacetsOf(_selection).toSet();
Set<String> get _categoryPart => _cachedCategoryPart ??= _selection
.where((facet) => !FacetHelper.isDimensionFacet(facet))
.toSet();

Set<String> get _categoryAncestors => _cachedCategoryAncestors ??=
ScopeTree.ancestorsOfSelectedFacets(_categoryPart);

void _replaceSelection(Set<String> next) {
_selection = next;
_cachedCategoryPart = null;
_cachedCategoryAncestors = null;
}

Set<String> get _dimensionPart =>
FacetHelper.dimensionFacetsOf(_selection).toSet();

Expand Down Expand Up @@ -825,7 +838,7 @@ class _ScopeMenuPanelState extends State<_ScopeMenuPanel> {
}

void _apply(Set<String> next) {
setState(() => _selection = next);
setState(() => _replaceSelection(next));
widget.onChanged(Set<String>.from(next));
widget.onKeepFocus();
}
Expand All @@ -837,9 +850,10 @@ class _ScopeMenuPanelState extends State<_ScopeMenuPanel> {
void _toggleCategoryFacet(String facet, bool select) {
final tree = widget.tree;
if (tree == null) return;
final categoryPart = Set<String>.of(_categoryPart);
final nextCategory = select
? tree.selectFacet(facet, _categoryPart)
: tree.deselectFacet(facet, _categoryPart);
? tree.selectFacet(facet, categoryPart)
: tree.deselectFacet(facet, categoryPart);
_setCategorySelection(nextCategory);
}

Expand Down Expand Up @@ -994,7 +1008,11 @@ class _ScopeMenuPanelState extends State<_ScopeMenuPanel> {
label: node.title,
icon: FluentIcons.folder_24_regular,
// סימון תיקיה בוחר את התיקיה כולה; לחיצה על השורה נכנסת פנימה (מעבר מסך).
check: tree.categoryCheckState(node.facet, _categoryPart),
check: tree.categoryCheckState(
node.facet,
_categoryPart,
selectedAncestors: _categoryAncestors,
),
onToggle: (v) => _toggleCategoryFacet(node.facet, v),
onDrill: () => _drillInto(node),
);
Expand Down
32 changes: 32 additions & 0 deletions test/search/scope_tree_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,38 @@ void main() {
expect(selection.lookups, lessThan(12));
});

test('אינדקס אבות שומר על הסימון בלי לסרוק שוב את הבחירה', () {
final selection = _CountingSelection({
for (var i = 0; i < 5000; i++) '/תיקייה$i/מדף/ספר',
'/מדרש/הלכה',
'/מדרש/אגדה',
'/תנ״ך',
});
final ancestors = ScopeTree.ancestorsOfSelectedFacets(selection);
final iterationsAfterIndex = selection.iterations;

final expected = <String, bool?>{
'/מדרש': null,
'/מדרש/הלכה': true,
'/מדרשים': false,
'/תנ״ך/תורה': true,
'/תיקייה4999/מדף': null,
'/תיקייה5000': false,
};
for (final entry in expected.entries) {
expect(
tree.categoryCheckState(
entry.key,
selection,
selectedAncestors: ancestors,
),
entry.value,
);
}
expect(iterationsAfterIndex, 1);
expect(selection.iterations, iterationsAfterIndex);
});

test('כיסוי נעצר בגבול מקטע ומשמר תחום שורש', () {
expect(tree.isFacetCovered('/מדרשים/הלכה', {'/מדרש'}), isFalse);
expect(tree.isFacetCovered('/מדרש', {'/מדרש/הלכה'}), isFalse);
Expand Down
38 changes: 33 additions & 5 deletions test/search/search_scope_menu_partial_check_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ Library _buildLibrary() {

Future<void> main() async {
final engineReady = await tryInitSearchEngine();
Future<void> pumpMenu(WidgetTester tester, Set<String> selected) async {
Future<ValueChanged<Set<String>>> pumpMenu(
WidgetTester tester,
Set<String> selected,
) async {
final libraryBloc = _MockLibraryBloc();
whenListen(
libraryBloc,
Expand All @@ -57,6 +60,7 @@ Future<void> main() async {

// רכיב נשלט: הבחירה מוזנת חזרה דרך onChanged, כמו בדיאלוג האמיתי.
var selection = selected;
late ValueChanged<Set<String>> updateSelection;
await tester.binding.setSurfaceSize(const Size(600, 700));
await tester.pumpWidget(
MaterialApp(
Expand All @@ -65,10 +69,13 @@ Future<void> main() async {
value: libraryBloc,
child: Scaffold(
body: StatefulBuilder(
builder: (context, setState) => SearchScopeMenuButton(
selected: selection,
onChanged: (next) => setState(() => selection = next),
),
builder: (context, setState) {
updateSelection = (next) => setState(() => selection = next);
return SearchScopeMenuButton(
selected: selection,
onChanged: updateSelection,
);
},
),
),
),
Expand All @@ -77,6 +84,7 @@ Future<void> main() async {
await tester.tap(find.byType(TextField));
await tester.pump();
await tester.pump(const Duration(milliseconds: 50));
return updateSelection;
}

/// ה-finder של התיבה שבאותה שורה של [label] בתפריט הפתוח.
Expand Down Expand Up @@ -120,6 +128,26 @@ Future<void> main() async {
expect(checkboxOf(tester, 'תנ״ך').value, isNull);
});

testWidgets('עדכון בחירה חיצוני מרענן תיבה פתוחה במצב ביניים', (
tester,
) async {
final updateSelection = await pumpMenu(tester, {'/תנ״ך/תורה/בראשית'});
await drillIntoAllBooks(tester);
expect(checkboxOf(tester, 'תנ״ך').value, isNull);

updateSelection({'/מדרשים/ספר'});
await tester.pump();
expect(checkboxOf(tester, 'תנ״ך').value, isFalse);

updateSelection({'/תנ״ך/תורה/בראשית', '/base'});
await tester.pump();
expect(checkboxOf(tester, 'תנ״ך').value, isNull);

await tester.tap(checkboxFinder('תנ״ך'));
await tester.pump();
expect(checkboxOf(tester, 'תנ״ך').value, isTrue);
});

testWidgets('לחיצה על התיבה של "כל הספרים" מבטלת את הסימון', (
tester,
) async {
Expand Down
Loading