113 lines
2.9 KiB
Dart
113 lines
2.9 KiB
Dart
import 'dart:io';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:path/path.dart' as path;
|
|
import 'package:uuid/uuid.dart';
|
|
import '../database/database_helper.dart';
|
|
import '../models/session.dart';
|
|
import '../models/shot.dart';
|
|
import '../models/target_type.dart';
|
|
|
|
class SessionRepository {
|
|
final DatabaseHelper _databaseHelper;
|
|
final Uuid _uuid;
|
|
|
|
SessionRepository({
|
|
DatabaseHelper? databaseHelper,
|
|
Uuid? uuid,
|
|
}) : _databaseHelper = databaseHelper ?? DatabaseHelper(),
|
|
_uuid = uuid ?? const Uuid();
|
|
|
|
Future<Session> createSession({
|
|
required TargetType targetType,
|
|
required String imagePath,
|
|
required List<Shot> shots,
|
|
required int totalScore,
|
|
double? groupingDiameter,
|
|
double? groupingCenterX,
|
|
double? groupingCenterY,
|
|
String? notes,
|
|
double? targetCenterX,
|
|
double? targetCenterY,
|
|
double? targetRadius,
|
|
}) async {
|
|
// Copy image to app documents directory
|
|
final savedImagePath = await _saveImage(imagePath);
|
|
|
|
final session = Session(
|
|
id: _uuid.v4(),
|
|
targetType: targetType,
|
|
imagePath: savedImagePath,
|
|
shots: shots,
|
|
totalScore: totalScore,
|
|
groupingDiameter: groupingDiameter,
|
|
groupingCenterX: groupingCenterX,
|
|
groupingCenterY: groupingCenterY,
|
|
createdAt: DateTime.now(),
|
|
notes: notes,
|
|
targetCenterX: targetCenterX,
|
|
targetCenterY: targetCenterY,
|
|
targetRadius: targetRadius,
|
|
);
|
|
|
|
await _databaseHelper.insertSession(session);
|
|
return session;
|
|
}
|
|
|
|
Future<String> _saveImage(String sourcePath) async {
|
|
final appDir = await getApplicationDocumentsDirectory();
|
|
final imagesDir = Directory(path.join(appDir.path, 'target_images'));
|
|
|
|
if (!await imagesDir.exists()) {
|
|
await imagesDir.create(recursive: true);
|
|
}
|
|
|
|
final fileName = '${_uuid.v4()}${path.extension(sourcePath)}';
|
|
final destPath = path.join(imagesDir.path, fileName);
|
|
|
|
final sourceFile = File(sourcePath);
|
|
await sourceFile.copy(destPath);
|
|
|
|
return destPath;
|
|
}
|
|
|
|
Future<Session?> getSession(String id) async {
|
|
return await _databaseHelper.getSession(id);
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
await _databaseHelper.deleteSession(id);
|
|
}
|
|
|
|
Future<Map<String, dynamic>> getStatistics() async {
|
|
return await _databaseHelper.getStatistics();
|
|
}
|
|
|
|
String generateShotId() {
|
|
return _uuid.v4();
|
|
}
|
|
}
|