Track online szinkronizáció refraktorálása

This commit is contained in:
2026-07-08 23:29:22 +02:00
parent 1ab656585b
commit d23f736ead
7 changed files with 131 additions and 154 deletions
+37 -5
View File
@@ -57,6 +57,14 @@ class AppDatabase {
Future<void> upgradeSchema(Database db, int oldVersion, int newVersion) =>
_onUpgrade(db, oldVersion, newVersion);
Future<void> _tryExec(Database db, String sql) async {
try {
await db.execute(sql);
} on DatabaseException catch (e) {
if (!e.toString().contains('duplicate column')) rethrow;
}
}
Future<void> _onCreate(Database db, int _) async {
// ── Projects ────────────────────────────────────────────────────
// uuid: a projekt globális azonosítója. Közös projekthez csatlakozáskor
@@ -261,6 +269,7 @@ class AppDatabase {
await _createStakeoutTable(db);
await _createContactsOutbox(db);
await _addAppInstanceIdColumns(db);
// Alap projekt létrehozása az első indításhoz
final now = DateTime.now().toIso8601String();
@@ -296,6 +305,8 @@ class AppDatabase {
if (oldVersion < 5) {
_createContactsOutbox(db);
}
await _addAppInstanceIdColumns(db);
}
Future<void> _migrateToV4(Database db) async {
@@ -499,6 +510,7 @@ class AppDatabase {
DeviceIdentityService.to.isReady
? DeviceIdentityService.to.appInstanceId
: null;
map['app_instance_id'] ??= DeviceIdentityService.to.appInstanceId;
return db.insert('tracks', map);
}
@@ -524,10 +536,14 @@ class AppDatabase {
);
}
Future<List<Track>> listTracks() async {
Future<List<Track>> listTracks({int? projectId}) async {
final db = await database;
final rows = await db.query('tracks',
where: 'deleted_at IS NULL', orderBy: 'start_time DESC');
where: projectId != null
? 'deleted_at IS NULL AND project_id = ?'
: 'deleted_at IS NULL',
whereArgs: projectId != null ? [projectId] : null,
orderBy: 'start_time DESC');
return rows.map(Track.fromMap).toList();
}
@@ -554,9 +570,11 @@ class AppDatabase {
await txn.rawUpdate('''
UPDATE tracks
SET distance_m = ?,
point_count = point_count + 1
point_count = point_count + 1,
updated_at = ?,
sync_status = 'pending'
WHERE id = ?
''', [newDistance, point.trackId]);
''', [newDistance, DateTime.now().toIso8601String(), point.trackId]);
});
} catch (e) {
print(
@@ -879,6 +897,8 @@ class AppDatabase {
DeviceIdentityService.to.isReady
? DeviceIdentityService.to.appInstanceId
: null;
map['app_instance_id'] ??= DeviceIdentityService.to.appInstanceId;
return db.insert('measured_points', map);
}
@@ -974,7 +994,7 @@ class AppDatabase {
DeviceIdentityService.to.isReady
? DeviceIdentityService.to.appInstanceId
: null;
;
map['app_instance_id'] ??= DeviceIdentityService.to.appInstanceId;
return db.insert('stakeout_points', map);
}
@@ -998,6 +1018,8 @@ class AppDatabase {
DeviceIdentityService.to.isReady
? DeviceIdentityService.to.appInstanceId
: null;
map['app_instance_id'] ??= DeviceIdentityService.to.appInstanceId;
await txn.insert('stakeout_points', map);
inserted++;
}
@@ -1353,6 +1375,16 @@ class AppDatabase {
0;
}
/// A device_id (ANDROID_ID, TEXT) MARAD — helyes, mert az oszlop nem
/// uuid típusú. Az app_instance_id egy KIEGÉSZÍTŐ azonosító: az adott
/// app-telepítéshez tartozó, saját generált uuid — mindkettő tárolva,
/// hogy az "eszköz" és a "konkrét app-példány" is visszakereshető legyen.
Future<void> _addAppInstanceIdColumns(Database db) async {
for (final table in ['tracks', 'measured_points', 'stakeout_points']) {
await _tryExec(db, 'ALTER TABLE $table ADD COLUMN app_instance_id TEXT');
}
}
// Future<void> testOnly() async {
// final db = await database;
// await db.execute(