Phase 7 - Added Firebase layer

This commit is contained in:
gitea
2025-11-05 21:53:38 +01:00
parent 13ec9e5a9d
commit 5d3f8c68bd
14 changed files with 1255 additions and 5 deletions
+28
View File
@@ -1,8 +1,10 @@
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart';
import '../local/local_storage_service.dart';
import '../sync/sync_engine.dart';
import '../firebase/firebase_service.dart';
import 'models/user.dart';
/// Exception thrown when session operations fail.
@@ -37,6 +39,9 @@ class SessionService {
/// Sync engine for coordinating sync operations (optional).
final SyncEngine? _syncEngine;
/// Firebase service for optional cloud sync (optional).
final FirebaseService? _firebaseService;
/// Map of user IDs to their session storage paths.
final Map<String, String> _userDbPaths = {};
@@ -53,15 +58,18 @@ class SessionService {
///
/// [localStorage] - Local storage service for data persistence.
/// [syncEngine] - Optional sync engine for coordinating sync operations.
/// [firebaseService] - Optional Firebase service for cloud sync.
/// [testDbPath] - Optional database path for testing.
/// [testCacheDir] - Optional cache directory for testing.
SessionService({
required LocalStorageService localStorage,
SyncEngine? syncEngine,
FirebaseService? firebaseService,
String? testDbPath,
Directory? testCacheDir,
}) : _localStorage = localStorage,
_syncEngine = syncEngine,
_firebaseService = firebaseService,
_testDbPath = testDbPath,
_testCacheDir = testCacheDir;
@@ -101,6 +109,16 @@ class SessionService {
// Create user-specific storage paths
await _setupUserStorage(user);
// Sync with Firebase if enabled
if (_firebaseService != null && _firebaseService!.isEnabled) {
try {
await _firebaseService!.syncItemsFromFirestore(user.id);
} catch (e) {
// Log error but don't fail login - offline-first behavior
debugPrint('Warning: Failed to sync from Firebase on login: $e');
}
}
// Set as current user
_currentUser = user;
@@ -123,6 +141,16 @@ class SessionService {
try {
final userId = _currentUser!.id;
// Sync to Firebase before logout if enabled
if (_firebaseService != null && _firebaseService!.isEnabled) {
try {
await _firebaseService!.syncItemsToFirestore(userId);
} catch (e) {
// Log error but don't fail logout - offline-first behavior
debugPrint('Warning: Failed to sync to Firebase on logout: $e');
}
}
// Clear user-specific data if requested
if (clearCache) {
await _clearUserData(userId);