Merge pull request #70 from GetStream/feature/lockChannelsOrder

add lockChannelsOrder property to channelsBloc
This commit is contained in:
Salvatore Giordano
2020-08-07 16:01:36 +02:00
committed by GitHub
+21 -15
View File
@@ -11,10 +11,14 @@ class ChannelsBloc extends StatefulWidget {
/// The widget child /// The widget child
final 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 /// Instantiate a new ChannelsBloc
const ChannelsBloc({ const ChannelsBloc({
Key key, Key key,
this.child, this.child,
this.lockChannelsOrder = false,
}) : super(key: key); }) : super(key: key);
@override @override
@@ -59,7 +63,7 @@ class ChannelsBlocState extends State<ChannelsBloc>
Stream<bool> get queryChannelsLoading => Stream<bool> get queryChannelsLoading =>
_queryChannelsLoadingController.stream; _queryChannelsLoadingController.stream;
List<Channel> _hiddenChannels = []; final List<Channel> _hiddenChannels = [];
/// Calls [client.queryChannels] updating [queryChannelsLoading] stream /// Calls [client.queryChannels] updating [queryChannelsLoading] stream
Future<void> queryChannels({ Future<void> queryChannels({
@@ -115,22 +119,24 @@ class ChannelsBlocState extends State<ChannelsBloc>
final client = StreamChat.of(context).client; final client = StreamChat.of(context).client;
_subscriptions.add(client.on(EventType.messageNew).listen((e) { if (!widget.lockChannelsOrder) {
final newChannels = List<Channel>.from(channels ?? []); _subscriptions.add(client.on(EventType.messageNew).listen((e) {
final index = newChannels.indexWhere((c) => c.cid == e.cid); final newChannels = List<Channel>.from(channels ?? []);
if (index > 0) { final index = newChannels.indexWhere((c) => c.cid == e.cid);
final channel = newChannels.removeAt(index); if (index > 0) {
newChannels.insert(0, channel); final channel = newChannels.removeAt(index);
_channelsController.add(newChannels); newChannels.insert(0, channel);
} else {
final hiddenIndex = _hiddenChannels.indexWhere((c) => c.cid == e.cid);
if (hiddenIndex > -1) {
newChannels.insert(0, _hiddenChannels[hiddenIndex]);
_hiddenChannels.removeAt(hiddenIndex);
_channelsController.add(newChannels); _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 { _subscriptions.add(client.on(EventType.channelHidden).listen((event) async {
final newChannels = List<Channel>.from(channels); final newChannels = List<Channel>.from(channels);