88 lines
2.8 KiB
Dart
88 lines
2.8 KiB
Dart
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<AnalysisState> build() async {
|
|
_repo = getIt<AnalysisRepository>();
|
|
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> 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));
|
|
}
|
|
}
|