You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.2 KiB
114 lines
3.2 KiB
/// 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;
|
|
|
|
/// Media service (Immich or Blossom).
|
|
dynamic _mediaService;
|
|
|
|
/// Recipe service.
|
|
dynamic _recipeService;
|
|
|
|
/// Theme notifier.
|
|
dynamic _themeNotifier;
|
|
|
|
/// Registers all services with the locator.
|
|
///
|
|
/// All services are optional and can be null if not configured.
|
|
/// If a service is not provided, the existing value is preserved.
|
|
void registerServices({
|
|
dynamic localStorageService,
|
|
dynamic nostrService,
|
|
dynamic syncEngine,
|
|
dynamic firebaseService,
|
|
dynamic sessionService,
|
|
dynamic mediaService,
|
|
dynamic recipeService,
|
|
dynamic themeNotifier,
|
|
}) {
|
|
if (localStorageService != null) _localStorageService = localStorageService;
|
|
if (nostrService != null) _nostrService = nostrService;
|
|
if (syncEngine != null) _syncEngine = syncEngine;
|
|
if (firebaseService != null) _firebaseService = firebaseService;
|
|
if (sessionService != null) _sessionService = sessionService;
|
|
if (mediaService != null) _mediaService = mediaService;
|
|
if (recipeService != null) _recipeService = recipeService;
|
|
if (themeNotifier != null) _themeNotifier = themeNotifier;
|
|
}
|
|
|
|
/// 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 Media service (nullable) - Immich or Blossom.
|
|
dynamic get mediaService => _mediaService;
|
|
|
|
/// Gets the Immich service (nullable) - for backward compatibility.
|
|
/// Returns null if Blossom is selected.
|
|
dynamic get immichService {
|
|
if (_mediaService != null && _mediaService.runtimeType.toString().contains('ImmichService')) {
|
|
return _mediaService;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Gets the Recipe service (nullable).
|
|
dynamic get recipeService => _recipeService;
|
|
|
|
/// Gets the Theme notifier (nullable).
|
|
dynamic get themeNotifier => _themeNotifier;
|
|
|
|
/// Clears all registered services (useful for testing).
|
|
void reset() {
|
|
_localStorageService = null;
|
|
_nostrService = null;
|
|
_syncEngine = null;
|
|
_firebaseService = null;
|
|
_sessionService = null;
|
|
_mediaService = null;
|
|
_recipeService = null;
|
|
_themeNotifier = null;
|
|
}
|
|
}
|
|
|