diff --git a/lib/src/model/study/study.dart b/lib/src/model/study/study.dart index 2a45906358..dc1916edf9 100644 --- a/lib/src/model/study/study.dart +++ b/lib/src/model/study/study.dart @@ -35,6 +35,26 @@ class Study with _$Study { required IList deviationComments, }) = _Study; + /// Returns the indexed name of a chapter given its [chapterId]. + /// + /// The indexed name is a string that combines the chapter's index (1-based) + /// and its name, formatted as "index. name". + /// + /// Throws a [RangeError] if the chapter with the given [chapterId] is not found. + /// + /// Example: + /// ```dart + /// final chapterName = study.getChapterIndexedName(chapterId); + /// print(chapterName); // Output: "1. Chapter Name" + /// ``` + /// + /// - Parameter chapterId: The ID of the chapter to find. + /// - Returns: A string representing the indexed name of the chapter. + String getChapterIndexedName(StudyChapterId chapterId) { + final index = chapters.indexWhere((c) => c.id == chapterId); + return '${index + 1}. ${chapters[index].name}'; + } + StudyChapterMeta get currentChapterMeta => chapters.firstWhere((c) => c.id == chapter.id); factory Study.fromServerJson(Map json) { diff --git a/lib/src/model/study/study_controller.dart b/lib/src/model/study/study_controller.dart index cc144f653b..d997aaca48 100644 --- a/lib/src/model/study/study_controller.dart +++ b/lib/src/model/study/study_controller.dart @@ -601,8 +601,7 @@ class StudyState with _$StudyState { bool get canGoNext => currentNode.children.isNotEmpty; bool get canGoBack => currentPath.size > UciPath.empty.size; - String get currentChapterTitle => - study.chapters.firstWhere((chapter) => chapter.id == currentChapter.id).name; + String get currentChapterTitle => study.getChapterIndexedName(study.chapter.id); bool get hasNextChapter => study.chapter.id != study.chapters.last.id; bool get isAtEndOfChapter => isOnMainline && currentNode.children.isEmpty; diff --git a/lib/src/view/study/study_bottom_bar.dart b/lib/src/view/study/study_bottom_bar.dart index 126783e02d..897340b36f 100644 --- a/lib/src/view/study/study_bottom_bar.dart +++ b/lib/src/view/study/study_bottom_bar.dart @@ -289,7 +289,7 @@ class _StudyChaptersMenuState extends ConsumerState<_StudyChaptersMenu> { for (final chapter in state.study.chapters) PlatformListTile( key: chapter.id == state.currentChapter.id ? currentChapterKey : null, - title: Text(chapter.name, maxLines: 2), + title: Text(state.study.getChapterIndexedName(chapter.id), maxLines: 2), onTap: () { ref.read(studyControllerProvider(widget.id).notifier).goToChapter(chapter.id); Navigator.of(context).pop(); diff --git a/test/view/study/study_screen_test.dart b/test/view/study/study_screen_test.dart index 03cbf9bb0a..001b9468fb 100644 --- a/test/view/study/study_screen_test.dart +++ b/test/view/study/study_screen_test.dart @@ -129,8 +129,8 @@ void main() { // Wait for study to load await tester.pumpAndSettle(); - expect(find.text('Chapter 1'), findsOneWidget); - expect(find.text('Chapter 2'), findsNothing); + expect(find.text('1. Chapter 1'), findsOneWidget); + expect(find.text('2. Chapter 2'), findsNothing); expect(find.text('pgn 1'), findsOneWidget); expect(find.text('pgn 2'), findsNothing); @@ -143,8 +143,8 @@ void main() { // Wait for next chapter to load (even though it shouldn't) await tester.pumpAndSettle(); - expect(find.text('Chapter 1'), findsNothing); - expect(find.text('Chapter 2'), findsOneWidget); + expect(find.text('1. Chapter 1'), findsNothing); + expect(find.text('2. Chapter 2'), findsOneWidget); expect(find.text('pgn 1'), findsNothing); expect(find.text('pgn 2'), findsOneWidget); @@ -155,20 +155,20 @@ void main() { await tester.pumpAndSettle(); expect( - find.descendant(of: find.byType(Scrollable), matching: find.text('Chapter 1')), + find.descendant(of: find.byType(Scrollable), matching: find.text('1. Chapter 1')), findsOneWidget, ); expect( - find.descendant(of: find.byType(Scrollable), matching: find.text('Chapter 2')), + find.descendant(of: find.byType(Scrollable), matching: find.text('2. Chapter 2')), findsOneWidget, ); - await tester.tap(find.text('Chapter 1')); + await tester.tap(find.text('1. Chapter 1')); // Wait for chapter to load await tester.pumpAndSettle(); - expect(find.text('Chapter 1'), findsOneWidget); - expect(find.text('Chapter 2'), findsNothing); + expect(find.text('1. Chapter 1'), findsOneWidget); + expect(find.text('2. Chapter 2'), findsNothing); expect(find.text('pgn 1'), findsOneWidget); expect(find.text('pgn 2'), findsNothing);