Transfer showLocalNotification, backgroundKeepAlive from LLC to Core

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-01-29 17:23:00 +05:30
parent 4b9a7d6837
commit 5310287fdd
7 changed files with 72 additions and 98 deletions
@@ -4,6 +4,8 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:stream_chat/stream_chat.dart';
typedef EventHandler = void Function(Event event);
/// Widget used to provide information about the chat to the widget tree
///
/// class MyApp extends StatelessWidget {
@@ -28,17 +30,26 @@ import 'package:stream_chat/stream_chat.dart';
class StreamChatCore extends StatefulWidget {
// ignore: public_member_api_docs
final Client client;
// ignore: public_member_api_docs
final Widget child;
/// The amount of time that will pass before disconnecting the client in the background
final Duration backgroundKeepAlive;
/// Handler called whenever the [client] receives a new [Event] while the app
/// is in background. Can be used to display various notifications depending
/// upon the [Event.type]
final EventHandler onBackgroundEventReceived;
// ignore: public_member_api_docs
StreamChatCore({
Key key,
@required this.client,
@required this.child,
}) : super(
key: key,
);
this.onBackgroundEventReceived,
this.backgroundKeepAlive = const Duration(minutes: 1),
}) : super(key: key);
@override
StreamChatCoreState createState() => StreamChatCoreState();
@@ -82,56 +93,28 @@ class StreamChatCoreState extends State<StreamChatCore>
WidgetsBinding.instance.addObserver(this);
}
StreamSubscription _newMessageSubscription;
StreamSubscription _eventSubscription;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (client.state?.user != null) {
if (state == AppLifecycleState.paused) {
if (client.showLocalNotification != null) {
_newMessageSubscription = client
.on(EventType.messageNew)
.where((e) => e.user?.id != user.id)
.where((e) => e.message.silent != true)
.where((e) => e.message.shadowed != true)
.listen((event) async {
final channel = client.channel(
event.channelType,
id: event.channelId,
);
client.showLocalNotification(
event.message,
ChannelModel(
id: channel.id,
createdAt: channel.createdAt,
extraData: channel.extraData,
type: channel.type,
memberCount: channel.memberCount,
frozen: channel.frozen,
cid: channel.cid,
deletedAt: channel.deletedAt,
config: channel.config,
createdBy: channel.createdBy,
updatedAt: channel.updatedAt,
lastMessageAt: channel.lastMessageAt,
),
);
});
_disconnectTimer = Timer(client.backgroundKeepAlive, () {
client.disconnect();
});
if (widget.onBackgroundEventReceived != null) {
_eventSubscription =
client.on().listen(widget.onBackgroundEventReceived);
_disconnectTimer = Timer(
widget.backgroundKeepAlive,
client.disconnect,
);
} else {
client.disconnect();
}
} else if (state == AppLifecycleState.resumed) {
_newMessageSubscription?.cancel();
_eventSubscription?.cancel();
if (_disconnectTimer?.isActive == true) {
_disconnectTimer.cancel();
} else {
if (client.wsConnectionStatus.value ==
ConnectionStatus.disconnected) {
NotificationService.handleIosMessageQueue(client);
if (client.wsConnectionStatus == ConnectionStatus.disconnected) {
client.connect();
}
}
@@ -142,6 +125,7 @@ class StreamChatCoreState extends State<StreamChatCore>
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_eventSubscription?.cancel();
_disconnectTimer?.cancel();
super.dispose();
}