Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cubits and hooks with empty #37

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/leancode_cubit_utils_cqrs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.2.0

* Add:
- `SimpleQueryWithEmptyCubit`
- `useQueryWithEmptyCubit`
- `SimpleArgsQueryWithEmptyCubit`
- `useArgsQueryWithEmptyCubit`

## 0.1.0

* Extract cqrs support from the `leancode_cubit_utils` package.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ mixin QueryResultHandler<TRes, TOut>
QueryResult<TRes> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ SimpleQueryCubit<TOut> useQueryCubit<TOut>(
RequestMode? requestMode,
bool callOnCreate = true,
List<Object?> keys = const [],
EmptyChecker<TOut>? isEmpty,
}) {
return useBloc(
() {
Expand All @@ -47,6 +48,61 @@ SimpleQueryCubit<TOut> useQueryCubit<TOut>(
);
}

/// 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<TOut> extends QueryCubit<TOut, TOut> {
/// Creates a new [SimpleQueryWithEmptyCubit].
SimpleQueryWithEmptyCubit(
super.loggerTag,
this._customRequest,
this._isEmpty, {
super.requestMode,
});

/// The request to be executed.
final Request<QueryResult<TOut>> _customRequest;

/// The function to check if the data is empty.
final EmptyChecker<TOut> _isEmpty;

@override
Future<QueryResult<TOut>> 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<TOut> useQueryWithEmptyCubit<TOut>(
Request<QueryResult<TOut>> request,
EmptyChecker<TOut> isEmpty, {
String loggerTag = 'SimpleQueryWithEmptyCubit',
RequestMode? requestMode,
bool callOnCreate = true,
List<Object?> keys = const [],
}) {
return useBloc(
() {
final cubit = SimpleQueryWithEmptyCubit<TOut>(
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<TArgs, TOut>
extends ArgsQueryCubit<TArgs, TOut, TOut> {
Expand Down Expand Up @@ -84,3 +140,53 @@ SimpleArgsQueryCubit<TArgs, TOut> useArgsQueryCubit<TArgs, TOut>(
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<TArgs, TOut>
extends ArgsQueryCubit<TArgs, TOut, TOut> {
/// Creates a new [SimpleArgsQueryWithEmptyCubit].
SimpleArgsQueryWithEmptyCubit(
super.loggerTag,
this._customRequest,
this._isEmpty, {
super.requestMode,
});

/// The request to be executed.
final ArgsRequest<TArgs, QueryResult<TOut>> _customRequest;

/// The function to check if the data is empty.
final EmptyChecker<TOut> _isEmpty;

@override
Future<QueryResult<TOut>> 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<TArgs, TOut>
useArgsQueryWithEmptyCubit<TArgs, TOut>(
ArgsRequest<TArgs, QueryResult<TOut>> request,
EmptyChecker<TOut> isEmpty, {
String loggerTag = 'SimpleArgsQueryWithEmptyCubit',
RequestMode? requestMode,
List<Object?> keys = const [],
}) {
return useBloc(
() => SimpleArgsQueryWithEmptyCubit<TArgs, TOut>(
loggerTag,
request,
isEmpty,
requestMode: requestMode,
),
keys,
);
}
4 changes: 2 additions & 2 deletions packages/leancode_cubit_utils_cqrs/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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:
Expand Down