Skip to content
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
32 changes: 13 additions & 19 deletions packages/graphql_flutter/lib/src/widgets/hooks/subscription.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ typedef OnSubscriptionResult<TParsed> = void Function(
GraphQLClient? client,
);

typedef SubscriptionBuilder<TParsed> = Widget Function(
QueryResult<TParsed> result);
typedef SubscriptionBuilder<TParsed> = Widget Function(QueryResult<TParsed> result);

QueryResult<TParsed> useSubscription<TParsed>(
SubscriptionOptions<TParsed> options, {
Expand Down Expand Up @@ -59,18 +58,16 @@ class _SubscriptionHook<TParsed> extends Hook<Stream<QueryResult<TParsed>>> {
required this.onSubscriptionResult,
});
@override
HookState<Stream<QueryResult<TParsed>>, Hook<Stream<QueryResult<TParsed>>>>
createState() {
HookState<Stream<QueryResult<TParsed>>, Hook<Stream<QueryResult<TParsed>>>> createState() {
return _SubscriptionHookState();
}
}

class _SubscriptionHookState<TParsed> extends HookState<
Stream<QueryResult<TParsed>>, _SubscriptionHook<TParsed>> {
class _SubscriptionHookState<TParsed> extends HookState<Stream<QueryResult<TParsed>>, _SubscriptionHook<TParsed>> {
late Stream<QueryResult<TParsed>> stream;

ConnectivityResult? _currentConnectivityResult;
StreamSubscription<ConnectivityResult>? _networkSubscription;
List<ConnectivityResult> _currentConnectivityResult = [ConnectivityResult.none];
StreamSubscription<List<ConnectivityResult>>? _networkSubscription;

void _initSubscription() {
final client = hook.client;
Expand All @@ -88,8 +85,7 @@ class _SubscriptionHookState<TParsed> extends HookState<
void initHook() {
super.initHook();
_initSubscription();
_networkSubscription =
Connectivity().onConnectivityChanged.listen(_onNetworkChange);
_networkSubscription = Connectivity().onConnectivityChanged.listen(_onNetworkChange);
}

@override
Expand All @@ -107,31 +103,29 @@ class _SubscriptionHookState<TParsed> extends HookState<
super.dispose();
}

Future<void> _onNetworkChange(ConnectivityResult result) async {
Future<void> _onNetworkChange(List<ConnectivityResult> results) async {
//if from offline to online
if (_currentConnectivityResult == ConnectivityResult.none &&
(result == ConnectivityResult.mobile ||
result == ConnectivityResult.wifi)) {
_currentConnectivityResult = result;
if (_currentConnectivityResult.contains(ConnectivityResult.none) &&
(results.contains(ConnectivityResult.mobile) || results.contains(ConnectivityResult.wifi))) {
_currentConnectivityResult = List.from(results, growable: false);

// android connectivitystate cannot be trusted
// validate with nslookup
if (Platform.isAndroid) {
try {
final nsLookupResult = await InternetAddress.lookup('google.com');
if (nsLookupResult.isNotEmpty &&
nsLookupResult[0].rawAddress.isNotEmpty) {
if (nsLookupResult.isNotEmpty && nsLookupResult[0].rawAddress.isNotEmpty) {
_initSubscription();
}
// on exception -> no real connection, set current state to none
} on SocketException catch (_) {
_currentConnectivityResult = ConnectivityResult.none;
_currentConnectivityResult = [ConnectivityResult.none];
}
} else {
_initSubscription();
}
} else {
_currentConnectivityResult = result;
_currentConnectivityResult = List.from(results, growable: false);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/graphql_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies:
meta: ^1.7.0
path_provider: ^2.0.1
path: ^1.8.0
connectivity_plus: ^5.0.0
connectivity_plus: ^6.0.1
hive: ^2.0.0
plugin_platform_interface: ^2.0.0
flutter_hooks: '>=0.18.2 <0.21.0'
Expand Down