Compare commits
2
Commits
5f8d8330f5
...
3debb6ad7d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3debb6ad7d | ||
|
|
8c6bf598f5 |
@@ -1,22 +1,8 @@
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import '../core/exceptions/invalid_environment_exception.dart';
|
||||
import 'app_config.dart';
|
||||
import '../data/firebase/models/firebase_config.dart';
|
||||
|
||||
/// Exception thrown when an invalid environment is provided to [ConfigLoader].
|
||||
class InvalidEnvironmentException implements Exception {
|
||||
/// The invalid environment that was provided.
|
||||
final String environment;
|
||||
|
||||
/// Creates an [InvalidEnvironmentException] with the provided environment.
|
||||
InvalidEnvironmentException(this.environment);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'InvalidEnvironmentException: Invalid environment "$environment". '
|
||||
'Valid environments are: dev, prod';
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads application configuration based on the specified environment.
|
||||
///
|
||||
/// This class provides a factory method to load configuration for either
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import '../config/config_loader.dart';
|
||||
import '../data/local/local_storage_service.dart';
|
||||
import '../data/nostr/nostr_service.dart';
|
||||
import '../data/sync/sync_engine.dart';
|
||||
import '../data/firebase/firebase_service.dart';
|
||||
import '../data/session/session_service.dart';
|
||||
import '../data/immich/immich_service.dart';
|
||||
import 'app_services.dart';
|
||||
import 'service_locator.dart';
|
||||
import 'logger.dart';
|
||||
|
||||
/// Initializes all application services.
|
||||
///
|
||||
/// Handles the complete initialization sequence:
|
||||
/// 1. Load environment configuration
|
||||
/// 2. Initialize Firebase (if enabled)
|
||||
/// 3. Initialize local storage
|
||||
/// 4. Initialize Nostr service and sync engine
|
||||
/// 5. Initialize Immich service
|
||||
/// 6. Initialize Firebase service (if enabled)
|
||||
/// 7. Initialize session service
|
||||
/// 8. Register all services with ServiceLocator
|
||||
class AppInitializer {
|
||||
/// Initializes all application services.
|
||||
///
|
||||
/// [environment] - The environment to use ('dev' or 'prod').
|
||||
///
|
||||
/// Returns an [AppServices] instance with all initialized services.
|
||||
///
|
||||
/// Throws if initialization fails.
|
||||
static Future<AppServices> initialize({
|
||||
String environment = 'dev',
|
||||
}) async {
|
||||
Logger.info('Starting application initialization...');
|
||||
|
||||
// Load .env file (optional - falls back to defaults if not found)
|
||||
try {
|
||||
await dotenv.load(fileName: '.env');
|
||||
Logger.debug('.env file loaded successfully');
|
||||
} catch (e) {
|
||||
Logger.warning('.env file not found, using default values: $e');
|
||||
}
|
||||
|
||||
// Load configuration based on environment
|
||||
final config = ConfigLoader.load(environment);
|
||||
Logger.setEnabled(config.enableLogging);
|
||||
Logger.info('Configuration loaded for environment: $environment');
|
||||
|
||||
// Initialize Firebase if enabled
|
||||
if (config.firebaseConfig.enabled) {
|
||||
try {
|
||||
await Firebase.initializeApp();
|
||||
Logger.info('Firebase initialized successfully');
|
||||
} catch (e) {
|
||||
Logger.error(
|
||||
'Firebase initialization failed: $e',
|
||||
e,
|
||||
);
|
||||
Logger.warning(
|
||||
'Note: Firebase requires google-services.json (Android) and GoogleService-Info.plist (iOS)',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize local storage service
|
||||
Logger.debug('Initializing local storage service...');
|
||||
final storageService = LocalStorageService();
|
||||
try {
|
||||
await storageService.initialize();
|
||||
Logger.info('Local storage service initialized');
|
||||
} catch (e) {
|
||||
Logger.error('Failed to initialize storage: $e', e);
|
||||
rethrow;
|
||||
}
|
||||
|
||||
// Initialize Nostr service and sync engine
|
||||
Logger.debug('Initializing Nostr service...');
|
||||
final nostrService = NostrService();
|
||||
final nostrKeyPair = nostrService.generateKeyPair();
|
||||
|
||||
final syncEngine = SyncEngine(
|
||||
localStorage: storageService,
|
||||
nostrService: nostrService,
|
||||
nostrKeyPair: nostrKeyPair,
|
||||
);
|
||||
Logger.info('Nostr service and sync engine initialized');
|
||||
|
||||
// Load relays from config
|
||||
for (final relayUrl in config.nostrRelays) {
|
||||
nostrService.addRelay(relayUrl);
|
||||
}
|
||||
Logger.debug('Loaded ${config.nostrRelays.length} relay(s) from config');
|
||||
|
||||
// Initialize Immich service
|
||||
Logger.debug('Initializing Immich service...');
|
||||
final immichService = ImmichService(
|
||||
baseUrl: config.immichBaseUrl,
|
||||
apiKey: config.immichApiKey,
|
||||
localStorage: storageService,
|
||||
);
|
||||
Logger.info('Immich service initialized');
|
||||
|
||||
// Initialize Firebase service if enabled
|
||||
FirebaseService? firebaseService;
|
||||
if (config.firebaseConfig.enabled) {
|
||||
try {
|
||||
Logger.debug('Initializing Firebase service...');
|
||||
firebaseService = FirebaseService(
|
||||
config: config.firebaseConfig,
|
||||
localStorage: storageService,
|
||||
);
|
||||
await firebaseService.initialize();
|
||||
Logger.info('Firebase service initialized: ${firebaseService.isEnabled}');
|
||||
} catch (e) {
|
||||
Logger.error('Firebase service initialization failed: $e', e);
|
||||
firebaseService = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize SessionService with Firebase and Nostr integration
|
||||
Logger.debug('Initializing session service...');
|
||||
final sessionService = SessionService(
|
||||
localStorage: storageService,
|
||||
syncEngine: syncEngine,
|
||||
firebaseService: firebaseService,
|
||||
nostrService: nostrService,
|
||||
);
|
||||
Logger.info('Session service initialized');
|
||||
|
||||
// Create AppServices container
|
||||
final appServices = AppServices(
|
||||
localStorageService: storageService,
|
||||
nostrService: nostrService,
|
||||
syncEngine: syncEngine,
|
||||
firebaseService: firebaseService,
|
||||
sessionService: sessionService,
|
||||
immichService: immichService,
|
||||
);
|
||||
|
||||
// Register all services with ServiceLocator
|
||||
ServiceLocator.instance.registerServices(
|
||||
localStorageService: storageService,
|
||||
nostrService: nostrService,
|
||||
syncEngine: syncEngine,
|
||||
firebaseService: firebaseService,
|
||||
sessionService: sessionService,
|
||||
immichService: immichService,
|
||||
);
|
||||
|
||||
Logger.info('Application initialization completed successfully');
|
||||
return appServices;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import '../data/local/local_storage_service.dart';
|
||||
import '../data/nostr/nostr_service.dart';
|
||||
import '../data/sync/sync_engine.dart';
|
||||
import '../data/firebase/firebase_service.dart';
|
||||
import '../data/session/session_service.dart';
|
||||
import '../data/immich/immich_service.dart';
|
||||
|
||||
/// Container for all application services.
|
||||
///
|
||||
/// Holds references to all initialized services and provides
|
||||
/// a convenient way to dispose of them.
|
||||
class AppServices {
|
||||
/// Local storage service.
|
||||
final LocalStorageService localStorageService;
|
||||
|
||||
/// Nostr service.
|
||||
final NostrService? nostrService;
|
||||
|
||||
/// Sync engine.
|
||||
final SyncEngine? syncEngine;
|
||||
|
||||
/// Firebase service.
|
||||
final FirebaseService? firebaseService;
|
||||
|
||||
/// Session service.
|
||||
final SessionService? sessionService;
|
||||
|
||||
/// Immich service.
|
||||
final ImmichService? immichService;
|
||||
|
||||
/// Creates an [AppServices] instance.
|
||||
AppServices({
|
||||
required this.localStorageService,
|
||||
this.nostrService,
|
||||
this.syncEngine,
|
||||
this.firebaseService,
|
||||
this.sessionService,
|
||||
this.immichService,
|
||||
});
|
||||
|
||||
/// Disposes of all services that need cleanup.
|
||||
Future<void> dispose() async {
|
||||
syncEngine?.dispose();
|
||||
nostrService?.dispose();
|
||||
firebaseService?.dispose();
|
||||
|
||||
// Close storage service
|
||||
try {
|
||||
await localStorageService.close();
|
||||
} catch (e) {
|
||||
// Ignore errors during cleanup
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/// Application-wide constants.
|
||||
class AppConstants {
|
||||
/// Application name.
|
||||
static const String appName = 'App Boilerplate';
|
||||
|
||||
/// Default connection timeout duration.
|
||||
static const Duration connectionTimeout = Duration(seconds: 3);
|
||||
|
||||
/// Default health check timeout duration.
|
||||
static const Duration healthCheckTimeout = Duration(seconds: 2);
|
||||
|
||||
/// Default retry delay for operations.
|
||||
static const Duration retryDelay = Duration(seconds: 1);
|
||||
|
||||
/// Maximum number of retries for failed operations.
|
||||
static const int maxRetries = 3;
|
||||
|
||||
/// Maximum queue size for sync operations.
|
||||
static const int maxQueueSize = 100;
|
||||
|
||||
/// Private constructor to prevent instantiation.
|
||||
AppConstants._();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/// Route names for navigation.
|
||||
class RouteNames {
|
||||
/// Home route.
|
||||
static const String home = '/home';
|
||||
|
||||
/// Immich route.
|
||||
static const String immich = '/immich';
|
||||
|
||||
/// Nostr Events route.
|
||||
static const String nostrEvents = '/nostr-events';
|
||||
|
||||
/// Session route.
|
||||
static const String session = '/session';
|
||||
|
||||
/// Settings route.
|
||||
static const String settings = '/settings';
|
||||
|
||||
/// Relay Management route.
|
||||
static const String relayManagement = '/relay-management';
|
||||
|
||||
/// Private constructor to prevent instantiation.
|
||||
RouteNames._();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
/// Barrel file for all exceptions.
|
||||
///
|
||||
/// Import this file to get access to all exception classes.
|
||||
export 'firebase_exception.dart' show FirebaseServiceException;
|
||||
export 'immich_exception.dart';
|
||||
export 'invalid_environment_exception.dart';
|
||||
export 'nostr_exception.dart';
|
||||
export 'session_exception.dart';
|
||||
export 'sync_exception.dart';
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
/// Exception thrown when Firebase service operations fail.
|
||||
///
|
||||
/// Note: This is different from Firebase SDK's FirebaseException.
|
||||
/// This exception is used for our FirebaseService wrapper.
|
||||
class FirebaseServiceException implements Exception {
|
||||
/// Error message.
|
||||
final String message;
|
||||
|
||||
/// Creates a [FirebaseServiceException] with the provided message.
|
||||
FirebaseServiceException(this.message);
|
||||
|
||||
@override
|
||||
String toString() => 'FirebaseServiceException: $message';
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
/// Exception thrown when Immich API operations fail.
|
||||
class ImmichException implements Exception {
|
||||
/// Error message.
|
||||
final String message;
|
||||
|
||||
/// HTTP status code if available.
|
||||
final int? statusCode;
|
||||
|
||||
/// Creates an [ImmichException] with the provided message.
|
||||
ImmichException(this.message, [this.statusCode]);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
if (statusCode != null) {
|
||||
return 'ImmichException: $message (Status: $statusCode)';
|
||||
}
|
||||
return 'ImmichException: $message';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
/// Exception thrown when an invalid environment is provided to [ConfigLoader].
|
||||
class InvalidEnvironmentException implements Exception {
|
||||
/// The invalid environment that was provided.
|
||||
final String environment;
|
||||
|
||||
/// Creates an [InvalidEnvironmentException] with the provided environment.
|
||||
InvalidEnvironmentException(this.environment);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'InvalidEnvironmentException: Invalid environment "$environment". '
|
||||
'Valid environments are: dev, prod';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
/// Exception thrown when Nostr operations fail.
|
||||
class NostrException implements Exception {
|
||||
/// Error message.
|
||||
final String message;
|
||||
|
||||
/// Creates a [NostrException] with the provided message.
|
||||
NostrException(this.message);
|
||||
|
||||
@override
|
||||
String toString() => 'NostrException: $message';
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
/// Exception thrown when session operations fail.
|
||||
class SessionException implements Exception {
|
||||
/// Error message.
|
||||
final String message;
|
||||
|
||||
/// Creates a [SessionException] with the provided message.
|
||||
SessionException(this.message);
|
||||
|
||||
@override
|
||||
String toString() => 'SessionException: $message';
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
/// Exception thrown when sync operations fail.
|
||||
class SyncException implements Exception {
|
||||
/// Error message.
|
||||
final String message;
|
||||
|
||||
/// Creates a [SyncException] with the provided message.
|
||||
SyncException(this.message);
|
||||
|
||||
@override
|
||||
String toString() => 'SyncException: $message';
|
||||
}
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// Log levels for different types of messages.
|
||||
enum LogLevel {
|
||||
/// Debug messages (only in debug mode).
|
||||
debug,
|
||||
|
||||
/// Informational messages.
|
||||
info,
|
||||
|
||||
/// Warning messages.
|
||||
warning,
|
||||
|
||||
/// Error messages (always shown).
|
||||
error,
|
||||
}
|
||||
|
||||
/// Centralized logging service for the application.
|
||||
///
|
||||
/// Provides consistent logging across the app with different log levels.
|
||||
/// In production, only errors are logged unless explicitly enabled.
|
||||
class Logger {
|
||||
/// Whether logging is enabled (can be configured via config).
|
||||
static bool _enabled = kDebugMode;
|
||||
|
||||
/// Enable or disable logging.
|
||||
static void setEnabled(bool enabled) {
|
||||
_enabled = enabled;
|
||||
}
|
||||
|
||||
/// Logs a message with the specified level.
|
||||
///
|
||||
/// [level] - The log level (debug, info, warning, error).
|
||||
/// [message] - The message to log.
|
||||
/// [error] - Optional error object.
|
||||
/// [stackTrace] - Optional stack trace.
|
||||
static void log(
|
||||
LogLevel level,
|
||||
String message, [
|
||||
Object? error,
|
||||
StackTrace? stackTrace,
|
||||
]) {
|
||||
// Always log errors, respect enabled flag for others
|
||||
if (!_enabled && level != LogLevel.error) {
|
||||
return;
|
||||
}
|
||||
|
||||
final prefix = '[${level.name.toUpperCase()}]';
|
||||
final timestamp = DateTime.now().toIso8601String();
|
||||
|
||||
debugPrint('$timestamp $prefix $message');
|
||||
|
||||
if (error != null) {
|
||||
debugPrint('$timestamp $prefix Error: $error');
|
||||
}
|
||||
|
||||
if (stackTrace != null) {
|
||||
debugPrint('$timestamp $prefix Stack trace: $stackTrace');
|
||||
}
|
||||
}
|
||||
|
||||
/// Logs a debug message (only in debug mode).
|
||||
static void debug(String message) {
|
||||
log(LogLevel.debug, message);
|
||||
}
|
||||
|
||||
/// Logs an informational message.
|
||||
static void info(String message) {
|
||||
log(LogLevel.info, message);
|
||||
}
|
||||
|
||||
/// Logs a warning message.
|
||||
static void warning(String message, [Object? error]) {
|
||||
log(LogLevel.warning, message, error);
|
||||
}
|
||||
|
||||
/// Logs an error message (always shown).
|
||||
static void error(String message, [Object? error, StackTrace? stackTrace]) {
|
||||
log(LogLevel.error, message, error, stackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/// Service locator for dependency injection.
|
||||
///
|
||||
/// Provides a centralized registry for all application services,
|
||||
/// making it easy to access services throughout the app
|
||||
/// and swap implementations for testing.
|
||||
class ServiceLocator {
|
||||
/// Private constructor to prevent instantiation.
|
||||
ServiceLocator._();
|
||||
|
||||
/// Singleton instance.
|
||||
static final ServiceLocator instance = ServiceLocator._();
|
||||
|
||||
/// Local storage service.
|
||||
dynamic _localStorageService;
|
||||
|
||||
/// Nostr service.
|
||||
dynamic _nostrService;
|
||||
|
||||
/// Sync engine.
|
||||
dynamic _syncEngine;
|
||||
|
||||
/// Firebase service.
|
||||
dynamic _firebaseService;
|
||||
|
||||
/// Session service.
|
||||
dynamic _sessionService;
|
||||
|
||||
/// Immich service.
|
||||
dynamic _immichService;
|
||||
|
||||
/// Registers all services with the locator.
|
||||
///
|
||||
/// All services are optional and can be null if not configured.
|
||||
void registerServices({
|
||||
dynamic localStorageService,
|
||||
dynamic nostrService,
|
||||
dynamic syncEngine,
|
||||
dynamic firebaseService,
|
||||
dynamic sessionService,
|
||||
dynamic immichService,
|
||||
}) {
|
||||
_localStorageService = localStorageService;
|
||||
_nostrService = nostrService;
|
||||
_syncEngine = syncEngine;
|
||||
_firebaseService = firebaseService;
|
||||
_sessionService = sessionService;
|
||||
_immichService = immichService;
|
||||
}
|
||||
|
||||
/// Gets the local storage service.
|
||||
///
|
||||
/// Throws [StateError] if service is not registered.
|
||||
dynamic get localStorageService {
|
||||
if (_localStorageService == null) {
|
||||
throw StateError('LocalStorageService not registered');
|
||||
}
|
||||
return _localStorageService;
|
||||
}
|
||||
|
||||
/// Gets the Nostr service (nullable).
|
||||
dynamic get nostrService => _nostrService;
|
||||
|
||||
/// Gets the sync engine (nullable).
|
||||
dynamic get syncEngine => _syncEngine;
|
||||
|
||||
/// Gets the Firebase service (nullable).
|
||||
dynamic get firebaseService => _firebaseService;
|
||||
|
||||
/// Gets the session service (nullable).
|
||||
dynamic get sessionService => _sessionService;
|
||||
|
||||
/// Gets the Immich service (nullable).
|
||||
dynamic get immichService => _immichService;
|
||||
|
||||
/// Clears all registered services (useful for testing).
|
||||
void reset() {
|
||||
_localStorageService = null;
|
||||
_nostrService = null;
|
||||
_syncEngine = null;
|
||||
_firebaseService = null;
|
||||
_sessionService = null;
|
||||
_immichService = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,32 +1,14 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../../core/logger.dart';
|
||||
import '../../core/exceptions/immich_exception.dart';
|
||||
import '../local/local_storage_service.dart';
|
||||
import '../local/models/item.dart';
|
||||
import 'models/immich_asset.dart';
|
||||
import 'models/upload_response.dart';
|
||||
|
||||
/// Exception thrown when Immich API operations fail.
|
||||
class ImmichException implements Exception {
|
||||
/// Error message.
|
||||
final String message;
|
||||
|
||||
/// HTTP status code if available.
|
||||
final int? statusCode;
|
||||
|
||||
/// Creates an [ImmichException] with the provided message.
|
||||
ImmichException(this.message, [this.statusCode]);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
if (statusCode != null) {
|
||||
return 'ImmichException: $message (Status: $statusCode)';
|
||||
}
|
||||
return 'ImmichException: $message';
|
||||
}
|
||||
}
|
||||
|
||||
/// Service for interacting with Immich API.
|
||||
///
|
||||
/// This service provides:
|
||||
@@ -175,15 +157,15 @@ class ImmichService {
|
||||
}
|
||||
|
||||
final uploadUrl = '$_baseUrl$endpointPath';
|
||||
debugPrint('=== Immich Upload Request ===');
|
||||
debugPrint('URL: $uploadUrl');
|
||||
debugPrint('Base URL: $_baseUrl');
|
||||
debugPrint('File: $fileName, Size: ${await imageFile.length()} bytes');
|
||||
debugPrint('Device ID: $deviceId');
|
||||
debugPrint('Device Asset ID: $deviceAssetId');
|
||||
debugPrint('File Created At: $fileCreatedAtIso');
|
||||
debugPrint('File Modified At: $fileModifiedAtIso');
|
||||
debugPrint('Metadata: $metadataJson');
|
||||
Logger.debug('=== Immich Upload Request ===');
|
||||
Logger.debug('URL: $uploadUrl');
|
||||
Logger.debug('Base URL: $_baseUrl');
|
||||
Logger.debug('File: $fileName, Size: ${await imageFile.length()} bytes');
|
||||
Logger.debug('Device ID: $deviceId');
|
||||
Logger.debug('Device Asset ID: $deviceAssetId');
|
||||
Logger.debug('File Created At: $fileCreatedAtIso');
|
||||
Logger.debug('File Modified At: $fileModifiedAtIso');
|
||||
Logger.debug('Metadata: $metadataJson');
|
||||
|
||||
final response = await _dio.post(
|
||||
endpointPath,
|
||||
@@ -196,17 +178,17 @@ class ImmichService {
|
||||
),
|
||||
);
|
||||
|
||||
debugPrint('=== Immich Upload Response ===');
|
||||
debugPrint('Status Code: ${response.statusCode}');
|
||||
debugPrint('Response Data: ${response.data}');
|
||||
debugPrint('Response Headers: ${response.headers}');
|
||||
Logger.debug('=== Immich Upload Response ===');
|
||||
Logger.debug('Status Code: ${response.statusCode}');
|
||||
Logger.debug('Response Data: ${response.data}');
|
||||
Logger.debug('Response Headers: ${response.headers}');
|
||||
|
||||
if (response.statusCode != 200 && response.statusCode != 201) {
|
||||
final errorMessage = response.data is Map
|
||||
? (response.data as Map)['message']?.toString() ??
|
||||
response.statusMessage
|
||||
: response.statusMessage;
|
||||
debugPrint(
|
||||
Logger.error(
|
||||
'Upload failed with status ${response.statusCode}: $errorMessage');
|
||||
throw ImmichException(
|
||||
'Upload failed: $errorMessage',
|
||||
@@ -216,15 +198,15 @@ class ImmichService {
|
||||
|
||||
// Log the response data structure
|
||||
if (response.data is Map) {
|
||||
debugPrint('Response is Map with keys: ${(response.data as Map).keys}');
|
||||
debugPrint('Full response map: ${response.data}');
|
||||
Logger.debug('Response is Map with keys: ${(response.data as Map).keys}');
|
||||
Logger.debug('Full response map: ${response.data}');
|
||||
} else if (response.data is List) {
|
||||
debugPrint(
|
||||
Logger.debug(
|
||||
'Response is List with ${(response.data as List).length} items');
|
||||
debugPrint('First item: ${(response.data as List).first}');
|
||||
Logger.debug('First item: ${(response.data as List).first}');
|
||||
} else {
|
||||
debugPrint('Response type: ${response.data.runtimeType}');
|
||||
debugPrint('Response value: ${response.data}');
|
||||
Logger.debug('Response type: ${response.data.runtimeType}');
|
||||
Logger.debug('Response value: ${response.data}');
|
||||
}
|
||||
|
||||
// Handle response - it might be a single object or an array
|
||||
@@ -232,7 +214,7 @@ class ImmichService {
|
||||
if (response.data is List && (response.data as List).isNotEmpty) {
|
||||
// If response is an array, take the first item
|
||||
responseData = (response.data as List).first as Map<String, dynamic>;
|
||||
debugPrint('Using first item from array response');
|
||||
Logger.debug('Using first item from array response');
|
||||
} else if (response.data is Map) {
|
||||
responseData = response.data as Map<String, dynamic>;
|
||||
} else {
|
||||
@@ -243,24 +225,24 @@ class ImmichService {
|
||||
}
|
||||
|
||||
final uploadResponse = UploadResponse.fromJson(responseData);
|
||||
debugPrint('Parsed Upload Response:');
|
||||
debugPrint(' ID: ${uploadResponse.id}');
|
||||
debugPrint(' Duplicate: ${uploadResponse.duplicate}');
|
||||
Logger.debug('Parsed Upload Response:');
|
||||
Logger.debug(' ID: ${uploadResponse.id}');
|
||||
Logger.debug(' Duplicate: ${uploadResponse.duplicate}');
|
||||
|
||||
// Fetch full asset details to store complete metadata
|
||||
debugPrint('Fetching full asset details for ID: ${uploadResponse.id}');
|
||||
Logger.debug('Fetching full asset details for ID: ${uploadResponse.id}');
|
||||
try {
|
||||
final asset = await _getAssetById(uploadResponse.id);
|
||||
debugPrint('Fetched asset: ${asset.id}, ${asset.fileName}');
|
||||
Logger.debug('Fetched asset: ${asset.id}, ${asset.fileName}');
|
||||
|
||||
// Store metadata in local storage
|
||||
debugPrint('Storing asset metadata in local storage');
|
||||
Logger.debug('Storing asset metadata in local storage');
|
||||
await _storeAssetMetadata(asset);
|
||||
debugPrint('Asset metadata stored successfully');
|
||||
Logger.debug('Asset metadata stored successfully');
|
||||
} catch (e) {
|
||||
// Log error but don't fail the upload - asset was uploaded successfully
|
||||
debugPrint('Warning: Failed to fetch/store asset metadata: $e');
|
||||
debugPrint('Upload was successful, but metadata caching failed');
|
||||
Logger.warning('Failed to fetch/store asset metadata: $e');
|
||||
Logger.warning('Upload was successful, but metadata caching failed');
|
||||
}
|
||||
|
||||
return uploadResponse;
|
||||
@@ -558,9 +540,9 @@ class ImmichService {
|
||||
}
|
||||
|
||||
try {
|
||||
debugPrint('=== Immich Delete Assets ===');
|
||||
debugPrint('Asset IDs to delete: $assetIds');
|
||||
debugPrint('Count: ${assetIds.length}');
|
||||
Logger.debug('=== Immich Delete Assets ===');
|
||||
Logger.debug('Asset IDs to delete: $assetIds');
|
||||
Logger.debug('Count: ${assetIds.length}');
|
||||
|
||||
// DELETE /api/assets with ids in request body
|
||||
// According to Immich API: DELETE /api/assets with body: {"ids": ["uuid1", "uuid2", ...]}
|
||||
@@ -568,7 +550,7 @@ class ImmichService {
|
||||
'ids': assetIds,
|
||||
};
|
||||
|
||||
debugPrint('Request body: $requestBody');
|
||||
Logger.debug('Request body: $requestBody');
|
||||
|
||||
final response = await _dio.delete(
|
||||
'/api/assets',
|
||||
@@ -581,9 +563,9 @@ class ImmichService {
|
||||
),
|
||||
);
|
||||
|
||||
debugPrint('=== Immich Delete Response ===');
|
||||
debugPrint('Status Code: ${response.statusCode}');
|
||||
debugPrint('Response Data: ${response.data}');
|
||||
Logger.debug('=== Immich Delete Response ===');
|
||||
Logger.debug('Status Code: ${response.statusCode}');
|
||||
Logger.debug('Response Data: ${response.data}');
|
||||
|
||||
if (response.statusCode != 200 && response.statusCode != 204) {
|
||||
final errorMessage = response.data is Map
|
||||
@@ -601,11 +583,11 @@ class ImmichService {
|
||||
try {
|
||||
await _localStorage.deleteItem('immich_$assetId');
|
||||
} catch (e) {
|
||||
debugPrint('Warning: Failed to remove asset $assetId from cache: $e');
|
||||
Logger.warning('Failed to remove asset $assetId from cache: $e');
|
||||
}
|
||||
}
|
||||
|
||||
debugPrint('Successfully deleted ${assetIds.length} asset(s)');
|
||||
Logger.info('Successfully deleted ${assetIds.length} asset(s)');
|
||||
} on DioException catch (e) {
|
||||
final statusCode = e.response?.statusCode;
|
||||
final errorData = e.response?.data;
|
||||
|
||||
@@ -1,26 +1,15 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:web_socket_channel/web_socket_channel.dart';
|
||||
import 'package:nostr_tools/nostr_tools.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import '../../core/logger.dart';
|
||||
import '../../core/exceptions/nostr_exception.dart';
|
||||
import 'models/nostr_keypair.dart';
|
||||
import 'models/nostr_event.dart';
|
||||
import 'models/nostr_relay.dart';
|
||||
import 'models/nostr_profile.dart';
|
||||
|
||||
/// Exception thrown when Nostr operations fail.
|
||||
class NostrException implements Exception {
|
||||
/// Error message.
|
||||
final String message;
|
||||
|
||||
/// Creates a [NostrException] with the provided message.
|
||||
NostrException(this.message);
|
||||
|
||||
@override
|
||||
String toString() => 'NostrException: $message';
|
||||
}
|
||||
|
||||
/// Service for interacting with Nostr protocol.
|
||||
///
|
||||
/// This service provides:
|
||||
@@ -492,7 +481,7 @@ class NostrService {
|
||||
}
|
||||
} catch (e) {
|
||||
// Ignore parsing errors
|
||||
debugPrint('Error parsing profile event: $e');
|
||||
Logger.warning('Error parsing profile event: $e');
|
||||
}
|
||||
} else if (message['type'] == 'EOSE' &&
|
||||
message['subscription_id'] == reqId) {
|
||||
@@ -634,7 +623,7 @@ class NostrService {
|
||||
addedCount++;
|
||||
} catch (e) {
|
||||
// Skip invalid relay URLs
|
||||
debugPrint('Warning: Invalid relay URL from NIP-05: $relayUrl');
|
||||
Logger.warning('Invalid relay URL from NIP-05: $relayUrl');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import '../../core/logger.dart';
|
||||
import '../../core/exceptions/session_exception.dart';
|
||||
import '../local/local_storage_service.dart';
|
||||
import '../sync/sync_engine.dart';
|
||||
import '../firebase/firebase_service.dart';
|
||||
@@ -10,18 +11,6 @@ import '../nostr/models/nostr_keypair.dart';
|
||||
import '../nostr/models/nostr_profile.dart';
|
||||
import 'models/user.dart';
|
||||
|
||||
/// Exception thrown when session operations fail.
|
||||
class SessionException implements Exception {
|
||||
/// Error message.
|
||||
final String message;
|
||||
|
||||
/// Creates a [SessionException] with the provided message.
|
||||
SessionException(this.message);
|
||||
|
||||
@override
|
||||
String toString() => 'SessionException: $message';
|
||||
}
|
||||
|
||||
/// Service for managing user sessions, login, logout, and session isolation.
|
||||
///
|
||||
/// This service provides:
|
||||
@@ -124,7 +113,7 @@ class SessionService {
|
||||
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');
|
||||
Logger.warning('Failed to sync from Firebase on login: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,7 +163,7 @@ class SessionService {
|
||||
try {
|
||||
profile = await _nostrService!.fetchProfile(keyPair.publicKey);
|
||||
} catch (e) {
|
||||
debugPrint('Warning: Failed to fetch Nostr profile: $e');
|
||||
Logger.warning('Failed to fetch Nostr profile: $e');
|
||||
// Continue without profile - offline-first behavior
|
||||
}
|
||||
|
||||
@@ -195,7 +184,7 @@ class SessionService {
|
||||
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');
|
||||
Logger.warning('Failed to sync from Firebase on login: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,7 +219,7 @@ class SessionService {
|
||||
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');
|
||||
Logger.warning('Failed to sync to Firebase on logout: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -407,7 +396,7 @@ class SessionService {
|
||||
try {
|
||||
profile = await _nostrService!.fetchProfile(_currentUser!.id);
|
||||
} catch (e) {
|
||||
debugPrint('Warning: Failed to refresh Nostr profile: $e');
|
||||
Logger.warning('Failed to refresh Nostr profile: $e');
|
||||
// Continue without profile update - offline-first behavior
|
||||
}
|
||||
|
||||
@@ -447,11 +436,11 @@ class SessionService {
|
||||
);
|
||||
|
||||
if (addedCount > 0) {
|
||||
debugPrint('Loaded $addedCount preferred relay(s) from NIP-05: $nip05');
|
||||
Logger.info('Loaded $addedCount preferred relay(s) from NIP-05: $nip05');
|
||||
}
|
||||
} catch (e) {
|
||||
// Log error but don't fail - offline-first behavior
|
||||
debugPrint('Warning: Failed to load preferred relays from NIP-05: $e');
|
||||
Logger.warning('Failed to load preferred relays from NIP-05: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import '../../core/exceptions/sync_exception.dart';
|
||||
import '../local/local_storage_service.dart';
|
||||
import '../immich/immich_service.dart';
|
||||
import '../nostr/nostr_service.dart';
|
||||
@@ -6,18 +7,6 @@ import '../nostr/models/nostr_keypair.dart';
|
||||
import 'models/sync_status.dart';
|
||||
import 'models/sync_operation.dart';
|
||||
|
||||
/// Exception thrown when sync operations fail.
|
||||
class SyncException implements Exception {
|
||||
/// Error message.
|
||||
final String message;
|
||||
|
||||
/// Creates a [SyncException] with the provided message.
|
||||
SyncException(this.message);
|
||||
|
||||
@override
|
||||
String toString() => 'SyncException: $message';
|
||||
}
|
||||
|
||||
/// Engine for coordinating data synchronization between local storage, Immich, and Nostr.
|
||||
///
|
||||
/// This service provides:
|
||||
|
||||
+28
-140
@@ -1,182 +1,70 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'config/config_loader.dart';
|
||||
import 'data/local/local_storage_service.dart';
|
||||
import 'data/nostr/nostr_service.dart';
|
||||
import 'data/sync/sync_engine.dart';
|
||||
import 'data/firebase/firebase_service.dart';
|
||||
import 'data/session/session_service.dart';
|
||||
import 'data/immich/immich_service.dart';
|
||||
import 'core/app_initializer.dart';
|
||||
import 'core/app_services.dart';
|
||||
import 'core/logger.dart';
|
||||
import 'ui/navigation/main_navigation_scaffold.dart';
|
||||
|
||||
Future<void> main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// Load .env file (optional - falls back to defaults if not found)
|
||||
try {
|
||||
await dotenv.load(fileName: '.env');
|
||||
} catch (e) {
|
||||
debugPrint('Note: .env file not found, using default values: $e');
|
||||
}
|
||||
|
||||
// Load configuration based on environment
|
||||
// Determine environment
|
||||
const String environment = String.fromEnvironment(
|
||||
'ENV',
|
||||
defaultValue: 'dev',
|
||||
);
|
||||
|
||||
final config = ConfigLoader.load(environment);
|
||||
|
||||
// Initialize Firebase if enabled
|
||||
if (config.firebaseConfig.enabled) {
|
||||
// Initialize all services
|
||||
AppServices? appServices;
|
||||
try {
|
||||
await Firebase.initializeApp();
|
||||
if (config.enableLogging) {
|
||||
debugPrint('Firebase initialized successfully');
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('Firebase initialization failed: $e');
|
||||
debugPrint('Note: Firebase requires google-services.json (Android) and GoogleService-Info.plist (iOS)');
|
||||
}
|
||||
appServices = await AppInitializer.initialize(environment: environment);
|
||||
} catch (e, stackTrace) {
|
||||
Logger.error('Failed to initialize application', e, stackTrace);
|
||||
// App will show error state
|
||||
}
|
||||
|
||||
if (config.enableLogging) {
|
||||
debugPrint('App initialized with config: $config');
|
||||
}
|
||||
|
||||
runApp(const MyApp());
|
||||
runApp(MyApp(appServices: appServices));
|
||||
}
|
||||
|
||||
/// The root widget of the application.
|
||||
class MyApp extends StatefulWidget {
|
||||
const MyApp({super.key});
|
||||
final AppServices? appServices;
|
||||
|
||||
const MyApp({super.key, this.appServices});
|
||||
|
||||
@override
|
||||
State<MyApp> createState() => _MyAppState();
|
||||
}
|
||||
|
||||
class _MyAppState extends State<MyApp> {
|
||||
LocalStorageService? _storageService;
|
||||
NostrService? _nostrService;
|
||||
SyncEngine? _syncEngine;
|
||||
FirebaseService? _firebaseService;
|
||||
SessionService? _sessionService;
|
||||
ImmichService? _immichService;
|
||||
bool _isInitialized = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_initializeStorage();
|
||||
}
|
||||
|
||||
Future<void> _initializeStorage() async {
|
||||
try {
|
||||
_storageService = LocalStorageService();
|
||||
await _storageService!.initialize();
|
||||
|
||||
// Initialize Nostr service and sync engine
|
||||
_nostrService = NostrService();
|
||||
final nostrKeyPair = _nostrService!.generateKeyPair();
|
||||
_syncEngine = SyncEngine(
|
||||
localStorage: _storageService!,
|
||||
nostrService: _nostrService!,
|
||||
nostrKeyPair: nostrKeyPair,
|
||||
);
|
||||
|
||||
// Load relays from config
|
||||
final config = ConfigLoader.load(
|
||||
const String.fromEnvironment('ENV', defaultValue: 'dev'),
|
||||
);
|
||||
for (final relayUrl in config.nostrRelays) {
|
||||
_nostrService!.addRelay(relayUrl);
|
||||
}
|
||||
|
||||
// Initialize Immich service
|
||||
_immichService = ImmichService(
|
||||
baseUrl: config.immichBaseUrl,
|
||||
apiKey: config.immichApiKey,
|
||||
localStorage: _storageService!,
|
||||
);
|
||||
|
||||
// Initialize Firebase service if enabled
|
||||
if (config.firebaseConfig.enabled) {
|
||||
try {
|
||||
_firebaseService = FirebaseService(
|
||||
config: config.firebaseConfig,
|
||||
localStorage: _storageService!,
|
||||
);
|
||||
await _firebaseService!.initialize();
|
||||
if (config.enableLogging) {
|
||||
debugPrint('Firebase service initialized: ${_firebaseService!.isEnabled}');
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('Firebase service initialization failed: $e');
|
||||
_firebaseService = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize SessionService with Firebase and Nostr integration
|
||||
_sessionService = SessionService(
|
||||
localStorage: _storageService!,
|
||||
syncEngine: _syncEngine,
|
||||
firebaseService: _firebaseService,
|
||||
nostrService: _nostrService,
|
||||
);
|
||||
|
||||
setState(() {
|
||||
_isInitialized = true;
|
||||
});
|
||||
} catch (e) {
|
||||
debugPrint('Failed to initialize storage: $e');
|
||||
// Reset to null if initialization failed
|
||||
_storageService = null;
|
||||
_nostrService = null;
|
||||
_syncEngine = null;
|
||||
_firebaseService = null;
|
||||
_sessionService = null;
|
||||
_immichService = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_syncEngine?.dispose();
|
||||
_nostrService?.dispose();
|
||||
_firebaseService?.dispose();
|
||||
// Only close if storage service was initialized
|
||||
if (_storageService != null) {
|
||||
try {
|
||||
_storageService!.close();
|
||||
} catch (e) {
|
||||
debugPrint('Error closing storage service: $e');
|
||||
}
|
||||
}
|
||||
// Dispose of all services
|
||||
widget.appServices?.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final appServices = widget.appServices;
|
||||
|
||||
return MaterialApp(
|
||||
title: 'App Boilerplate',
|
||||
theme: ThemeData(
|
||||
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue),
|
||||
useMaterial3: true,
|
||||
),
|
||||
home: _isInitialized
|
||||
? MainNavigationScaffold(
|
||||
sessionService: _sessionService,
|
||||
localStorageService: _storageService,
|
||||
nostrService: _nostrService,
|
||||
syncEngine: _syncEngine,
|
||||
firebaseService: _firebaseService,
|
||||
immichService: _immichService,
|
||||
)
|
||||
home: appServices != null
|
||||
? const MainNavigationScaffold()
|
||||
: const Scaffold(
|
||||
body: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
CircularProgressIndicator(),
|
||||
SizedBox(height: 16),
|
||||
Text('Initializing application...'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1,15 +1,11 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../core/service_locator.dart';
|
||||
import '../../data/local/local_storage_service.dart';
|
||||
import '../../data/local/models/item.dart';
|
||||
|
||||
/// Home screen showing local storage and cached content.
|
||||
class HomeScreen extends StatefulWidget {
|
||||
final LocalStorageService? localStorageService;
|
||||
|
||||
const HomeScreen({
|
||||
super.key,
|
||||
this.localStorageService,
|
||||
});
|
||||
const HomeScreen({super.key});
|
||||
|
||||
@override
|
||||
State<HomeScreen> createState() => _HomeScreenState();
|
||||
@@ -26,15 +22,16 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
}
|
||||
|
||||
Future<void> _loadItems() async {
|
||||
if (widget.localStorageService == null) {
|
||||
try {
|
||||
final localStorageService = ServiceLocator.instance.localStorageService;
|
||||
if (localStorageService == null) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final items = await widget.localStorageService!.getAllItems();
|
||||
final items = await localStorageService.getAllItems();
|
||||
setState(() {
|
||||
_items = items;
|
||||
_isLoading = false;
|
||||
@@ -90,7 +87,8 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
trailing: IconButton(
|
||||
icon: const Icon(Icons.delete_outline),
|
||||
onPressed: () async {
|
||||
await widget.localStorageService?.deleteItem(item.id);
|
||||
final localStorageService = ServiceLocator.instance.localStorageService;
|
||||
await localStorageService?.deleteItem(item.id);
|
||||
_loadItems();
|
||||
},
|
||||
),
|
||||
@@ -98,9 +96,10 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
},
|
||||
),
|
||||
),
|
||||
floatingActionButton: widget.localStorageService != null
|
||||
floatingActionButton: ServiceLocator.instance.localStorageService != null
|
||||
? FloatingActionButton(
|
||||
onPressed: () async {
|
||||
final localStorageService = ServiceLocator.instance.localStorageService;
|
||||
final item = Item(
|
||||
id: 'item-${DateTime.now().millisecondsSinceEpoch}',
|
||||
data: {
|
||||
@@ -108,7 +107,7 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||
'timestamp': DateTime.now().toIso8601String(),
|
||||
},
|
||||
);
|
||||
await widget.localStorageService!.insertItem(item);
|
||||
await localStorageService!.insertItem(item);
|
||||
_loadItems();
|
||||
},
|
||||
child: const Icon(Icons.add),
|
||||
|
||||
@@ -2,6 +2,8 @@ import 'dart:io';
|
||||
import 'dart:typed_data';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import '../../core/logger.dart';
|
||||
import '../../core/service_locator.dart';
|
||||
import '../../data/local/local_storage_service.dart';
|
||||
import '../../data/immich/immich_service.dart';
|
||||
import '../../data/immich/models/immich_asset.dart';
|
||||
@@ -11,14 +13,7 @@ import '../../data/immich/models/immich_asset.dart';
|
||||
/// Displays images from Immich in a grid layout with pull-to-refresh.
|
||||
/// Shows cached images first, then fetches from API.
|
||||
class ImmichScreen extends StatefulWidget {
|
||||
final LocalStorageService? localStorageService;
|
||||
final ImmichService? immichService;
|
||||
|
||||
const ImmichScreen({
|
||||
super.key,
|
||||
this.localStorageService,
|
||||
this.immichService,
|
||||
});
|
||||
const ImmichScreen({super.key});
|
||||
|
||||
@override
|
||||
State<ImmichScreen> createState() => _ImmichScreenState();
|
||||
@@ -41,7 +36,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
|
||||
/// Loads assets from cache first, then fetches from API.
|
||||
Future<void> _loadAssets({bool forceRefresh = false}) async {
|
||||
if (widget.immichService == null) {
|
||||
if (ServiceLocator.instance.immichService == null) {
|
||||
setState(() {
|
||||
_errorMessage = 'Immich service not available';
|
||||
_isLoading = false;
|
||||
@@ -57,7 +52,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
try {
|
||||
// First, try to load cached assets
|
||||
if (!forceRefresh) {
|
||||
final cachedAssets = await widget.immichService!.getCachedAssets();
|
||||
final cachedAssets = await ServiceLocator.instance.immichService!.getCachedAssets();
|
||||
if (cachedAssets.isNotEmpty) {
|
||||
setState(() {
|
||||
_assets = cachedAssets;
|
||||
@@ -82,7 +77,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
/// Fetches assets from Immich API.
|
||||
Future<void> _fetchFromApi() async {
|
||||
try {
|
||||
final assets = await widget.immichService!.fetchAssets(limit: 100);
|
||||
final assets = await ServiceLocator.instance.immichService!.fetchAssets(limit: 100);
|
||||
setState(() {
|
||||
_assets = assets;
|
||||
_isLoading = false;
|
||||
@@ -97,7 +92,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
|
||||
/// Gets the thumbnail URL for an asset with proper headers.
|
||||
String _getThumbnailUrl(ImmichAsset asset) {
|
||||
return widget.immichService!.getThumbnailUrl(asset.id);
|
||||
return ServiceLocator.instance.immichService!.getThumbnailUrl(asset.id);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -305,7 +300,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
// Use FutureBuilder to fetch image bytes via ImmichService with proper auth
|
||||
return FutureBuilder<Uint8List?>(
|
||||
future:
|
||||
widget.immichService?.fetchImageBytes(asset.id, isThumbnail: true),
|
||||
ServiceLocator.instance.immichService?.fetchImageBytes(asset.id, isThumbnail: true),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(
|
||||
@@ -350,7 +345,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
),
|
||||
Expanded(
|
||||
child: FutureBuilder<Uint8List?>(
|
||||
future: widget.immichService
|
||||
future: ServiceLocator.instance.immichService
|
||||
?.fetchImageBytes(asset.id, isThumbnail: false),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
@@ -405,7 +400,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
|
||||
/// Tests the connection to Immich server by calling /api/server/about.
|
||||
Future<void> _testServerConnection() async {
|
||||
if (widget.immichService == null) {
|
||||
if (ServiceLocator.instance.immichService == null) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
@@ -426,7 +421,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
);
|
||||
|
||||
try {
|
||||
final serverInfo = await widget.immichService!.getServerInfo();
|
||||
final serverInfo = await ServiceLocator.instance.immichService!.getServerInfo();
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
@@ -493,7 +488,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
}
|
||||
|
||||
Future<void> _deleteSelectedAssets() async {
|
||||
if (_selectedAssetIds.isEmpty || widget.immichService == null) {
|
||||
if (_selectedAssetIds.isEmpty || ServiceLocator.instance.immichService == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -527,7 +522,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
|
||||
try {
|
||||
final assetIdsToDelete = _selectedAssetIds.toList();
|
||||
await widget.immichService!.deleteAssets(assetIdsToDelete);
|
||||
await ServiceLocator.instance.immichService!.deleteAssets(assetIdsToDelete);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -585,7 +580,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
|
||||
/// Opens image picker and uploads selected images to Immich.
|
||||
Future<void> _pickAndUploadImages() async {
|
||||
if (widget.immichService == null) {
|
||||
if (ServiceLocator.instance.immichService == null) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
@@ -644,8 +639,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
}
|
||||
} catch (pickError, stackTrace) {
|
||||
// Handle image picker specific errors
|
||||
debugPrint('Image picker error: $pickError');
|
||||
debugPrint('Stack trace: $stackTrace');
|
||||
Logger.error('Image picker error: $pickError', pickError, stackTrace);
|
||||
|
||||
if (mounted) {
|
||||
String errorMessage = 'Failed to open gallery';
|
||||
@@ -732,13 +726,12 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
for (final pickedFile in pickedFiles) {
|
||||
try {
|
||||
final file = File(pickedFile.path);
|
||||
debugPrint('Uploading file: ${pickedFile.name}');
|
||||
final uploadResponse = await widget.immichService!.uploadImage(file);
|
||||
debugPrint('Upload successful for ${pickedFile.name}: ${uploadResponse.id}');
|
||||
Logger.debug('Uploading file: ${pickedFile.name}');
|
||||
final uploadResponse = await ServiceLocator.instance.immichService!.uploadImage(file);
|
||||
Logger.info('Upload successful for ${pickedFile.name}: ${uploadResponse.id}');
|
||||
successCount++;
|
||||
} catch (e, stackTrace) {
|
||||
debugPrint('Upload failed for ${pickedFile.name}: $e');
|
||||
debugPrint('Stack trace: $stackTrace');
|
||||
Logger.error('Upload failed for ${pickedFile.name}: $e', e, stackTrace);
|
||||
failureCount++;
|
||||
errors.add('${pickedFile.name}: ${e.toString()}');
|
||||
}
|
||||
@@ -788,9 +781,9 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
}
|
||||
|
||||
// Refresh the asset list
|
||||
debugPrint('Refreshing asset list after upload');
|
||||
Logger.debug('Refreshing asset list after upload');
|
||||
await _loadAssets(forceRefresh: true);
|
||||
debugPrint('Asset list refreshed, current count: ${_assets.length}');
|
||||
Logger.debug('Asset list refreshed, current count: ${_assets.length}');
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
setState(() {
|
||||
@@ -799,8 +792,7 @@ class _ImmichScreenState extends State<ImmichScreen> {
|
||||
|
||||
if (mounted) {
|
||||
// Log the full error for debugging
|
||||
debugPrint('Image picker error: $e');
|
||||
debugPrint('Stack trace: $stackTrace');
|
||||
Logger.error('Image picker error: $e', e, stackTrace);
|
||||
|
||||
String errorMessage = 'Failed to pick images';
|
||||
if (e.toString().contains('Permission')) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../core/service_locator.dart';
|
||||
import '../home/home_screen.dart';
|
||||
import '../immich/immich_screen.dart';
|
||||
import '../nostr_events/nostr_events_screen.dart';
|
||||
@@ -6,11 +7,6 @@ import '../relay_management/relay_management_screen.dart';
|
||||
import '../relay_management/relay_management_controller.dart';
|
||||
import '../session/session_screen.dart';
|
||||
import '../settings/settings_screen.dart';
|
||||
import '../../data/nostr/nostr_service.dart';
|
||||
import '../../data/sync/sync_engine.dart';
|
||||
import '../../data/session/session_service.dart';
|
||||
import '../../data/local/local_storage_service.dart';
|
||||
import '../../data/firebase/firebase_service.dart';
|
||||
|
||||
/// Route names for the app navigation.
|
||||
class AppRoutes {
|
||||
@@ -86,31 +82,25 @@ class AppRouter {
|
||||
switch (settings.name) {
|
||||
case AppRoutes.home:
|
||||
return MaterialPageRoute(
|
||||
builder: (_) => HomeScreen(
|
||||
localStorageService: localStorageService,
|
||||
),
|
||||
builder: (_) => const HomeScreen(),
|
||||
settings: settings,
|
||||
);
|
||||
|
||||
case AppRoutes.immich:
|
||||
return MaterialPageRoute(
|
||||
builder: (_) => ImmichScreen(
|
||||
localStorageService: localStorageService,
|
||||
),
|
||||
builder: (_) => const ImmichScreen(),
|
||||
settings: settings,
|
||||
);
|
||||
|
||||
case AppRoutes.nostrEvents:
|
||||
return MaterialPageRoute(
|
||||
builder: (_) => NostrEventsScreen(
|
||||
nostrService: nostrService,
|
||||
syncEngine: syncEngine,
|
||||
sessionService: sessionService,
|
||||
),
|
||||
builder: (_) => const NostrEventsScreen(),
|
||||
settings: settings,
|
||||
);
|
||||
|
||||
case AppRoutes.relayManagement:
|
||||
final nostrService = ServiceLocator.instance.nostrService;
|
||||
final syncEngine = ServiceLocator.instance.syncEngine;
|
||||
if (nostrService == null || syncEngine == null) {
|
||||
return MaterialPageRoute(
|
||||
builder: (_) => _buildErrorScreen('Nostr service not available'),
|
||||
@@ -120,8 +110,8 @@ class AppRouter {
|
||||
return MaterialPageRoute(
|
||||
builder: (_) => RelayManagementScreen(
|
||||
controller: RelayManagementController(
|
||||
nostrService: nostrService!,
|
||||
syncEngine: syncEngine!,
|
||||
nostrService: nostrService,
|
||||
syncEngine: syncEngine,
|
||||
),
|
||||
),
|
||||
settings: settings,
|
||||
@@ -129,19 +119,13 @@ class AppRouter {
|
||||
|
||||
case AppRoutes.session:
|
||||
return MaterialPageRoute(
|
||||
builder: (_) => SessionScreen(
|
||||
sessionService: sessionService,
|
||||
firebaseService: firebaseService,
|
||||
nostrService: nostrService,
|
||||
),
|
||||
builder: (_) => const SessionScreen(),
|
||||
settings: settings,
|
||||
);
|
||||
|
||||
case AppRoutes.settings:
|
||||
return MaterialPageRoute(
|
||||
builder: (_) => SettingsScreen(
|
||||
firebaseService: firebaseService,
|
||||
),
|
||||
builder: (_) => const SettingsScreen(),
|
||||
settings: settings,
|
||||
);
|
||||
|
||||
|
||||
@@ -4,31 +4,11 @@ import '../immich/immich_screen.dart';
|
||||
import '../nostr_events/nostr_events_screen.dart';
|
||||
import '../session/session_screen.dart';
|
||||
import '../settings/settings_screen.dart';
|
||||
import '../../data/session/session_service.dart';
|
||||
import '../../data/local/local_storage_service.dart';
|
||||
import '../../data/nostr/nostr_service.dart';
|
||||
import '../../data/sync/sync_engine.dart';
|
||||
import '../../data/firebase/firebase_service.dart';
|
||||
import '../../data/immich/immich_service.dart';
|
||||
import '../../core/service_locator.dart';
|
||||
|
||||
/// Main navigation scaffold with bottom navigation bar.
|
||||
class MainNavigationScaffold extends StatefulWidget {
|
||||
final SessionService? sessionService;
|
||||
final LocalStorageService? localStorageService;
|
||||
final NostrService? nostrService;
|
||||
final SyncEngine? syncEngine;
|
||||
final FirebaseService? firebaseService;
|
||||
final ImmichService? immichService;
|
||||
|
||||
const MainNavigationScaffold({
|
||||
super.key,
|
||||
this.sessionService,
|
||||
this.localStorageService,
|
||||
this.nostrService,
|
||||
this.syncEngine,
|
||||
this.firebaseService,
|
||||
this.immichService,
|
||||
});
|
||||
const MainNavigationScaffold({super.key});
|
||||
|
||||
@override
|
||||
State<MainNavigationScaffold> createState() => _MainNavigationScaffoldState();
|
||||
@@ -43,7 +23,8 @@ class _MainNavigationScaffoldState extends State<MainNavigationScaffold> {
|
||||
setState(() {
|
||||
_currentIndex = index;
|
||||
// If accessing a protected route (Immich=1, Nostr=2) while not logged in, remember it
|
||||
if ((index == 1 || index == 2) && !(widget.sessionService?.isLoggedIn ?? false)) {
|
||||
final sessionService = ServiceLocator.instance.sessionService;
|
||||
if ((index == 1 || index == 2) && !(sessionService?.isLoggedIn ?? false)) {
|
||||
_pendingProtectedRoute = index;
|
||||
} else {
|
||||
_pendingProtectedRoute = null;
|
||||
@@ -57,7 +38,8 @@ class _MainNavigationScaffoldState extends State<MainNavigationScaffold> {
|
||||
_loginStateVersion++; // Force rebuild when login state changes
|
||||
|
||||
// If user just logged in and was trying to access a protected route, navigate there
|
||||
if (widget.sessionService?.isLoggedIn == true && _pendingProtectedRoute != null) {
|
||||
final sessionService = ServiceLocator.instance.sessionService;
|
||||
if (sessionService?.isLoggedIn == true && _pendingProtectedRoute != null) {
|
||||
_currentIndex = _pendingProtectedRoute!;
|
||||
_pendingProtectedRoute = null;
|
||||
}
|
||||
@@ -65,43 +47,30 @@ class _MainNavigationScaffoldState extends State<MainNavigationScaffold> {
|
||||
}
|
||||
|
||||
Widget _buildScreen(int index) {
|
||||
final locator = ServiceLocator.instance;
|
||||
final sessionService = locator.sessionService;
|
||||
|
||||
switch (index) {
|
||||
case 0:
|
||||
return HomeScreen(
|
||||
localStorageService: widget.localStorageService,
|
||||
);
|
||||
return const HomeScreen();
|
||||
case 1:
|
||||
// Check auth guard for Immich
|
||||
if (!(widget.sessionService?.isLoggedIn ?? false)) {
|
||||
if (!(sessionService?.isLoggedIn ?? false)) {
|
||||
return _buildLoginRequiredScreen();
|
||||
}
|
||||
return ImmichScreen(
|
||||
localStorageService: widget.localStorageService,
|
||||
immichService: widget.immichService,
|
||||
);
|
||||
return const ImmichScreen();
|
||||
case 2:
|
||||
// Check auth guard for Nostr Events
|
||||
if (!(widget.sessionService?.isLoggedIn ?? false)) {
|
||||
if (!(sessionService?.isLoggedIn ?? false)) {
|
||||
return _buildLoginRequiredScreen();
|
||||
}
|
||||
return NostrEventsScreen(
|
||||
nostrService: widget.nostrService,
|
||||
syncEngine: widget.syncEngine,
|
||||
sessionService: widget.sessionService,
|
||||
);
|
||||
return const NostrEventsScreen();
|
||||
case 3:
|
||||
return SessionScreen(
|
||||
sessionService: widget.sessionService,
|
||||
firebaseService: widget.firebaseService,
|
||||
nostrService: widget.nostrService,
|
||||
onSessionChanged: _onSessionStateChanged,
|
||||
);
|
||||
case 4:
|
||||
return SettingsScreen(
|
||||
firebaseService: widget.firebaseService,
|
||||
nostrService: widget.nostrService,
|
||||
syncEngine: widget.syncEngine,
|
||||
);
|
||||
return const SettingsScreen();
|
||||
default:
|
||||
return const SizedBox();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../core/service_locator.dart';
|
||||
import '../../data/nostr/nostr_service.dart';
|
||||
import '../../data/sync/sync_engine.dart';
|
||||
import '../../data/session/session_service.dart';
|
||||
@@ -9,16 +10,7 @@ import '../relay_management/relay_management_controller.dart';
|
||||
|
||||
/// Screen for displaying and testing Nostr events.
|
||||
class NostrEventsScreen extends StatefulWidget {
|
||||
final NostrService? nostrService;
|
||||
final SyncEngine? syncEngine;
|
||||
final SessionService? sessionService;
|
||||
|
||||
const NostrEventsScreen({
|
||||
super.key,
|
||||
this.nostrService,
|
||||
this.syncEngine,
|
||||
this.sessionService,
|
||||
});
|
||||
const NostrEventsScreen({super.key});
|
||||
|
||||
@override
|
||||
State<NostrEventsScreen> createState() => _NostrEventsScreenState();
|
||||
@@ -29,7 +21,7 @@ class _NostrEventsScreenState extends State<NostrEventsScreen> {
|
||||
bool _isLoading = false;
|
||||
|
||||
Future<void> _publishTestEvent() async {
|
||||
if (widget.nostrService == null) {
|
||||
if (ServiceLocator.instance.nostrService == null) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
@@ -42,7 +34,7 @@ class _NostrEventsScreenState extends State<NostrEventsScreen> {
|
||||
}
|
||||
|
||||
// Check if user is logged in with Nostr
|
||||
final currentUser = widget.sessionService?.currentUser;
|
||||
final currentUser = ServiceLocator.instance.sessionService?.currentUser;
|
||||
if (currentUser == null || currentUser.nostrPrivateKey == null) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -56,7 +48,7 @@ class _NostrEventsScreenState extends State<NostrEventsScreen> {
|
||||
}
|
||||
|
||||
// Get relays
|
||||
final relays = widget.nostrService!.getRelays();
|
||||
final relays = ServiceLocator.instance.nostrService!.getRelays();
|
||||
if (relays.isEmpty) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -85,7 +77,7 @@ class _NostrEventsScreenState extends State<NostrEventsScreen> {
|
||||
);
|
||||
|
||||
// Publish to all enabled relays
|
||||
final results = await widget.nostrService!.publishEventToAllRelays(event);
|
||||
final results = await ServiceLocator.instance.nostrService!.publishEventToAllRelays(event);
|
||||
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
@@ -169,13 +161,13 @@ class _NostrEventsScreenState extends State<NostrEventsScreen> {
|
||||
const SizedBox(height: 8),
|
||||
TextButton.icon(
|
||||
onPressed: () {
|
||||
if (widget.nostrService != null && widget.syncEngine != null) {
|
||||
if (ServiceLocator.instance.nostrService != null && ServiceLocator.instance.syncEngine != null) {
|
||||
Navigator.of(context).push(
|
||||
MaterialPageRoute(
|
||||
builder: (_) => RelayManagementScreen(
|
||||
controller: RelayManagementController(
|
||||
nostrService: widget.nostrService!,
|
||||
syncEngine: widget.syncEngine!,
|
||||
nostrService: ServiceLocator.instance.nostrService!,
|
||||
syncEngine: ServiceLocator.instance.syncEngine!,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../core/logger.dart';
|
||||
import '../../core/service_locator.dart';
|
||||
import '../../data/session/session_service.dart';
|
||||
import '../../data/firebase/firebase_service.dart';
|
||||
import '../../data/nostr/nostr_service.dart';
|
||||
@@ -6,16 +8,10 @@ import '../../data/nostr/models/nostr_keypair.dart';
|
||||
|
||||
/// Screen for user session management (login/logout).
|
||||
class SessionScreen extends StatefulWidget {
|
||||
final SessionService? sessionService;
|
||||
final FirebaseService? firebaseService;
|
||||
final NostrService? nostrService;
|
||||
final VoidCallback? onSessionChanged;
|
||||
|
||||
const SessionScreen({
|
||||
super.key,
|
||||
this.sessionService,
|
||||
this.firebaseService,
|
||||
this.nostrService,
|
||||
this.onSessionChanged,
|
||||
});
|
||||
|
||||
@@ -38,8 +34,8 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Check if Firebase Auth is available
|
||||
_useFirebaseAuth = widget.firebaseService?.isEnabled == true &&
|
||||
widget.firebaseService?.config.authEnabled == true;
|
||||
_useFirebaseAuth = ServiceLocator.instance.firebaseService?.isEnabled == true &&
|
||||
ServiceLocator.instance.firebaseService?.config.authEnabled == true;
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -53,7 +49,7 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
}
|
||||
|
||||
Future<void> _handleLogin() async {
|
||||
if (widget.sessionService == null) return;
|
||||
if (ServiceLocator.instance.sessionService == null) return;
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
@@ -89,7 +85,7 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
}
|
||||
|
||||
// Login with Nostr
|
||||
await widget.sessionService!.loginWithNostr(nostrKey);
|
||||
await ServiceLocator.instance.sessionService!.loginWithNostr(nostrKey);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -104,7 +100,7 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
}
|
||||
|
||||
// Handle Firebase or regular login
|
||||
if (_useFirebaseAuth && widget.firebaseService != null) {
|
||||
if (_useFirebaseAuth && ServiceLocator.instance.firebaseService != null) {
|
||||
// Use Firebase Auth for authentication
|
||||
final email = _emailController.text.trim();
|
||||
final password = _passwordController.text.trim();
|
||||
@@ -121,13 +117,13 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
}
|
||||
|
||||
// Authenticate with Firebase
|
||||
final firebaseUser = await widget.firebaseService!.loginWithEmailPassword(
|
||||
final firebaseUser = await ServiceLocator.instance.firebaseService!.loginWithEmailPassword(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
|
||||
// Create session with Firebase user info
|
||||
await widget.sessionService!.login(
|
||||
await ServiceLocator.instance.sessionService!.login(
|
||||
id: firebaseUser.uid,
|
||||
username: firebaseUser.email?.split('@').first ?? firebaseUser.uid,
|
||||
token: await firebaseUser.getIdToken(),
|
||||
@@ -183,7 +179,7 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
}
|
||||
|
||||
// Create session (demo mode - no real authentication)
|
||||
await widget.sessionService!.login(
|
||||
await ServiceLocator.instance.sessionService!.login(
|
||||
id: userId,
|
||||
username: username,
|
||||
);
|
||||
@@ -204,7 +200,7 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Login failed: ${e.toString().replaceAll('FirebaseException: ', '').replaceAll('SessionException: ', '')}'),
|
||||
content: Text('Login failed: ${e.toString().replaceAll('FirebaseServiceException: ', '').replaceAll('SessionException: ', '')}'),
|
||||
backgroundColor: Colors.red,
|
||||
),
|
||||
);
|
||||
@@ -219,7 +215,7 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
}
|
||||
|
||||
Future<void> _handleLogout() async {
|
||||
if (widget.sessionService == null) return;
|
||||
if (ServiceLocator.instance.sessionService == null) return;
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
@@ -227,15 +223,15 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
|
||||
try {
|
||||
// Logout from session service first
|
||||
await widget.sessionService!.logout();
|
||||
await ServiceLocator.instance.sessionService!.logout();
|
||||
|
||||
// Also logout from Firebase Auth if enabled
|
||||
if (_useFirebaseAuth && widget.firebaseService != null) {
|
||||
if (_useFirebaseAuth && ServiceLocator.instance.firebaseService != null) {
|
||||
try {
|
||||
await widget.firebaseService!.logout();
|
||||
await ServiceLocator.instance.firebaseService!.logout();
|
||||
} catch (e) {
|
||||
// Log error but don't fail logout - session is already cleared
|
||||
debugPrint('Warning: Firebase logout failed: $e');
|
||||
Logger.warning('Firebase logout failed: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,10 +264,10 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
}
|
||||
|
||||
Future<void> _handleRefresh() async {
|
||||
if (widget.sessionService == null) return;
|
||||
if (ServiceLocator.instance.sessionService == null) return;
|
||||
|
||||
try {
|
||||
await widget.sessionService!.refreshNostrProfile();
|
||||
await ServiceLocator.instance.sessionService!.refreshNostrProfile();
|
||||
if (mounted) {
|
||||
setState(() {});
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -296,8 +292,8 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isLoggedIn = widget.sessionService?.isLoggedIn ?? false;
|
||||
final currentUser = widget.sessionService?.currentUser;
|
||||
final isLoggedIn = ServiceLocator.instance.sessionService?.isLoggedIn ?? false;
|
||||
final currentUser = ServiceLocator.instance.sessionService?.currentUser;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
@@ -389,7 +385,7 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
_Nip05Section(
|
||||
nip05: currentUser.nostrProfile!.nip05!,
|
||||
publicKey: currentUser.id,
|
||||
nostrService: widget.nostrService,
|
||||
nostrService: ServiceLocator.instance.nostrService,
|
||||
),
|
||||
if (currentUser.nostrProfile?.nip05 != null &&
|
||||
currentUser.nostrProfile!.nip05!.isNotEmpty)
|
||||
@@ -476,7 +472,7 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
const SizedBox(height: 24),
|
||||
if (_useNostrLogin) ...[
|
||||
// Key pair generation section
|
||||
if (widget.nostrService != null) ...[
|
||||
if (ServiceLocator.instance.nostrService != null) ...[
|
||||
Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
@@ -496,7 +492,7 @@ class _SessionScreenState extends State<SessionScreen> {
|
||||
ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
_generatedKeyPair = widget.nostrService!.generateKeyPair();
|
||||
_generatedKeyPair = ServiceLocator.instance.nostrService!.generateKeyPair();
|
||||
});
|
||||
},
|
||||
icon: const Icon(Icons.refresh, size: 18),
|
||||
@@ -691,7 +687,7 @@ class _Nip05SectionState extends State<_Nip05Section> {
|
||||
}
|
||||
|
||||
Future<void> _loadPreferredRelays() async {
|
||||
if (widget.nostrService == null) {
|
||||
if (ServiceLocator.instance.nostrService == null) {
|
||||
setState(() {
|
||||
_error = 'Nostr service not available';
|
||||
});
|
||||
@@ -704,7 +700,7 @@ class _Nip05SectionState extends State<_Nip05Section> {
|
||||
});
|
||||
|
||||
try {
|
||||
final relays = await widget.nostrService!.fetchPreferredRelaysFromNip05(
|
||||
final relays = await ServiceLocator.instance.nostrService!.fetchPreferredRelaysFromNip05(
|
||||
widget.nip05,
|
||||
widget.publicKey,
|
||||
);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../core/service_locator.dart';
|
||||
import '../../data/firebase/firebase_service.dart';
|
||||
import '../../data/nostr/nostr_service.dart';
|
||||
import '../../data/sync/sync_engine.dart';
|
||||
@@ -7,16 +8,7 @@ import '../relay_management/relay_management_controller.dart';
|
||||
|
||||
/// Settings screen (placeholder).
|
||||
class SettingsScreen extends StatelessWidget {
|
||||
final FirebaseService? firebaseService;
|
||||
final NostrService? nostrService;
|
||||
final SyncEngine? syncEngine;
|
||||
|
||||
const SettingsScreen({
|
||||
super.key,
|
||||
this.firebaseService,
|
||||
this.nostrService,
|
||||
this.syncEngine,
|
||||
});
|
||||
const SettingsScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -26,18 +18,31 @@ class SettingsScreen extends StatelessWidget {
|
||||
),
|
||||
body: ListView(
|
||||
children: [
|
||||
if (firebaseService != null)
|
||||
SwitchListTile(
|
||||
Builder(
|
||||
builder: (context) {
|
||||
final firebaseService = ServiceLocator.instance.firebaseService;
|
||||
if (firebaseService != null) {
|
||||
return SwitchListTile(
|
||||
title: const Text('Firebase Enabled'),
|
||||
subtitle: Text(
|
||||
firebaseService!.isEnabled
|
||||
firebaseService.isEnabled
|
||||
? 'Firebase services are active'
|
||||
: 'Firebase services are disabled',
|
||||
),
|
||||
value: firebaseService!.isEnabled,
|
||||
value: firebaseService.isEnabled,
|
||||
onChanged: null, // Read-only for now
|
||||
);
|
||||
}
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
if (nostrService != null && syncEngine != null) ...[
|
||||
Builder(
|
||||
builder: (context) {
|
||||
final nostrService = ServiceLocator.instance.nostrService;
|
||||
final syncEngine = ServiceLocator.instance.syncEngine;
|
||||
if (nostrService != null && syncEngine != null) {
|
||||
return Column(
|
||||
children: [
|
||||
const Divider(),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.cloud),
|
||||
@@ -49,8 +54,8 @@ class SettingsScreen extends StatelessWidget {
|
||||
MaterialPageRoute(
|
||||
builder: (_) => RelayManagementScreen(
|
||||
controller: RelayManagementController(
|
||||
nostrService: nostrService!,
|
||||
syncEngine: syncEngine!,
|
||||
nostrService: nostrService,
|
||||
syncEngine: syncEngine,
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -58,6 +63,11 @@ class SettingsScreen extends StatelessWidget {
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
const Divider(),
|
||||
const ListTile(
|
||||
leading: Icon(Icons.info_outline),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:app_boilerplate/core/exceptions/invalid_environment_exception.dart';
|
||||
import 'package:app_boilerplate/config/config_loader.dart';
|
||||
|
||||
void main() {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mockito/annotations.dart';
|
||||
import 'package:app_boilerplate/core/exceptions/firebase_exception.dart' show FirebaseServiceException;
|
||||
import 'package:app_boilerplate/data/firebase/firebase_service.dart';
|
||||
import 'package:app_boilerplate/data/firebase/models/firebase_config.dart';
|
||||
import 'package:app_boilerplate/data/local/local_storage_service.dart';
|
||||
@@ -81,7 +82,7 @@ void main() {
|
||||
// but that's expected - Firebase requires actual project setup
|
||||
expect(
|
||||
() => service.initialize(),
|
||||
throwsA(isA<FirebaseException>()),
|
||||
throwsA(isA<FirebaseServiceException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -102,7 +103,7 @@ void main() {
|
||||
email: '[email protected]',
|
||||
password: 'password123',
|
||||
),
|
||||
throwsA(isA<FirebaseException>()),
|
||||
throwsA(isA<FirebaseServiceException>()),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -118,7 +119,7 @@ void main() {
|
||||
email: '[email protected]',
|
||||
password: 'password123',
|
||||
),
|
||||
throwsA(isA<FirebaseException>()),
|
||||
throwsA(isA<FirebaseServiceException>()),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -134,7 +135,7 @@ void main() {
|
||||
|
||||
expect(
|
||||
() => service.logout(),
|
||||
throwsA(isA<FirebaseException>()),
|
||||
throwsA(isA<FirebaseServiceException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -152,7 +153,7 @@ void main() {
|
||||
|
||||
expect(
|
||||
() => service.syncItemsToFirestore('user1'),
|
||||
throwsA(isA<FirebaseException>()),
|
||||
throwsA(isA<FirebaseServiceException>()),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -165,7 +166,7 @@ void main() {
|
||||
|
||||
expect(
|
||||
() => service.syncItemsToFirestore('user1'),
|
||||
throwsA(isA<FirebaseException>()),
|
||||
throwsA(isA<FirebaseServiceException>()),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -181,7 +182,7 @@ void main() {
|
||||
|
||||
expect(
|
||||
() => service.syncItemsFromFirestore('user1'),
|
||||
throwsA(isA<FirebaseException>()),
|
||||
throwsA(isA<FirebaseServiceException>()),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -194,7 +195,7 @@ void main() {
|
||||
|
||||
expect(
|
||||
() => service.syncItemsFromFirestore('user1'),
|
||||
throwsA(isA<FirebaseException>()),
|
||||
throwsA(isA<FirebaseServiceException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -215,7 +216,7 @@ void main() {
|
||||
|
||||
expect(
|
||||
() => service.uploadFile(testFile, 'test/path.txt'),
|
||||
throwsA(isA<FirebaseException>()),
|
||||
throwsA(isA<FirebaseServiceException>()),
|
||||
);
|
||||
});
|
||||
|
||||
@@ -231,7 +232,7 @@ void main() {
|
||||
|
||||
expect(
|
||||
() => service.uploadFile(testFile, 'test/path.txt'),
|
||||
throwsA(isA<FirebaseException>()),
|
||||
throwsA(isA<FirebaseServiceException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:app_boilerplate/core/exceptions/immich_exception.dart';
|
||||
import 'package:app_boilerplate/data/immich/immich_service.dart';
|
||||
import 'package:app_boilerplate/data/immich/models/immich_asset.dart';
|
||||
import 'package:app_boilerplate/data/local/local_storage_service.dart';
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:app_boilerplate/core/exceptions/nostr_exception.dart';
|
||||
import 'package:app_boilerplate/data/nostr/nostr_service.dart';
|
||||
import 'package:app_boilerplate/data/nostr/models/nostr_keypair.dart';
|
||||
import 'package:app_boilerplate/data/nostr/models/nostr_event.dart';
|
||||
|
||||
@@ -2,6 +2,7 @@ import 'dart:io';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
||||
import 'package:app_boilerplate/core/exceptions/session_exception.dart';
|
||||
import 'package:app_boilerplate/data/session/session_service.dart';
|
||||
import 'package:app_boilerplate/data/local/local_storage_service.dart';
|
||||
import 'package:app_boilerplate/data/local/models/item.dart';
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:app_boilerplate/core/exceptions/sync_exception.dart';
|
||||
import 'package:app_boilerplate/data/sync/sync_engine.dart';
|
||||
import 'package:app_boilerplate/data/sync/models/sync_status.dart';
|
||||
import 'package:app_boilerplate/data/sync/models/sync_operation.dart';
|
||||
|
||||
@@ -9,6 +9,7 @@ import 'package:app_boilerplate/data/nostr/models/nostr_keypair.dart';
|
||||
import 'package:app_boilerplate/data/sync/sync_engine.dart';
|
||||
import 'package:app_boilerplate/data/session/session_service.dart';
|
||||
import 'package:app_boilerplate/data/firebase/firebase_service.dart';
|
||||
import 'package:app_boilerplate/core/service_locator.dart';
|
||||
|
||||
import 'main_navigation_scaffold_test.mocks.dart';
|
||||
|
||||
@@ -42,17 +43,25 @@ void main() {
|
||||
// Stub NostrService methods that might be called by UI
|
||||
final mockKeyPair = NostrKeyPair.generate();
|
||||
when(mockNostrService.generateKeyPair()).thenReturn(mockKeyPair);
|
||||
|
||||
// Register services with ServiceLocator
|
||||
ServiceLocator.instance.registerServices(
|
||||
localStorageService: mockLocalStorageService,
|
||||
nostrService: mockNostrService,
|
||||
syncEngine: mockSyncEngine,
|
||||
sessionService: mockSessionService,
|
||||
firebaseService: mockFirebaseService,
|
||||
);
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
// Reset ServiceLocator after each test
|
||||
ServiceLocator.instance.reset();
|
||||
});
|
||||
|
||||
Widget createTestWidget() {
|
||||
return MaterialApp(
|
||||
home: MainNavigationScaffold(
|
||||
sessionService: mockSessionService,
|
||||
localStorageService: mockLocalStorageService,
|
||||
nostrService: mockNostrService,
|
||||
syncEngine: mockSyncEngine,
|
||||
firebaseService: mockFirebaseService,
|
||||
),
|
||||
home: const MainNavigationScaffold(),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user