mudular code ready to fork
This commit is contained in:
@@ -5,22 +5,11 @@ import 'package:firebase_storage/firebase_storage.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart' as firebase_auth;
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:firebase_analytics/firebase_analytics.dart';
|
||||
import '../../core/exceptions/firebase_exception.dart' show FirebaseServiceException;
|
||||
import '../local/local_storage_service.dart';
|
||||
import '../local/models/item.dart';
|
||||
import 'models/firebase_config.dart';
|
||||
|
||||
/// Exception thrown when Firebase operations fail.
|
||||
class FirebaseException implements Exception {
|
||||
/// Error message.
|
||||
final String message;
|
||||
|
||||
/// Creates a [FirebaseException] with the provided message.
|
||||
FirebaseException(this.message);
|
||||
|
||||
@override
|
||||
String toString() => 'FirebaseException: $message';
|
||||
}
|
||||
|
||||
/// Service for Firebase integration (optional cloud sync, storage, auth, notifications, analytics).
|
||||
///
|
||||
/// This service provides:
|
||||
@@ -86,7 +75,7 @@ class FirebaseService {
|
||||
/// Must be called before using any Firebase services.
|
||||
/// If Firebase is disabled, this method does nothing.
|
||||
///
|
||||
/// Throws [FirebaseException] if initialization fails.
|
||||
/// Throws [FirebaseServiceException] if initialization fails.
|
||||
Future<void> initialize() async {
|
||||
if (!config.enabled) {
|
||||
return; // Firebase disabled, nothing to initialize
|
||||
@@ -135,7 +124,7 @@ class FirebaseService {
|
||||
|
||||
_initialized = true;
|
||||
} catch (e) {
|
||||
throw FirebaseException('Failed to initialize Firebase: $e');
|
||||
throw FirebaseServiceException('Failed to initialize Firebase: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,17 +135,17 @@ class FirebaseService {
|
||||
///
|
||||
/// Returns the Firebase Auth user.
|
||||
///
|
||||
/// Throws [FirebaseException] if auth is disabled or login fails.
|
||||
/// Throws [FirebaseServiceException] if auth is disabled or login fails.
|
||||
Future<firebase_auth.User> loginWithEmailPassword({
|
||||
required String email,
|
||||
required String password,
|
||||
}) async {
|
||||
if (!config.enabled || !config.authEnabled) {
|
||||
throw FirebaseException('Firebase Auth is not enabled');
|
||||
throw FirebaseServiceException('Firebase Auth is not enabled');
|
||||
}
|
||||
|
||||
if (!_initialized || _auth == null) {
|
||||
throw FirebaseException(
|
||||
throw FirebaseServiceException(
|
||||
'Firebase not initialized. Call initialize() first.');
|
||||
}
|
||||
|
||||
@@ -168,20 +157,20 @@ class FirebaseService {
|
||||
_firebaseUser = credential.user;
|
||||
return _firebaseUser!;
|
||||
} catch (e) {
|
||||
throw FirebaseException('Failed to login: $e');
|
||||
throw FirebaseServiceException('Failed to login: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Logs out the current user.
|
||||
///
|
||||
/// Throws [FirebaseException] if auth is disabled or logout fails.
|
||||
/// Throws [FirebaseServiceException] if auth is disabled or logout fails.
|
||||
Future<void> logout() async {
|
||||
if (!config.enabled || !config.authEnabled) {
|
||||
throw FirebaseException('Firebase Auth is not enabled');
|
||||
throw FirebaseServiceException('Firebase Auth is not enabled');
|
||||
}
|
||||
|
||||
if (!_initialized || _auth == null) {
|
||||
throw FirebaseException(
|
||||
throw FirebaseServiceException(
|
||||
'Firebase not initialized. Call initialize() first.');
|
||||
}
|
||||
|
||||
@@ -189,7 +178,7 @@ class FirebaseService {
|
||||
await _auth!.signOut();
|
||||
_firebaseUser = null;
|
||||
} catch (e) {
|
||||
throw FirebaseException('Failed to logout: $e');
|
||||
throw FirebaseServiceException('Failed to logout: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,14 +186,14 @@ class FirebaseService {
|
||||
///
|
||||
/// [userId] - User ID to associate items with (for multi-user support).
|
||||
///
|
||||
/// Throws [FirebaseException] if Firestore is disabled or sync fails.
|
||||
/// Throws [FirebaseServiceException] if Firestore is disabled or sync fails.
|
||||
Future<void> syncItemsToFirestore(String userId) async {
|
||||
if (!config.enabled || !config.firestoreEnabled) {
|
||||
throw FirebaseException('Firestore is not enabled');
|
||||
throw FirebaseServiceException('Firestore is not enabled');
|
||||
}
|
||||
|
||||
if (!_initialized || _firestore == null) {
|
||||
throw FirebaseException(
|
||||
throw FirebaseServiceException(
|
||||
'Firestore not initialized. Call initialize() first.');
|
||||
}
|
||||
|
||||
@@ -229,7 +218,7 @@ class FirebaseService {
|
||||
|
||||
await batch.commit();
|
||||
} catch (e) {
|
||||
throw FirebaseException('Failed to sync items to Firestore: $e');
|
||||
throw FirebaseServiceException('Failed to sync items to Firestore: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,14 +226,14 @@ class FirebaseService {
|
||||
///
|
||||
/// [userId] - User ID to fetch items for.
|
||||
///
|
||||
/// Throws [FirebaseException] if Firestore is disabled or sync fails.
|
||||
/// Throws [FirebaseServiceException] if Firestore is disabled or sync fails.
|
||||
Future<void> syncItemsFromFirestore(String userId) async {
|
||||
if (!config.enabled || !config.firestoreEnabled) {
|
||||
throw FirebaseException('Firestore is not enabled');
|
||||
throw FirebaseServiceException('Firestore is not enabled');
|
||||
}
|
||||
|
||||
if (!_initialized || _firestore == null) {
|
||||
throw FirebaseException(
|
||||
throw FirebaseServiceException(
|
||||
'Firestore not initialized. Call initialize() first.');
|
||||
}
|
||||
|
||||
@@ -271,7 +260,7 @@ class FirebaseService {
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
throw FirebaseException('Failed to sync items from Firestore: $e');
|
||||
throw FirebaseServiceException('Failed to sync items from Firestore: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,14 +271,14 @@ class FirebaseService {
|
||||
///
|
||||
/// Returns the download URL.
|
||||
///
|
||||
/// Throws [FirebaseException] if Storage is disabled or upload fails.
|
||||
/// Throws [FirebaseServiceException] if Storage is disabled or upload fails.
|
||||
Future<String> uploadFile(File file, String path) async {
|
||||
if (!config.enabled || !config.storageEnabled) {
|
||||
throw FirebaseException('Firebase Storage is not enabled');
|
||||
throw FirebaseServiceException('Firebase Storage is not enabled');
|
||||
}
|
||||
|
||||
if (!_initialized || _storage == null) {
|
||||
throw FirebaseException(
|
||||
throw FirebaseServiceException(
|
||||
'Firebase Storage not initialized. Call initialize() first.');
|
||||
}
|
||||
|
||||
@@ -298,7 +287,7 @@ class FirebaseService {
|
||||
await ref.putFile(file);
|
||||
return await ref.getDownloadURL();
|
||||
} catch (e) {
|
||||
throw FirebaseException('Failed to upload file: $e');
|
||||
throw FirebaseServiceException('Failed to upload file: $e');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user