getLastSyncTime method

Future<DateTime?> getLastSyncTime(
  1. String userId
)

Gets the most recent last sync time across all registered entities for the specified user.

This method queries the sync metadata of all registered entity managers and returns the most recent lastSyncTime found. Returns null if no sync has ever been performed for this user across any entity type.

This is useful for displaying when the last synchronization occurred in UI components.

Example:

final lastSync = await Datum.instance.getLastSyncTime('user123');
if (lastSync != null) {
  print('Last sync was ${DateTime.now().difference(lastSync).inMinutes} minutes ago');
} else {
  print('No sync has been performed yet');
}

Implementation

Future<DateTime?> getLastSyncTime(String userId) async {
  if (_managers.isEmpty) return null;

  DateTime? mostRecentSync;
  for (final manager in _managers.allManagers) {
    try {
      final metadata = await manager.localAdapter.getSyncMetadata(userId);
      if (metadata?.lastSyncTime != null) {
        if (mostRecentSync == null || metadata!.lastSyncTime!.isAfter(mostRecentSync)) {
          mostRecentSync = metadata!.lastSyncTime!;
        }
      }
    } catch (e) {
      // Continue with other managers if one fails
      logger.debug('Failed to get sync metadata from ${manager.runtimeType}: $e');
    }
  }
  return mostRecentSync;
}