bible-tracker/lib/services/progress.dart

57 lines
1.6 KiB
Dart
Raw Normal View History

2026-04-20 18:39:49 +00:00
import '../data/bible.dart';
import '../models/tracker.dart';
/// Pure progress calculations over a [Tracker].
///
/// Kept free of Flutter imports so it can be covered by plain unit tests.
class TrackerProgress {
TrackerProgress(this.tracker);
final Tracker tracker;
int chaptersReadIn(Book book) {
int count = 0;
for (int chapter = 1; chapter <= book.chapterCount; chapter++) {
if (tracker.readChapters.contains(
Bible.chapterKey(book.index, chapter),
)) {
count++;
}
}
return count;
}
double fractionOf(Book book) {
if (book.chapterCount == 0) return 0;
return chaptersReadIn(book) / book.chapterCount;
}
bool isBookComplete(Book book) =>
chaptersReadIn(book) == book.chapterCount && book.chapterCount > 0;
bool isBookStarted(Book book) => chaptersReadIn(book) > 0;
int get totalRead => tracker.readChapters.length;
double get overall =>
Bible.totalChapters == 0 ? 0 : totalRead / Bible.totalChapters;
int booksCompleted([Testament? testament]) => Bible.books
.where(
(Book book) =>
(testament == null || book.testament == testament) &&
isBookComplete(book),
)
.length;
int chaptersReadInTestament(Testament testament) => Bible.books
.where((Book book) => book.testament == testament)
.fold<int>(0, (int sum, Book book) => sum + chaptersReadIn(book));
double testamentFraction(Testament testament) {
final int total = Bible.totalChaptersIn(testament);
if (total == 0) return 0;
return chaptersReadInTestament(testament) / total;
}
}