Skip to content
Draft
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
30 changes: 7 additions & 23 deletions lib/library/view/category_details_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/material.dart';
import 'package:otzaria/book_common/view/book_source_dialog.dart'
show splitShortAndFullDescription;
import 'package:otzaria/library/models/library.dart';
import 'package:otzaria/library/view/shelf_description.dart';
import 'package:otzaria/theme/app_tokens.dart';
import 'package:otzaria/widgets/dialogs/dialogs_exports.dart';
import 'package:otzaria/widgets/misc/app_selection_area.dart';
Expand All @@ -15,7 +14,7 @@ Future<void> showCategoryDetailsDialog(
) async {
await showSingleActionDialog(
context: context,
title: 'אודות הקטגוריה',
title: 'תיאור המדף',
icon: FluentIcons.folder_24_regular,
size: AppDialogSize.medium,
confirmText: 'סגור',
Expand Down Expand Up @@ -46,16 +45,6 @@ class _CategoryDetailsDialogContentState
@override
Widget build(BuildContext context) {
final category = widget.category;
final shortDescription = category.shortDescription.trim();
// התיאור הקצר כבר בכותרת; כשהמורחב מתחיל בו, בכרטיס נשאר רק ההמשך.
final split = splitShortAndFullDescription(
shortDescription,
category.description,
);
final fullDescription = split == null
? category.description.trim()
: split.rest;

// פס גלילה קבוע: תיאור מורחב ארוך נחתך בתחתית הדיאלוג, ובלי פס אין סימן
// שיש המשך.
return SizedBox(
Expand All @@ -70,17 +59,12 @@ class _CategoryDetailsDialogContentState
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
DetailsHeading(
title: category.title,
subtitle: shortDescription,
),
if (fullDescription.isNotEmpty) ...[
DetailsHeading(title: category.title),
// הקצר (מודגש) והארוך ברצף, תחת כותרת אחת. כשזה כל התוכן אין
// צורך בכותרת מקטע.
if (categoryHasDescription(category)) ...[
const SizedBox(height: AppTokens.spaceMD),
DetailsCard(
title: 'תיאור',
icon: FluentIcons.document_text_24_regular,
children: [DetailsText(fullDescription)],
),
ShelfDescription(category: category),
],
],
),
Expand Down
15 changes: 8 additions & 7 deletions lib/library/view/category_preview_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:positioned_sliver_list/positioned_sliver_list.dart'
show SuperListView;
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:otzaria/library/models/library.dart';
import 'package:otzaria/library/view/grid_items.dart';
import 'package:otzaria/library/view/shelf_description.dart';
import 'package:otzaria/models/books.dart';
import 'package:otzaria/theme/app_tokens.dart';
import 'package:otzaria/utils/ui/book_format_icon.dart';
Expand Down Expand Up @@ -40,7 +40,7 @@ class CategoryPreviewPanel extends StatelessWidget {
Widget build(BuildContext context) {
final theme = Theme.of(context);
final cs = theme.colorScheme;
final description = categoryInfoText(category);
final hasDescription = categoryHasDescription(category);
final counts = categoryContentCountsText(
subCategories: subCategories.length,
books: books.length,
Expand All @@ -67,11 +67,11 @@ class CategoryPreviewPanel extends StatelessWidget {
padding: headerInset,
child: _header(theme, cs),
),
if (description != null) ...[
if (hasDescription) ...[
const SizedBox(height: 16),
Padding(
padding: headerInset,
child: Text(description, style: theme.textTheme.bodyMedium),
child: ShelfDescription(category: category),
),
],
if (counts != null) ...[
Expand All @@ -80,9 +80,10 @@ class CategoryPreviewPanel extends StatelessWidget {
padding: headerInset,
child: Text(
counts,
style: AppTextStyles.settingTitle.copyWith(
fontSize: AppTokens.fontMD,
color: cs.onSurfaceVariant,
// שורת סיכום: קטנה, דקה ואפורה.
style: theme.textTheme.bodySmall?.copyWith(
fontWeight: FontWeight.w300,
color: cs.outline,
),
),
),
Expand Down
102 changes: 102 additions & 0 deletions lib/library/view/shelf_description.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import 'package:flutter/material.dart';
import 'package:otzaria/book_common/view/book_source_dialog.dart'
show splitShortAndFullDescription;
import 'package:otzaria/library/models/library.dart';
import 'package:otzaria/theme/app_tokens.dart';

/// מחלק את תיאור הקטגוריה לקצר ([lead]) ולארוך ([paragraphs], פסקה לכל שורה).
///
/// ארוך שפותח בקצר מוצג בלי הקצר שבראשו, וארוך הזהה לקצר אינו חוזר.
@visibleForTesting
({String lead, List<String> paragraphs}) categoryDescriptionParts(
Category category,
) {
final lead = category.shortDescription.trim();
final full = category.description.trim();
final split = splitShortAndFullDescription(lead, full);
final rest = split?.rest ?? (full == lead ? '' : full);
final paragraphs = rest
.split('\n')
.map((paragraph) => paragraph.trim())
.where((paragraph) => paragraph.isNotEmpty)
.toList();
return (lead: lead, paragraphs: paragraphs);
}

/// האם לקטגוריה יש תיאור כלשהו להצגה.
bool categoryHasDescription(Category category) {
final parts = categoryDescriptionParts(category);
return parts.lead.isNotEmpty || parts.paragraphs.isNotEmpty;
}

final _bracketed = RegExp(r'\([^()]*\)|\[[^\[\]]*\]');

/// מפרק טקסט לקטעים: סוגריים עגולות מוקטנות משמעותית ובלי הדגשה, ומרובעות
/// מוקטנות מעט. [fontSize] הוא גודל הטקסט שסביבן.
@visibleForTesting
List<TextSpan> shelfDescriptionSpans(String text, double fontSize) {
final spans = <TextSpan>[];
var from = 0;
for (final match in _bracketed.allMatches(text)) {
if (match.start > from) {
spans.add(TextSpan(text: text.substring(from, match.start)));
}
final round = text.startsWith('(', match.start);
spans.add(
TextSpan(
text: match.group(0),
style: round
? TextStyle(fontSize: fontSize * 0.76, fontWeight: FontWeight.w400)
: TextStyle(fontSize: fontSize * 0.92),
),
);
from = match.end;
}
if (from < text.length) spans.add(TextSpan(text: text.substring(from)));
return spans;
}

/// תיאור קטגוריה ברצף: פסקה לקצר (מודגש), ואחריה פסקה לכל שורה בארוך.
class ShelfDescription extends StatelessWidget {
const ShelfDescription({
super.key,
required this.category,
this.fontSize = AppTokens.fontMD,
});

final Category category;
final double fontSize;

@override
Widget build(BuildContext context) {
final parts = categoryDescriptionParts(category);
final color = Theme.of(context).colorScheme.onSurface;

Widget paragraph(String text, {bool bold = false}) => Text.rich(
TextSpan(
style: TextStyle(
fontSize: fontSize,
height: 1.4,
color: color,
fontWeight: bold ? FontWeight.bold : null,
),
children: shelfDescriptionSpans(text, fontSize),
),
);

final paragraphs = <Widget>[
if (parts.lead.isNotEmpty) paragraph(parts.lead, bold: true),
for (final text in parts.paragraphs) paragraph(text),
];
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
for (var i = 0; i < paragraphs.length; i++) ...[
if (i > 0) const SizedBox(height: AppTokens.spaceSM),
paragraphs[i],
],
],
);
}
}
16 changes: 8 additions & 8 deletions test/library/view/grid_items_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ void main() {
Future<void> closeDialog(WidgetTester tester) async {
await tester.tap(find.text('סגור'));
await tester.pumpAndSettle();
expect(find.text('אודות הקטגוריה'), findsNothing);
expect(find.text('תיאור המדף'), findsNothing);
}

testWidgets('קצר וארוך: הקצר אינו בכרטיס (זמני), הארוך בריחוף ובדיאלוג', (
Expand All @@ -439,11 +439,11 @@ void main() {
await tester.tap(infoButton());
await tester.pumpAndSettle();

expect(find.text('אודות הקטגוריה'), findsOneWidget);
// הכותרת: שם הקטגוריה ומתחתיו התיאור הקצר; התיאור המורחב בכרטיס.
expect(find.text('תיאור המדף'), findsOneWidget);
// הכותרת: שם הקטגוריה; ומתחתיה הקצר והארוך ברצף, בלי כותרת מקטע.
expect(find.text('קטגוריית בדיקה'), findsNWidgets(2));
expect(find.text('תיאור קצר'), findsOneWidget);
expect(find.text('תיאור'), findsOneWidget);
expect(find.text('תיאור'), findsNothing);
expect(find.text('תיאור מורחב'), findsOneWidget);
await closeDialog(tester);
});
Expand All @@ -468,7 +468,7 @@ void main() {
await tester.tap(infoButton());
await tester.pumpAndSettle();

expect(find.text('אודות הקטגוריה'), findsOneWidget);
expect(find.text('תיאור המדף'), findsOneWidget);
expect(find.text('תיאור קצר בלבד'), findsOneWidget);
expect(find.text('תיאור'), findsNothing);
await closeDialog(tester);
Expand All @@ -494,8 +494,8 @@ void main() {
await tester.tap(infoButton());
await tester.pumpAndSettle();

expect(find.text('אודות הקטגוריה'), findsOneWidget);
expect(find.text('תיאור'), findsOneWidget);
expect(find.text('תיאור המדף'), findsOneWidget);
expect(find.text('תיאור'), findsNothing);
expect(find.text('תיאור מורחב בלבד'), findsOneWidget);
await closeDialog(tester);
});
Expand All @@ -511,7 +511,7 @@ void main() {
.map((tooltip) => tooltip.message),
isNot(contains('')),
);
expect(find.text('אודות הקטגוריה'), findsNothing);
expect(find.text('תיאור המדף'), findsNothing);
});
});

Expand Down
86 changes: 86 additions & 0 deletions test/library/view/shelf_description_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:otzaria/library/models/library.dart';
import 'package:otzaria/library/view/shelf_description.dart';

Category _category(String shortDescription, String description) => Category(
title: 'משנה',
description: description,
shortDescription: shortDescription,
order: 0,
subCategories: [],
books: [],
parent: null,
);

void main() {
group('categoryDescriptionParts', () {
test('ארוך שפותח בקצר: הקצר ראשון, ואחריו ההמשך בלבד', () {
final parts = categoryDescriptionParts(
_category('יסוד המסירה.', 'יסוד המסירה. והוא קיבץ הכל.'),
);
expect(parts.lead, 'יסוד המסירה.');
expect(parts.paragraphs, ['והוא קיבץ הכל.']);
});

test('ארוך שאינו פותח בקצר: שניהם, הקצר ראשון', () {
final parts = categoryDescriptionParts(_category('קצר', 'ארוך אחר'));
expect(parts.lead, 'קצר');
expect(parts.paragraphs, ['ארוך אחר']);
});

test('ארוך בכמה שורות: פסקה לכל שורה', () {
final parts = categoryDescriptionParts(_category('', 'א\n\nב'));
expect(parts.lead, isEmpty);
expect(parts.paragraphs, ['א', 'ב']);
});

test('ארוך זהה לקצר אינו חוזר', () {
final parts = categoryDescriptionParts(_category('קצר.', 'קצר.'));
expect(parts.lead, 'קצר.');
expect(parts.paragraphs, isEmpty);
});

test('בלי תיאורים: אין מה להציג', () {
expect(categoryHasDescription(_category('', '')), isFalse);
expect(categoryHasDescription(_category('קצר', '')), isTrue);
expect(categoryHasDescription(_category('', 'ארוך')), isTrue);
});
});

group('shelfDescriptionSpans', () {
test('עגולות מוקטנות משמעותית ובלי הדגשה, מרובעות מוקטנות מעט', () {
final spans = shelfDescriptionSpans('א (ב) ג [ד] ה', 20);
expect(spans.map((s) => s.text), ['א ', '(ב)', ' ג ', '[ד]', ' ה']);
expect(spans[1].style?.fontSize, closeTo(15.2, 0.001));
expect(spans[1].style?.fontWeight, FontWeight.w400);
expect(spans[3].style?.fontSize, closeTo(18.4, 0.001));
expect(spans[3].style?.fontWeight, isNull);
});

test('סוגריים שלא נסגרו נשארות כטקסט רגיל', () {
final spans = shelfDescriptionSpans('א (ב', 20);
expect(spans.map((s) => s.text), ['א (ב']);
expect(spans.single.style, isNull);
});
});

testWidgets('הקצר מודגש ואחריו הארוך בפסקה נפרדת', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: Directionality(
textDirection: TextDirection.rtl,
child: Scaffold(
body: ShelfDescription(category: _category('קצר (עי׳ שם)', 'ארוך')),
),
),
),
);

final lead = tester.widget<Text>(find.text('קצר (עי׳ שם)'));
expect((lead.textSpan! as TextSpan).style?.fontWeight, FontWeight.bold);
final longRect = tester.getRect(find.text('ארוך'));
final leadRect = tester.getRect(find.text('קצר (עי׳ שם)'));
expect(longRect.top, greaterThan(leadRect.bottom));
});
}
3 changes: 0 additions & 3 deletions tool/ui_lint/baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,6 @@
{"scope":"_BookPreviewPanelState.build","fp":"a9c1e30c","n":1},
{"scope":"_BookPreviewPanelState.build","fp":"adc1e958","n":1}
],
"lib/library/view/category_preview_panel.dart": [
{"scope":"CategoryPreviewPanel.build","fp":"2d3a28a0","n":1}
],
"lib/library/view/library_browser.dart": [
{"scope":"_LibraryBrowserState._libraryListTitleStyle","fp":"8d9b7249","n":1}
],
Expand Down
Loading