Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

auth: coalesce sync and batch bulk code updates

Neeraj Gupta689d5572b830
diff --git a/mobile/apps/auth/lib/services/authenticator_service.dart b/mobile/apps/auth/lib/services/authenticator_service.dart
index a2eda627d5..6530d42261 100644
--- a/mobile/apps/auth/lib/services/authenticator_service.dart
+++ b/mobile/apps/auth/lib/services/authenticator_service.dart
@@ -39,6 +39,8 @@ class AuthenticatorService {
late AuthenticatorDB _db;
late OfflineAuthenticatorDB _offlineDb;
final String _lastEntitySyncTime = "lastEntitySyncTime";
+ Future<bool>? _onlineSyncInFlight;
+ bool _onlineSyncRerunRequested = false;
AuthenticatorService._privateConstructor();
@@ -163,7 +165,42 @@ class AuthenticatorService {
}
}
- Future<bool> onlineSync() async {
+ Future<bool> onlineSync() {
+ final inFlightSync = _onlineSyncInFlight;
+ if (inFlightSync != null) {
+ _onlineSyncRerunRequested = true;
+ return inFlightSync;
+ }
+
+ final completer = Completer<bool>();
+ _onlineSyncInFlight = completer.future;
+ unawaited(_runCoalescedOnlineSync(completer));
+ return completer.future;
+ }
+
+ Future<void> _runCoalescedOnlineSync(Completer<bool> completer) async {
+ bool lastResult = false;
+ try {
+ while (true) {
+ _onlineSyncRerunRequested = false;
+ lastResult = await _onlineSyncOnce();
+ if (!_onlineSyncRerunRequested) {
+ break;
+ }
+ _logger.info("Sync rerun requested while syncing; running again");
+ }
+ } catch (e, s) {
+ _logger.severe("Unexpected failure while coalescing syncs", e, s);
+ lastResult = false;
+ } finally {
+ _onlineSyncInFlight = null;
+ if (!completer.isCompleted) {
+ completer.complete(lastResult);
+ }
+ }
+ }
+
+ Future<bool> _onlineSyncOnce() async {
try {
if (getAccountMode().isOffline) {
debugPrint("Skipping sync since account mode is offline");
diff --git a/mobile/apps/auth/lib/store/code_display_store.dart b/mobile/apps/auth/lib/store/code_display_store.dart
index 035ba70db0..e809c2d6be 100644
--- a/mobile/apps/auth/lib/store/code_display_store.dart
+++ b/mobile/apps/auth/lib/store/code_display_store.dart
@@ -140,7 +140,7 @@ class CodeDisplayStore {
// traverse through all the codes and edit this tag's value
final relevantCodes = await _getCodesByTag(tag);
- final tasks = <Future>[];
+ final tasks = <Future<AddResult>>[];
for (final code in relevantCodes) {
final tags = code.display.tags;
@@ -150,11 +150,17 @@ class CodeDisplayStore {
code.copyWith(
display: code.display.copyWith(tags: tags),
),
+ shouldSync: false,
),
);
}
- await Future.wait(tasks);
+ final results = await Future.wait(tasks);
+ if (results.any((r) => r != AddResult.duplicate) &&
+ AuthenticatorService.instance.getAccountMode() ==
+ AccountMode.online) {
+ AuthenticatorService.instance.onlineSync().ignore();
+ }
},
);
}
@@ -183,7 +189,7 @@ class CodeDisplayStore {
// traverse through all the codes and edit this tag's value
final relevantCodes = await _getCodesByTag(previousTag);
- final tasks = <Future>[];
+ final tasks = <Future<AddResult>>[];
for (final code in relevantCodes) {
final tags = code.display.tags;
@@ -194,10 +200,15 @@ class CodeDisplayStore {
code.copyWith(
display: code.display.copyWith(tags: tags),
),
+ shouldSync: false,
),
);
}
- await Future.wait(tasks);
+ final results = await Future.wait(tasks);
+ if (results.any((r) => r != AddResult.duplicate) &&
+ AuthenticatorService.instance.getAccountMode() == AccountMode.online) {
+ AuthenticatorService.instance.onlineSync().ignore();
+ }
}
}
diff --git a/mobile/apps/auth/lib/ui/home/add_tag_sheet.dart b/mobile/apps/auth/lib/ui/home/add_tag_sheet.dart
index bb088ccb1d..b666c606d8 100644
--- a/mobile/apps/auth/lib/ui/home/add_tag_sheet.dart
+++ b/mobile/apps/auth/lib/ui/home/add_tag_sheet.dart
@@ -4,6 +4,7 @@ import 'package:ente_auth/l10n/l10n.dart';
import 'package:ente_auth/models/code.dart';
import 'package:ente_auth/onboarding/model/tag_enums.dart';
import 'package:ente_auth/onboarding/view/common/tag_chip.dart';
+import 'package:ente_auth/services/authenticator_service.dart';
import 'package:ente_auth/store/code_display_store.dart';
import 'package:ente_auth/store/code_store.dart';
import 'package:ente_auth/theme/ente_theme.dart';
@@ -70,10 +71,16 @@ class _AddTagSheetState extends State<AddTagSheet> {
code.copyWith(
display: code.display.copyWith(tags: updatedTags.toList()),
),
+ shouldSync: false,
);
});
- await Future.wait(updateFutures);
+ final updateResults = await Future.wait(updateFutures);
+ final hasChanges = updateResults.any((r) => r != AddResult.duplicate);
+ if (hasChanges &&
+ AuthenticatorService.instance.getAccountMode() == AccountMode.online) {
+ AuthenticatorService.instance.onlineSync().ignore();
+ }
if (mounted) {
Navigator.of(context).pop();
}
diff --git a/mobile/apps/auth/lib/ui/home_page.dart b/mobile/apps/auth/lib/ui/home_page.dart
index 3b5e157966..a777bd546b 100644
--- a/mobile/apps/auth/lib/ui/home_page.dart
+++ b/mobile/apps/auth/lib/ui/home_page.dart
@@ -15,6 +15,7 @@ import 'package:ente_auth/models/code.dart';
import 'package:ente_auth/onboarding/model/tag_enums.dart';
import 'package:ente_auth/onboarding/view/common/tag_chip.dart';
import 'package:ente_auth/onboarding/view/setup_enter_secret_key_page.dart';
+import 'package:ente_auth/services/authenticator_service.dart';
import 'package:ente_auth/services/local_backup_service.dart';
import 'package:ente_auth/services/preference_service.dart';
import 'package:ente_auth/store/code_display_store.dart';
@@ -160,6 +161,20 @@ class _HomePageState extends State<HomePage> {
});
}
+ Future<void> _saveCodesWithSingleSync(List<Code> updatedCodes) async {
+ if (updatedCodes.isEmpty) {
+ return;
+ }
+ final results = await Future.wait(
+ updatedCodes.map(
+ (code) => CodeStore.instance.addCode(code, shouldSync: false),
+ ),
+ );
+ if (results.any((result) => result != AddResult.duplicate)) {
+ AuthenticatorService.instance.onlineSync().ignore();
+ }
+ }
+
Future<void> _onRestoreSelectedPressed() async {
final selectedIds = _codeDisplayStore.selectedCodeIds.value;
if (selectedIds.isEmpty) return;
@@ -171,11 +186,13 @@ class _HomePageState extends State<HomePage> {
?.where((c) => selectedIds.contains(c.selectionKey))
.toList() ??
[];
- for (final code in codesToRestore) {
- final updatedCode =
- code.copyWith(display: code.display.copyWith(trashed: false));
- unawaited(CodeStore.instance.addCode(updatedCode));
- }
+ final updatedCodes = codesToRestore
+ .map(
+ (code) =>
+ code.copyWith(display: code.display.copyWith(trashed: false)),
+ )
+ .toList();
+ await _saveCodesWithSingleSync(updatedCodes);
} catch (e) {
if (mounted) {
showGenericErrorDialog(context: context, error: e).ignore();
@@ -285,46 +302,55 @@ class _HomePageState extends State<HomePage> {
[];
if (codesToUpdate.isEmpty) return;
- // Determine the state of the current selection (pinned/unpinned)
- final bool allArePinned = codesToUpdate.every((code) => code.isPinned);
-
- if (allArePinned) {
- // if all are pinned, unpin all
- for (final code in codesToUpdate) {
- final updatedCode =
- code.copyWith(display: code.display.copyWith(pinned: false));
- unawaited(CodeStore.instance.addCode(updatedCode));
- }
+ try {
+ // Determine the state of the current selection (pinned/unpinned)
+ final bool allArePinned = codesToUpdate.every((code) => code.isPinned);
+ final updatedCodes = <Code>[];
+
+ if (allArePinned) {
+ // if all are pinned, unpin all
+ for (final code in codesToUpdate) {
+ updatedCodes.add(
+ code.copyWith(display: code.display.copyWith(pinned: false)));
+ }
- if (codesToUpdate.length == 1) {
- showToast(
- context,
- context.l10n.unpinnedCodeMessage(codesToUpdate.first.issuer),
- );
+ if (codesToUpdate.length == 1) {
+ showToast(
+ context,
+ context.l10n.unpinnedCodeMessage(codesToUpdate.first.issuer),
+ );
+ } else {
+ showToast(context, context.l10n.unpinnedCount(codesToUpdate.length));
+ }
} else {
- showToast(context, context.l10n.unpinnedCount(codesToUpdate.length));
- }
- } else {
- int pinnedCount = 0;
- for (final code in codesToUpdate) {
- if (!code.isPinned) {
- // Only pin the codes that are currently unpinned
- final updatedCode =
- code.copyWith(display: code.display.copyWith(pinned: true));
- unawaited(CodeStore.instance.addCode(updatedCode));
- pinnedCount++;
+ int pinnedCount = 0;
+ for (final code in codesToUpdate) {
+ if (!code.isPinned) {
+ // Only pin the codes that are currently unpinned
+ updatedCodes.add(
+ code.copyWith(display: code.display.copyWith(pinned: true)),
+ );
+ pinnedCount++;
+ }
+ }
+
+ if (pinnedCount == 1) {
+ final pinnedCode = codesToUpdate.firstWhere((c) => !c.isPinned);
+ showToast(context, context.l10n.pinnedCodeMessage(pinnedCode.issuer));
+ } else if (pinnedCount > 0) {
+ showToast(context, context.l10n.pinnedCount(pinnedCount));
}
}
- if (pinnedCount == 1) {
- final pinnedCode = codesToUpdate.firstWhere((c) => !c.isPinned);
- showToast(context, context.l10n.pinnedCodeMessage(pinnedCode.issuer));
- } else if (pinnedCount > 0) {
- showToast(context, context.l10n.pinnedCount(pinnedCount));
+ await _saveCodesWithSingleSync(updatedCodes);
+ } catch (e, s) {
+ _logger.severe('Failed to update pin state for selected code(s)', e, s);
+ if (mounted) {
+ showGenericErrorDialog(context: context, error: e).ignore();
}
+ } finally {
+ _codeDisplayStore.clearSelection();
}
-
- _codeDisplayStore.clearSelection();
}
Future<void> _onUnpinSelectedPressed() async {
@@ -337,25 +363,37 @@ class _HomePageState extends State<HomePage> {
[];
if (codesToUpdate.isEmpty) return;
- int unpinnedCount = 0;
- for (final code in codesToUpdate) {
- if (code.isPinned) {
- // only unpin the codes that are currently pinned
- final updatedCode =
- code.copyWith(display: code.display.copyWith(pinned: false));
- unawaited(CodeStore.instance.addCode(updatedCode));
- unpinnedCount++;
+ try {
+ int unpinnedCount = 0;
+ final updatedCodes = <Code>[];
+ for (final code in codesToUpdate) {
+ if (code.isPinned) {
+ // only unpin the codes that are currently pinned
+ updatedCodes.add(
+ code.copyWith(display: code.display.copyWith(pinned: false)));
+ unpinnedCount++;
+ }
}
- }
- if (unpinnedCount == 1) {
- final unpinnedCode = codesToUpdate.firstWhere((c) => c.isPinned);
- showToast(context, context.l10n.unpinnedCodeMessage(unpinnedCode.issuer));
- } else if (unpinnedCount > 0) {
- showToast(context, context.l10n.unpinnedCount(unpinnedCount));
- }
+ if (unpinnedCount == 1) {
+ final unpinnedCode = codesToUpdate.firstWhere((c) => c.isPinned);
+ showToast(
+ context,
+ context.l10n.unpinnedCodeMessage(unpinnedCode.issuer),
+ );
+ } else if (unpinnedCount > 0) {
+ showToast(context, context.l10n.unpinnedCount(unpinnedCount));
+ }
- _codeDisplayStore.clearSelection();
+ await _saveCodesWithSingleSync(updatedCodes);
+ } catch (e, s) {
+ _logger.severe('Failed to unpin selected code(s)', e, s);
+ if (mounted) {
+ showGenericErrorDialog(context: context, error: e).ignore();
+ }
+ } finally {
+ _codeDisplayStore.clearSelection();
+ }
}
Future<void> _onTrashSelectedPressed() async {
@@ -394,13 +432,14 @@ class _HomePageState extends State<HomePage> {
?.where((c) => selectedIds.contains(c.selectionKey))
.toList() ??
[];
-
- for (final code in codesToTrash) {
- final updatedCode = code.copyWith(
- display: code.display.copyWith(trashed: true),
- );
- unawaited(CodeStore.instance.addCode(updatedCode));
- }
+ final updatedCodes = codesToTrash
+ .map(
+ (code) => code.copyWith(
+ display: code.display.copyWith(trashed: true),
+ ),
+ )
+ .toList();
+ await _saveCodesWithSingleSync(updatedCodes);
} catch (e) {
_logger.severe('Failed to trash code(s): ${e.toString()}');
if (mounted) {