mangayomi-mirror/lib/modules/more/statistics/statistics_screen.dart
Moustapha Kodjo Amadou 236a0cfc6d feat: add localization strings for statistics and reading time tracking #672
- Added new localization strings for total, mean per title, completion rate, watching time, reading time, average chapters per title, read percentage, and entries in multiple languages.
- Enhanced the History model to include readingTimeSeconds.
- Updated AnimeStreamController and ReaderController to track reading time and save it to history.
- Implemented reading time tracking in Anime and Novel reader views.
- Introduced statistics calculations for total reading time across histories.
- Updated statistics screen to display total reading time and average reading time per title.
2026-03-23 11:19:04 +01:00

370 lines
12 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:mangayomi/l10n/generated/app_localizations.dart';
import 'package:mangayomi/models/manga.dart';
import 'package:mangayomi/modules/more/settings/reader/providers/reader_state_provider.dart';
import 'package:mangayomi/modules/more/statistics/statistics_provider.dart';
import 'package:mangayomi/providers/l10n_providers.dart';
import 'package:mangayomi/utils/extensions/build_context_extensions.dart';
import 'package:mangayomi/utils/item_type_filters.dart';
import 'package:mangayomi/utils/item_type_localization.dart';
class StatisticsScreen extends ConsumerStatefulWidget {
const StatisticsScreen({super.key});
@override
ConsumerState<StatisticsScreen> createState() => _StatisticsScreenState();
}
class _StatisticsScreenState extends ConsumerState<StatisticsScreen>
with SingleTickerProviderStateMixin {
late TabController _tabController;
late final List<ItemType> _visibleTabTypes;
@override
void initState() {
super.initState();
_visibleTabTypes = hiddenItemTypes(ref.read(hideItemsStateProvider));
_tabController = TabController(
length: _visibleTabTypes.length,
vsync: this,
);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
if (_visibleTabTypes.isEmpty) {
return Scaffold(
appBar: AppBar(title: Text(context.l10n.statistics)),
body: Center(child: Text("EMPTY\nMPTY\nMTY\nMT\n\n")),
);
}
final l10n = context.l10n;
return Scaffold(
appBar: AppBar(
title: Text(l10n.statistics),
bottom: TabBar(
controller: _tabController,
tabs: _visibleTabTypes.map((type) {
return Tab(text: type.localized(l10n));
}).toList(),
),
),
body: TabBarView(
controller: _tabController,
children: _visibleTabTypes.map((type) {
return _buildStatisticsTab(itemType: type);
}).toList(),
),
);
}
Widget _buildStatisticsTab({required ItemType itemType}) {
final l10n = context.l10n;
final stats = ref.watch(getStatisticsProvider(itemType: itemType));
final chapterLabel = switch (itemType) {
ItemType.anime => l10n.episodes,
_ => l10n.chapters,
};
final unreadLabel = switch (itemType) {
ItemType.anime => l10n.unwatched,
_ => l10n.unread,
};
return stats.when(
loading: () => const Center(child: CircularProgressIndicator()),
error: (err, stack) => Center(child: Text("Err: $err")),
data: (stats) {
final totalItems = stats.totalItems;
final totalChapters = stats.totalChapters;
final readChapters = stats.readChapters;
final unreadChapters = totalChapters - readChapters;
final completedItems = stats.completedItems;
final downloadedItems = stats.downloadedItems;
final averageChapters = totalItems > 0 ? totalChapters / totalItems : 0;
final readPercentage = totalChapters > 0
? (readChapters / totalChapters) * 100
: 0;
final completedPercentage = totalItems > 0
? (completedItems / totalItems) * 100
: 0;
final totalReadingTimeSeconds = stats.totalReadingTimeSeconds;
return SingleChildScrollView(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_buildSectionHeader(l10n.entries),
_buildEntriesCard(
totalItems: totalItems,
completedItems: completedItems,
completedPercentage: completedPercentage.toDouble(),
),
const SizedBox(height: 10),
_buildSectionHeader(chapterLabel),
_buildChaptersCard(
totalChapters: totalChapters,
readChapters: readChapters,
unreadChapters: unreadChapters,
downloadedItems: downloadedItems,
averageChapters: averageChapters.toDouble(),
readPercentage: readPercentage.toDouble(),
title: itemType.localized(l10n),
context: context,
unreadLabel: unreadLabel,
l10n: l10n,
),
const SizedBox(height: 10),
_buildSectionHeader(_trackingTimeLabel(itemType, l10n)),
_buildTrackingTimeCard(
totalSeconds: totalReadingTimeSeconds,
totalItems: totalItems,
itemType: itemType,
l10n: l10n,
),
],
),
);
},
);
}
Widget _buildSectionHeader(String title) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Text(title, style: const TextStyle(fontWeight: FontWeight.bold)),
);
}
Widget _buildEntriesCard({
required int totalItems,
required int completedItems,
required double completedPercentage,
}) {
final l10n = context.l10n;
return Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
_buildStatisticColumn(
value: "$totalItems",
label: l10n.in_library,
icon: Icons.collections_bookmark_outlined,
),
_buildStatisticColumn(
value: "$completedItems",
label: l10n.completed,
icon: Icons.local_library_outlined,
),
_buildStatisticColumn(
value: "${completedPercentage.toStringAsFixed(1)}%",
label: l10n.completion_rate,
icon: Icons.percent,
),
],
),
),
);
}
Widget _buildChaptersCard({
required int totalChapters,
required int readChapters,
required int unreadChapters,
required int downloadedItems,
required double averageChapters,
required double readPercentage,
required String title,
required BuildContext context,
required String unreadLabel,
required AppLocalizations l10n,
}) {
return Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
_buildStatisticColumn(
value: "$totalChapters",
label: l10n.total,
icon: Icons.format_list_numbered,
),
_buildStatisticColumn(
value: "$readChapters",
label: l10n.read,
icon: Icons.done_all,
),
_buildStatisticColumn(
value: "$unreadChapters",
label: unreadLabel,
icon: Icons.remove,
),
_buildStatisticColumn(
value: "$downloadedItems",
label: context.l10n.downloaded,
icon: Icons.download_done,
),
],
),
),
ListTile(
title: Text(l10n.average_chapters_per_title(title)),
subtitle: Text(averageChapters.toStringAsFixed(2)),
leading: Icon(Icons.bar_chart, color: context.primaryColor),
),
_buildReadPercentageGraph(readPercentage, context),
],
),
);
}
Widget _buildStatisticColumn({
required String value,
required String label,
required IconData icon,
}) {
return Column(
children: [
Padding(
padding: const EdgeInsets.all(4),
child: Text(
value,
style: const TextStyle(fontWeight: FontWeight.bold),
),
),
Text(label, style: const TextStyle(fontSize: 10)),
Padding(
padding: const EdgeInsets.only(top: 20, bottom: 8),
child: Icon(icon, color: context.primaryColor),
),
],
);
}
Widget _buildReadPercentageGraph(
double readPercentage,
BuildContext context,
) {
return Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.pie_chart, color: context.primaryColor),
Padding(
padding: EdgeInsets.all(8.0),
child: Text(context.l10n.read_percentage),
),
],
),
const SizedBox(height: 10),
Center(
child: SizedBox(
width: 120,
height: 120,
child: Stack(
alignment: Alignment.center,
children: [
Container(
width: 120,
height: 120,
decoration: BoxDecoration(
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: Colors.grey.withValues(alpha: 0.5),
blurRadius: 10,
spreadRadius: 5,
offset: const Offset(0, 5),
),
],
),
child: CircularProgressIndicator(
value: readPercentage / 100,
strokeWidth: 10,
backgroundColor: Colors.grey.shade300,
valueColor: AlwaysStoppedAnimation<Color>(
context.primaryColor,
),
),
),
Text(
"${readPercentage.toStringAsFixed(1)}%",
style: TextStyle(fontSize: 18, color: context.secondaryColor),
),
],
),
),
),
const SizedBox(height: 10),
],
);
}
String _trackingTimeLabel(ItemType itemType, AppLocalizations l10n) {
return switch (itemType) {
ItemType.anime => l10n.watching_time,
_ => l10n.reading_time,
};
}
String _formatDuration(int totalSeconds) {
if (totalSeconds <= 0) return '0m';
final days = totalSeconds ~/ 86400;
final hours = (totalSeconds % 86400) ~/ 3600;
final minutes = (totalSeconds % 3600) ~/ 60;
final parts = <String>[];
if (days > 0) parts.add('${days}d');
if (hours > 0) parts.add('${hours}h');
if (minutes > 0 || parts.isEmpty) parts.add('${minutes}m');
return parts.join(' ');
}
Widget _buildTrackingTimeCard({
required int totalSeconds,
required int totalItems,
required ItemType itemType,
required AppLocalizations l10n,
}) {
final averagePerTitle = totalItems > 0
? (totalSeconds / totalItems).round()
: 0;
final isAnime = itemType == ItemType.anime;
return Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(25)),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
_buildStatisticColumn(
value: _formatDuration(totalSeconds),
label: l10n.total,
icon: isAnime ? Icons.play_circle_outline : Icons.auto_stories,
),
_buildStatisticColumn(
value: _formatDuration(averagePerTitle),
label: l10n.mean_per_title,
icon: Icons.timer_outlined,
),
],
),
),
);
}
}