import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../data/bible.dart'; import '../models/tracker.dart'; import '../services/progress.dart'; import '../services/tracker_service.dart'; import '../widgets/progress_bar.dart'; import '../widgets/theme.dart'; class StatsScreen extends ConsumerWidget { const StatsScreen({super.key, required this.trackerId}); final String trackerId; @override Widget build(BuildContext context, WidgetRef ref) { final Tracker? tracker = ref.watch(trackerByIdProvider(trackerId)); if (tracker == null) { return Scaffold( appBar: AppBar(), body: const Center(child: Text('Tracker not found')), ); } final Color accent = Color(tracker.colorValue); final TrackerProgress progress = TrackerProgress(tracker); final int days = DateTime.now().difference(tracker.startDate).inDays; return Scaffold( appBar: AppBar(title: const Text('Stats')), body: ListView( padding: const EdgeInsets.all(AppTokens.spaceL), children: [ _OverallCard(tracker: tracker, accent: accent, progress: progress), const SizedBox(height: AppTokens.spaceL), _SectionCard( title: 'Old Testament', chapters: progress.chaptersReadInTestament(Testament.old), chaptersTotal: Bible.totalChaptersIn(Testament.old), books: progress.booksCompleted(Testament.old), booksTotal: Bible.totalBooksIn(Testament.old), fraction: progress.testamentFraction(Testament.old), accent: accent, ), const SizedBox(height: AppTokens.spaceL), _SectionCard( title: 'New Testament', chapters: progress.chaptersReadInTestament(Testament.newT), chaptersTotal: Bible.totalChaptersIn(Testament.newT), books: progress.booksCompleted(Testament.newT), booksTotal: Bible.totalBooksIn(Testament.newT), fraction: progress.testamentFraction(Testament.newT), accent: accent, ), const SizedBox(height: AppTokens.spaceL), Card( child: Padding( padding: const EdgeInsets.all(AppTokens.spaceL), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text( 'Since you started', style: Theme.of(context).textTheme.titleMedium, ), const SizedBox(height: AppTokens.spaceM), _StatRow( label: 'Start date', value: _formatDate(tracker.startDate), ), _StatRow(label: 'Days reading', value: '$days'), _StatRow( label: 'Chapters read', value: '${progress.totalRead} / ${Bible.totalChapters}', ), _StatRow( label: 'Books completed', value: '${progress.booksCompleted()} / ${Bible.totalBooks}', ), ], ), ), ), ], ), ); } static String _formatDate(DateTime date) { const List months = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December', ]; return '${months[date.month - 1]} ${date.day}, ${date.year}'; } } class _OverallCard extends StatelessWidget { const _OverallCard({ required this.tracker, required this.accent, required this.progress, }); final Tracker tracker; final Color accent; final TrackerProgress progress; @override Widget build(BuildContext context) { return Card( child: Padding( padding: const EdgeInsets.all(AppTokens.spaceL), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text(tracker.name, style: Theme.of(context).textTheme.titleMedium), const SizedBox(height: AppTokens.spaceS), Text( '${(progress.overall * 100).toStringAsFixed(1)}%', style: Theme.of(context).textTheme.displaySmall?.copyWith( color: accent, fontWeight: FontWeight.w700, ), ), const SizedBox(height: AppTokens.spaceM), ProgressBar(value: progress.overall, color: accent, height: 10), ], ), ), ); } } class _SectionCard extends StatelessWidget { const _SectionCard({ required this.title, required this.chapters, required this.chaptersTotal, required this.books, required this.booksTotal, required this.fraction, required this.accent, }); final String title; final int chapters; final int chaptersTotal; final int books; final int booksTotal; final double fraction; final Color accent; @override Widget build(BuildContext context) { return Card( child: Padding( padding: const EdgeInsets.all(AppTokens.spaceL), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text(title, style: Theme.of(context).textTheme.titleMedium), ProgressPill(fraction: fraction, color: accent), ], ), const SizedBox(height: AppTokens.spaceM), ProgressBar(value: fraction, color: accent), const SizedBox(height: AppTokens.spaceM), _StatRow(label: 'Chapters', value: '$chapters / $chaptersTotal'), _StatRow(label: 'Books', value: '$books / $booksTotal'), ], ), ), ); } } class _StatRow extends StatelessWidget { const _StatRow({required this.label, required this.value}); final String label; final String value; @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.symmetric(vertical: AppTokens.spaceXS), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( label, style: Theme.of(context).textTheme.bodyMedium?.copyWith( color: Theme.of(context).colorScheme.onSurfaceVariant, ), ), Text( value, style: Theme.of( context, ).textTheme.bodyMedium?.copyWith(fontWeight: FontWeight.w600), ), ], ), ); } }