import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:trainhub_flutter/injection.dart'; import 'package:trainhub_flutter/domain/repositories/analysis_repository.dart'; import 'package:trainhub_flutter/presentation/analysis/analysis_state.dart'; part 'analysis_controller.g.dart'; @riverpod class AnalysisController extends _$AnalysisController { late AnalysisRepository _repo; @override Future build() async { _repo = getIt(); final sessions = await _repo.getAllSessions(); return AnalysisState(sessions: sessions); } Future createSession(String name, String videoPath) async { final session = await _repo.createSession(name, videoPath); final sessions = await _repo.getAllSessions(); final annotations = await _repo.getAnnotations(session.id); state = AsyncValue.data( AnalysisState( sessions: sessions, activeSession: session, annotations: annotations, ), ); } Future loadSession(String id) async { final session = await _repo.getSession(id); if (session == null) return; final annotations = await _repo.getAnnotations(id); final current = state.valueOrNull ?? const AnalysisState(); state = AsyncValue.data( current.copyWith( activeSession: session, annotations: annotations, ), ); } Future deleteSession(String id) async { await _repo.deleteSession(id); final sessions = await _repo.getAllSessions(); final current = state.valueOrNull ?? const AnalysisState(); state = AsyncValue.data( current.copyWith( sessions: sessions, activeSession: current.activeSession?.id == id ? null : current.activeSession, annotations: current.activeSession?.id == id ? [] : current.annotations, ), ); } Future addAnnotation({ required String name, required String description, required double startTime, required double endTime, required String color, }) async { final current = state.valueOrNull; if (current?.activeSession == null) return; await _repo.addAnnotation( sessionId: current!.activeSession!.id, name: name, description: description, startTime: startTime, endTime: endTime, color: color, ); final annotations = await _repo.getAnnotations(current.activeSession!.id); state = AsyncValue.data(current.copyWith(annotations: annotations)); } Future deleteAnnotation(String id) async { await _repo.deleteAnnotation(id); final current = state.valueOrNull; if (current?.activeSession == null) return; final annotations = await _repo.getAnnotations(current!.activeSession!.id); state = AsyncValue.data(current.copyWith(annotations: annotations)); } }