Skip to content

Commit 4ed9df7

Browse files
committed
Add cubits and with empty
1 parent ff7c218 commit 4ed9df7

4 files changed

Lines changed: 122 additions & 3 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 0.2.0
2+
3+
* Add:
4+
- `SimpleQueryWithEmptyCubit`
5+
- `useQueryWithEmptyCubit`
6+
- `SimpleArgsQueryWithEmptyCubit`
7+
- `useArgsQueryWithEmptyCubit`
8+
19
## 0.1.0
210

311
* Extract cqrs support from the `leancode_cubit_utils` package.

packages/leancode_cubit_utils_cqrs/lib/src/request/query_cubit.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ mixin QueryResultHandler<TRes, TOut>
99
QueryResult<TRes> result,
1010
) async {
1111
if (result case QuerySuccess(:final data)) {
12+
final mappedData = map(data);
13+
if (isEmpty(mappedData)) {
14+
logger.warning('Query success but data is empty');
15+
return RequestEmptyState();
16+
}
1217
logger.info('Query success. Data: $data');
13-
return RequestSuccessState(map(data));
18+
return RequestSuccessState(mappedData);
1419
} else if (result case QueryFailure(:final error)) {
1520
logger.severe('Query error. Error: $error');
1621
try {

packages/leancode_cubit_utils_cqrs/lib/src/request/use_query_cubit.dart

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ SimpleQueryCubit<TOut> useQueryCubit<TOut>(
3030
RequestMode? requestMode,
3131
bool callOnCreate = true,
3232
List<Object?> keys = const [],
33+
EmptyChecker<TOut>? isEmpty,
3334
}) {
3435
return useBloc(
3536
() {
@@ -47,6 +48,61 @@ SimpleQueryCubit<TOut> useQueryCubit<TOut>(
4748
);
4849
}
4950

51+
/// Simplified implementation of [QueryCubit] created in order to be used by [useQueryCubit].
52+
/// Differ from [SimpleQueryCubit] because it uses a custom function to check if the data is empty.
53+
class SimpleQueryWithEmptyCubit<TOut> extends QueryCubit<TOut, TOut> {
54+
/// Creates a new [SimpleQueryWithEmptyCubit].
55+
SimpleQueryWithEmptyCubit(
56+
super.loggerTag,
57+
this._customRequest,
58+
this._isEmpty, {
59+
super.requestMode,
60+
});
61+
62+
/// The request to be executed.
63+
final Request<QueryResult<TOut>> _customRequest;
64+
65+
/// The function to check if the data is empty.
66+
final EmptyChecker<TOut> _isEmpty;
67+
68+
@override
69+
Future<QueryResult<TOut>> request() => _customRequest();
70+
71+
@override
72+
TOut map(TOut data) => data;
73+
74+
@override
75+
bool isEmpty(TOut data) => _isEmpty(data);
76+
}
77+
78+
/// Provides a [QueryCubit] specialized for [QueryResult] that is automatically disposed without having
79+
/// to use BlocProvider and does not require any arguments. It is a wrapper of [useBloc] that creates a [SimpleQueryWithEmptyCubit].
80+
/// Differ from [useQueryCubit] because it uses a custom function to check if the data is empty.
81+
SimpleQueryWithEmptyCubit<TOut> useQueryWithEmptyCubit<TOut>(
82+
Request<QueryResult<TOut>> request,
83+
EmptyChecker<TOut> isEmpty, {
84+
String loggerTag = 'SimpleQueryWithEmptyCubit',
85+
RequestMode? requestMode,
86+
bool callOnCreate = true,
87+
List<Object?> keys = const [],
88+
}) {
89+
return useBloc(
90+
() {
91+
final cubit = SimpleQueryWithEmptyCubit<TOut>(
92+
loggerTag,
93+
request,
94+
isEmpty,
95+
requestMode: requestMode,
96+
);
97+
if (callOnCreate) {
98+
cubit.run();
99+
}
100+
return cubit;
101+
},
102+
keys,
103+
);
104+
}
105+
50106
/// Simplified implementation of [ArgsQueryCubit] created in order to be used by [useArgsQueryCubit].
51107
class SimpleArgsQueryCubit<TArgs, TOut>
52108
extends ArgsQueryCubit<TArgs, TOut, TOut> {
@@ -84,3 +140,53 @@ SimpleArgsQueryCubit<TArgs, TOut> useArgsQueryCubit<TArgs, TOut>(
84140
keys,
85141
);
86142
}
143+
144+
/// Simplified implementation of [ArgsQueryCubit] created in order to be used by [useArgsQueryCubit].
145+
/// Differ from [SimpleArgsQueryCubit] because it uses a custom function to check if the data is empty.
146+
class SimpleArgsQueryWithEmptyCubit<TArgs, TOut>
147+
extends ArgsQueryCubit<TArgs, TOut, TOut> {
148+
/// Creates a new [SimpleArgsQueryWithEmptyCubit].
149+
SimpleArgsQueryWithEmptyCubit(
150+
super.loggerTag,
151+
this._customRequest,
152+
this._isEmpty, {
153+
super.requestMode,
154+
});
155+
156+
/// The request to be executed.
157+
final ArgsRequest<TArgs, QueryResult<TOut>> _customRequest;
158+
159+
/// The function to check if the data is empty.
160+
final EmptyChecker<TOut> _isEmpty;
161+
162+
@override
163+
Future<QueryResult<TOut>> request(TArgs args) => _customRequest(args);
164+
165+
@override
166+
TOut map(TOut data) => data;
167+
168+
@override
169+
bool isEmpty(TOut data) => _isEmpty(data);
170+
}
171+
172+
/// Provides a [ArgsQueryCubit] specialized for [QueryResult] that is automatically disposed without having
173+
/// to use BlocProvider and requires arguments. It is a wrapper of [useBloc] that creates a [SimpleArgsQueryWithEmptyCubit].
174+
/// Differ from [useArgsQueryCubit] because it uses a custom function to check if the data is empty.
175+
SimpleArgsQueryWithEmptyCubit<TArgs, TOut>
176+
useArgsQueryWithEmptyCubit<TArgs, TOut>(
177+
ArgsRequest<TArgs, QueryResult<TOut>> request,
178+
EmptyChecker<TOut> isEmpty, {
179+
String loggerTag = 'SimpleArgsQueryWithEmptyCubit',
180+
RequestMode? requestMode,
181+
List<Object?> keys = const [],
182+
}) {
183+
return useBloc(
184+
() => SimpleArgsQueryWithEmptyCubit<TArgs, TOut>(
185+
loggerTag,
186+
request,
187+
isEmpty,
188+
requestMode: requestMode,
189+
),
190+
keys,
191+
);
192+
}

packages/leancode_cubit_utils_cqrs/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: leancode_cubit_utils_cqrs
22
description: An extension of leancode_cubit_utils that provides cqrs support.
3-
version: 0.1.0
3+
version: 0.2.0
44
repository: https://github.com/leancodepl/leancode_cubit_utils
55

66
environment:
@@ -12,7 +12,7 @@ dependencies:
1212
flutter:
1313
sdk: flutter
1414
flutter_bloc: ^8.0.0
15-
leancode_cubit_utils: ^0.1.0
15+
leancode_cubit_utils: ^0.2.0
1616
leancode_hooks: ^0.0.6
1717

1818
dev_dependencies:

0 commit comments

Comments
 (0)