diff --git a/lib/arcane_framework.dart b/lib/arcane_framework.dart index 46a1ccb..6ca32ec 100644 --- a/lib/arcane_framework.dart +++ b/lib/arcane_framework.dart @@ -40,7 +40,7 @@ library; export "package:arcane_framework/src/arcane.dart"; export "package:arcane_framework/src/arcane_app.dart"; export "package:arcane_framework/src/providers/environment_provider.dart"; -export "package:arcane_framework/src/providers/service_provider.dart"; +export "package:arcane_framework/src/providers/service/arcane_service.dart"; export "package:arcane_framework/src/services/authentication/authentication_service.dart"; export "package:arcane_framework/src/services/feature_flags/feature_flags_service.dart"; export "package:arcane_framework/src/services/logging/logging_service.dart"; diff --git a/lib/src/providers/service/arcane_service.dart b/lib/src/providers/service/arcane_service.dart new file mode 100644 index 0000000..f782142 --- /dev/null +++ b/lib/src/providers/service/arcane_service.dart @@ -0,0 +1,36 @@ +import "package:arcane_framework/arcane_framework.dart"; +import "package:collection/collection.dart"; +import "package:flutter/widgets.dart"; + +part "service_provider.dart"; +part "service_provider_extensions.dart"; + +/// An abstract class representing a service in the Arcane architecture. +/// +/// Classes that extend `ArcaneService` can use `ChangeNotifier` functionality +/// to notify listeners of changes. Services are typically registered in +/// `ArcaneServiceProvider` and can be accessed using the `service` +/// method on `BuildContext`. +abstract class ArcaneService with ChangeNotifier { + /// Retrieves a service of the specified type from the context. + /// + /// Returns null if no service of type `T` is found. + /// + /// Example: + /// ```dart + /// final myService = ArcaneService.ofType(context); + /// ``` + static T? ofType(BuildContext context) => + context.service(); + + /// Retrieves a service of the specified type from the context. + /// + /// Throws an assertion error if no service of type `T` is found. + /// + /// Example: + /// ```dart + /// final myService = ArcaneService.requiredOfType(context); + /// ``` + static T requiredOfType(BuildContext context) => + context.requiredService(); +} diff --git a/lib/src/providers/service_provider.dart b/lib/src/providers/service/service_provider.dart similarity index 56% rename from lib/src/providers/service_provider.dart rename to lib/src/providers/service/service_provider.dart index a7ad80c..990691d 100644 --- a/lib/src/providers/service_provider.dart +++ b/lib/src/providers/service/service_provider.dart @@ -1,6 +1,4 @@ -import "package:arcane_framework/arcane_framework.dart"; -import "package:collection/collection.dart"; -import "package:flutter/widgets.dart"; +part of "arcane_service.dart"; /// A provider that makes a list of `ArcaneService` instances available to the widget tree. /// @@ -122,83 +120,3 @@ class ArcaneServiceProvider } } } - -/// An extension on `BuildContext` to provide easy access to `ArcaneService` instances -/// that are registered in an `ArcaneServiceProvider`. -/// -/// This extension provides methods for retrieving services in various ways. -/// -/// Example usage: -/// ```dart -/// final myService = context.service(); -/// ``` -extension ServiceProviderExtension on BuildContext { - /// Finds and returns the `ArcaneService` instance of type `T` that has been registered - /// in the `ArcaneServiceProvider` or in the list of built-in services (`Arcane.services`). - /// - /// If no such service is found, it returns `null`. - /// - /// Example: - /// ```dart - /// final myService = context.service(); - /// ``` - T? service() { - // First check built-in services - final builtInService = Arcane.services.whereType().firstOrNull; - if (builtInService != null) return builtInService; - - // Then check provider - return ArcaneServiceProvider.serviceOfType(this); - } - - /// Finds and returns the `ArcaneService` instance of type `T` that has been registered - /// in the `ArcaneServiceProvider` or in the list of built-in services (`Arcane.services`). - /// - /// Throws an assertion error if no service is found. - /// - /// Example: - /// ```dart - /// final myService = context.requiredService(); - /// ``` - T requiredService() { - final service = this.service(); - assert(service != null, "No service of type $T found"); - return service!; - } - - /// Legacy method to maintain backward compatibility. - /// - /// Prefer using `service()` instead. - @Deprecated("Use service() instead") - T? serviceOfType() => service(); -} - -/// An abstract class representing a service in the Arcane architecture. -/// -/// Classes that extend `ArcaneService` can use `ChangeNotifier` functionality -/// to notify listeners of changes. Services are typically registered in -/// `ArcaneServiceProvider` and can be accessed using the `service` -/// method on `BuildContext`. -abstract class ArcaneService with ChangeNotifier { - /// Retrieves a service of the specified type from the context. - /// - /// Returns null if no service of type `T` is found. - /// - /// Example: - /// ```dart - /// final myService = ArcaneService.ofType(context); - /// ``` - static T? ofType(BuildContext context) => - context.service(); - - /// Retrieves a service of the specified type from the context. - /// - /// Throws an assertion error if no service of type `T` is found. - /// - /// Example: - /// ```dart - /// final myService = ArcaneService.requiredOfType(context); - /// ``` - static T requiredOfType(BuildContext context) => - context.requiredService(); -} diff --git a/lib/src/providers/service/service_provider_extensions.dart b/lib/src/providers/service/service_provider_extensions.dart new file mode 100644 index 0000000..e71647d --- /dev/null +++ b/lib/src/providers/service/service_provider_extensions.dart @@ -0,0 +1,51 @@ +part of "arcane_service.dart"; + +/// An extension on `BuildContext` to provide easy access to `ArcaneService` instances +/// that are registered in an `ArcaneServiceProvider`. +/// +/// This extension provides methods for retrieving services in various ways. +/// +/// Example usage: +/// ```dart +/// final myService = context.service(); +/// ``` +extension ServiceProviderExtension on BuildContext { + /// Finds and returns the `ArcaneService` instance of type `T` that has been registered + /// in the `ArcaneServiceProvider` or in the list of built-in services (`Arcane.services`). + /// + /// If no such service is found, it returns `null`. + /// + /// Example: + /// ```dart + /// final myService = context.service(); + /// ``` + T? service() { + // First check built-in services + final builtInService = Arcane.services.whereType().firstOrNull; + if (builtInService != null) return builtInService; + + // Then check provider + return ArcaneServiceProvider.serviceOfType(this); + } + + /// Finds and returns the `ArcaneService` instance of type `T` that has been registered + /// in the `ArcaneServiceProvider` or in the list of built-in services (`Arcane.services`). + /// + /// Throws an assertion error if no service is found. + /// + /// Example: + /// ```dart + /// final myService = context.requiredService(); + /// ``` + T requiredService() { + final service = this.service(); + assert(service != null, "No service of type $T found"); + return service!; + } + + /// Legacy method to maintain backward compatibility. + /// + /// Prefer using `service()` instead. + @Deprecated("Use service() instead") + T? serviceOfType() => service(); +}