feat: implement base architecture and core repositories for weapon tracking and target analysis functionality
This commit is contained in:
@@ -4,8 +4,10 @@ import 'package:path/path.dart' as path;
|
||||
import 'package:uuid/uuid.dart';
|
||||
import '../database/database_helper.dart';
|
||||
import '../models/session.dart';
|
||||
import '../models/target_analysis.dart';
|
||||
import '../models/shot.dart';
|
||||
import '../models/target_type.dart';
|
||||
import '../models/weapon.dart';
|
||||
import '../models/maintenance.dart';
|
||||
|
||||
class SessionRepository {
|
||||
final DatabaseHelper _databaseHelper;
|
||||
@@ -18,8 +20,37 @@ class SessionRepository {
|
||||
_uuid = uuid ?? const Uuid();
|
||||
|
||||
Future<Session> createSession({
|
||||
required TargetType targetType,
|
||||
String? id,
|
||||
required String weapon,
|
||||
String? weaponId,
|
||||
required int maxShotsPerTarget,
|
||||
required List<TargetAnalysis> analyses,
|
||||
String? notes,
|
||||
int distance = 25,
|
||||
}) async {
|
||||
final session = Session(
|
||||
id: id ?? _uuid.v4(),
|
||||
weapon: weapon,
|
||||
weaponId: weaponId,
|
||||
maxShotsPerTarget: maxShotsPerTarget,
|
||||
analyses: analyses,
|
||||
createdAt: DateTime.now(),
|
||||
notes: notes,
|
||||
distance: distance,
|
||||
);
|
||||
|
||||
await _databaseHelper.insertSession(session);
|
||||
return session;
|
||||
}
|
||||
|
||||
Future<void> addAnalysisToSession(String sessionId, TargetAnalysis analysis) async {
|
||||
await _databaseHelper.insertTargetAnalysis(analysis, sessionId);
|
||||
}
|
||||
|
||||
Future<TargetAnalysis> prepareTargetAnalysis({
|
||||
required String imagePath,
|
||||
required String sessionId,
|
||||
required dynamic targetType, // TargetType
|
||||
required List<Shot> shots,
|
||||
required int totalScore,
|
||||
double? groupingDiameter,
|
||||
@@ -33,8 +64,9 @@ class SessionRepository {
|
||||
// Copy image to app documents directory
|
||||
final savedImagePath = await _saveImage(imagePath);
|
||||
|
||||
final session = Session(
|
||||
return TargetAnalysis(
|
||||
id: _uuid.v4(),
|
||||
sessionId: sessionId,
|
||||
targetType: targetType,
|
||||
imagePath: savedImagePath,
|
||||
shots: shots,
|
||||
@@ -48,9 +80,6 @@ class SessionRepository {
|
||||
targetCenterY: targetCenterY,
|
||||
targetRadius: targetRadius,
|
||||
);
|
||||
|
||||
await _databaseHelper.insertSession(session);
|
||||
return session;
|
||||
}
|
||||
|
||||
Future<String> _saveImage(String sourcePath) async {
|
||||
@@ -75,28 +104,23 @@ class SessionRepository {
|
||||
}
|
||||
|
||||
Future<List<Session>> getAllSessions({
|
||||
TargetType? targetType,
|
||||
int? limit,
|
||||
int? offset,
|
||||
}) async {
|
||||
return await _databaseHelper.getAllSessions(
|
||||
targetType: targetType?.name,
|
||||
limit: limit,
|
||||
offset: offset,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> updateSession(Session session) async {
|
||||
await _databaseHelper.updateSession(session);
|
||||
}
|
||||
|
||||
Future<void> deleteSession(String id) async {
|
||||
final session = await getSession(id);
|
||||
if (session != null) {
|
||||
// Delete the image file
|
||||
final imageFile = File(session.imagePath);
|
||||
if (await imageFile.exists()) {
|
||||
await imageFile.delete();
|
||||
for (final analysis in session.analyses) {
|
||||
final imageFile = File(analysis.imagePath);
|
||||
if (await imageFile.exists()) {
|
||||
await imageFile.delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
await _databaseHelper.deleteSession(id);
|
||||
@@ -106,7 +130,79 @@ class SessionRepository {
|
||||
return await _databaseHelper.getStatistics();
|
||||
}
|
||||
|
||||
String generateShotId() {
|
||||
String generateId() {
|
||||
return _uuid.v4();
|
||||
}
|
||||
|
||||
// Weapon Repository methods
|
||||
Future<Weapon> addWeapon({
|
||||
required String name,
|
||||
required WeaponType type,
|
||||
required String caliber,
|
||||
required int magazineCount,
|
||||
required int magazineCapacity,
|
||||
String? notes,
|
||||
String? optic,
|
||||
String? silencer,
|
||||
String? trigger,
|
||||
String? customName,
|
||||
}) async {
|
||||
final weapon = Weapon(
|
||||
id: _uuid.v4(),
|
||||
name: name,
|
||||
type: type,
|
||||
caliber: caliber,
|
||||
magazineCount: magazineCount,
|
||||
magazineCapacity: magazineCapacity,
|
||||
notes: notes,
|
||||
createdAt: DateTime.now(),
|
||||
optic: optic,
|
||||
silencer: silencer,
|
||||
trigger: trigger,
|
||||
customName: customName,
|
||||
);
|
||||
await _databaseHelper.insertWeapon(weapon);
|
||||
return weapon;
|
||||
}
|
||||
|
||||
Future<List<Weapon>> getWeapons() async {
|
||||
return await _databaseHelper.getAllWeapons();
|
||||
}
|
||||
|
||||
Future<void> updateWeapon(Weapon weapon) async {
|
||||
await _databaseHelper.updateWeapon(weapon);
|
||||
}
|
||||
|
||||
Future<void> deleteWeapon(String id) async {
|
||||
await _databaseHelper.deleteWeapon(id);
|
||||
}
|
||||
|
||||
Future<int> getRoundsFiredForWeapon(String weaponId) async {
|
||||
return await _databaseHelper.getRoundsFiredForWeapon(weaponId);
|
||||
}
|
||||
|
||||
Future<void> addMaintenanceEntry({
|
||||
required String weaponId,
|
||||
required MaintenanceType type,
|
||||
required String description,
|
||||
int? roundsSinceLast,
|
||||
}) async {
|
||||
final entry = MaintenanceEntry(
|
||||
id: _uuid.v4(),
|
||||
weaponId: weaponId,
|
||||
type: type,
|
||||
description: description,
|
||||
date: DateTime.now(),
|
||||
roundsSinceLastMaintenance: roundsSinceLast,
|
||||
);
|
||||
await _databaseHelper.insertMaintenance(entry);
|
||||
}
|
||||
|
||||
Future<List<MaintenanceEntry>> getMaintenanceHistory(String weaponId) async {
|
||||
return await _databaseHelper.getMaintenanceForWeapon(weaponId);
|
||||
}
|
||||
|
||||
Future<void> deleteMaintenanceEntry(String id) async {
|
||||
await _databaseHelper.deleteMaintenance(id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user