library; import 'package:fluttery/logger/logger.dart'; import 'package:fluttery/preferences/preferences.dart'; import 'package:fluttery/secure_storage.dart'; import 'package:fluttery/src/logger/logger_impl.dart'; import 'package:fluttery/src/preferences/preferences_impl.dart'; import 'package:fluttery/src/storage/secure/secure_storage_impl.dart'; import 'package:kiwi/kiwi.dart'; import 'package:shared_preferences/shared_preferences.dart'; /// A class to manage services. class App { static final _AppService _appService = _AppService(); /// Registers a service with a factory function to instantiate the implementation. /// /// This ensures that the implementation is created when the service is requested. /// /// `implFactory` - A factory method to create the service implementation. static void registerService(T Function() implFactory) { _appService.registerSingleton(implFactory); } /// Retrieves the registered service. /// /// Returns an instance of the registered service. static T service() { return _appService.resolve(); } /// Registers the default services required by the application. static void registerDefaultServices() { registerService(() => LoggerImpl()); SharedPreferences.getInstance().then((instance) { registerService(() => PreferencesImpl(instance: instance)); }); registerService(() => SecureStorageImpl()); } } /// Abstract class to represent a service. abstract class Service {} /// Internal class to manage the registration and resolution of services. class _AppService { static _AppService? _singleton; static final KiwiContainer _kiwi = KiwiContainer(); /// Factory constructor to ensure singleton instance of _AppService. factory _AppService() => _singleton ??= _AppService._(); /// Private constructor. _AppService._(); /// Registers a singleton service with a factory function to create the instance. /// /// `serviceFactory` - A factory method to create the service implementation. void registerSingleton(T Function() serviceFactory) { _kiwi.registerFactory((c) => serviceFactory()); } /// Resolves and retrieves the registered service. /// /// Returns an instance of the registered service. T resolve() { return _kiwi.resolve(); } }