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
20 changes: 20 additions & 0 deletions lib/src/model/study/study.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ class Study with _$Study {
required IList<String?> 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<String, Object?> json) {
Expand Down
3 changes: 1 addition & 2 deletions lib/src/model/study/study_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/view/study/study_bottom_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 9 additions & 9 deletions test/view/study/study_screen_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down