feat adds proper syncing feature

Signed-off-by: Menno van Leeuwen <menno@vleeuwen.me>
This commit is contained in:
2025-08-27 20:51:29 +02:00
parent b0d5130cbf
commit 2017fd097d
22 changed files with 1518 additions and 3258 deletions

View File

@@ -0,0 +1,106 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import '../services/database_sync_service.dart';
class SimpleSyncProvider with ChangeNotifier {
final DatabaseSyncService _syncService = DatabaseSyncService();
// Callback for UI refresh after sync
VoidCallback? _onSyncCompleteCallback;
// Getters
SyncStatus get status => _syncService.status;
String? get lastError => _syncService.lastError;
DateTime? get lastSyncTime => _syncService.lastSyncTime;
bool get isConfigured => _syncService.isConfigured;
bool get isSyncing => status == SyncStatus.downloading ||
status == SyncStatus.merging ||
status == SyncStatus.uploading;
// Configuration getters
String? get serverUrl => _syncService.serverUrl;
String? get username => _syncService.username;
String? get password => _syncService.password;
String? get remotePath => _syncService.remotePath;
SimpleSyncProvider() {
// Set up callbacks to notify listeners
_syncService.onStatusChanged = (_) => notifyListeners();
_syncService.onError = (_) => notifyListeners();
_syncService.onSyncCompleted = () {
notifyListeners();
// Trigger UI refresh callback if set
_onSyncCompleteCallback?.call();
};
// Load saved configuration and notify listeners when done
_loadConfiguration();
}
/// Set callback to refresh UI data after sync completes
void setOnSyncCompleteCallback(VoidCallback? callback) {
_onSyncCompleteCallback = callback;
}
Future<void> _loadConfiguration() async {
await _syncService.loadSavedConfiguration();
notifyListeners(); // Notify UI that configuration might be available
}
Future<void> configure({
required String serverUrl,
required String username,
required String password,
required String remotePath,
}) async {
_syncService.configure(
serverUrl: serverUrl,
username: username,
password: password,
remotePath: remotePath,
);
notifyListeners();
}
Future<bool> testConnection() async {
return await _syncService.testConnection();
}
Future<void> syncDatabase() async {
if (!isConfigured) {
throw Exception('Sync not configured');
}
try {
await _syncService.syncDatabase();
} catch (e) {
if (kDebugMode) {
print('SupplementsLog: Sync failed in provider: $e');
}
rethrow;
}
}
void clearError() {
_syncService.clearError();
notifyListeners();
}
String getStatusText() {
switch (status) {
case SyncStatus.idle:
return 'Ready to sync';
case SyncStatus.downloading:
return 'Downloading remote database...';
case SyncStatus.merging:
return 'Merging databases...';
case SyncStatus.uploading:
return 'Uploading database...';
case SyncStatus.completed:
return 'Sync completed successfully';
case SyncStatus.error:
return 'Sync failed: ${lastError ?? 'Unknown error'}';
}
}
}