From 9d9d2aa456f3ad5041ef5d42db92e94864b26702 Mon Sep 17 00:00:00 2001 From: Achintha Isuru Date: Wed, 21 Jan 2026 19:58:22 -0500 Subject: [PATCH] refactor: decompose hub arguments into separate, dedicated classes for improved modularity. --- .../hub_repository_impl.dart | 20 +++++++---- .../arguments/assign_nfc_tag_arguments.dart | 20 +++++++++++ .../arguments/create_hub_arguments.dart | 20 +++++++++++ .../arguments/delete_hub_arguments.dart | 17 ++++++++++ .../src/domain/arguments/hub_arguments.dart | 33 ------------------- .../hub_repository_interface.dart | 12 +++++-- .../usecases/assign_nfc_tag_usecase.dart | 8 ++++- .../domain/usecases/create_hub_usecase.dart | 9 ++++- .../domain/usecases/delete_hub_usecase.dart | 7 +++- .../src/domain/usecases/get_hubs_usecase.dart | 6 ++++ .../presentation/blocs/client_hubs_bloc.dart | 9 +++-- 11 files changed, 115 insertions(+), 46 deletions(-) create mode 100644 apps/packages/features/client/hubs/lib/src/domain/arguments/assign_nfc_tag_arguments.dart create mode 100644 apps/packages/features/client/hubs/lib/src/domain/arguments/create_hub_arguments.dart create mode 100644 apps/packages/features/client/hubs/lib/src/domain/arguments/delete_hub_arguments.dart delete mode 100644 apps/packages/features/client/hubs/lib/src/domain/arguments/hub_arguments.dart diff --git a/apps/packages/features/client/hubs/lib/src/data/repositories_impl/hub_repository_impl.dart b/apps/packages/features/client/hubs/lib/src/data/repositories_impl/hub_repository_impl.dart index 6224816a..578a9ea2 100644 --- a/apps/packages/features/client/hubs/lib/src/data/repositories_impl/hub_repository_impl.dart +++ b/apps/packages/features/client/hubs/lib/src/data/repositories_impl/hub_repository_impl.dart @@ -2,36 +2,44 @@ import 'package:krow_data_connect/krow_data_connect.dart'; import 'package:krow_domain/krow_domain.dart'; import '../../domain/repositories/hub_repository_interface.dart'; -/// Implementation of [HubRepositoryInterface]. +/// Implementation of [HubRepositoryInterface] that uses [BusinessRepositoryMock]. /// -/// This implementation delegates data access to the [BusinessRepositoryMock] -/// from the `data_connect` package. +/// This class serves as a data adapter that bridges the domain repository interface +/// with the backend data source provided by the `data_connect` package. It strictly +/// delegates all operations to the [BusinessRepositoryMock], ensuring no business +/// logic resides in the data layer. class HubRepositoryImpl implements HubRepositoryInterface { /// The business repository mock from data connect. final BusinessRepositoryMock mock; - /// Creates a [HubRepositoryImpl] with the required [mock]. + /// Creates a [HubRepositoryImpl] instance. + /// + /// Takes a [BusinessRepositoryMock] as a dependency to perform data operations. HubRepositoryImpl({required this.mock}); @override Future> getHubs() { - // In a real app, we would get the business ID from a session or state. - // For this prototype/mock, we use a hardcoded value. + // In a production environment, the business ID would be retrieved from + // a session manager or authentication state. For the current mock strategy, + // we use a hardcoded value 'biz_1' to represent the active client. return mock.getHubs('biz_1'); } @override Future createHub({required String name, required String address}) { + // Delegates hub creation to the mock repository. return mock.createHub(businessId: 'biz_1', name: name, address: address); } @override Future deleteHub(String id) { + // Delegates hub deletion to the mock repository. return mock.deleteHub(id); } @override Future assignNfcTag({required String hubId, required String nfcTagId}) { + // Delegates NFC tag assignment to the mock repository. return mock.assignNfcTag(hubId: hubId, nfcTagId: nfcTagId); } } diff --git a/apps/packages/features/client/hubs/lib/src/domain/arguments/assign_nfc_tag_arguments.dart b/apps/packages/features/client/hubs/lib/src/domain/arguments/assign_nfc_tag_arguments.dart new file mode 100644 index 00000000..250b0d2b --- /dev/null +++ b/apps/packages/features/client/hubs/lib/src/domain/arguments/assign_nfc_tag_arguments.dart @@ -0,0 +1,20 @@ +import 'package:krow_core/core.dart'; + +/// Represents the arguments required for the AssignNfcTagUseCase. +/// +/// Encapsulates the hub ID and the NFC tag ID to be assigned. +class AssignNfcTagArguments extends UseCaseArgument { + /// The unique identifier of the hub. + final String hubId; + + /// The unique identifier of the NFC tag. + final String nfcTagId; + + /// Creates an [AssignNfcTagArguments] instance. + /// + /// Both [hubId] and [nfcTagId] are required. + const AssignNfcTagArguments({required this.hubId, required this.nfcTagId}); + + @override + List get props => [hubId, nfcTagId]; +} diff --git a/apps/packages/features/client/hubs/lib/src/domain/arguments/create_hub_arguments.dart b/apps/packages/features/client/hubs/lib/src/domain/arguments/create_hub_arguments.dart new file mode 100644 index 00000000..e24c33b2 --- /dev/null +++ b/apps/packages/features/client/hubs/lib/src/domain/arguments/create_hub_arguments.dart @@ -0,0 +1,20 @@ +import 'package:krow_core/core.dart'; + +/// Represents the arguments required for the CreateHubUseCase. +/// +/// Encapsulates the name and address of the hub to be created. +class CreateHubArguments extends UseCaseArgument { + /// The name of the hub. + final String name; + + /// The physical address of the hub. + final String address; + + /// Creates a [CreateHubArguments] instance. + /// + /// Both [name] and [address] are required. + const CreateHubArguments({required this.name, required this.address}); + + @override + List get props => [name, address]; +} diff --git a/apps/packages/features/client/hubs/lib/src/domain/arguments/delete_hub_arguments.dart b/apps/packages/features/client/hubs/lib/src/domain/arguments/delete_hub_arguments.dart new file mode 100644 index 00000000..5ed5b9af --- /dev/null +++ b/apps/packages/features/client/hubs/lib/src/domain/arguments/delete_hub_arguments.dart @@ -0,0 +1,17 @@ +import 'package:krow_core/core.dart'; + +/// Represents the arguments required for the DeleteHubUseCase. +/// +/// Encapsulates the hub ID of the hub to be deleted. +class DeleteHubArguments extends UseCaseArgument { + /// The unique identifier of the hub to delete. + final String hubId; + + /// Creates a [DeleteHubArguments] instance. + /// + /// The [hubId] is required. + const DeleteHubArguments({required this.hubId}); + + @override + List get props => [hubId]; +} diff --git a/apps/packages/features/client/hubs/lib/src/domain/arguments/hub_arguments.dart b/apps/packages/features/client/hubs/lib/src/domain/arguments/hub_arguments.dart deleted file mode 100644 index d85c16dc..00000000 --- a/apps/packages/features/client/hubs/lib/src/domain/arguments/hub_arguments.dart +++ /dev/null @@ -1,33 +0,0 @@ -import 'package:krow_core/core.dart'; - -/// Arguments for creating a new hub. -class CreateHubArguments extends UseCaseArgument { - final String name; - final String address; - - const CreateHubArguments({required this.name, required this.address}); - - @override - List get props => [name, address]; -} - -/// Arguments for assigning an NFC tag to a hub. -class AssignNfcTagArguments extends UseCaseArgument { - final String hubId; - final String nfcTagId; - - const AssignNfcTagArguments({required this.hubId, required this.nfcTagId}); - - @override - List get props => [hubId, nfcTagId]; -} - -/// Arguments for deleting a hub. -class DeleteHubArguments extends UseCaseArgument { - final String hubId; - - const DeleteHubArguments({required this.hubId}); - - @override - List get props => [hubId]; -} diff --git a/apps/packages/features/client/hubs/lib/src/domain/repositories/hub_repository_interface.dart b/apps/packages/features/client/hubs/lib/src/domain/repositories/hub_repository_interface.dart index 8c7b7d3c..5b03fced 100644 --- a/apps/packages/features/client/hubs/lib/src/domain/repositories/hub_repository_interface.dart +++ b/apps/packages/features/client/hubs/lib/src/domain/repositories/hub_repository_interface.dart @@ -2,18 +2,26 @@ import 'package:krow_domain/krow_domain.dart'; /// Interface for the Hub repository. /// -/// This repository handles hub-related operations such as -/// fetching, creating, deleting hubs and assigning NFC tags. +/// This repository defines the contract for hub-related operations in the +/// domain layer. It handles fetching, creating, deleting hubs and assigning +/// NFC tags. The implementation will be provided in the data layer. abstract interface class HubRepositoryInterface { /// Fetches the list of hubs for the current client. + /// + /// Returns a list of [Hub] entities. Future> getHubs(); /// Creates a new hub. + /// + /// Takes the [name] and [address] of the new hub. + /// Returns the created [Hub] entity. Future createHub({required String name, required String address}); /// Deletes a hub by its [id]. Future deleteHub(String id); /// Assigns an NFC tag to a hub. + /// + /// Takes the [hubId] and the [nfcTagId] to be associated. Future assignNfcTag({required String hubId, required String nfcTagId}); } diff --git a/apps/packages/features/client/hubs/lib/src/domain/usecases/assign_nfc_tag_usecase.dart b/apps/packages/features/client/hubs/lib/src/domain/usecases/assign_nfc_tag_usecase.dart index 4b3ce693..ed627c9c 100644 --- a/apps/packages/features/client/hubs/lib/src/domain/usecases/assign_nfc_tag_usecase.dart +++ b/apps/packages/features/client/hubs/lib/src/domain/usecases/assign_nfc_tag_usecase.dart @@ -1,11 +1,17 @@ import 'package:krow_core/core.dart'; -import '../arguments/hub_arguments.dart'; +import '../arguments/assign_nfc_tag_arguments.dart'; import '../repositories/hub_repository_interface.dart'; /// Use case for assigning an NFC tag to a hub. +/// +/// This use case handles the association of a physical NFC tag with a specific +/// hub by calling the [HubRepositoryInterface]. class AssignNfcTagUseCase implements UseCase { final HubRepositoryInterface _repository; + /// Creates an [AssignNfcTagUseCase]. + /// + /// Requires a [HubRepositoryInterface] to interact with the backend. AssignNfcTagUseCase(this._repository); @override diff --git a/apps/packages/features/client/hubs/lib/src/domain/usecases/create_hub_usecase.dart b/apps/packages/features/client/hubs/lib/src/domain/usecases/create_hub_usecase.dart index f6f80baa..bbfc1403 100644 --- a/apps/packages/features/client/hubs/lib/src/domain/usecases/create_hub_usecase.dart +++ b/apps/packages/features/client/hubs/lib/src/domain/usecases/create_hub_usecase.dart @@ -1,12 +1,19 @@ import 'package:krow_core/core.dart'; import 'package:krow_domain/krow_domain.dart'; -import '../arguments/hub_arguments.dart'; +import '../arguments/create_hub_arguments.dart'; import '../repositories/hub_repository_interface.dart'; /// Use case for creating a new hub. +/// +/// This use case orchestrates the creation of a hub by interacting with the +/// [HubRepositoryInterface]. It requires [CreateHubArguments] which includes +/// the name and address of the hub. class CreateHubUseCase implements UseCase { final HubRepositoryInterface _repository; + /// Creates a [CreateHubUseCase]. + /// + /// Requires a [HubRepositoryInterface] to perform the actual creation. CreateHubUseCase(this._repository); @override diff --git a/apps/packages/features/client/hubs/lib/src/domain/usecases/delete_hub_usecase.dart b/apps/packages/features/client/hubs/lib/src/domain/usecases/delete_hub_usecase.dart index 5b035569..ee8d14df 100644 --- a/apps/packages/features/client/hubs/lib/src/domain/usecases/delete_hub_usecase.dart +++ b/apps/packages/features/client/hubs/lib/src/domain/usecases/delete_hub_usecase.dart @@ -1,11 +1,16 @@ import 'package:krow_core/core.dart'; -import '../arguments/hub_arguments.dart'; +import '../arguments/delete_hub_arguments.dart'; import '../repositories/hub_repository_interface.dart'; /// Use case for deleting a hub. +/// +/// This use case removes a hub from the system via the [HubRepositoryInterface]. class DeleteHubUseCase implements UseCase { final HubRepositoryInterface _repository; + /// Creates a [DeleteHubUseCase]. + /// + /// Requires a [HubRepositoryInterface] to perform the deletion. DeleteHubUseCase(this._repository); @override diff --git a/apps/packages/features/client/hubs/lib/src/domain/usecases/get_hubs_usecase.dart b/apps/packages/features/client/hubs/lib/src/domain/usecases/get_hubs_usecase.dart index cb6c937c..7db32de4 100644 --- a/apps/packages/features/client/hubs/lib/src/domain/usecases/get_hubs_usecase.dart +++ b/apps/packages/features/client/hubs/lib/src/domain/usecases/get_hubs_usecase.dart @@ -3,9 +3,15 @@ import 'package:krow_domain/krow_domain.dart'; import '../repositories/hub_repository_interface.dart'; /// Use case for fetching the list of hubs. +/// +/// This use case retrieves all hubs associated with the current client +/// by interacting with the [HubRepositoryInterface]. class GetHubsUseCase implements NoInputUseCase> { final HubRepositoryInterface _repository; + /// Creates a [GetHubsUseCase]. + /// + /// Requires a [HubRepositoryInterface] to fetch the data. GetHubsUseCase(this._repository); @override diff --git a/apps/packages/features/client/hubs/lib/src/presentation/blocs/client_hubs_bloc.dart b/apps/packages/features/client/hubs/lib/src/presentation/blocs/client_hubs_bloc.dart index a874e690..57366632 100644 --- a/apps/packages/features/client/hubs/lib/src/presentation/blocs/client_hubs_bloc.dart +++ b/apps/packages/features/client/hubs/lib/src/presentation/blocs/client_hubs_bloc.dart @@ -1,6 +1,8 @@ import 'package:bloc/bloc.dart'; import 'package:flutter_modular/flutter_modular.dart'; -import '../../domain/arguments/hub_arguments.dart'; +import '../../domain/arguments/assign_nfc_tag_arguments.dart'; +import '../../domain/arguments/create_hub_arguments.dart'; +import '../../domain/arguments/delete_hub_arguments.dart'; import '../../domain/usecases/assign_nfc_tag_usecase.dart'; import '../../domain/usecases/create_hub_usecase.dart'; import '../../domain/usecases/delete_hub_usecase.dart'; @@ -8,7 +10,10 @@ import '../../domain/usecases/get_hubs_usecase.dart'; import 'client_hubs_event.dart'; import 'client_hubs_state.dart'; -/// BLoC responsible for managing client hubs. +/// BLoC responsible for managing the state of the Client Hubs feature. +/// +/// It orchestrates the flow between the UI and the domain layer by invoking +/// specific use cases for fetching, creating, deleting, and assigning tags to hubs. class ClientHubsBloc extends Bloc implements Disposable { final GetHubsUseCase _getHubsUseCase;