From 42ed371d484529a778a3b7a21be04ea42315c036 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Fri, 7 Aug 2020 15:58:52 +0200 Subject: [PATCH] add lockChannelsOrder property to channelsBloc --- lib/src/channels_bloc.dart | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/src/channels_bloc.dart b/lib/src/channels_bloc.dart index 6d230faa..e2cf9cd9 100644 --- a/lib/src/channels_bloc.dart +++ b/lib/src/channels_bloc.dart @@ -11,10 +11,14 @@ class ChannelsBloc extends StatefulWidget { /// The widget child final Widget child; + /// Set this to false to prevent channels to be brought to the top of the list when a new message arrives + final bool lockChannelsOrder; + /// Instantiate a new ChannelsBloc const ChannelsBloc({ Key key, this.child, + this.lockChannelsOrder = false, }) : super(key: key); @override @@ -59,7 +63,7 @@ class ChannelsBlocState extends State Stream get queryChannelsLoading => _queryChannelsLoadingController.stream; - List _hiddenChannels = []; + final List _hiddenChannels = []; /// Calls [client.queryChannels] updating [queryChannelsLoading] stream Future queryChannels({ @@ -115,22 +119,24 @@ class ChannelsBlocState extends State final client = StreamChat.of(context).client; - _subscriptions.add(client.on(EventType.messageNew).listen((e) { - final newChannels = List.from(channels ?? []); - final index = newChannels.indexWhere((c) => c.cid == e.cid); - if (index > 0) { - final channel = newChannels.removeAt(index); - newChannels.insert(0, channel); - _channelsController.add(newChannels); - } else { - final hiddenIndex = _hiddenChannels.indexWhere((c) => c.cid == e.cid); - if (hiddenIndex > -1) { - newChannels.insert(0, _hiddenChannels[hiddenIndex]); - _hiddenChannels.removeAt(hiddenIndex); + if (!widget.lockChannelsOrder) { + _subscriptions.add(client.on(EventType.messageNew).listen((e) { + final newChannels = List.from(channels ?? []); + final index = newChannels.indexWhere((c) => c.cid == e.cid); + if (index > 0) { + final channel = newChannels.removeAt(index); + newChannels.insert(0, channel); _channelsController.add(newChannels); + } else { + final hiddenIndex = _hiddenChannels.indexWhere((c) => c.cid == e.cid); + if (hiddenIndex > -1) { + newChannels.insert(0, _hiddenChannels[hiddenIndex]); + _hiddenChannels.removeAt(hiddenIndex); + _channelsController.add(newChannels); + } } - } - })); + })); + } _subscriptions.add(client.on(EventType.channelHidden).listen((event) async { final newChannels = List.from(channels);