mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-12-08 06:02:34 +00:00
107 lines
3.0 KiB
Dart
107 lines
3.0 KiB
Dart
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'}';
|
|
}
|
|
}
|
|
}
|