implement connectivity plus

This commit is contained in:
Salvatore Giordano
2021-05-27 11:04:30 +02:00
parent b954cc2872
commit 4f5958a067
5 changed files with 236 additions and 26 deletions
@@ -1,5 +1,6 @@
import 'dart:async';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:stream_chat/stream_chat.dart';
@@ -43,6 +44,7 @@ class StreamChatCore extends StatefulWidget {
required this.child,
this.onBackgroundEventReceived,
this.backgroundKeepAlive = const Duration(minutes: 1),
this.connectivityStream,
}) : super(key: key);
/// Instance of Stream Chat Client containing information about the current
@@ -61,6 +63,11 @@ class StreamChatCore extends StatefulWidget {
/// upon the [Event.type]
final EventHandler? onBackgroundEventReceived;
/// Stream of connectivity result
/// Visible for testing
@visibleForTesting
final Stream<ConnectivityResult>? connectivityStream;
@override
StreamChatCoreState createState() => StreamChatCoreState();
@@ -96,50 +103,84 @@ class StreamChatCoreState extends State<StreamChatCore>
/// The current user as a stream
Stream<User?> get userStream => client.state.userStream;
late final StreamSubscription<ConnectivityResult> _connectivitySubscription;
var _isInForeground = true;
var _isConnectionAvailable = true;
@override
void initState() {
super.initState();
WidgetsBinding.instance?.addObserver(this);
_connectivitySubscription =
(widget.connectivityStream ?? Connectivity().onConnectivityChanged)
.listen((ConnectivityResult result) async {
_isConnectionAvailable = result != ConnectivityResult.none;
if (!_isInForeground) {
return;
}
if (_isConnectionAvailable) {
if (client.wsConnectionStatus == ConnectionStatus.disconnected) {
await client.connect();
}
} else {
if (client.wsConnectionStatus == ConnectionStatus.connected) {
await client.disconnect();
}
}
});
}
StreamSubscription? _eventSubscription;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
_isInForeground = state == AppLifecycleState.resumed;
if (user != null) {
if (state == AppLifecycleState.paused) {
if (widget.onBackgroundEventReceived == null) {
client.disconnect();
return;
}
_eventSubscription = client.on().listen(
widget.onBackgroundEventReceived,
);
void onTimerComplete() {
_eventSubscription?.cancel();
client.disconnect();
}
_disconnectTimer = Timer(widget.backgroundKeepAlive, onTimerComplete);
} else if (state == AppLifecycleState.resumed) {
if (_disconnectTimer?.isActive == true) {
_eventSubscription?.cancel();
_disconnectTimer?.cancel();
} else {
if (client.wsConnectionStatus == ConnectionStatus.disconnected) {
client.connect();
}
}
if (!_isInForeground) {
_onBackground();
} else {
_onForeground();
}
}
}
void _onForeground() {
if (_disconnectTimer?.isActive == true) {
_eventSubscription?.cancel();
_disconnectTimer?.cancel();
} else if (client.wsConnectionStatus == ConnectionStatus.disconnected &&
_isConnectionAvailable) {
client.connect();
}
}
void _onBackground() {
if (widget.onBackgroundEventReceived == null) {
if (client.wsConnectionStatus != ConnectionStatus.disconnected) {
client.disconnect();
}
return;
}
_eventSubscription = client.on().listen(
widget.onBackgroundEventReceived,
);
void onTimerComplete() {
_eventSubscription?.cancel();
client.disconnect();
}
_disconnectTimer = Timer(widget.backgroundKeepAlive, onTimerComplete);
return;
}
@override
void dispose() {
WidgetsBinding.instance?.removeObserver(this);
_eventSubscription?.cancel();
_disconnectTimer?.cancel();
_connectivitySubscription.cancel();
super.dispose();
}
}