mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-09-11 18:29:12 +02:00
feat adds proper syncing feature
Signed-off-by: Menno van Leeuwen <menno@vleeuwen.me>
This commit is contained in:
106
lib/providers/simple_sync_provider.dart
Normal file
106
lib/providers/simple_sync_provider.dart
Normal 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'}';
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user