aded preview for sharing targets.

This commit is contained in:
Jan Pleterski
2026-11-13 11:15:01 +01:00
parent 30708c73b1
commit f6974e3e26
5 changed files with 472 additions and 0 deletions
+60
View File
@@ -8,6 +8,8 @@ import 'package:image/image.dart' as img;
import 'package:shared_preferences/shared_preferences.dart';
import '../utils/translations.dart';
import '../utils/target_colors.dart';
import '../widgets/result_share.dart';
import 'session_screen.dart' show targetCount, shotCount;
class TargetViewScreen extends StatefulWidget {
final List<File> jsonFiles;
@@ -30,6 +32,7 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
List<Offset> _bullets = [];
List<Offset> _scaledBullets = []; // Bullets scaled to display target size
int _totalScore = 0;
int _shotCount = 0; // shots fired, misses included (bulletCount)
Map<int, int> _folderChartScores = {};
Offset? _targetCenter;
Uint8List? _targetImageBytes;
@@ -172,11 +175,13 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
final allBullets = <Offset>[];
String latestDate = 'Unknown Date';
var shots = 0;
for (var file in widget.jsonFiles) {
final jsonString = await file.readAsString();
final data = jsonDecode(jsonString);
final bulletsJson = data['bullets'] as List?;
shots += (data['bulletCount'] as int?) ?? bulletsJson?.length ?? 0;
if (bulletsJson != null) {
allBullets.addAll(
bulletsJson.map((b) => Offset(b['x'].toDouble(), b['y'].toDouble())).toList(),
@@ -209,6 +214,7 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
}
_bullets = allBullets;
_shotCount = shots;
} else {
// Single file view - load from current file
final currentFile = widget.jsonFiles[_currentIndex];
@@ -236,6 +242,7 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
}
final bulletsJson = data['bullets'] as List;
_bullets = bulletsJson.map((b) => Offset(b['x'].toDouble(), b['y'].toDouble())).toList();
_shotCount = (data['bulletCount'] as int?) ?? _bullets.length;
}
_targetFilePath = widget.isCombinedView
@@ -308,6 +315,54 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
}
}
// What the screen shows, as one image: target and tiles left, chart right
void _share() {
final center = _targetCenter ?? Offset.zero;
final scores = [for (final b in _bullets) _scoreForBullet(b, center)];
final List<(String, String)> tiles;
final List<MapEntry<int, int>> chart;
if (widget.isCombinedView) {
final groups = <int, int>{};
for (final s in scores) {
groups[s] = (groups[s] ?? 0) + 1;
}
final sorted = groups.entries.toList()..sort((a, b) => b.key.compareTo(a.key));
tiles = [for (final e in sorted) ('${e.value}x', '${e.key}')];
chart = _folderChartScores.entries.toList()..sort((a, b) => a.key.compareTo(b.key));
} else {
final order = List.generate(scores.length, (i) => i)
..sort((a, b) => scores[b].compareTo(scores[a]));
tiles = [for (final i in order) ('${i + 1}', '${scores[i]}')];
chart = [for (int i = 0; i < scores.length; i++) MapEntry(i + 1, scores[i])];
}
final folder = File(_targetFilePath).parent.path.split('/').last;
shareResult(
context,
ResultShareData(
title: folder,
date: _date.replaceAll('/', '.'),
countsLine: widget.isCombinedView
? '${targetCount(widget.jsonFiles.length)} · ${shotCount(_shotCount)}'
: shotCount(_shotCount),
total: _totalScore,
averagePerShot: _shotCount == 0 ? null : _totalScore / _shotCount,
targetImage: _targetImageBytes!,
targetImageSize: Size(_targetImageDecoded!.width.toDouble(),
_targetImageDecoded!.height.toDouble()),
bullets: _scaledBullets,
bulletDiameter: ((_scaleFactorX + _scaleFactorY) / 2) * 53,
bulletColor: _bulletColor,
targetBackground: _backgroundColor,
tiles: tiles,
chart: chart,
chartPerShot: !widget.isCombinedView,
t: _t,
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
@@ -342,6 +397,11 @@ class _TargetViewScreenState extends State<TargetViewScreen> {
centerTitle: true,
iconTheme: const IconThemeData(color: Colors.white),
actions: [
IconButton(
tooltip: _t('Share'),
icon: const Icon(Icons.share_rounded),
onPressed: _targetImageDecoded == null ? null : _share,
),
// The combined view shows a whole session; deleting would remove an
// arbitrary one of its targets. Sessions are deleted from the Book list.
if (!widget.isCombinedView)