new UI + new targets.
This commit is contained in:
@@ -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