add lockChannelsOrder property to channelsBloc

This commit is contained in:
Salvatore Giordano
2020-08-07 15:58:52 +02:00
parent cdd968f39f
commit 42ed371d48
+21 -15
View File
@@ -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<ChannelsBloc>
Stream<bool> get queryChannelsLoading =>
_queryChannelsLoadingController.stream;
List<Channel> _hiddenChannels = [];
final List<Channel> _hiddenChannels = [];
/// Calls [client.queryChannels] updating [queryChannelsLoading] stream
Future<void> queryChannels({
@@ -115,22 +119,24 @@ class ChannelsBlocState extends State<ChannelsBloc>
final client = StreamChat.of(context).client;
_subscriptions.add(client.on(EventType.messageNew).listen((e) {
final newChannels = List<Channel>.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<Channel>.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<Channel>.from(channels);