import 'package:drift/drift.dart'; import '../../domain/mission/drone_profile.dart' as domain; import 'app_database.dart'; /// Persistenz-Zugriff fuer Drohnenprofile (Doku 3.5/7.3). /// /// `domain.DroneProfile` (das reine Datenmodell) und `DroneProfile` (die von /// Drift aus der `DroneProfiles`-Tabelle generierte Zeilenklasse) heissen /// gleich - hier daher qualifiziert importiert, um beide zu unterscheiden. class DroneProfileRepository { DroneProfileRepository(this._db); final AppDatabase _db; Stream> watchAll() { return _db.select(_db.droneProfiles).watch().map( (rows) => [for (final row in rows) _fromRow(row)], ); } Future> loadAll() async { final rows = await _db.select(_db.droneProfiles).get(); return [for (final row in rows) _fromRow(row)]; } /// Legt beim allerersten Start ein T1-Ranger-Standardprofil an, falls /// noch keines existiert (analog `if (!drones) drones=[DEFAULT_DRONE]` /// im HTML-Demonstrator). Gibt dessen id zurueck. Future ensureSeeded() async { final existing = await loadAll(); if (existing.isNotEmpty) return existing.first.id!; return insert(domain.DroneProfile.t1Ranger()); } Future insert(domain.DroneProfile profile) { return _db.into(_db.droneProfiles).insert(_toCompanion(profile)); } Future update(domain.DroneProfile profile) async { await (_db.update(_db.droneProfiles)..where((d) => d.id.equals(profile.id!))) .write(_toCompanion(profile)); } Future delete(int id) async { await (_db.delete(_db.droneProfiles)..where((d) => d.id.equals(id))).go(); } domain.DroneProfile _fromRow(DroneProfile row) => domain.DroneProfile( id: row.id, name: row.name, minSpeed: row.minSpeed, cruiseSpeed: row.cruiseSpeed, maxSpeed: row.maxSpeed, minTurnRadius: row.minTurnRadius, loiterRadius: row.loiterRadius, catchRadius: row.catchRadius, maxClimbRate: row.maxClimbRate, maxDescentRate: row.maxDescentRate, maxRangeM: row.maxRangeM, maxEnduranceMin: row.maxEnduranceMin, maxAltitudeM: row.maxAltitudeM, ); DroneProfilesCompanion _toCompanion(domain.DroneProfile profile) => DroneProfilesCompanion( name: Value(profile.name), minSpeed: Value(profile.minSpeed), cruiseSpeed: Value(profile.cruiseSpeed), maxSpeed: Value(profile.maxSpeed), minTurnRadius: Value(profile.minTurnRadius), loiterRadius: Value(profile.loiterRadius), catchRadius: Value(profile.catchRadius), maxClimbRate: Value(profile.maxClimbRate), maxDescentRate: Value(profile.maxDescentRate), maxRangeM: Value(profile.maxRangeM), maxEnduranceMin: Value(profile.maxEnduranceMin), maxAltitudeM: Value(profile.maxAltitudeM), ); }