Skip to content

Commit e447b95

Browse files
authored
Merge pull request #1438 from vincendep/feat/study-chapter-numbers
feat: show chapter numbers
2 parents 4248fb6 + 37f7ed4 commit e447b95

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

lib/src/model/study/study.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ class Study with _$Study {
3535
required IList<String?> deviationComments,
3636
}) = _Study;
3737

38+
/// Returns the indexed name of a chapter given its [chapterId].
39+
///
40+
/// The indexed name is a string that combines the chapter's index (1-based)
41+
/// and its name, formatted as "index. name".
42+
///
43+
/// Throws a [RangeError] if the chapter with the given [chapterId] is not found.
44+
///
45+
/// Example:
46+
/// ```dart
47+
/// final chapterName = study.getChapterIndexedName(chapterId);
48+
/// print(chapterName); // Output: "1. Chapter Name"
49+
/// ```
50+
///
51+
/// - Parameter chapterId: The ID of the chapter to find.
52+
/// - Returns: A string representing the indexed name of the chapter.
53+
String getChapterIndexedName(StudyChapterId chapterId) {
54+
final index = chapters.indexWhere((c) => c.id == chapterId);
55+
return '${index + 1}. ${chapters[index].name}';
56+
}
57+
3858
StudyChapterMeta get currentChapterMeta => chapters.firstWhere((c) => c.id == chapter.id);
3959

4060
factory Study.fromServerJson(Map<String, Object?> json) {

lib/src/model/study/study_controller.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,7 @@ class StudyState with _$StudyState {
601601
bool get canGoNext => currentNode.children.isNotEmpty;
602602
bool get canGoBack => currentPath.size > UciPath.empty.size;
603603

604-
String get currentChapterTitle =>
605-
study.chapters.firstWhere((chapter) => chapter.id == currentChapter.id).name;
604+
String get currentChapterTitle => study.getChapterIndexedName(study.chapter.id);
606605
bool get hasNextChapter => study.chapter.id != study.chapters.last.id;
607606

608607
bool get isAtEndOfChapter => isOnMainline && currentNode.children.isEmpty;

lib/src/view/study/study_bottom_bar.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class _StudyChaptersMenuState extends ConsumerState<_StudyChaptersMenu> {
289289
for (final chapter in state.study.chapters)
290290
PlatformListTile(
291291
key: chapter.id == state.currentChapter.id ? currentChapterKey : null,
292-
title: Text(chapter.name, maxLines: 2),
292+
title: Text(state.study.getChapterIndexedName(chapter.id), maxLines: 2),
293293
onTap: () {
294294
ref.read(studyControllerProvider(widget.id).notifier).goToChapter(chapter.id);
295295
Navigator.of(context).pop();

test/view/study/study_screen_test.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ void main() {
129129
// Wait for study to load
130130
await tester.pumpAndSettle();
131131

132-
expect(find.text('Chapter 1'), findsOneWidget);
133-
expect(find.text('Chapter 2'), findsNothing);
132+
expect(find.text('1. Chapter 1'), findsOneWidget);
133+
expect(find.text('2. Chapter 2'), findsNothing);
134134

135135
expect(find.text('pgn 1'), findsOneWidget);
136136
expect(find.text('pgn 2'), findsNothing);
@@ -143,8 +143,8 @@ void main() {
143143
// Wait for next chapter to load (even though it shouldn't)
144144
await tester.pumpAndSettle();
145145

146-
expect(find.text('Chapter 1'), findsNothing);
147-
expect(find.text('Chapter 2'), findsOneWidget);
146+
expect(find.text('1. Chapter 1'), findsNothing);
147+
expect(find.text('2. Chapter 2'), findsOneWidget);
148148

149149
expect(find.text('pgn 1'), findsNothing);
150150
expect(find.text('pgn 2'), findsOneWidget);
@@ -155,20 +155,20 @@ void main() {
155155
await tester.pumpAndSettle();
156156

157157
expect(
158-
find.descendant(of: find.byType(Scrollable), matching: find.text('Chapter 1')),
158+
find.descendant(of: find.byType(Scrollable), matching: find.text('1. Chapter 1')),
159159
findsOneWidget,
160160
);
161161
expect(
162-
find.descendant(of: find.byType(Scrollable), matching: find.text('Chapter 2')),
162+
find.descendant(of: find.byType(Scrollable), matching: find.text('2. Chapter 2')),
163163
findsOneWidget,
164164
);
165165

166-
await tester.tap(find.text('Chapter 1'));
166+
await tester.tap(find.text('1. Chapter 1'));
167167
// Wait for chapter to load
168168
await tester.pumpAndSettle();
169169

170-
expect(find.text('Chapter 1'), findsOneWidget);
171-
expect(find.text('Chapter 2'), findsNothing);
170+
expect(find.text('1. Chapter 1'), findsOneWidget);
171+
expect(find.text('2. Chapter 2'), findsNothing);
172172

173173
expect(find.text('pgn 1'), findsOneWidget);
174174
expect(find.text('pgn 2'), findsNothing);

0 commit comments

Comments
 (0)