This commit is contained in:
Jan Pleterski
2026-11-13 09:15:17 +01:00
parent d3927aac8a
commit d4bc01f202
21 changed files with 294 additions and 1148 deletions
+114 -29
View File
@@ -1,6 +1,7 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../services/app_settings.dart';
import '../services/target_repository.dart';
import '../theme/app_theme.dart';
@@ -44,7 +45,60 @@ class _BookScreenState extends State<BookScreen> with WidgetsBindingObserver {
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_load();
_restoreSettings().then((_) => _load());
}
// The view settings survive leaving the screen and restarting the app.
// Which month the graph is scrolled to is not a setting: it opens on the latest.
static const _prefix = 'book.';
Future<void> _restoreSettings() async {
final p = await SharedPreferences.getInstance();
T pick<T extends Enum>(List<T> values, String key, T fallback) {
final i = p.getInt(_prefix + key);
return i != null && i >= 0 && i < values.length ? values[i] : fallback;
}
final start = p.getInt('${_prefix}customStart');
final end = p.getInt('${_prefix}customEnd');
if (!mounted) return;
setState(() {
_filter = pick(_Filter.values, 'filter', _filter);
_period = pick(_Period.values, 'period', _period);
_shots = p.getInt('${_prefix}shots');
_sortByScore = p.getBool('${_prefix}sortByScore') ?? _sortByScore;
_chartByYear = p.getBool('${_prefix}chartByYear') ?? _chartByYear;
if (start != null && end != null) {
_custom = DateTimeRange(
start: DateTime.fromMillisecondsSinceEpoch(start),
end: DateTime.fromMillisecondsSinceEpoch(end));
}
// A custom period without its range can't be shown
if (_period == _Period.custom && _custom == null) _period = _Period.year;
});
}
Future<void> _saveSettings() async {
final p = await SharedPreferences.getInstance();
await p.setInt('${_prefix}filter', _filter.index);
await p.setInt('${_prefix}period', _period.index);
if (_shots == null) {
await p.remove('${_prefix}shots');
} else {
await p.setInt('${_prefix}shots', _shots!);
}
await p.setBool('${_prefix}sortByScore', _sortByScore);
await p.setBool('${_prefix}chartByYear', _chartByYear);
if (_custom != null) {
await p.setInt('${_prefix}customStart', _custom!.start.millisecondsSinceEpoch);
await p.setInt('${_prefix}customEnd', _custom!.end.millisecondsSinceEpoch);
}
}
// setState for a view setting, then remember it
void _change(VoidCallback update) {
setState(update);
_saveSettings();
}
@override
@@ -124,7 +178,7 @@ class _BookScreenState extends State<BookScreen> with WidgetsBindingObserver {
if (picked == null) return;
_custom = picked;
}
setState(() {
_change(() {
_period = p;
_shots = null;
});
@@ -260,7 +314,7 @@ class _BookScreenState extends State<BookScreen> with WidgetsBindingObserver {
BarTab(s.t('Tournaments'), Icons.emoji_events_rounded),
],
selected: _filter.index,
onSelect: (i) => setState(() {
onSelect: (i) => _change(() {
_filter = _Filter.values[i];
_shots = null;
_chartAt = null;
@@ -293,7 +347,7 @@ class _BookScreenState extends State<BookScreen> with WidgetsBindingObserver {
selected: n == selected,
empty: !formats.contains(n),
onTap: formats.contains(n)
? () => setState(() => _shots = n)
? () => _change(() => _shots = n)
: null),
),
],
@@ -533,7 +587,7 @@ class _BookScreenState extends State<BookScreen> with WidgetsBindingObserver {
labels: [s.t('Month'), s.t('Year')],
selected: _chartByYear ? 1 : 0,
color: s.bulletColor,
onSelect: (i) => setState(() {
onSelect: (i) => _change(() {
_chartByYear = i == 1;
_chartAt = at;
}),
@@ -624,14 +678,15 @@ class _BookScreenState extends State<BookScreen> with WidgetsBindingObserver {
icon: Icon(
_sortByScore ? Icons.leaderboard_rounded : Icons.event_rounded,
color: surf.textSecondary),
onPressed: () => setState(() => _sortByScore = !_sortByScore),
onPressed: () => _change(() => _sortByScore = !_sortByScore),
),
],
),
);
}
// Date block left; average per shot right, with how far it is from [average].
// Date block left; points (Skupek) and average per shot right, the average
// with how far it is from [average].
// Tap shows this session's targets on one target; long press opens details.
Widget _sessionCard(Session session, double? average) {
final s = AppSettings.instance;
@@ -683,7 +738,7 @@ class _BookScreenState extends State<BookScreen> with WidgetsBindingObserver {
),
const SizedBox(height: 3),
Text(
'${targetCount(session.targets.length)} · ${s.t('Total').toLowerCase()} ${session.totalScore}',
targetCount(session.targets.length),
style: TextStyle(
fontSize: 12, color: surf.textSecondary),
),
@@ -691,27 +746,14 @@ class _BookScreenState extends State<BookScreen> with WidgetsBindingObserver {
),
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
avg?.toStringAsFixed(2) ?? '–',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w800,
color: surf.textPrimary),
),
if (delta != null)
Text(_signed(delta),
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w700,
color: _deltaColor(delta))),
],
),
_CardFigure(
label: s.t('Sum'), value: '${session.totalScore}'),
const SizedBox(width: 14),
_CardFigure(
label: s.t('Average'),
value: avg?.toStringAsFixed(2) ?? '–',
note: delta == null ? null : _signed(delta),
noteColor: delta == null ? null : _deltaColor(delta),
),
IconButton(
tooltip: s.t('Details'),
@@ -1015,6 +1057,49 @@ class _Toggle extends StatelessWidget {
}
}
/// One figure on a history card: small uppercase label over a bold number,
/// with an optional coloured note underneath.
class _CardFigure extends StatelessWidget {
final String label;
final String value;
final String? note;
final Color? noteColor;
const _CardFigure(
{required this.label, required this.value, this.note, this.noteColor});
@override
Widget build(BuildContext context) {
final surf = Surfaces.current;
return Padding(
padding: const EdgeInsets.symmetric(vertical: 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(label.toUpperCase(),
style: TextStyle(
fontSize: 9,
fontWeight: FontWeight.bold,
letterSpacing: 1,
color: surf.textSecondary)),
Text(value,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.w800,
color: surf.textPrimary)),
if (note != null)
Text(note!,
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w700,
color: noteColor)),
],
),
);
}
}
/// Small pill on the dark bar: white when selected, outlined otherwise.
class _BarPill extends StatelessWidget {
final String label;