89 lines
2.6 KiB
Dart
89 lines
2.6 KiB
Dart
import 'package:sqflite/sqflite.dart';
|
|
import 'remote_models.dart';
|
|
|
|
class RemoteRepository {
|
|
final Database db;
|
|
RemoteRepository(this.db);
|
|
|
|
Future<void> upsertAll(List<RemotePhotoItem> items) async {
|
|
await db.transaction((txn) async {
|
|
for (final it in items) {
|
|
// cerca se esiste già una entry per quel remoteId
|
|
final existing = await txn.query(
|
|
'entry',
|
|
columns: ['id'],
|
|
where: 'remoteId = ?',
|
|
whereArgs: [it.id],
|
|
limit: 1,
|
|
);
|
|
|
|
final int? existingId =
|
|
existing.isNotEmpty ? (existing.first['id'] as int?) : null;
|
|
|
|
final row = <String, Object?>{
|
|
// se esiste sostituiamo, altrimenti INSERT nuovo
|
|
'id': existingId,
|
|
'contentId': null,
|
|
'uri': null,
|
|
'path': null,
|
|
'sourceMimeType': it.mimeType,
|
|
'width': it.width,
|
|
'height': it.height,
|
|
'sourceRotationDegrees': null,
|
|
'sizeBytes': it.sizeBytes,
|
|
'title': it.name,
|
|
'dateAddedSecs':
|
|
DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
|
'dateModifiedMillis': null,
|
|
'sourceDateTakenMillis':
|
|
it.takenAtUtc?.millisecondsSinceEpoch,
|
|
'durationMillis': it.durationMillis,
|
|
'trashed': 0,
|
|
'origin': 1,
|
|
'provider': 'json@patachina',
|
|
|
|
// ⭐ AGGIUNTA: GPS direttamente nel DB di Aves
|
|
'latitude': it.lat,
|
|
'longitude': it.lng,
|
|
'altitude': it.alt,
|
|
|
|
'remoteId': it.id,
|
|
'remotePath': it.path,
|
|
'remoteThumb1': it.thub1,
|
|
'remoteThumb2': it.thub2,
|
|
};
|
|
|
|
// INSERT OR REPLACE (se 'id' è valorizzato, sostituisce; se null, crea nuovo)
|
|
final newId = await txn.insert(
|
|
'entry',
|
|
row,
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
|
|
// opzionale: salva indirizzo (se il backend lo fornisce)
|
|
if (it.location != null) {
|
|
final addr = <String, Object?>{
|
|
'id': newId,
|
|
'addressLine': it.location!.address,
|
|
'countryCode': null, // county_code != country code
|
|
'countryName': it.location!.country,
|
|
'adminArea': it.location!.region,
|
|
'locality': it.location!.city,
|
|
};
|
|
|
|
await txn.insert(
|
|
'address',
|
|
addr,
|
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
|
);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<int> countRemote() async {
|
|
final rows = await db
|
|
.rawQuery('SELECT COUNT(1) AS c FROM entry WHERE origin=1');
|
|
return (rows.first['c'] as int?) ?? 0;
|
|
}
|
|
}
|