re-subscribe connectivityStream if widget.connectivityStream changes
Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
@@ -103,7 +103,7 @@ class StreamChatCoreState extends State<StreamChatCore>
|
|||||||
/// The current user as a stream
|
/// The current user as a stream
|
||||||
Stream<User?> get userStream => client.state.userStream;
|
Stream<User?> get userStream => client.state.userStream;
|
||||||
|
|
||||||
late final StreamSubscription<ConnectivityResult> _connectivitySubscription;
|
StreamSubscription<ConnectivityResult>? _connectivitySubscription;
|
||||||
|
|
||||||
var _isInForeground = true;
|
var _isInForeground = true;
|
||||||
var _isConnectionAvailable = true;
|
var _isConnectionAvailable = true;
|
||||||
@@ -112,23 +112,46 @@ class StreamChatCoreState extends State<StreamChatCore>
|
|||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
WidgetsBinding.instance?.addObserver(this);
|
WidgetsBinding.instance?.addObserver(this);
|
||||||
_connectivitySubscription =
|
_subscribeToConnectivityChange(widget.connectivityStream);
|
||||||
(widget.connectivityStream ?? Connectivity().onConnectivityChanged)
|
}
|
||||||
.listen((ConnectivityResult result) async {
|
|
||||||
_isConnectionAvailable = result != ConnectivityResult.none;
|
void _subscribeToConnectivityChange([
|
||||||
if (!_isInForeground) {
|
Stream<ConnectivityResult>? connectivityStream,
|
||||||
return;
|
]) {
|
||||||
}
|
if (_connectivitySubscription == null) {
|
||||||
if (_isConnectionAvailable) {
|
connectivityStream ??= Connectivity().onConnectivityChanged;
|
||||||
if (client.wsConnectionStatus == ConnectionStatus.disconnected) {
|
_connectivitySubscription =
|
||||||
await client.connect();
|
connectivityStream.distinct().listen((result) {
|
||||||
|
_isConnectionAvailable = result != ConnectivityResult.none;
|
||||||
|
if (!_isInForeground) return;
|
||||||
|
if (_isConnectionAvailable) {
|
||||||
|
if (client.wsConnectionStatus == ConnectionStatus.disconnected) {
|
||||||
|
client.connect();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (client.wsConnectionStatus == ConnectionStatus.connected) {
|
||||||
|
client.disconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
});
|
||||||
if (client.wsConnectionStatus == ConnectionStatus.connected) {
|
}
|
||||||
await client.disconnect();
|
}
|
||||||
}
|
|
||||||
}
|
void _unsubscribeFromConnectivityChange() {
|
||||||
});
|
if (_connectivitySubscription != null) {
|
||||||
|
_connectivitySubscription?.cancel();
|
||||||
|
_connectivitySubscription = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void didUpdateWidget(StreamChatCore oldWidget) {
|
||||||
|
super.didUpdateWidget(oldWidget);
|
||||||
|
final connectivityStream = widget.connectivityStream;
|
||||||
|
if (connectivityStream != oldWidget.connectivityStream) {
|
||||||
|
_unsubscribeFromConnectivityChange();
|
||||||
|
_subscribeToConnectivityChange(connectivityStream);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
StreamSubscription? _eventSubscription;
|
StreamSubscription? _eventSubscription;
|
||||||
@@ -137,10 +160,10 @@ class StreamChatCoreState extends State<StreamChatCore>
|
|||||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||||
_isInForeground = state == AppLifecycleState.resumed;
|
_isInForeground = state == AppLifecycleState.resumed;
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
if (!_isInForeground) {
|
if (_isInForeground) {
|
||||||
_onBackground();
|
|
||||||
} else {
|
|
||||||
_onForeground();
|
_onForeground();
|
||||||
|
} else {
|
||||||
|
_onBackground();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -162,9 +185,8 @@ class StreamChatCoreState extends State<StreamChatCore>
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_eventSubscription = client.on().listen(
|
|
||||||
widget.onBackgroundEventReceived,
|
_eventSubscription = client.on().listen(widget.onBackgroundEventReceived);
|
||||||
);
|
|
||||||
|
|
||||||
void onTimerComplete() {
|
void onTimerComplete() {
|
||||||
_eventSubscription?.cancel();
|
_eventSubscription?.cancel();
|
||||||
@@ -178,9 +200,9 @@ class StreamChatCoreState extends State<StreamChatCore>
|
|||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
WidgetsBinding.instance?.removeObserver(this);
|
WidgetsBinding.instance?.removeObserver(this);
|
||||||
|
_unsubscribeFromConnectivityChange();
|
||||||
_eventSubscription?.cancel();
|
_eventSubscription?.cancel();
|
||||||
_disconnectTimer?.cancel();
|
_disconnectTimer?.cancel();
|
||||||
_connectivitySubscription.cancel();
|
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user