diff --git a/packages/leancode_cubit_utils_cqrs/CHANGELOG.md b/packages/leancode_cubit_utils_cqrs/CHANGELOG.md index bc33cf7..ee2a370 100644 --- a/packages/leancode_cubit_utils_cqrs/CHANGELOG.md +++ b/packages/leancode_cubit_utils_cqrs/CHANGELOG.md @@ -1,3 +1,11 @@ +## 0.2.0 + +* Add: + - `SimpleQueryWithEmptyCubit` + - `useQueryWithEmptyCubit` + - `SimpleArgsQueryWithEmptyCubit` + - `useArgsQueryWithEmptyCubit` + ## 0.1.0 * Extract cqrs support from the `leancode_cubit_utils` package. diff --git a/packages/leancode_cubit_utils_cqrs/lib/src/request/query_cubit.dart b/packages/leancode_cubit_utils_cqrs/lib/src/request/query_cubit.dart index 8057d58..623d946 100644 --- a/packages/leancode_cubit_utils_cqrs/lib/src/request/query_cubit.dart +++ b/packages/leancode_cubit_utils_cqrs/lib/src/request/query_cubit.dart @@ -9,8 +9,13 @@ mixin QueryResultHandler QueryResult result, ) async { if (result case QuerySuccess(:final data)) { + final mappedData = map(data); + if (isEmpty(mappedData)) { + logger.warning('Query success but data is empty'); + return RequestEmptyState(); + } logger.info('Query success. Data: $data'); - return RequestSuccessState(map(data)); + return RequestSuccessState(mappedData); } else if (result case QueryFailure(:final error)) { logger.severe('Query error. Error: $error'); try { diff --git a/packages/leancode_cubit_utils_cqrs/lib/src/request/use_query_cubit.dart b/packages/leancode_cubit_utils_cqrs/lib/src/request/use_query_cubit.dart index e28db59..7e79b7d 100644 --- a/packages/leancode_cubit_utils_cqrs/lib/src/request/use_query_cubit.dart +++ b/packages/leancode_cubit_utils_cqrs/lib/src/request/use_query_cubit.dart @@ -30,6 +30,7 @@ SimpleQueryCubit useQueryCubit( RequestMode? requestMode, bool callOnCreate = true, List keys = const [], + EmptyChecker? isEmpty, }) { return useBloc( () { @@ -47,6 +48,61 @@ SimpleQueryCubit useQueryCubit( ); } +/// Simplified implementation of [QueryCubit] created in order to be used by [useQueryCubit]. +/// Differ from [SimpleQueryCubit] because it uses a custom function to check if the data is empty. +class SimpleQueryWithEmptyCubit extends QueryCubit { + /// Creates a new [SimpleQueryWithEmptyCubit]. + SimpleQueryWithEmptyCubit( + super.loggerTag, + this._customRequest, + this._isEmpty, { + super.requestMode, + }); + + /// The request to be executed. + final Request> _customRequest; + + /// The function to check if the data is empty. + final EmptyChecker _isEmpty; + + @override + Future> request() => _customRequest(); + + @override + TOut map(TOut data) => data; + + @override + bool isEmpty(TOut data) => _isEmpty(data); +} + +/// Provides a [QueryCubit] specialized for [QueryResult] that is automatically disposed without having +/// to use BlocProvider and does not require any arguments. It is a wrapper of [useBloc] that creates a [SimpleQueryWithEmptyCubit]. +/// Differ from [useQueryCubit] because it uses a custom function to check if the data is empty. +SimpleQueryWithEmptyCubit useQueryWithEmptyCubit( + Request> request, + EmptyChecker isEmpty, { + String loggerTag = 'SimpleQueryWithEmptyCubit', + RequestMode? requestMode, + bool callOnCreate = true, + List keys = const [], +}) { + return useBloc( + () { + final cubit = SimpleQueryWithEmptyCubit( + loggerTag, + request, + isEmpty, + requestMode: requestMode, + ); + if (callOnCreate) { + cubit.run(); + } + return cubit; + }, + keys, + ); +} + /// Simplified implementation of [ArgsQueryCubit] created in order to be used by [useArgsQueryCubit]. class SimpleArgsQueryCubit extends ArgsQueryCubit { @@ -84,3 +140,53 @@ SimpleArgsQueryCubit useArgsQueryCubit( keys, ); } + +/// Simplified implementation of [ArgsQueryCubit] created in order to be used by [useArgsQueryCubit]. +/// Differ from [SimpleArgsQueryCubit] because it uses a custom function to check if the data is empty. +class SimpleArgsQueryWithEmptyCubit + extends ArgsQueryCubit { + /// Creates a new [SimpleArgsQueryWithEmptyCubit]. + SimpleArgsQueryWithEmptyCubit( + super.loggerTag, + this._customRequest, + this._isEmpty, { + super.requestMode, + }); + + /// The request to be executed. + final ArgsRequest> _customRequest; + + /// The function to check if the data is empty. + final EmptyChecker _isEmpty; + + @override + Future> request(TArgs args) => _customRequest(args); + + @override + TOut map(TOut data) => data; + + @override + bool isEmpty(TOut data) => _isEmpty(data); +} + +/// Provides a [ArgsQueryCubit] specialized for [QueryResult] that is automatically disposed without having +/// to use BlocProvider and requires arguments. It is a wrapper of [useBloc] that creates a [SimpleArgsQueryWithEmptyCubit]. +/// Differ from [useArgsQueryCubit] because it uses a custom function to check if the data is empty. +SimpleArgsQueryWithEmptyCubit + useArgsQueryWithEmptyCubit( + ArgsRequest> request, + EmptyChecker isEmpty, { + String loggerTag = 'SimpleArgsQueryWithEmptyCubit', + RequestMode? requestMode, + List keys = const [], +}) { + return useBloc( + () => SimpleArgsQueryWithEmptyCubit( + loggerTag, + request, + isEmpty, + requestMode: requestMode, + ), + keys, + ); +} diff --git a/packages/leancode_cubit_utils_cqrs/pubspec.yaml b/packages/leancode_cubit_utils_cqrs/pubspec.yaml index 0206906..def9ed1 100644 --- a/packages/leancode_cubit_utils_cqrs/pubspec.yaml +++ b/packages/leancode_cubit_utils_cqrs/pubspec.yaml @@ -1,6 +1,6 @@ name: leancode_cubit_utils_cqrs description: An extension of leancode_cubit_utils that provides cqrs support. -version: 0.1.0 +version: 0.2.0 repository: https://github.com/leancodepl/leancode_cubit_utils environment: @@ -12,7 +12,7 @@ dependencies: flutter: sdk: flutter flutter_bloc: ^8.0.0 - leancode_cubit_utils: ^0.1.0 + leancode_cubit_utils: ^0.2.0 leancode_hooks: ^0.0.6 dev_dependencies: