diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c98b09..5010ca1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.0.4 + +* Remove unused, unexported code from `query` directory + ## 0.0.3 * Make `retry` in `RequestErrorBuilder` and `PaginatedErrorBuilder` non-nullable diff --git a/lib/src/query/query_config_provider.dart b/lib/src/query/query_config_provider.dart deleted file mode 100644 index 7f674cf..0000000 --- a/lib/src/query/query_config_provider.dart +++ /dev/null @@ -1,60 +0,0 @@ -import 'package:flutter/widgets.dart'; -import 'package:leancode_cubit_utils/src/query/query_cubit.dart'; -import 'package:leancode_cubit_utils/src/query/query_cubit_builder.dart'; -import 'package:leancode_cubit_utils/src/query/query_cubit_config.dart'; -import 'package:provider/provider.dart'; - -/// A default root config. -class QueryConfig { - /// Creates a new [QueryConfig]. - QueryConfig({ - required this.onLoading, - required this.onError, - }); - - /// The builder that creates a widget when query is loading. - final WidgetBuilder onLoading; - - /// The builder that creates a widget when query failed. - final QueryErrorBuilder onError; -} - -/// A widget that provides default loading and error widgets for [QueryCubitBuilder]. -class QueryConfigProvider extends StatelessWidget { - /// Creates a new [QueryConfigProvider]. - const QueryConfigProvider({ - super.key, - this.requestMode, - required this.onLoading, - required this.onError, - required this.child, - }); - - /// The default request mode used by all [QueryCubit]s. - final RequestMode? requestMode; - - /// The builder that creates a widget when query is loading. - final WidgetBuilder onLoading; - - /// The builder that creates a widget when query failed. - final QueryErrorBuilder onError; - - /// The child widget. - final Widget child; - - @override - Widget build(BuildContext context) { - // Sets the default request mode. - if (requestMode != null) { - QueryCubitConfig.requestMode = requestMode!; - } - - return Provider( - create: (context) => QueryConfig( - onLoading: onLoading, - onError: onError, - ), - child: child, - ); - } -} diff --git a/lib/src/query/query_cubit.dart b/lib/src/query/query_cubit.dart deleted file mode 100644 index abf91ad..0000000 --- a/lib/src/query/query_cubit.dart +++ /dev/null @@ -1,257 +0,0 @@ -import 'dart:async'; - -import 'package:async/async.dart'; -import 'package:cqrs/cqrs.dart'; -import 'package:equatable/equatable.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:leancode_cubit_utils/src/query/query_cubit_config.dart'; -import 'package:logging/logging.dart'; - -/// Signature for a function that returns a [QueryResult]. -typedef QueryRequest = Future> Function(); - -/// Signature for a function that returns a [QueryResult] and takes arguments. -typedef QueryArgsRequest = Future> Function( - TArgs, -); - -/// Signature for a function that maps query response of to the output type. -typedef QueryResponseMapper = TOut Function(TRes); - -/// Signature for a function that maps query error to other state. -typedef QueryErrorMapper = Future> Function( - QueryErrorState, -); - -/// Defines how to handle a new request when the previous one is still running. -enum RequestMode { - /// When a new request is triggered while the previous one is still running, - /// the previous request is cancelled and the new one is executed. - replace, - - /// When a new request is triggered while the previous one is still running, - /// the new request is ignored. - ignore; -} - -/// Base class for all query cubits. -abstract class BaseQueryCubit extends Cubit> { - /// Creates a new [BaseQueryCubit] with the given [loggerTag] and [requestMode]. - BaseQueryCubit( - String loggerTag, { - this.requestMode, - }) : _logger = Logger(loggerTag), - super(QueryInitialState()); - - final Logger _logger; - - /// The request mode used by this cubit to handle duplicated requests. - final RequestMode? requestMode; - - CancelableOperation>? _operation; - - Future _run( - Future> Function() callback, { - bool isRefresh = false, - }) async { - try { - switch (requestMode ?? QueryCubitConfig.requestMode) { - case RequestMode.replace: - await _operation?.cancel(); - case RequestMode.ignore: - if (_operation?.isCompleted == false) { - _logger.info('Previous operation is not completed. Ignoring.'); - return; - } - } - - if (isRefresh) { - _logger.info('Refreshing query.'); - emit( - QueryRefreshState( - switch (state) { - QuerySuccessState(:final data) => data, - QueryRefreshState(:final data) => data, - _ => null, - }, - ), - ); - } else { - _logger.info('Query started.'); - emit(QueryLoadingState()); - } - - _operation = CancelableOperation.fromFuture( - callback(), - onCancel: () { - _logger.info('Canceling previous operation.'); - }, - ); - - final result = await _operation?.valueOrCancellation(); - if (result == null) { - return; - } - - if (result case QuerySuccess(:final data)) { - _logger.info('Query success. Data: $data'); - emit(QuerySuccessState(map(data))); - } else if (result case QueryFailure(:final error)) { - _logger.severe('Query error. Error: $error'); - try { - emit(await onQueryError(QueryErrorState(error: error))); - } catch (e, s) { - _logger.severe( - 'Processing error failed. Exception: $e. Stack trace: $s', - ); - emit(QueryErrorState(exception: e, stackTrace: s)); - } - } - } catch (e, s) { - _logger.severe('Query error. Exception: $e. Stack trace: $s'); - try { - emit(await onQueryError(QueryErrorState(exception: e, stackTrace: s))); - } catch (e, s) { - _logger.severe( - 'Processing error failed. Exception: $e. Stack trace: $s', - ); - emit(QueryErrorState(exception: e, stackTrace: s)); - } - } - } - - /// Maps the given [data] to the output type [TOut]. - TOut map(TRes data); - - /// Handles the given [errorState] and returns the corresponding state. - Future> onQueryError( - QueryErrorState errorState, - ) async { - return errorState; - } - - /// Refreshes the query. Handling duplicated requests depends on the - /// [requestMode]. - Future refresh(); -} - -/// Base class for all query cubits which don't require any arguments. -abstract class QueryCubit extends BaseQueryCubit { - /// Creates a new [QueryCubit] with the given [requestMode]. - QueryCubit( - super.loggerTag, { - super.requestMode, - }); - - /// Gets the data from the request and emits the corresponding state. - Future run() => _run(request); - - /// A request to be executed. - Future> request(); - - /// Refreshes the query. - @override - Future refresh() { - return _run(request, isRefresh: true); - } -} - -/// Base class for all query cubits which require arguments. -abstract class ArgsQueryCubit - extends BaseQueryCubit { - /// Creates a new [ArgsQueryCubit] with the given [requestMode]. - ArgsQueryCubit( - super.loggerTag, { - super.requestMode, - }); - - TArgs? _lastGetArgs; - - /// The arguments used by this cubit to refresh the query. - TArgs? get lastFetchArgs => _lastGetArgs; - - /// Gets the data from the request and emits the corresponding state. - Future run(TArgs args) { - _lastGetArgs = args; - return _run(() => request(args)); - } - - /// A request to be executed. - Future> request(TArgs args); - - @override - Future refresh() { - if (_lastGetArgs == null) { - _logger.severe('No query was executed yet. Cannot refresh.'); - throw StateError('No query was executed yet. Cannot refresh.'); - } else { - // ignore: null_check_on_nullable_type_parameter - return _run(() => request(_lastGetArgs!), isRefresh: true); - } - } -} - -/// Represents the state of a query. -sealed class QueryState with EquatableMixin {} - -/// Represents the initial state of a query. -final class QueryInitialState extends QueryState { - @override - List get props => []; -} - -/// Represents the loading state of a query. -final class QueryLoadingState extends QueryState { - /// Creates a new [QueryLoadingState]. - QueryLoadingState(); - - @override - List get props => []; -} - -/// Represents the refresh state of a query. -final class QueryRefreshState extends QueryState { - /// Creates a new [QueryRefreshState] with the previous [data]. - QueryRefreshState([this.data]); - - /// The previous data. - final TOut? data; - - @override - List get props => [data]; -} - -/// Represents a successful query. -final class QuerySuccessState extends QueryState { - /// Creates a new [QuerySuccessState] with the given [data]. - QuerySuccessState(this.data); - - /// The data returned by the query. - final TOut data; - - @override - List get props => [data]; -} - -/// Represents a failed query. -final class QueryErrorState extends QueryState { - /// Creates a new [QueryErrorState] with the given [error], [exception] and - /// [stackTrace]. - QueryErrorState({ - this.error, - this.exception, - this.stackTrace, - }); - - /// The error returned by the query. - final QueryError? error; - - /// The exception thrown when processing the query. - final Object? exception; - - /// The stack trace of the exception thrown when processing the query. - final StackTrace? stackTrace; - - @override - List get props => [error, exception, stackTrace]; -} diff --git a/lib/src/query/query_cubit_builder.dart b/lib/src/query/query_cubit_builder.dart deleted file mode 100644 index 812a12e..0000000 --- a/lib/src/query/query_cubit_builder.dart +++ /dev/null @@ -1,74 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:leancode_cubit_utils/src/query/query_config_provider.dart'; -import 'package:leancode_cubit_utils/src/query/query_cubit.dart'; - -/// Signature for a function that creates a widget when data successfully loaded. -typedef QueryWidgetBuilder = Widget Function( - BuildContext context, - TOut data, -); - -/// Signature for a function that creates a widget when query is loading. -typedef QueryErrorBuilder = Widget Function( - BuildContext context, - QueryErrorState error, - VoidCallback? retry, -); - -/// A widget that builds itself based on the latest query state. -class QueryCubitBuilder extends StatelessWidget { - /// Creates a new [QueryCubitBuilder] with the given [queryCubit] and - /// [builder]. - const QueryCubitBuilder({ - super.key, - required this.queryCubit, - required this.builder, - this.onInitial, - this.onLoading, - this.onError, - this.onErrorCallback, - }); - - /// The query cubit to which this widget is listening. - final BaseQueryCubit queryCubit; - - /// The builder that creates a widget when data successfully loaded. - final QueryWidgetBuilder builder; - - /// The builder that creates a widget when state is initial. - final WidgetBuilder? onInitial; - - /// The builder that creates a widget when query is loading. - final WidgetBuilder? onLoading; - - /// The builder that creates a widget when query failed. - final QueryErrorBuilder? onError; - - /// Callback to be called on error widget; - final VoidCallback? onErrorCallback; - - @override - Widget build(BuildContext context) { - final config = context.read(); - - return BlocBuilder, QueryState>( - bloc: queryCubit, - builder: (context, state) { - return switch (state) { - QueryInitialState() => onInitial?.call(context) ?? - onLoading?.call(context) ?? - config.onLoading(context), - QueryLoadingState() => - onLoading?.call(context) ?? config.onLoading(context), - QuerySuccessState(:final data) => builder(context, data), - QueryRefreshState(:final data) => data != null - ? builder(context, data) - : onLoading?.call(context) ?? config.onLoading(context), - QueryErrorState() => onError?.call(context, state, onErrorCallback) ?? - config.onError(context, state, onErrorCallback), - }; - }, - ); - } -} diff --git a/lib/src/query/query_cubit_config.dart b/lib/src/query/query_cubit_config.dart deleted file mode 100644 index fdfab86..0000000 --- a/lib/src/query/query_cubit_config.dart +++ /dev/null @@ -1,11 +0,0 @@ -import 'package:leancode_cubit_utils/src/query/query_cubit.dart'; - -/// Configures the [QueryCubit]s. -class QueryCubitConfig { - /// The default request mode used by all [QueryCubit]s. - static RequestMode get requestMode => _requestMode ?? RequestMode.ignore; - - static set requestMode(RequestMode mode) => _requestMode = mode; - - static RequestMode? _requestMode; -} diff --git a/lib/src/query/use_args_query_cubit.dart b/lib/src/query/use_args_query_cubit.dart deleted file mode 100644 index ddb7805..0000000 --- a/lib/src/query/use_args_query_cubit.dart +++ /dev/null @@ -1,41 +0,0 @@ -import 'package:cqrs/cqrs.dart'; -import 'package:leancode_cubit_utils/src/query/query_cubit.dart'; -import 'package:leancode_hooks/leancode_hooks.dart'; - -/// Implementation of [ArgsQueryCubit] created in order to be used by [useArgsQueryCubit]. -class SimpleArgsQueryCubit - extends ArgsQueryCubit { - /// Creates a new [SimpleArgsQueryCubit]. - SimpleArgsQueryCubit( - super.loggerTag, - this._customRequest, { - super.requestMode, - }); - - /// The request to be executed. - final QueryArgsRequest _customRequest; - - @override - TOut map(TOut data) => data; - - @override - Future> request(TArgs args) => _customRequest(args); -} - -/// Creates a new [SimpleArgsQueryCubit] with the given [loggerTag], [query] and -/// [requestMode]. -SimpleArgsQueryCubit useArgsQueryCubit({ - String loggerTag = 'SimpleArgsQueryCubit', - required QueryArgsRequest query, - RequestMode? requestMode, - List keys = const [], -}) { - return useBloc( - () => SimpleArgsQueryCubit( - loggerTag, - query, - requestMode: requestMode, - ), - keys, - ); -} diff --git a/lib/src/query/use_query_cubit.dart b/lib/src/query/use_query_cubit.dart deleted file mode 100644 index 361cc31..0000000 --- a/lib/src/query/use_query_cubit.dart +++ /dev/null @@ -1,47 +0,0 @@ -import 'package:cqrs/cqrs.dart'; -import 'package:leancode_cubit_utils/src/query/query_cubit.dart'; -import 'package:leancode_hooks/leancode_hooks.dart'; - -/// Implementation of [QueryCubit] created in order to be used by [useQueryCubit]. -class SimpleQueryCubit extends QueryCubit { - /// Creates a new [SimpleQueryCubit]. - SimpleQueryCubit( - super.loggerTag, - this._customRequest, { - super.requestMode, - }); - - /// The request to be executed. - final QueryRequest _customRequest; - - @override - TOut map(TOut data) => data; - - @override - Future> request() => _customRequest(); -} - -/// Creates a new [SimpleQueryCubit] with the given [loggerTag], [query] and -/// [requestMode]. -QueryCubit useQueryCubit( - QueryRequest query, { - String loggerTag = 'SimpleQueryCubit', - RequestMode? requestMode, - bool callOnCreate = true, - List keys = const [], -}) { - return useBloc( - () { - final cubit = SimpleQueryCubit( - loggerTag, - query, - requestMode: requestMode, - ); - if (callOnCreate) { - cubit.run(); - } - return cubit; - }, - keys, - ); -} diff --git a/pubspec.yaml b/pubspec.yaml index 85fac9d..32032a1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: leancode_cubit_utils description: A collection of cubits and widgets that facilitate the creation of repetitive pages, eliminating boilerplate. -version: 0.0.3 +version: 0.0.4 repository: https://github.com/leancodepl/leancode_cubit_utils environment: