import 'package:flutter/material.dart'; import 'package:drift/drift.dart'; import 'package:trainhub_flutter/database/database.dart'; class AnalysisProvider extends ChangeNotifier { final AppDatabase database; List _sessions = []; List _currentAnnotations = []; AnalysisSession? _activeSession; List get sessions => _sessions; List get currentAnnotations => _currentAnnotations; AnalysisSession? get activeSession => _activeSession; AnalysisProvider(this.database) { _loadSessions(); } Future _loadSessions() async { _sessions = await database.select(database.analysisSessions).get(); notifyListeners(); } Future createSession(String name, String videoPath) async { final id = DateTime.now().toIso8601String(); final session = AnalysisSessionsCompanion.insert( id: id, name: name, videoPath: Value(videoPath), date: DateTime.now().toIso8601String(), ); await database.into(database.analysisSessions).insert(session); await _loadSessions(); await loadSession(id); } Future deleteSession(String id) async { await (database.delete( database.analysisSessions, )..where((t) => t.id.equals(id))).go(); if (_activeSession?.id == id) { _activeSession = null; _currentAnnotations = []; } await _loadSessions(); } Future loadSession(String id) async { final session = await (database.select( database.analysisSessions, )..where((t) => t.id.equals(id))).getSingleOrNull(); if (session != null) { _activeSession = session; await _loadAnnotations(id); } } Future _loadAnnotations(String sessionId) async { _currentAnnotations = await (database.select( database.annotations, )..where((t) => t.sessionId.equals(sessionId))).get(); notifyListeners(); } Future addAnnotation({ required String name, required String description, required double startTime, required double endTime, required String color, }) async { if (_activeSession == null) return; final id = DateTime.now().toIso8601String(); await database .into(database.annotations) .insert( AnnotationsCompanion.insert( id: id, sessionId: _activeSession!.id, name: Value(name), description: Value(description), startTime: startTime, endTime: endTime, color: Value(color), ), ); await _loadAnnotations(_activeSession!.id); } Future updateAnnotation(Annotation annotation) async { await (database.update( database.annotations, )..where((t) => t.id.equals(annotation.id))).write( AnnotationsCompanion( name: Value(annotation.name), description: Value(annotation.description), startTime: Value(annotation.startTime), endTime: Value(annotation.endTime), color: Value(annotation.color), ), ); if (_activeSession != null) { await _loadAnnotations(_activeSession!.id); } } Future deleteAnnotation(String id) async { await (database.delete( database.annotations, )..where((t) => t.id.equals(id))).go(); if (_activeSession != null) { await _loadAnnotations(_activeSession!.id); } } }