attempt to fix test txs 2
This commit is contained in:
@@ -91,25 +91,6 @@ class BankController extends ChangeNotifier {
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Drop tombstones that match any of [txs] (fingerprint-aware).
|
||||
void clearRemoteDeletesMatching(Iterable<BankTransaction> txs) {
|
||||
final list = txs.toList(growable: false);
|
||||
if (list.isEmpty) return;
|
||||
final next = [
|
||||
for (final tomb in _pendingRemoteDeletes)
|
||||
if (!list.any(tomb.matchesForDelete)) tomb,
|
||||
];
|
||||
if (next.length == _pendingRemoteDeletes.length) return;
|
||||
_pendingRemoteDeletes = next;
|
||||
unawaited(_persistPendingDeletes());
|
||||
}
|
||||
|
||||
void clearAllPendingRemoteDeletes() {
|
||||
if (_pendingRemoteDeletes.isEmpty) return;
|
||||
_pendingRemoteDeletes = const [];
|
||||
unawaited(_persistPendingDeletes());
|
||||
}
|
||||
|
||||
void requeueRemoteDeletes(Iterable<BankTransaction> txs) {
|
||||
for (final tx in txs) {
|
||||
_queueRemoteDelete(tx, notify: false);
|
||||
@@ -120,11 +101,54 @@ class BankController extends ChangeNotifier {
|
||||
void _queueRemoteDelete(BankTransaction tx, {bool notify = true}) {
|
||||
if (tx.id.isEmpty) return;
|
||||
if (_isTombstoned(tx)) return;
|
||||
_pendingRemoteDeletes = [..._pendingRemoteDeletes, tx];
|
||||
_pendingRemoteDeletes = [
|
||||
..._pendingRemoteDeletes,
|
||||
tx.copyWith(isManual: true),
|
||||
];
|
||||
unawaited(_persistPendingDeletes());
|
||||
if (notify) onPendingRemoteDeletesChanged?.call();
|
||||
}
|
||||
|
||||
/// Merge delete tombstones from another device and strip matching local rows.
|
||||
Future<void> applyRemoteDeletes(
|
||||
List<BankTransaction> remote, {
|
||||
bool notify = true,
|
||||
}) async {
|
||||
var tombsChanged = false;
|
||||
for (final tomb in remote) {
|
||||
if (_isTombstoned(tomb)) continue;
|
||||
_pendingRemoteDeletes = [
|
||||
..._pendingRemoteDeletes,
|
||||
tomb.copyWith(isManual: true),
|
||||
];
|
||||
tombsChanged = true;
|
||||
}
|
||||
if (_pendingRemoteDeletes.isEmpty) return;
|
||||
|
||||
final beforeRemote = _remoteTransactions.length;
|
||||
final beforeImported = _importedTransactions.length;
|
||||
_remoteTransactions = [
|
||||
for (final tx in _remoteTransactions)
|
||||
if (!_isTombstoned(tx)) tx,
|
||||
];
|
||||
_importedTransactions = [
|
||||
for (final tx in _importedTransactions)
|
||||
if (!_isTombstoned(tx)) tx,
|
||||
];
|
||||
final stripped = beforeRemote != _remoteTransactions.length ||
|
||||
beforeImported != _importedTransactions.length;
|
||||
|
||||
if (tombsChanged || stripped) {
|
||||
_invalidateMerged();
|
||||
if (tombsChanged) await _persistPendingDeletes();
|
||||
if (stripped) {
|
||||
await _persistCache();
|
||||
await _persistImported();
|
||||
}
|
||||
if (notify) notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _persistPendingDeletes() async {
|
||||
final storage = _storage;
|
||||
if (storage == null) return;
|
||||
|
||||
@@ -274,23 +274,42 @@ class SyncController extends ChangeNotifier {
|
||||
_armPushDebounce();
|
||||
}
|
||||
|
||||
/// Removes tombstoned bank txs from PocketBase before pull/push can resurrect them.
|
||||
/// Pull shared delete tombstones, then remove matching local bank rows.
|
||||
Future<void> _pullAndApplyTxDeletes(PbSyncService service) async {
|
||||
try {
|
||||
final remote = await service.pullTxDeletes(onStatus: _setStatus);
|
||||
await _bank.applyRemoteDeletes(remote, notify: false);
|
||||
} catch (error) {
|
||||
debugPrint('SyncController: tx_deletes pull skipped ($error)');
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes tombstoned bank txs from the transactions collection.
|
||||
///
|
||||
/// Tombstones themselves stay local + synced via `tx_deletes` so other
|
||||
/// devices learn about the delete and do not push the row back.
|
||||
Future<void> _flushPendingTxDeletes(PbSyncService service) async {
|
||||
final pending = List<BankTransaction>.of(_bank.pendingRemoteDeletes);
|
||||
if (pending.isEmpty) return;
|
||||
_setStatus('Removing deleted bank txs…');
|
||||
try {
|
||||
final matched = await service.deleteTransactionsMatching(pending);
|
||||
if (matched.isNotEmpty) {
|
||||
_bank.clearRemoteDeletesMatching(matched);
|
||||
}
|
||||
// Do NOT clear unmatched tombstones — they still block pull resurrection
|
||||
// until pushTransactions GC removes the remote rows.
|
||||
await service.deleteTransactionsMatching(pending);
|
||||
// Keep tombstones — they must remain until every device has seen them.
|
||||
} catch (error) {
|
||||
debugPrint('SyncController: tx deletes failed ($error)');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _pushTxDeletes(PbSyncService service) async {
|
||||
final tombs = List<BankTransaction>.of(_bank.pendingRemoteDeletes);
|
||||
if (tombs.isEmpty) return;
|
||||
try {
|
||||
await service.pushTxDeletes(tombs, onStatus: _setStatus);
|
||||
} catch (error) {
|
||||
debugPrint('SyncController: tx_deletes push skipped ($error)');
|
||||
}
|
||||
}
|
||||
|
||||
void _armPushDebounce() {
|
||||
if (!_auth.isLoggedIn) return;
|
||||
if (_syncing) {
|
||||
@@ -323,7 +342,8 @@ class SyncController extends ChangeNotifier {
|
||||
|
||||
try {
|
||||
final service = PbSyncService(_auth.client);
|
||||
// Delete first — otherwise pull re-merges the row and push recreates it.
|
||||
// Learn deletes from other devices before merging bank txs / pushing.
|
||||
await _pullAndApplyTxDeletes(service);
|
||||
await _flushPendingTxDeletes(service);
|
||||
final remoteMeta = await service.pull(
|
||||
onProgress: (p) {
|
||||
@@ -438,6 +458,7 @@ class SyncController extends ChangeNotifier {
|
||||
}
|
||||
// Retry deletes in case any were queued during the pull/merge window.
|
||||
await _flushPendingTxDeletes(service);
|
||||
await _pushTxDeletes(service);
|
||||
_pushBankPending = false;
|
||||
final tombs = List<BankTransaction>.of(_bank.pendingRemoteDeletes);
|
||||
final bankSummary = await service.pushTransactions(
|
||||
@@ -448,8 +469,6 @@ class SyncController extends ChangeNotifier {
|
||||
onProgress: _setProgress,
|
||||
onStatus: _setStatus,
|
||||
);
|
||||
// Push GC removes orphan admin test rows; tombstones are done.
|
||||
_bank.clearAllPendingRemoteDeletes();
|
||||
_lastSyncedAt = DateTime.now();
|
||||
_lastError = null;
|
||||
_lastSyncSummary = '$metaSummary\n$bankSummary';
|
||||
@@ -601,7 +620,10 @@ class SyncController extends ChangeNotifier {
|
||||
}
|
||||
if (pushBank) {
|
||||
_setStatus('Updating bank txs…');
|
||||
// Other devices may have deleted test txs — learn that before push.
|
||||
await _pullAndApplyTxDeletes(service);
|
||||
await _flushPendingTxDeletes(service);
|
||||
await _pushTxDeletes(service);
|
||||
try {
|
||||
final tombs = List<BankTransaction>.of(_bank.pendingRemoteDeletes);
|
||||
final summary = await service.pushTransactions(
|
||||
@@ -612,7 +634,6 @@ class SyncController extends ChangeNotifier {
|
||||
onProgress: _setProgress,
|
||||
onStatus: _setStatus,
|
||||
);
|
||||
_bank.clearAllPendingRemoteDeletes();
|
||||
parts.add(summary);
|
||||
} catch (error) {
|
||||
debugPrint('SyncController: bank tx push skipped ($error)');
|
||||
|
||||
Reference in New Issue
Block a user