@@ -4,7 +4,6 @@ import 'package:stream_chat_persistence/stream_chat_persistence.dart';
|
|||||||
|
|
||||||
final chatPersistentClient = StreamChatPersistenceClient(
|
final chatPersistentClient = StreamChatPersistenceClient(
|
||||||
logLevel: Level.INFO,
|
logLevel: Level.INFO,
|
||||||
connectionMode: ConnectionMode.background,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
|
|||||||
@@ -12,6 +12,9 @@ import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart';
|
|||||||
/// Callback called when tapping on a channel
|
/// Callback called when tapping on a channel
|
||||||
typedef ChannelTapCallback = void Function(Channel, Widget?);
|
typedef ChannelTapCallback = void Function(Channel, Widget?);
|
||||||
|
|
||||||
|
/// Callback called when tapping on a channel
|
||||||
|
typedef ChannelInfoCallback = void Function(Channel);
|
||||||
|
|
||||||
/// Builder used to create a custom [ChannelPreview] from a [Channel]
|
/// Builder used to create a custom [ChannelPreview] from a [Channel]
|
||||||
typedef ChannelPreviewBuilder = Widget Function(BuildContext, Channel);
|
typedef ChannelPreviewBuilder = Widget Function(BuildContext, Channel);
|
||||||
|
|
||||||
@@ -84,6 +87,9 @@ class ChannelListView extends StatefulWidget {
|
|||||||
this.emptyBuilder,
|
this.emptyBuilder,
|
||||||
this.loadingBuilder,
|
this.loadingBuilder,
|
||||||
this.listBuilder,
|
this.listBuilder,
|
||||||
|
this.onMoreDetailsPressed,
|
||||||
|
this.onDeletePressed,
|
||||||
|
this.swipeActions,
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
|
|
||||||
/// If true a default swipe to action behaviour will be added to this widget
|
/// If true a default swipe to action behaviour will be added to this widget
|
||||||
@@ -169,6 +175,15 @@ class ChannelListView extends StatefulWidget {
|
|||||||
/// The builder used when the channel list is empty.
|
/// The builder used when the channel list is empty.
|
||||||
final WidgetBuilder? emptyBuilder;
|
final WidgetBuilder? emptyBuilder;
|
||||||
|
|
||||||
|
/// Callback used when the more details slidable option is pressed
|
||||||
|
final ChannelInfoCallback? onMoreDetailsPressed;
|
||||||
|
|
||||||
|
/// Callback used when the delete slidable option is pressed
|
||||||
|
final ChannelInfoCallback? onDeletePressed;
|
||||||
|
|
||||||
|
/// List of actions for slidable
|
||||||
|
final List<SwipeAction>? swipeActions;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_ChannelListViewState createState() => _ChannelListViewState();
|
_ChannelListViewState createState() => _ChannelListViewState();
|
||||||
}
|
}
|
||||||
@@ -477,61 +492,79 @@ class _ChannelListViewState extends State<ChannelListView> {
|
|||||||
enabled: widget.swipeToAction,
|
enabled: widget.swipeToAction,
|
||||||
actionPane: const SlidableBehindActionPane(),
|
actionPane: const SlidableBehindActionPane(),
|
||||||
actionExtentRatio: 0.12,
|
actionExtentRatio: 0.12,
|
||||||
secondaryActions: <Widget>[
|
secondaryActions: widget.swipeActions
|
||||||
IconSlideAction(
|
?.map((e) => IconSlideAction(
|
||||||
color: backgroundColor,
|
color: e.color,
|
||||||
icon: Icons.more_horiz,
|
iconWidget: e.iconWidget,
|
||||||
onTap: () {
|
onTap: () {
|
||||||
showModalBottomSheet(
|
e.onTap?.call(channel);
|
||||||
clipBehavior: Clip.hardEdge,
|
},
|
||||||
shape: const RoundedRectangleBorder(
|
))
|
||||||
borderRadius: BorderRadius.only(
|
.toList() ??
|
||||||
topLeft: Radius.circular(32),
|
<Widget>[
|
||||||
topRight: Radius.circular(32),
|
IconSlideAction(
|
||||||
),
|
color: backgroundColor,
|
||||||
),
|
icon: Icons.more_horiz,
|
||||||
context: context,
|
onTap: widget.onMoreDetailsPressed != null
|
||||||
builder: (context) => StreamChannel(
|
? () {
|
||||||
channel: channel,
|
widget.onMoreDetailsPressed!(channel);
|
||||||
child: ChannelBottomSheet(
|
}
|
||||||
onViewInfoTap: () {
|
: () {
|
||||||
widget.onViewInfoTap?.call(channel);
|
showModalBottomSheet(
|
||||||
},
|
clipBehavior: Clip.hardEdge,
|
||||||
),
|
shape: const RoundedRectangleBorder(
|
||||||
),
|
borderRadius: BorderRadius.only(
|
||||||
);
|
topLeft: Radius.circular(32),
|
||||||
},
|
topRight: Radius.circular(32),
|
||||||
),
|
),
|
||||||
if ([
|
),
|
||||||
'admin',
|
context: context,
|
||||||
'owner',
|
builder: (context) => StreamChannel(
|
||||||
].contains(channel.state!.members
|
channel: channel,
|
||||||
.firstWhereOrNull(
|
child: ChannelBottomSheet(
|
||||||
(m) => m.userId == channel.client.state.user?.id)
|
onViewInfoTap: () {
|
||||||
?.role))
|
widget.onViewInfoTap?.call(channel);
|
||||||
IconSlideAction(
|
},
|
||||||
color: backgroundColor,
|
),
|
||||||
iconWidget: StreamSvgIcon.delete(
|
),
|
||||||
color: chatThemeData.colorTheme.accentRed,
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
onTap: () async {
|
if ([
|
||||||
final res = await showConfirmationDialog(
|
'admin',
|
||||||
context,
|
'owner',
|
||||||
title: 'Delete Conversation',
|
].contains(channel.state!.members
|
||||||
okText: 'DELETE',
|
.firstWhereOrNull(
|
||||||
question:
|
(m) => m.userId == channel.client.state.user?.id)
|
||||||
'Are you sure you want to delete this conversation?',
|
?.role))
|
||||||
cancelText: 'CANCEL',
|
IconSlideAction(
|
||||||
icon: StreamSvgIcon.delete(
|
color: backgroundColor,
|
||||||
|
iconWidget: StreamSvgIcon.delete(
|
||||||
color: chatThemeData.colorTheme.accentRed,
|
color: chatThemeData.colorTheme.accentRed,
|
||||||
),
|
),
|
||||||
);
|
onTap: widget.onDeletePressed != null
|
||||||
if (res == true) {
|
? () {
|
||||||
await channel.delete();
|
widget.onDeletePressed!(channel);
|
||||||
}
|
}
|
||||||
},
|
: () async {
|
||||||
),
|
final res = await showConfirmationDialog(
|
||||||
],
|
context,
|
||||||
|
title: 'Delete Conversation',
|
||||||
|
okText: 'DELETE',
|
||||||
|
question:
|
||||||
|
// ignore: lines_longer_than_80_chars
|
||||||
|
'Are you sure you want to delete this conversation?',
|
||||||
|
cancelText: 'CANCEL',
|
||||||
|
icon: StreamSvgIcon.delete(
|
||||||
|
color: chatThemeData.colorTheme.accentRed,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (res == true) {
|
||||||
|
await channel.delete();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
child: Container(
|
child: Container(
|
||||||
color: chatThemeData.colorTheme.whiteSnow,
|
color: chatThemeData.colorTheme.whiteSnow,
|
||||||
child: widget.channelPreviewBuilder?.call(context, channel) ??
|
child: widget.channelPreviewBuilder?.call(context, channel) ??
|
||||||
@@ -652,3 +685,22 @@ class _ChannelListViewState extends State<ChannelListView> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Class for slidable action
|
||||||
|
class SwipeAction {
|
||||||
|
/// Constructor for creating [SwipeAction]
|
||||||
|
SwipeAction({
|
||||||
|
this.color,
|
||||||
|
required this.iconWidget,
|
||||||
|
this.onTap,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Background color of action
|
||||||
|
Color? color;
|
||||||
|
|
||||||
|
/// Widget to display as icon
|
||||||
|
Widget iconWidget;
|
||||||
|
|
||||||
|
/// Callback when icon is tapped
|
||||||
|
ChannelInfoCallback? onTap;
|
||||||
|
}
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ class StreamChat extends StatefulWidget {
|
|||||||
this.streamChatThemeData,
|
this.streamChatThemeData,
|
||||||
this.onBackgroundEventReceived,
|
this.onBackgroundEventReceived,
|
||||||
this.backgroundKeepAlive = const Duration(minutes: 1),
|
this.backgroundKeepAlive = const Duration(minutes: 1),
|
||||||
|
this.connectivityStream,
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
|
|
||||||
/// Client to do chat ops with
|
/// Client to do chat ops with
|
||||||
@@ -59,6 +60,11 @@ class StreamChat extends StatefulWidget {
|
|||||||
/// upon the [Event.type]
|
/// upon the [Event.type]
|
||||||
final EventHandler? onBackgroundEventReceived;
|
final EventHandler? onBackgroundEventReceived;
|
||||||
|
|
||||||
|
/// Stream of connectivity result
|
||||||
|
/// Visible for testing
|
||||||
|
@visibleForTesting
|
||||||
|
final Stream<ConnectivityResult>? connectivityStream;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
StreamChatState createState() => StreamChatState();
|
StreamChatState createState() => StreamChatState();
|
||||||
|
|
||||||
@@ -102,6 +108,7 @@ class StreamChatState extends State<StreamChat> {
|
|||||||
client: client,
|
client: client,
|
||||||
onBackgroundEventReceived: widget.onBackgroundEventReceived,
|
onBackgroundEventReceived: widget.onBackgroundEventReceived,
|
||||||
backgroundKeepAlive: widget.backgroundKeepAlive,
|
backgroundKeepAlive: widget.backgroundKeepAlive,
|
||||||
|
connectivityStream: widget.connectivityStream,
|
||||||
child: widget.child ?? const Offstage(),
|
child: widget.child ?? const Offstage(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ void main() {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
connectivityStream: Stream.value(ConnectivityResult.mobile),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
surfaceSize: const Size.square(200),
|
surfaceSize: const Size.square(200),
|
||||||
@@ -131,6 +132,7 @@ void main() {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
connectivityStream: Stream.value(ConnectivityResult.mobile),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
surfaceSize: const Size.square(200),
|
surfaceSize: const Size.square(200),
|
||||||
@@ -187,6 +189,7 @@ void main() {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
connectivityStream: Stream.value(ConnectivityResult.mobile),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
surfaceSize: const Size.square(200),
|
surfaceSize: const Size.square(200),
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ void main() {
|
|||||||
maskColor: theme.ownMessageTheme.reactionsMaskColor!,
|
maskColor: theme.ownMessageTheme.reactionsMaskColor!,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
connectivityStream: Stream.value(ConnectivityResult.mobile),
|
||||||
),
|
),
|
||||||
surfaceSize: const Size(100, 100),
|
surfaceSize: const Size(100, 100),
|
||||||
);
|
);
|
||||||
@@ -96,6 +97,7 @@ void main() {
|
|||||||
maskColor: theme.ownMessageTheme.reactionsMaskColor!,
|
maskColor: theme.ownMessageTheme.reactionsMaskColor!,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
connectivityStream: Stream.value(ConnectivityResult.mobile),
|
||||||
),
|
),
|
||||||
surfaceSize: const Size(100, 100),
|
surfaceSize: const Size(100, 100),
|
||||||
);
|
);
|
||||||
@@ -118,6 +120,7 @@ void main() {
|
|||||||
StreamChat(
|
StreamChat(
|
||||||
client: client,
|
client: client,
|
||||||
streamChatThemeData: StreamChatThemeData.fromTheme(themeData),
|
streamChatThemeData: StreamChatThemeData.fromTheme(themeData),
|
||||||
|
connectivityStream: Stream.value(ConnectivityResult.mobile),
|
||||||
child: Container(
|
child: Container(
|
||||||
color: Colors.black,
|
color: Colors.black,
|
||||||
child: ReactionBubble(
|
child: ReactionBubble(
|
||||||
@@ -162,6 +165,7 @@ void main() {
|
|||||||
StreamChat(
|
StreamChat(
|
||||||
client: client,
|
client: client,
|
||||||
streamChatThemeData: StreamChatThemeData.fromTheme(themeData),
|
streamChatThemeData: StreamChatThemeData.fromTheme(themeData),
|
||||||
|
connectivityStream: Stream.value(ConnectivityResult.mobile),
|
||||||
child: Container(
|
child: Container(
|
||||||
color: Colors.black,
|
color: Colors.black,
|
||||||
child: ReactionBubble(
|
child: ReactionBubble(
|
||||||
@@ -204,6 +208,7 @@ void main() {
|
|||||||
await tester.pumpWidgetBuilder(
|
await tester.pumpWidgetBuilder(
|
||||||
StreamChat(
|
StreamChat(
|
||||||
client: client,
|
client: client,
|
||||||
|
connectivityStream: Stream.value(ConnectivityResult.mobile),
|
||||||
streamChatThemeData: StreamChatThemeData.fromTheme(themeData),
|
streamChatThemeData: StreamChatThemeData.fromTheme(themeData),
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
child: ReactionBubble(
|
child: ReactionBubble(
|
||||||
|
|||||||
@@ -100,6 +100,7 @@ void main() {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
connectivityStream: Stream.value(ConnectivityResult.mobile),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
surfaceSize: const Size.square(200),
|
surfaceSize: const Size.square(200),
|
||||||
@@ -151,6 +152,7 @@ void main() {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
connectivityStream: Stream.value(ConnectivityResult.mobile),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
surfaceSize: const Size.square(200),
|
surfaceSize: const Size.square(200),
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:stream_chat/stream_chat.dart';
|
import 'package:stream_chat/stream_chat.dart';
|
||||||
@@ -43,6 +44,7 @@ class StreamChatCore extends StatefulWidget {
|
|||||||
required this.child,
|
required this.child,
|
||||||
this.onBackgroundEventReceived,
|
this.onBackgroundEventReceived,
|
||||||
this.backgroundKeepAlive = const Duration(minutes: 1),
|
this.backgroundKeepAlive = const Duration(minutes: 1),
|
||||||
|
this.connectivityStream,
|
||||||
}) : super(key: key);
|
}) : super(key: key);
|
||||||
|
|
||||||
/// Instance of Stream Chat Client containing information about the current
|
/// Instance of Stream Chat Client containing information about the current
|
||||||
@@ -61,6 +63,11 @@ class StreamChatCore extends StatefulWidget {
|
|||||||
/// upon the [Event.type]
|
/// upon the [Event.type]
|
||||||
final EventHandler? onBackgroundEventReceived;
|
final EventHandler? onBackgroundEventReceived;
|
||||||
|
|
||||||
|
/// Stream of connectivity result
|
||||||
|
/// Visible for testing
|
||||||
|
@visibleForTesting
|
||||||
|
final Stream<ConnectivityResult>? connectivityStream;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
StreamChatCoreState createState() => StreamChatCoreState();
|
StreamChatCoreState createState() => StreamChatCoreState();
|
||||||
|
|
||||||
@@ -96,48 +103,104 @@ 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;
|
||||||
|
|
||||||
|
StreamSubscription<ConnectivityResult>? _connectivitySubscription;
|
||||||
|
|
||||||
|
var _isInForeground = true;
|
||||||
|
var _isConnectionAvailable = true;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
WidgetsBinding.instance?.addObserver(this);
|
WidgetsBinding.instance?.addObserver(this);
|
||||||
|
_subscribeToConnectivityChange(widget.connectivityStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _subscribeToConnectivityChange([
|
||||||
|
Stream<ConnectivityResult>? connectivityStream,
|
||||||
|
]) {
|
||||||
|
if (_connectivitySubscription == null) {
|
||||||
|
connectivityStream ??= Connectivity().onConnectivityChanged;
|
||||||
|
_connectivitySubscription =
|
||||||
|
connectivityStream.distinct().listen((result) {
|
||||||
|
_isConnectionAvailable = result != ConnectivityResult.none;
|
||||||
|
if (!_isInForeground) return;
|
||||||
|
if (_isConnectionAvailable) {
|
||||||
|
if (client.wsConnectionStatus == ConnectionStatus.disconnected) {
|
||||||
|
client.openConnection();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (client.wsConnectionStatus == ConnectionStatus.connected) {
|
||||||
|
client.closeConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||||
|
_isInForeground = state == AppLifecycleState.resumed;
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
if (state == AppLifecycleState.paused) {
|
if (_isInForeground) {
|
||||||
if (widget.onBackgroundEventReceived == null) {
|
_onForeground();
|
||||||
client.closeConnection();
|
} else {
|
||||||
return;
|
_onBackground();
|
||||||
}
|
|
||||||
_eventSubscription = client.on().listen(
|
|
||||||
widget.onBackgroundEventReceived,
|
|
||||||
);
|
|
||||||
|
|
||||||
void onTimerComplete() {
|
|
||||||
_eventSubscription?.cancel();
|
|
||||||
client.closeConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
_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.openConnection();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _onForeground() {
|
||||||
|
if (_disconnectTimer?.isActive == true) {
|
||||||
|
_eventSubscription?.cancel();
|
||||||
|
_disconnectTimer?.cancel();
|
||||||
|
} else if (client.wsConnectionStatus == ConnectionStatus.disconnected &&
|
||||||
|
_isConnectionAvailable) {
|
||||||
|
client.openConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _onBackground() {
|
||||||
|
if (widget.onBackgroundEventReceived == null) {
|
||||||
|
if (client.wsConnectionStatus != ConnectionStatus.disconnected) {
|
||||||
|
client.closeConnection();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_eventSubscription = client.on().listen(widget.onBackgroundEventReceived);
|
||||||
|
|
||||||
|
void onTimerComplete() {
|
||||||
|
_eventSubscription?.cancel();
|
||||||
|
client.closeConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
_disconnectTimer = Timer(widget.backgroundKeepAlive, onTimerComplete);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
WidgetsBinding.instance?.removeObserver(this);
|
WidgetsBinding.instance?.removeObserver(this);
|
||||||
|
_unsubscribeFromConnectivityChange();
|
||||||
_eventSubscription?.cancel();
|
_eventSubscription?.cancel();
|
||||||
_disconnectTimer?.cancel();
|
_disconnectTimer?.cancel();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
library stream_chat_flutter_core;
|
library stream_chat_flutter_core;
|
||||||
|
|
||||||
|
export 'package:connectivity_plus/connectivity_plus.dart';
|
||||||
export 'package:stream_chat/stream_chat.dart';
|
export 'package:stream_chat/stream_chat.dart';
|
||||||
|
|
||||||
export 'src/channel_list_core.dart' hide ChannelListCoreState;
|
export 'src/channel_list_core.dart' hide ChannelListCoreState;
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ environment:
|
|||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
collection: ^1.15.0
|
collection: ^1.15.0
|
||||||
|
connectivity_plus: ^1.0.1
|
||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
meta: ^1.3.0
|
meta: ^1.3.0
|
||||||
@@ -25,5 +26,5 @@ dev_dependencies:
|
|||||||
fake_async: ^1.2.0
|
fake_async: ^1.2.0
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
mocktail: ^0.1.1
|
mocktail: ^0.1.3
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:mocktail/mocktail.dart';
|
import 'package:mocktail/mocktail.dart';
|
||||||
|
import 'package:rxdart/rxdart.dart';
|
||||||
import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart';
|
import 'package:stream_chat_flutter_core/stream_chat_flutter_core.dart';
|
||||||
|
|
||||||
import 'mocks.dart';
|
import 'mocks.dart';
|
||||||
@@ -100,6 +102,7 @@ void main() {
|
|||||||
child: Offstage(key: childKey),
|
child: Offstage(key: childKey),
|
||||||
onBackgroundEventReceived: mockOnBackgroundEventReceived,
|
onBackgroundEventReceived: mockOnBackgroundEventReceived,
|
||||||
backgroundKeepAlive: backgroundKeepAlive,
|
backgroundKeepAlive: backgroundKeepAlive,
|
||||||
|
connectivityStream: Stream.value(ConnectivityResult.mobile),
|
||||||
);
|
);
|
||||||
|
|
||||||
await tester.pumpWidget(streamChatCore);
|
await tester.pumpWidget(streamChatCore);
|
||||||
@@ -191,6 +194,7 @@ void main() {
|
|||||||
key: streamChatCoreKey,
|
key: streamChatCoreKey,
|
||||||
client: mockClient,
|
client: mockClient,
|
||||||
child: Offstage(key: childKey),
|
child: Offstage(key: childKey),
|
||||||
|
connectivityStream: Stream.value(ConnectivityResult.mobile),
|
||||||
);
|
);
|
||||||
|
|
||||||
await tester.pumpWidget(streamChatCore);
|
await tester.pumpWidget(streamChatCore);
|
||||||
@@ -222,6 +226,51 @@ void main() {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
testWidgets(
|
||||||
|
'didChangeAppLifecycleState should not call client.connect() '
|
||||||
|
'if connection is not available in case the '
|
||||||
|
'widget lifestyle changes to AppLifecycleState.resume',
|
||||||
|
(tester) async {
|
||||||
|
await tester.runAsync(() async {
|
||||||
|
final mockClient = MockClient();
|
||||||
|
const streamChatCoreKey = Key('streamChatCore');
|
||||||
|
const childKey = Key('child');
|
||||||
|
final streamChatCore = StreamChatCore(
|
||||||
|
key: streamChatCoreKey,
|
||||||
|
client: mockClient,
|
||||||
|
child: Offstage(key: childKey),
|
||||||
|
connectivityStream: Stream.value(ConnectivityResult.none),
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.pumpWidget(streamChatCore);
|
||||||
|
|
||||||
|
expect(find.byKey(streamChatCoreKey), findsOneWidget);
|
||||||
|
expect(find.byKey(childKey), findsOneWidget);
|
||||||
|
|
||||||
|
final event = Event();
|
||||||
|
when(() => mockClient.on()).thenAnswer((_) => Stream.value(event));
|
||||||
|
when(() => mockClient.connect()).thenAnswer((_) async => event);
|
||||||
|
when(mockClient.disconnect).thenAnswer((_) async => null);
|
||||||
|
when(() => mockClient.wsConnectionStatus)
|
||||||
|
.thenReturn(ConnectionStatus.disconnected);
|
||||||
|
|
||||||
|
final streamChatCoreState = tester.state<StreamChatCoreState>(
|
||||||
|
find.byKey(streamChatCoreKey),
|
||||||
|
);
|
||||||
|
|
||||||
|
streamChatCoreState
|
||||||
|
.didChangeAppLifecycleState(AppLifecycleState.paused);
|
||||||
|
|
||||||
|
await Future.delayed(const Duration(seconds: 1));
|
||||||
|
|
||||||
|
streamChatCoreState
|
||||||
|
.didChangeAppLifecycleState(AppLifecycleState.resumed);
|
||||||
|
|
||||||
|
verifyNever(() => mockClient.connect());
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
testWidgets(
|
testWidgets(
|
||||||
'streamChatCoreState.userStream should emit all the user events '
|
'streamChatCoreState.userStream should emit all the user events '
|
||||||
'provided by client',
|
'provided by client',
|
||||||
@@ -264,4 +313,124 @@ void main() {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
testWidgets(
|
||||||
|
'should call connect if in foreground and connection is back',
|
||||||
|
(tester) async {
|
||||||
|
await tester.runAsync(() async {
|
||||||
|
final mockClient = MockClient();
|
||||||
|
const streamChatCoreKey = Key('streamChatCore');
|
||||||
|
const childKey = Key('child');
|
||||||
|
final _connectivityController =
|
||||||
|
BehaviorSubject.seeded(ConnectivityResult.none);
|
||||||
|
final streamChatCore = StreamChatCore(
|
||||||
|
key: streamChatCoreKey,
|
||||||
|
client: mockClient,
|
||||||
|
child: Offstage(key: childKey),
|
||||||
|
connectivityStream: _connectivityController.stream,
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.pumpWidget(streamChatCore);
|
||||||
|
|
||||||
|
expect(find.byKey(streamChatCoreKey), findsOneWidget);
|
||||||
|
expect(find.byKey(childKey), findsOneWidget);
|
||||||
|
|
||||||
|
final event = Event();
|
||||||
|
when(() => mockClient.on()).thenAnswer((_) => Stream.value(event));
|
||||||
|
when(() => mockClient.connect()).thenAnswer((_) async => event);
|
||||||
|
when(mockClient.disconnect).thenAnswer((_) async => null);
|
||||||
|
when(() => mockClient.wsConnectionStatus)
|
||||||
|
.thenReturn(ConnectionStatus.disconnected);
|
||||||
|
|
||||||
|
_connectivityController.add(ConnectivityResult.mobile);
|
||||||
|
|
||||||
|
await Future.delayed(const Duration(seconds: 1));
|
||||||
|
|
||||||
|
verify(() => mockClient.connect()).called(1);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
testWidgets(
|
||||||
|
'should call disconnect if in foreground and connection goes away',
|
||||||
|
(tester) async {
|
||||||
|
await tester.runAsync(() async {
|
||||||
|
final mockClient = MockClient();
|
||||||
|
const streamChatCoreKey = Key('streamChatCore');
|
||||||
|
const childKey = Key('child');
|
||||||
|
final _connectivityController =
|
||||||
|
BehaviorSubject.seeded(ConnectivityResult.mobile);
|
||||||
|
final streamChatCore = StreamChatCore(
|
||||||
|
key: streamChatCoreKey,
|
||||||
|
client: mockClient,
|
||||||
|
child: Offstage(key: childKey),
|
||||||
|
connectivityStream: _connectivityController.stream,
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.pumpWidget(streamChatCore);
|
||||||
|
|
||||||
|
expect(find.byKey(streamChatCoreKey), findsOneWidget);
|
||||||
|
expect(find.byKey(childKey), findsOneWidget);
|
||||||
|
|
||||||
|
final event = Event();
|
||||||
|
when(() => mockClient.on()).thenAnswer((_) => Stream.value(event));
|
||||||
|
when(() => mockClient.connect()).thenAnswer((_) async => event);
|
||||||
|
when(mockClient.disconnect).thenAnswer((_) async => null);
|
||||||
|
when(() => mockClient.wsConnectionStatus)
|
||||||
|
.thenReturn(ConnectionStatus.connected);
|
||||||
|
|
||||||
|
_connectivityController.add(ConnectivityResult.none);
|
||||||
|
|
||||||
|
await Future.delayed(const Duration(seconds: 1));
|
||||||
|
|
||||||
|
verify(() => mockClient.disconnect()).called(1);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
testWidgets(
|
||||||
|
'should ignore connectivity in background',
|
||||||
|
(tester) async {
|
||||||
|
await tester.runAsync(() async {
|
||||||
|
final mockClient = MockClient();
|
||||||
|
const streamChatCoreKey = Key('streamChatCore');
|
||||||
|
const childKey = Key('child');
|
||||||
|
final _connectivityController =
|
||||||
|
BehaviorSubject.seeded(ConnectivityResult.none);
|
||||||
|
final streamChatCore = StreamChatCore(
|
||||||
|
key: streamChatCoreKey,
|
||||||
|
client: mockClient,
|
||||||
|
child: Offstage(key: childKey),
|
||||||
|
connectivityStream: _connectivityController.stream,
|
||||||
|
);
|
||||||
|
|
||||||
|
await tester.pumpWidget(streamChatCore);
|
||||||
|
|
||||||
|
expect(find.byKey(streamChatCoreKey), findsOneWidget);
|
||||||
|
expect(find.byKey(childKey), findsOneWidget);
|
||||||
|
|
||||||
|
final event = Event();
|
||||||
|
when(() => mockClient.on()).thenAnswer((_) => Stream.value(event));
|
||||||
|
when(() => mockClient.connect()).thenAnswer((_) async => event);
|
||||||
|
when(mockClient.disconnect).thenAnswer((_) async => null);
|
||||||
|
when(() => mockClient.wsConnectionStatus)
|
||||||
|
.thenReturn(ConnectionStatus.disconnected);
|
||||||
|
|
||||||
|
final streamChatCoreState = tester.state<StreamChatCoreState>(
|
||||||
|
find.byKey(streamChatCoreKey),
|
||||||
|
);
|
||||||
|
|
||||||
|
streamChatCoreState
|
||||||
|
.didChangeAppLifecycleState(AppLifecycleState.paused);
|
||||||
|
|
||||||
|
await Future.delayed(const Duration(seconds: 1));
|
||||||
|
|
||||||
|
_connectivityController.add(ConnectivityResult.mobile);
|
||||||
|
|
||||||
|
await Future.delayed(const Duration(seconds: 1));
|
||||||
|
|
||||||
|
verifyNever(() => mockClient.disconnect());
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user