new UI + new targets.
This commit is contained in:
+159
-48
@@ -6,7 +6,9 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../models/camera_setup.dart';
|
||||
import '../models/contact.dart';
|
||||
import 'club_crypto.dart';
|
||||
import '../utils/target_colors.dart';
|
||||
import '../models/holiday_theme.dart';
|
||||
import '../models/target_style.dart';
|
||||
import '../theme/app_theme.dart';
|
||||
import '../utils/translations.dart';
|
||||
|
||||
/// Single source of truth for user settings. Loaded once in main(); widgets
|
||||
@@ -14,11 +16,10 @@ import '../utils/translations.dart';
|
||||
///
|
||||
/// Writes the same SharedPreferences keys the rest of the app already reads,
|
||||
/// so screens can migrate to this class one at a time.
|
||||
class AppSettings extends ChangeNotifier {
|
||||
class AppSettings extends ChangeNotifier with WidgetsBindingObserver {
|
||||
AppSettings._();
|
||||
static final AppSettings instance = AppSettings._();
|
||||
|
||||
static const String defaultTarget = 'assets/target.png';
|
||||
// Shots per target, same limit for camera and stream capture
|
||||
static const int minBullets = 1;
|
||||
static const int maxBullets = 5;
|
||||
@@ -31,38 +32,36 @@ class AppSettings extends ChangeNotifier {
|
||||
];
|
||||
static const String defaultLanguage = 'Slovenian';
|
||||
|
||||
static const Map<String, Color> bulletColors = {
|
||||
'Blue': Colors.blue,
|
||||
'Green': Colors.green,
|
||||
'Yellow': Colors.yellow,
|
||||
'Purple': Colors.purple,
|
||||
'Cyan': Colors.cyan,
|
||||
'Teal': Colors.teal,
|
||||
'Indigo': Colors.indigo,
|
||||
'Pink': Colors.pink,
|
||||
'Lime': Colors.lime,
|
||||
'Mint': Colors.greenAccent,
|
||||
'Orange': Colors.orange,
|
||||
'Violet': Colors.deepPurpleAccent,
|
||||
'Amber': Colors.amber,
|
||||
'Brown': Colors.brown,
|
||||
'Grey': Colors.grey,
|
||||
/// Hit colours, each tuned for the dark and the light theme. The chosen
|
||||
/// one is the accent of the whole interface.
|
||||
static const Map<String, (Color dark, Color light)> bulletColors = {
|
||||
'Blue': (Color(0xFF4B8EF5), Color(0xFF2D6BD8)),
|
||||
'Green': (Color(0xFF3CC764), Color(0xFF1E9A4C)),
|
||||
'Yellow': (Color(0xFFF2C12E), Color(0xFFC08E00)),
|
||||
'Purple': (Color(0xFFB45EE3), Color(0xFF8E3FBF)),
|
||||
'Cyan': (Color(0xFF22C7E0), Color(0xFF12B4CE)),
|
||||
'Teal': (Color(0xFF17B3A3), Color(0xFF0E9384)),
|
||||
'Indigo': (Color(0xFF6E6CF2), Color(0xFF4F46E5)),
|
||||
'Pink': (Color(0xFFF0457A), Color(0xFFD63366)),
|
||||
'Lime': (Color(0xFFC9DC3C), Color(0xFF85A00E)),
|
||||
'Mint': (Color(0xFF5EE6B0), Color(0xFF1FAE7A)),
|
||||
'Orange': (Color(0xFFFF8A1F), Color(0xFFE06F00)),
|
||||
'Violet': (Color(0xFF8E62F7), Color(0xFF7C3AED)),
|
||||
'Amber': (Color(0xFFFFB020), Color(0xFFD18A00)),
|
||||
'Brown': (Color(0xFFB07A55), Color(0xFF8A5536)),
|
||||
'Grey': (Color(0xFFA3AAB2), Color(0xFF6F757C)),
|
||||
};
|
||||
static const String defaultBulletColor = 'Cyan';
|
||||
|
||||
// 13 was dropped earlier; 21 and 31–34 were Christmas / Easter themes
|
||||
static const Set<int> removedTargets = {13, 21, 31, 32, 33, 34};
|
||||
|
||||
static final List<String> targets = [
|
||||
defaultTarget,
|
||||
for (int i = 1; i <= 35; i++)
|
||||
if (!removedTargets.contains(i)) 'assets/targets/Target$i.jpg',
|
||||
];
|
||||
static const List<String> themeModes = ['system', 'light', 'dark'];
|
||||
|
||||
late SharedPreferences _prefs;
|
||||
|
||||
String _language = defaultLanguage;
|
||||
String _bulletColorName = 'Blue';
|
||||
String _selectedTarget = defaultTarget;
|
||||
String _bulletColorName = defaultBulletColor;
|
||||
String _themeMode = 'dark';
|
||||
TargetStyle _target = TargetStyle.fallback;
|
||||
Palette? _palette;
|
||||
int _reminderHours = 1;
|
||||
bool _notifyDezuranje = true;
|
||||
bool _notifySestanek = true;
|
||||
@@ -80,10 +79,96 @@ class AppSettings extends ChangeNotifier {
|
||||
};
|
||||
String get dateLocale => dateLocaleFor(_language);
|
||||
String get bulletColorName => _bulletColorName;
|
||||
Color get bulletColor => bulletColors[_bulletColorName] ?? Colors.blue;
|
||||
String get selectedTarget => _selectedTarget;
|
||||
Color get backgroundColor => TargetColors.getBackgroundColor(_selectedTarget);
|
||||
bool get isLightBackground => backgroundColor.computeLuminance() > 0.4;
|
||||
|
||||
/// The hit colour in the current theme; also the interface accent.
|
||||
Color get bulletColor => palette.accent;
|
||||
|
||||
// --- Holidays (a hidden easter egg, no setting) ---
|
||||
// During a holiday's window the app shows that holiday's background and
|
||||
// target (locked) and its hit colour, until the user picks a colour of their
|
||||
// own for this holiday. Afterwards their own settings are back.
|
||||
|
||||
/// Debug builds only: pretend today is this day, to preview a holiday.
|
||||
DateTime? debugToday;
|
||||
|
||||
DateTime get today => debugToday ?? DateTime.now();
|
||||
|
||||
HolidayTheme? get holiday => HolidayTheme.on(today);
|
||||
|
||||
/// True while a holiday decides the target.
|
||||
bool get targetLocked => holiday != null;
|
||||
|
||||
// "<holiday id>-<first day>" of the holiday whose colour the user replaced
|
||||
String? _holidayColorFor;
|
||||
|
||||
String _holidayKey(HolidayTheme h) {
|
||||
final (start, _) = h.window(today);
|
||||
return '${h.id}-${start.year}${start.month.toString().padLeft(2, '0')}${start.day.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
/// The holiday colour is in use (the user has not picked another one).
|
||||
bool get holidayColorActive {
|
||||
final h = holiday;
|
||||
return h != null && _holidayColorFor != _holidayKey(h);
|
||||
}
|
||||
|
||||
/// Back to the holiday's own colour (after picking another one).
|
||||
Future<void> useHolidayColor() async {
|
||||
_holidayColorFor = null;
|
||||
notifyListeners();
|
||||
await _prefs.remove('holidayColorFor');
|
||||
}
|
||||
|
||||
/// The holiday's screen background (the everyday one is painted, see AppBackground).
|
||||
String get background => holiday?.background(isDark) ?? '';
|
||||
|
||||
// A new day may start or end a holiday
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state == AppLifecycleState.resumed) notifyListeners();
|
||||
}
|
||||
|
||||
/// Swatch of [name] in the current theme.
|
||||
Color colorFor(String name) {
|
||||
final pair = bulletColors[name] ?? bulletColors[defaultBulletColor]!;
|
||||
return isDark ? pair.$1 : pair.$2;
|
||||
}
|
||||
|
||||
String get themeMode => _themeMode;
|
||||
|
||||
int _forceDark = 0;
|
||||
|
||||
/// Screens that must stay dark whatever the theme (the photo camera) call
|
||||
/// this while they are open, and [releaseDark] when they close.
|
||||
void holdDark() {
|
||||
_forceDark++;
|
||||
if (_forceDark == 1) notifyListeners();
|
||||
}
|
||||
|
||||
void releaseDark() {
|
||||
if (_forceDark == 0) return;
|
||||
_forceDark--;
|
||||
if (_forceDark == 0) notifyListeners();
|
||||
}
|
||||
|
||||
bool get isDark => _forceDark > 0 || switch (_themeMode) {
|
||||
'light' => false,
|
||||
'system' => WidgetsBinding.instance.platformDispatcher.platformBrightness == Brightness.dark,
|
||||
_ => true,
|
||||
};
|
||||
|
||||
Palette get palette {
|
||||
final accent = holidayColorActive ? holiday!.color : colorFor(_bulletColorName);
|
||||
final p = _palette;
|
||||
if (p != null && p.isDark == isDark && p.accent == accent) return p;
|
||||
return _palette = isDark ? Palette.dark(accent) : Palette.light(accent);
|
||||
}
|
||||
|
||||
/// The target shown: the holiday's while one is on, otherwise the user's.
|
||||
TargetStyle get target {
|
||||
final h = holiday;
|
||||
return h == null ? _target : TargetStyle.byId(h.targetId);
|
||||
}
|
||||
int get reminderHours => _reminderHours;
|
||||
bool get notifyDezuranje => _notifyDezuranje;
|
||||
bool get notifySestanek => _notifySestanek;
|
||||
@@ -96,11 +181,6 @@ class AppSettings extends ChangeNotifier {
|
||||
|
||||
String t(String key) => AppTranslations.translate(key, _language);
|
||||
|
||||
String targetName(String path) {
|
||||
if (path == defaultTarget) return t('Default Target');
|
||||
return t(path.replaceAll('assets/targets/', '').replaceAll('.jpg', ''));
|
||||
}
|
||||
|
||||
Future<void> load() async {
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
// New installs start in Slovenian. Whoever had "automatic" keeps the language
|
||||
@@ -110,15 +190,21 @@ class AppSettings extends ChangeNotifier {
|
||||
await _prefs.setString('language', _language); // other screens read this key
|
||||
await _prefs.remove('languageFollowsSystem');
|
||||
|
||||
final color = _prefs.getString('bulletColor') ?? 'Blue';
|
||||
_bulletColorName = bulletColors.containsKey(color) ? color : 'Blue';
|
||||
final color = _prefs.getString('bulletColor') ?? defaultBulletColor;
|
||||
_bulletColorName = bulletColors.containsKey(color) ? color : defaultBulletColor;
|
||||
_holidayColorFor = _prefs.getString('holidayColorFor');
|
||||
final mode = _prefs.getString('themeMode');
|
||||
_themeMode = themeModes.contains(mode) ? mode! : 'dark';
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
|
||||
_selectedTarget = _prefs.getString('selectedTarget') ?? defaultTarget;
|
||||
if (!targets.contains(_selectedTarget)) {
|
||||
// Target no longer shipped: fall back so screens reading prefs don't load a missing image
|
||||
_selectedTarget = defaultTarget;
|
||||
await _prefs.setString('selectedTarget', defaultTarget);
|
||||
// v3 targets keep the numbers of the old skins (assets/targets/Target<n>.jpg)
|
||||
var targetId = _prefs.getInt('targetStyle');
|
||||
if (targetId == null) {
|
||||
final old = RegExp(r'Target(\d+)\.jpg$').firstMatch(_prefs.getString('selectedTarget') ?? '');
|
||||
targetId = old == null ? 0 : int.parse(old.group(1)!);
|
||||
await _prefs.setInt('targetStyle', TargetStyle.byId(targetId).id);
|
||||
}
|
||||
_target = TargetStyle.byId(targetId);
|
||||
_reminderHours = _prefs.getInt('notificationHours_single') ?? 1;
|
||||
_notifyDezuranje = _prefs.getBool('notifyDezuranje') ?? true;
|
||||
_notifySestanek = _prefs.getBool('notifySestanek') ?? true;
|
||||
@@ -154,14 +240,39 @@ class AppSettings extends ChangeNotifier {
|
||||
|
||||
Future<void> setBulletColor(String name) async {
|
||||
_bulletColorName = name;
|
||||
// Picked during a holiday: this colour replaces the holiday's until it ends
|
||||
final h = holiday;
|
||||
if (h != null) {
|
||||
_holidayColorFor = _holidayKey(h);
|
||||
await _prefs.setString('holidayColorFor', _holidayColorFor!);
|
||||
}
|
||||
notifyListeners();
|
||||
await _prefs.setString('bulletColor', name);
|
||||
}
|
||||
|
||||
Future<void> setTarget(String path) async {
|
||||
_selectedTarget = path;
|
||||
Future<void> setTarget(TargetStyle style) async {
|
||||
if (targetLocked) return; // the holiday decides
|
||||
_target = style;
|
||||
notifyListeners();
|
||||
await _prefs.setString('selectedTarget', path);
|
||||
await _prefs.setInt('targetStyle', style.id);
|
||||
}
|
||||
|
||||
/// Debug: show the look of [day] (null = today).
|
||||
void previewDay(DateTime? day) {
|
||||
debugToday = day;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> setThemeMode(String mode) async {
|
||||
_themeMode = mode;
|
||||
notifyListeners();
|
||||
await _prefs.setString('themeMode', mode);
|
||||
}
|
||||
|
||||
// Theme "Sistem" follows the phone
|
||||
@override
|
||||
void didChangePlatformBrightness() {
|
||||
if (_themeMode == 'system') notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> setReminderHours(int hours) async {
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'dart:ui' show Offset;
|
||||
import 'package:flutter/foundation.dart' show ValueNotifier;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import '../utils/target_scoring.dart';
|
||||
|
||||
/// One saved target (a JSON file written by the preview screen).
|
||||
class TargetRecord {
|
||||
@@ -17,6 +20,49 @@ class TargetRecord {
|
||||
required this.shots});
|
||||
}
|
||||
|
||||
/// The hits of one saved target, in target space (see [TargetScoring]).
|
||||
class TargetHits {
|
||||
final List<Offset> bullets;
|
||||
final List<int> scores; // same order as [bullets]
|
||||
final int total;
|
||||
final int shots; // shots fired, misses included
|
||||
final String date; // as saved: dd/MM/yyyy
|
||||
|
||||
const TargetHits({required this.bullets, required this.scores, required this.total, required this.shots, required this.date});
|
||||
|
||||
static const empty = TargetHits(bullets: [], scores: [], total: 0, shots: 0, date: '');
|
||||
|
||||
/// All hits of several targets on one target.
|
||||
static TargetHits combine(List<TargetHits> list) => TargetHits(
|
||||
bullets: [for (final t in list) ...t.bullets],
|
||||
scores: [for (final t in list) ...t.scores],
|
||||
total: list.fold(0, (a, t) => a + t.total),
|
||||
shots: list.fold(0, (a, t) => a + t.shots),
|
||||
date: list.isEmpty ? '' : list.last.date,
|
||||
);
|
||||
|
||||
// Newer saves store each shot's score (hand-entered shots keep the score that
|
||||
// was entered); older ones are scored from the positions, by the same rule.
|
||||
factory TargetHits.fromJson(Map<String, dynamic> data) {
|
||||
final bullets = [
|
||||
for (final b in (data['bullets'] as List?) ?? const [])
|
||||
Offset((b['x'] as num).toDouble(), (b['y'] as num).toDouble())
|
||||
];
|
||||
final saved = data['scores'];
|
||||
final scores = saved is List && saved.length == bullets.length
|
||||
? [for (final x in saved) (x as num).toInt()]
|
||||
: [for (final b in bullets) TargetScoring.scoreAt(b)];
|
||||
final total = data['totalScore'] is int ? data['totalScore'] as int : scores.fold(0, (a, b) => a + b);
|
||||
return TargetHits(
|
||||
bullets: bullets,
|
||||
scores: scores,
|
||||
total: total,
|
||||
shots: (data['bulletCount'] as int?) ?? bullets.length,
|
||||
date: data['date'] as String? ?? '',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A folder of targets: one training or tournament.
|
||||
class Session {
|
||||
final String name;
|
||||
@@ -114,6 +160,11 @@ class ShooterStats {
|
||||
class TargetRepository {
|
||||
static Map<String, dynamic>? _index; // path in saved_images -> entry
|
||||
|
||||
/// Bumped whenever a target or session is saved, edited or deleted, so open
|
||||
/// screens (Streljanje, Knjiga) reload.
|
||||
static final ValueNotifier<int> changed = ValueNotifier(0);
|
||||
static void notifyChanged() => changed.value++;
|
||||
|
||||
static Future<Directory> _root() async {
|
||||
final dir = Directory(
|
||||
'${(await getApplicationDocumentsDirectory()).path}/saved_images');
|
||||
@@ -275,16 +326,31 @@ class TargetRepository {
|
||||
'isTournament': isTournament,
|
||||
'createdAt': meta['createdAt'] ?? session.date.toIso8601String(),
|
||||
}));
|
||||
notifyChanged();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Hits of a saved target file; empty when it can't be read.
|
||||
static Future<TargetHits> readHits(File file) async {
|
||||
try {
|
||||
return TargetHits.fromJson(jsonDecode(await file.readAsString()) as Map<String, dynamic>);
|
||||
} catch (_) {
|
||||
return TargetHits.empty;
|
||||
}
|
||||
}
|
||||
|
||||
/// Hits of several targets, in the order given.
|
||||
static Future<List<TargetHits>> readAllHits(List<File> files) => Future.wait(files.map(readHits));
|
||||
|
||||
static Future<void> deleteSession(Session session) async {
|
||||
final folder = Directory('${(await _root()).path}/${session.name}');
|
||||
if (await folder.exists()) await folder.delete(recursive: true);
|
||||
notifyChanged();
|
||||
}
|
||||
|
||||
static Future<void> deleteTarget(TargetRecord target) async {
|
||||
if (await target.file.exists()) await target.file.delete();
|
||||
notifyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user