Initial commit
This commit is contained in:
117
lib/providers/analysis_provider.dart
Normal file
117
lib/providers/analysis_provider.dart
Normal file
@@ -0,0 +1,117 @@
|
||||
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<AnalysisSession> _sessions = [];
|
||||
List<Annotation> _currentAnnotations = [];
|
||||
AnalysisSession? _activeSession;
|
||||
|
||||
List<AnalysisSession> get sessions => _sessions;
|
||||
List<Annotation> get currentAnnotations => _currentAnnotations;
|
||||
AnalysisSession? get activeSession => _activeSession;
|
||||
|
||||
AnalysisProvider(this.database) {
|
||||
_loadSessions();
|
||||
}
|
||||
|
||||
Future<void> _loadSessions() async {
|
||||
_sessions = await database.select(database.analysisSessions).get();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> 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<void> 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<void> 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<void> _loadAnnotations(String sessionId) async {
|
||||
_currentAnnotations = await (database.select(
|
||||
database.annotations,
|
||||
)..where((t) => t.sessionId.equals(sessionId))).get();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> 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<void> 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<void> deleteAnnotation(String id) async {
|
||||
await (database.delete(
|
||||
database.annotations,
|
||||
)..where((t) => t.id.equals(id))).go();
|
||||
if (_activeSession != null) {
|
||||
await _loadAnnotations(_activeSession!.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user