cyberhybridhub/lib/data/local/user_profile_local_store.dart
2026-05-20 10:22:58 -05:00

71 lines
1.9 KiB
Dart

import '../../models/user_profile.dart';
import 'app_database.dart';
import 'user_profile_mapper.dart';
/// Drift-backed local profile storage (mobile and desktop native).
class UserProfileLocalStore {
UserProfileLocalStore(this._db);
final AppDatabase _db;
Stream<UserProfile?> watchProfile(String firebaseUid) {
return _db.watchProfile(firebaseUid).map((UserProfileRow? row) {
if (row == null) {
return null;
}
return profileFromRow(row);
});
}
Future<UserProfile?> getProfile(String firebaseUid) async {
final UserProfileRow? row = await _db.getProfile(firebaseUid);
if (row == null) {
return null;
}
return profileFromRow(row);
}
Future<void> saveProfile(UserProfile profile, {required bool dirty}) async {
await _db.upsertProfile(
companionFromProfile(profile.copyWith(dirty: dirty)),
);
if (dirty) {
await _db.enqueueOutbox(
SyncOutboxRowsCompanion.insert(
firebaseUid: profile.firebaseUid,
payloadJson: encodeProfilePayload(profile),
createdAt: DateTime.now().toUtc(),
),
);
}
}
Future<List<UserProfile>> pendingOutboxProfiles(String firebaseUid) async {
final List<SyncOutboxRow> rows = await _db.pendingOutbox(firebaseUid);
return rows
.map((SyncOutboxRow row) => decodeProfilePayload(row.payloadJson))
.toList();
}
Future<void> clearOutbox(String firebaseUid) async {
await _db.clearOutbox(firebaseUid);
}
Future<void> deleteOutboxEntries(String firebaseUid) async {
final List<SyncOutboxRow> rows = await _db.pendingOutbox(firebaseUid);
for (final SyncOutboxRow row in rows) {
await _db.deleteOutboxEntry(row.id);
}
}
Future<void> clearLocal(String firebaseUid) async {
await _db.clearOutbox(firebaseUid);
await _db.deleteProfile(firebaseUid);
}
Future<void> clearAll() async {
await _db.clearAllProfiles();
await _db.clearAllOutbox();
}
}