Files
trainhub-flutter/lib/presentation/analysis/analysis_controller.dart
Kazimierz Ciołek 0c9eb8878d
Some checks failed
Build Linux App / build (push) Failing after 1m33s
Refactoring
2026-02-23 10:02:23 -05:00

117 lines
3.9 KiB
Dart

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:trainhub_flutter/injection.dart';
import 'package:trainhub_flutter/domain/entities/annotation.dart';
import 'package:trainhub_flutter/domain/repositories/analysis_repository.dart';
import 'package:trainhub_flutter/domain/repositories/exercise_repository.dart';
import 'package:trainhub_flutter/presentation/analysis/analysis_state.dart';
part 'analysis_controller.g.dart';
@riverpod
class AnalysisController extends _$AnalysisController {
late AnalysisRepository _repo;
late ExerciseRepository _exerciseRepo;
@override
Future<AnalysisState> build() async {
_repo = getIt<AnalysisRepository>();
_exerciseRepo = getIt<ExerciseRepository>();
final sessions = await _repo.getAllSessions();
return AnalysisState(sessions: sessions);
}
Future<void> 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<void> 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<void> 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<void> 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<void> updateAnnotation(AnnotationEntity annotation) async {
final current = state.valueOrNull;
if (current?.activeSession == null) return;
await _repo.updateAnnotation(
id: annotation.id,
name: annotation.name ?? '',
description: annotation.description ?? '',
color: annotation.color ?? 'grey',
);
final annotations = await _repo.getAnnotations(current!.activeSession!.id);
state = AsyncValue.data(current.copyWith(annotations: annotations));
}
Future<void> createExerciseFromAnnotation({
required String name,
required String instructions,
required String videoPath,
required double startTime,
required double endTime,
}) async {
final videoRef = '$videoPath#t=${startTime.toStringAsFixed(2)},${endTime.toStringAsFixed(2)}';
await _exerciseRepo.create(
name: name,
instructions: instructions.isEmpty ? null : instructions,
videoUrl: videoRef,
);
}
Future<void> 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));
}
}