feat: resolve and resolveWith helper support
This commit is contained in:
@@ -1,4 +1,15 @@
|
||||
abstract class ConnectivityAdapter {
|
||||
bool _isConnectedSync;
|
||||
|
||||
ConnectivityAdapter() : _isConnectedSync = true {
|
||||
isConnected.then((c) => _isConnectedSync = c);
|
||||
onConnectivityChanged.listen((isConnected) {
|
||||
_isConnectedSync = isConnected;
|
||||
});
|
||||
}
|
||||
|
||||
bool get isConnectedSync => _isConnectedSync;
|
||||
|
||||
Future<bool> get isConnected;
|
||||
|
||||
Stream<bool> get onConnectivityChanged;
|
||||
|
||||
@@ -2,10 +2,11 @@ import 'dart:async';
|
||||
|
||||
import 'package:async/async.dart';
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:fl_query/fl_query.dart';
|
||||
import 'package:fl_query/src/core/client.dart';
|
||||
import 'package:fl_query/src/core/mixins/retryer.dart';
|
||||
import 'package:fl_query/src/core/mixins/validation.dart';
|
||||
import 'package:flutter/material.dart' hide Listener;
|
||||
import 'package:fl_query/src/widgets/state_resolvers/infinite_query_state.dart';
|
||||
import 'package:flutter/widgets.dart' hide Listener;
|
||||
import 'package:hive_flutter/adapters.dart';
|
||||
import 'package:mutex/mutex.dart';
|
||||
import 'package:state_notifier/state_notifier.dart';
|
||||
@@ -251,7 +252,11 @@ class InfiniteQuery<DataType, ErrorType, PageType>
|
||||
|
||||
bool get hasNextPage => getNextPage != null;
|
||||
|
||||
Future<void> _operate(PageType page) {
|
||||
Future<void> _operate(PageType page) async {
|
||||
if (!await QueryClient.connectivity.isConnected &&
|
||||
retryConfig.cancelWhenOffline) {
|
||||
return;
|
||||
}
|
||||
return _mutex.protect(() async {
|
||||
state = state.copyWith();
|
||||
_operation = cancellableRetryOperation(
|
||||
@@ -409,6 +414,52 @@ class InfiniteQuery<DataType, ErrorType, PageType>
|
||||
);
|
||||
}
|
||||
|
||||
Widget resolve(
|
||||
Widget Function(List<DataType> data) data, {
|
||||
required Widget Function(List<ErrorType> errors) error,
|
||||
required Widget Function() loading,
|
||||
Widget Function()? offline,
|
||||
}) {
|
||||
if (hasPages) {
|
||||
return data(this.pages);
|
||||
} else if (!QueryClient.connectivity.isConnectedSync) {
|
||||
return offline != null ? offline() : loading();
|
||||
} else if (hasErrors) {
|
||||
return error(this.errors);
|
||||
} else {
|
||||
return loading();
|
||||
}
|
||||
}
|
||||
|
||||
Widget resolveWith(
|
||||
BuildContext context,
|
||||
Widget Function(List<DataType> data) data, {
|
||||
required Widget Function(List<ErrorType> error)? error,
|
||||
required Widget Function()? loading,
|
||||
Widget Function()? offline,
|
||||
}) {
|
||||
final resolvents = InfiniteQueryStateResolverProvider.of(context);
|
||||
|
||||
assert(
|
||||
resolvents.error != null || error != null,
|
||||
'You must provide an error widget or an error resolver using `InfiniteQueryStateResolverProvider`',
|
||||
);
|
||||
|
||||
assert(
|
||||
resolvents.loading != null || loading != null,
|
||||
'You must provide a loading widget or a loading resolver using `InfiniteQueryStateResolverProvider`',
|
||||
);
|
||||
|
||||
return resolve(
|
||||
data,
|
||||
error: resolvents.error != null
|
||||
? (e) => resolvents.error!(e.cast<dynamic>())
|
||||
: error!,
|
||||
loading: (resolvents.loading ?? loading)!,
|
||||
offline: resolvents.offline ?? offline,
|
||||
);
|
||||
}
|
||||
|
||||
/// Reset all data, pages, error, events of this query
|
||||
///
|
||||
/// This will also remove every data of this [InfiniteQuery] from
|
||||
|
||||
@@ -4,6 +4,8 @@ import 'package:async/async.dart';
|
||||
import 'package:fl_query/src/collections/retry_config.dart';
|
||||
import 'package:fl_query/src/core/client.dart';
|
||||
import 'package:fl_query/src/core/mixins/retryer.dart';
|
||||
import 'package:fl_query/src/widgets/state_resolvers/mutation_state.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:mutex/mutex.dart';
|
||||
import 'package:state_notifier/state_notifier.dart';
|
||||
|
||||
@@ -88,7 +90,11 @@ class Mutation<DataType, ErrorType, VariablesType>
|
||||
Stream<ErrorType> get errorStream => _errorController.stream;
|
||||
Stream<VariablesType> get mutationStream => _mutationController.stream;
|
||||
|
||||
Future<void> _operate(VariablesType variables) {
|
||||
Future<void> _operate(VariablesType variables) async {
|
||||
if (!await QueryClient.connectivity.isConnected &&
|
||||
retryConfig.cancelWhenOffline) {
|
||||
return;
|
||||
}
|
||||
return _mutex.protect(() async {
|
||||
state = state.copyWith();
|
||||
_operation = cancellableRetryOperation(
|
||||
@@ -128,6 +134,57 @@ class Mutation<DataType, ErrorType, VariablesType>
|
||||
_mutationFn = mutationFn;
|
||||
}
|
||||
|
||||
Widget resolve(
|
||||
Widget Function(DataType data) data, {
|
||||
required Widget Function(ErrorType error) error,
|
||||
required Widget Function() loading,
|
||||
Widget Function()? mutating,
|
||||
Widget Function()? offline,
|
||||
}) {
|
||||
if (hasData) {
|
||||
return data(this.data!);
|
||||
} else if (!QueryClient.connectivity.isConnectedSync) {
|
||||
return offline != null ? offline() : loading();
|
||||
} else if (isMutating) {
|
||||
return mutating != null ? mutating() : loading();
|
||||
} else if (hasError) {
|
||||
return error(this.error!);
|
||||
} else {
|
||||
return loading();
|
||||
}
|
||||
}
|
||||
|
||||
Widget resolveWith(
|
||||
BuildContext context,
|
||||
Widget Function(DataType data) data, {
|
||||
required Widget Function(ErrorType error)? error,
|
||||
required Widget Function()? loading,
|
||||
Widget Function()? offline,
|
||||
Widget Function()? mutating,
|
||||
}) {
|
||||
final resolvents = MutationStateResolverProvider.of(context);
|
||||
|
||||
assert(
|
||||
resolvents.error != null || error != null,
|
||||
'You must provide an error widget or an error resolver using `MutationStateResolverProvider`',
|
||||
);
|
||||
|
||||
assert(
|
||||
resolvents.loading != null || loading != null,
|
||||
'You must provide a loading widget or a loading resolver using `MutationStateResolverProvider`',
|
||||
);
|
||||
|
||||
return resolve(
|
||||
data,
|
||||
error: resolvents.error != null
|
||||
? (e) => resolvents.error!(e as dynamic)
|
||||
: error!,
|
||||
loading: (resolvents.loading ?? loading)!,
|
||||
offline: resolvents.offline ?? offline,
|
||||
mutating: resolvents.mutating ?? mutating,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> reset() async {
|
||||
await _operation?.cancel();
|
||||
state = MutationState<DataType, ErrorType, VariablesType>();
|
||||
|
||||
@@ -6,6 +6,8 @@ import 'package:fl_query/src/collections/retry_config.dart';
|
||||
import 'package:fl_query/src/core/client.dart';
|
||||
import 'package:fl_query/src/core/mixins/retryer.dart';
|
||||
import 'package:fl_query/src/core/mixins/validation.dart';
|
||||
import 'package:fl_query/src/widgets/state_resolvers/query_state.dart';
|
||||
import 'package:flutter/widgets.dart' hide Listener;
|
||||
import 'package:hive_flutter/adapters.dart';
|
||||
import 'package:mutex/mutex.dart';
|
||||
import 'package:state_notifier/state_notifier.dart';
|
||||
@@ -137,7 +139,11 @@ class Query<DataType, ErrorType>
|
||||
|
||||
CancelableOperation<void>? _operation;
|
||||
|
||||
Future<void> _operate() {
|
||||
Future<void> _operate() async {
|
||||
if (!await QueryClient.connectivity.isConnected &&
|
||||
retryConfig.cancelWhenOffline) {
|
||||
return;
|
||||
}
|
||||
return _mutex.protect(() async {
|
||||
state = state.copyWith();
|
||||
_operation = cancellableRetryOperation(
|
||||
@@ -196,6 +202,52 @@ class Query<DataType, ErrorType>
|
||||
_box.delete(key);
|
||||
}
|
||||
|
||||
Widget resolve(
|
||||
Widget Function(DataType data) data, {
|
||||
required Widget Function(ErrorType data) error,
|
||||
required Widget Function() loading,
|
||||
Widget Function()? offline,
|
||||
}) {
|
||||
if (hasData) {
|
||||
return data(this.data!);
|
||||
} else if (!QueryClient.connectivity.isConnectedSync) {
|
||||
return offline != null ? offline() : loading();
|
||||
} else if (hasError) {
|
||||
return error(this.error!);
|
||||
} else {
|
||||
return loading();
|
||||
}
|
||||
}
|
||||
|
||||
Widget resolveWith(
|
||||
BuildContext context,
|
||||
Widget Function(DataType data) data, {
|
||||
required Widget Function(ErrorType error)? error,
|
||||
required Widget Function()? loading,
|
||||
Widget Function()? offline,
|
||||
}) {
|
||||
final resolvents = QueryStateResolverProvider.of(context);
|
||||
|
||||
assert(
|
||||
resolvents.error != null || error != null,
|
||||
'You must provide an error widget or an error resolver using `QueryStateResolverProvider`',
|
||||
);
|
||||
|
||||
assert(
|
||||
resolvents.loading != null || loading != null,
|
||||
'You must provide a loading widget or a loading resolver using `QueryStateResolverProvider`',
|
||||
);
|
||||
|
||||
return resolve(
|
||||
data,
|
||||
error: resolvents.error != null
|
||||
? (e) => resolvents.error!(e as dynamic)
|
||||
: error!,
|
||||
loading: (resolvents.loading ?? loading)!,
|
||||
offline: resolvents.offline ?? offline,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
RemoveListener addListener(Listener<QueryState<DataType, ErrorType>> listener,
|
||||
{bool fireImmediately = true}) {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
class InfiniteQueryStateResolverProvider extends InheritedWidget {
|
||||
final Widget Function(List errors)? error;
|
||||
final Widget Function()? loading;
|
||||
final Widget Function()? offline;
|
||||
|
||||
InfiniteQueryStateResolverProvider({
|
||||
this.error,
|
||||
this.loading,
|
||||
this.offline,
|
||||
required super.child,
|
||||
}) : assert(
|
||||
error != null || loading != null || offline != null,
|
||||
'Why are you using `InfiniteQueryStateResolverProvider` with no resolver?\n'
|
||||
'You should provide at least one resolver.',
|
||||
);
|
||||
|
||||
static InfiniteQueryStateResolverProvider? maybeOf(BuildContext context) {
|
||||
return context.dependOnInheritedWidgetOfExactType<
|
||||
InfiniteQueryStateResolverProvider>();
|
||||
}
|
||||
|
||||
static InfiniteQueryStateResolverProvider of(BuildContext context) {
|
||||
final provider = maybeOf(context);
|
||||
assert(
|
||||
provider != null,
|
||||
'You are trying to access a `InfiniteQueryStateResolverProvider` outside of its scope.\n'
|
||||
'You should wrap your widget tree with `InfiniteQueryStateResolverProvider`.',
|
||||
);
|
||||
return provider!;
|
||||
}
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(InfiniteQueryStateResolverProvider oldWidget) {
|
||||
return error != oldWidget.error ||
|
||||
loading != oldWidget.loading ||
|
||||
offline != oldWidget.offline;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
class MutationStateResolverProvider extends InheritedWidget {
|
||||
final Widget Function(dynamic error)? error;
|
||||
final Widget Function()? loading;
|
||||
final Widget Function()? offline;
|
||||
final Widget Function()? mutating;
|
||||
|
||||
MutationStateResolverProvider({
|
||||
this.error,
|
||||
this.loading,
|
||||
this.offline,
|
||||
this.mutating,
|
||||
required super.child,
|
||||
}) : assert(
|
||||
error != null || loading != null || offline != null,
|
||||
'Why are you using `MutationStateResolverProvider` with no resolver?\n'
|
||||
'You should provide at least one resolver.',
|
||||
);
|
||||
|
||||
static MutationStateResolverProvider? maybeOf(BuildContext context) {
|
||||
return context
|
||||
.dependOnInheritedWidgetOfExactType<MutationStateResolverProvider>();
|
||||
}
|
||||
|
||||
static MutationStateResolverProvider of(BuildContext context) {
|
||||
final provider = maybeOf(context);
|
||||
assert(
|
||||
provider != null,
|
||||
'You are trying to access a `MutationStateResolverProvider` outside of its scope.\n'
|
||||
'You should wrap your widget tree with `MutationStateResolverProvider`.',
|
||||
);
|
||||
return provider!;
|
||||
}
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(MutationStateResolverProvider oldWidget) {
|
||||
return error != oldWidget.error ||
|
||||
loading != oldWidget.loading ||
|
||||
offline != oldWidget.offline ||
|
||||
mutating != oldWidget.mutating;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
class QueryStateResolverProvider extends InheritedWidget {
|
||||
final Widget Function(dynamic error)? error;
|
||||
final Widget Function()? loading;
|
||||
final Widget Function()? offline;
|
||||
|
||||
QueryStateResolverProvider({
|
||||
this.error,
|
||||
this.loading,
|
||||
this.offline,
|
||||
required super.child,
|
||||
}) : assert(
|
||||
error != null || loading != null || offline != null,
|
||||
'Why are you using `QueryStateResolverProvider` with no resolver?\n'
|
||||
'You should provide at least one resolver.',
|
||||
);
|
||||
|
||||
static QueryStateResolverProvider? maybeOf(BuildContext context) {
|
||||
return context
|
||||
.dependOnInheritedWidgetOfExactType<QueryStateResolverProvider>();
|
||||
}
|
||||
|
||||
static QueryStateResolverProvider of(BuildContext context) {
|
||||
final provider = maybeOf(context);
|
||||
assert(
|
||||
provider != null,
|
||||
'You are trying to access a `QueryStateResolverProvider` outside of its scope.\n'
|
||||
'You should wrap your widget tree with `QueryStateResolverProvider`.',
|
||||
);
|
||||
return provider!;
|
||||
}
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(QueryStateResolverProvider oldWidget) {
|
||||
return error != oldWidget.error ||
|
||||
loading != oldWidget.loading ||
|
||||
offline != oldWidget.offline;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user