refactor(llc): minor event handler changes.
Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
-123
@@ -1,123 +0,0 @@
|
||||
import 'package:stream_chat/stream_chat.dart' hide Success;
|
||||
import 'package:stream_chat_flutter/src/v4/channel_list_view/stream_channel_list_controller.dart';
|
||||
import 'package:stream_chat_flutter/src/v4/channel_list_view/stream_channel_list_event_handler.dart'
|
||||
as event_handler;
|
||||
|
||||
/// Contains handlers that are called from [StreamChannelListController] for
|
||||
/// certain [Event]s.
|
||||
///
|
||||
/// This class can be mixed in or extended to create custom overrides.
|
||||
class ChannelEventHandlers {
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.channelDeleted].
|
||||
///
|
||||
/// By default, calls [event_handler.onChannelDeleted]
|
||||
/// with the [Event] and the [StreamChannelListController].
|
||||
void onChannelDeleted(Event event, StreamChannelListController controller) {
|
||||
event_handler.onChannelDeleted(event, controller);
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.channelHidden].
|
||||
///
|
||||
/// By default, calls [event_handler.onChannelHidden]
|
||||
/// with the [Event] and the [StreamChannelListController].
|
||||
void onChannelHidden(Event event, StreamChannelListController controller) {
|
||||
event_handler.onChannelHidden(event, controller);
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.channelTruncated].
|
||||
///
|
||||
/// By default, calls [event_handler.onChannelTruncated]
|
||||
/// with the [Event] and the [StreamChannelListController].
|
||||
void onChannelTruncated(Event event, StreamChannelListController controller) {
|
||||
event_handler.onChannelTruncated(event, controller);
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.channelUpdated].
|
||||
///
|
||||
/// By default, calls [event_handler.onChannelUpdated]
|
||||
/// with the [Event] and the [StreamChannelListController].
|
||||
void onChannelUpdated(Event event, StreamChannelListController controller) {
|
||||
event_handler.onChannelUpdated(event, controller);
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.channelVisible].
|
||||
///
|
||||
/// By default, calls [event_handler.onChannelVisible]
|
||||
/// with the [Event] and the [StreamChannelListController].
|
||||
void onChannelVisible(Event event, StreamChannelListController controller) {
|
||||
event_handler.onChannelVisible(event, controller);
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.connectionRecovered].
|
||||
///
|
||||
/// By default, calls [event_handler.onConnectionRecovered]
|
||||
/// with the [Event] and the [StreamChannelListController].
|
||||
void onConnectionRecovered(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
event_handler.onConnectionRecovered(event, controller);
|
||||
}
|
||||
|
||||
/// Function which gets called for the event [EventType.messageNew].
|
||||
///
|
||||
/// By default, calls [event_handler.onMessageNew]
|
||||
/// with the [Event] and the [StreamChannelListController].
|
||||
void onMessageNew(Event event, StreamChannelListController controller) {
|
||||
event_handler.onMessageNew(event, controller);
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.notificationAddedToChannel].
|
||||
///
|
||||
/// By default, calls [event_handler.onNotificationAddedToChannel]
|
||||
/// with the [Event] and the [StreamChannelListController].
|
||||
void onNotificationAddedToChannel(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
event_handler.onNotificationAddedToChannel(event, controller);
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.notificationMessageNew].
|
||||
///
|
||||
/// By default, calls [event_handler.onNotificationMessageNew]
|
||||
/// with the [Event] and the [StreamChannelListController].
|
||||
void onNotificationMessageNew(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
event_handler.onNotificationMessageNew(event, controller);
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.notificationRemovedFromChannel].
|
||||
///
|
||||
/// By default, calls [event_handler.onNotificationRemovedFromChannel]
|
||||
/// with the [Event] and the [StreamChannelListController].
|
||||
void onNotificationRemovedFromChannel(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
event_handler.onNotificationRemovedFromChannel(event, controller);
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// 'user.presence.changed' and [EventType.userUpdated].
|
||||
///
|
||||
/// By default, calls [event_handler.onUserPresenceChanged]
|
||||
/// with the [Event] and the [StreamChannelListController].
|
||||
void onUserPresenceChanged(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
event_handler.onUserPresenceChanged(event, controller);
|
||||
}
|
||||
}
|
||||
+26
-22
@@ -2,7 +2,7 @@ import 'dart:async';
|
||||
|
||||
import 'package:stream_chat/stream_chat.dart' hide Success;
|
||||
import 'package:stream_chat_flutter/src/paged_value_notifier.dart';
|
||||
import 'package:stream_chat_flutter/src/v4/channel_list_view/channel_event_handlers.dart';
|
||||
import 'package:stream_chat_flutter/src/v4/channel_list_view/stream_channel_list_event_handler.dart';
|
||||
|
||||
/// The default channel page limit to load.
|
||||
const defaultChannelPagedLimit = 10;
|
||||
@@ -15,7 +15,6 @@ const defaultChannelPagedLimit = 10;
|
||||
/// * Load more data using [loadMore].
|
||||
/// * Replace the previously loaded channels.
|
||||
/// * Return/Create a new channel and start watching it.
|
||||
/// * Unsubscribe from all channel list events.
|
||||
/// * Pause and Resume all subscriptions added to this composite.
|
||||
class StreamChannelListController extends PagedValueNotifier<int, Channel> {
|
||||
/// Creates a Stream channel list controller.
|
||||
@@ -24,7 +23,7 @@ class StreamChannelListController extends PagedValueNotifier<int, Channel> {
|
||||
///
|
||||
/// * `channelEventHandlers` is the channel events to use for the channels
|
||||
/// list. This class can be mixed in or extended to create custom overrides.
|
||||
/// See [ChannelEventHandlers] for advice.
|
||||
/// See [StreamChannelListEventHandler] for advice.
|
||||
///
|
||||
/// * `filter` is the query filters to use.
|
||||
///
|
||||
@@ -40,46 +39,51 @@ class StreamChannelListController extends PagedValueNotifier<int, Channel> {
|
||||
/// * `memberLimit` is the number of members to fetch in each channel.
|
||||
StreamChannelListController({
|
||||
required this.client,
|
||||
ChannelEventHandlers? channelEventHandlers,
|
||||
StreamChannelListEventHandler? eventHandler,
|
||||
this.filter,
|
||||
this.sort,
|
||||
this.presence = true,
|
||||
this.limit = defaultChannelPagedLimit,
|
||||
this.messageLimit,
|
||||
this.memberLimit,
|
||||
}) : _channelEventHandlers = channelEventHandlers ?? ChannelEventHandlers(),
|
||||
}) : _eventHandler = eventHandler ?? StreamChannelListEventHandler(),
|
||||
super(const PagedValue.loading());
|
||||
|
||||
/// Creates a [StreamChannelListController] from the passed [value].
|
||||
StreamChannelListController.fromValue(
|
||||
PagedValue<int, Channel> value, {
|
||||
required this.client,
|
||||
ChannelEventHandlers? channelEventHandlers,
|
||||
StreamChannelListEventHandler? eventHandler,
|
||||
this.filter,
|
||||
this.sort,
|
||||
this.presence = true,
|
||||
this.limit = defaultChannelPagedLimit,
|
||||
this.messageLimit,
|
||||
this.memberLimit,
|
||||
}) : _channelEventHandlers = channelEventHandlers ?? ChannelEventHandlers(),
|
||||
}) : _eventHandler = eventHandler ?? StreamChannelListEventHandler(),
|
||||
super(value);
|
||||
|
||||
/// The channel events to use for the channels list.
|
||||
final ChannelEventHandlers _channelEventHandlers;
|
||||
|
||||
/// The client to use for the channels list.
|
||||
final StreamChatClient client;
|
||||
|
||||
/// The channel event handlers to use for the channels list.
|
||||
final StreamChannelListEventHandler _eventHandler;
|
||||
|
||||
/// The query filters to use.
|
||||
///
|
||||
/// You can query on any of the custom fields you've defined on the [Channel].
|
||||
///
|
||||
/// You can also filter other built-in channel fields.
|
||||
final Filter? filter;
|
||||
|
||||
/// The sorting used for the channels matching the filters.
|
||||
///
|
||||
/// Sorting is based on field and direction, multiple sorting options
|
||||
/// can be provided.
|
||||
///
|
||||
/// You can sort based on last_updated, last_message_at, updated_at,
|
||||
/// created_at or member_count.
|
||||
///
|
||||
/// Direction can be ascending or descending.
|
||||
final List<SortOption<ChannelModel>>? sort;
|
||||
|
||||
@@ -183,32 +187,32 @@ class StreamChannelListController extends PagedValueNotifier<int, Channel> {
|
||||
_channelEventSubscription = client.on().listen((event) {
|
||||
final eventType = event.type;
|
||||
if (eventType == EventType.channelDeleted) {
|
||||
_channelEventHandlers.onChannelDeleted(event, this);
|
||||
_eventHandler.onChannelDeleted(event, this);
|
||||
} else if (eventType == EventType.channelHidden) {
|
||||
_channelEventHandlers.onChannelHidden(event, this);
|
||||
_eventHandler.onChannelHidden(event, this);
|
||||
} else if (eventType == EventType.channelTruncated) {
|
||||
_channelEventHandlers.onChannelTruncated(event, this);
|
||||
_eventHandler.onChannelTruncated(event, this);
|
||||
} else if (eventType == EventType.channelUpdated) {
|
||||
_channelEventHandlers.onChannelUpdated(event, this);
|
||||
_eventHandler.onChannelUpdated(event, this);
|
||||
} else if (eventType == EventType.channelVisible) {
|
||||
_channelEventHandlers.onChannelVisible(event, this);
|
||||
_eventHandler.onChannelVisible(event, this);
|
||||
} else if (eventType == EventType.connectionRecovered) {
|
||||
_channelEventHandlers.onConnectionRecovered(event, this);
|
||||
_eventHandler.onConnectionRecovered(event, this);
|
||||
} else if (eventType == EventType.connectionChanged) {
|
||||
if (event.online != null) {
|
||||
_channelEventHandlers.onConnectionRecovered(event, this);
|
||||
_eventHandler.onConnectionRecovered(event, this);
|
||||
}
|
||||
} else if (eventType == EventType.messageNew) {
|
||||
_channelEventHandlers.onMessageNew(event, this);
|
||||
_eventHandler.onMessageNew(event, this);
|
||||
} else if (eventType == EventType.notificationAddedToChannel) {
|
||||
_channelEventHandlers.onNotificationAddedToChannel(event, this);
|
||||
_eventHandler.onNotificationAddedToChannel(event, this);
|
||||
} else if (eventType == EventType.notificationMessageNew) {
|
||||
_channelEventHandlers.onNotificationMessageNew(event, this);
|
||||
_eventHandler.onNotificationMessageNew(event, this);
|
||||
} else if (eventType == EventType.notificationRemovedFromChannel) {
|
||||
_channelEventHandlers.onNotificationRemovedFromChannel(event, this);
|
||||
_eventHandler.onNotificationRemovedFromChannel(event, this);
|
||||
} else if (eventType == 'user.presence.changed' ||
|
||||
eventType == EventType.userUpdated) {
|
||||
_channelEventHandlers.onUserPresenceChanged(event, this);
|
||||
_eventHandler.onUserPresenceChanged(event, this);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+197
-295
@@ -1,311 +1,213 @@
|
||||
import 'package:stream_chat/stream_chat.dart' show Event;
|
||||
import 'package:stream_chat/stream_chat.dart' hide Success;
|
||||
import 'package:stream_chat_flutter/src/v4/channel_list_view/stream_channel_list_controller.dart';
|
||||
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
|
||||
|
||||
/// Handles [EventType.channelDeleted] event.
|
||||
/// Contains handlers that are called from [StreamChannelListController] for
|
||||
/// certain [Event]s.
|
||||
///
|
||||
/// This event is fired when a channel is deleted.
|
||||
///
|
||||
/// By default, this removes the channel from the list of channels.
|
||||
///
|
||||
/// ```dart
|
||||
/// StreamChannelListController(
|
||||
/// client: client,
|
||||
/// onChannelDeleted: (event, controller) {
|
||||
/// // Do something
|
||||
/// },
|
||||
/// );
|
||||
/// ```
|
||||
void onChannelDeleted(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
final channels = [...controller.currentItems];
|
||||
/// This class can be mixed in or extended to create custom overrides.
|
||||
class StreamChannelListEventHandler {
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.channelDeleted].
|
||||
///
|
||||
/// This event is fired when a channel is deleted.
|
||||
///
|
||||
/// By default, this removes the channel from the list of channels.
|
||||
void onChannelDeleted(Event event, StreamChannelListController controller) {
|
||||
final channels = [...controller.currentItems];
|
||||
|
||||
final updatedChannels = channels
|
||||
..removeWhere(
|
||||
(it) => it.cid == (event.cid ?? event.channel?.cid),
|
||||
);
|
||||
final updatedChannels = channels
|
||||
..removeWhere(
|
||||
(it) => it.cid == (event.cid ?? event.channel?.cid),
|
||||
);
|
||||
|
||||
controller.channels = updatedChannels;
|
||||
}
|
||||
|
||||
/// Handles [EventType.channelHidden] event.
|
||||
///
|
||||
/// This event is fired when a channel is hidden.
|
||||
///
|
||||
/// By default, this removes the channel from the list of channels.
|
||||
///
|
||||
/// ```dart
|
||||
/// StreamChannelListController(
|
||||
/// client: client,
|
||||
/// onChannelHidden: (event, controller) {
|
||||
/// // Do something
|
||||
/// },
|
||||
/// );
|
||||
/// ```
|
||||
void onChannelHidden(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
onChannelDeleted(event, controller);
|
||||
}
|
||||
|
||||
/// Handles [EventType.channelTruncated] event.
|
||||
///
|
||||
/// This event is fired when a channel is truncated.
|
||||
///
|
||||
/// By default, this refreshes the whole channel list.
|
||||
///
|
||||
/// ```dart
|
||||
/// StreamChannelListController(
|
||||
/// client: client,
|
||||
/// onChannelTruncated: (event, controller) {
|
||||
/// // Do something
|
||||
/// },
|
||||
/// );
|
||||
/// ```
|
||||
void onChannelTruncated(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
controller.refresh();
|
||||
}
|
||||
|
||||
/// Handles [EventType.channelUpdated] event.
|
||||
///
|
||||
/// This event is fired when a channel is updated.
|
||||
///
|
||||
/// By default, this updates the channel received in the event.
|
||||
///
|
||||
/// ```dart
|
||||
/// StreamChannelListController(
|
||||
/// client: client,
|
||||
/// onChannelUpdated: (event, controller) {
|
||||
/// // Do something
|
||||
/// },
|
||||
/// );
|
||||
/// ```
|
||||
void onChannelUpdated(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
final eventChannel = event.channel;
|
||||
if (eventChannel == null) return;
|
||||
|
||||
final channels = [...controller.currentItems];
|
||||
final channelIndex = channels.indexWhere(
|
||||
(it) => it.cid == (event.cid ?? eventChannel.cid),
|
||||
);
|
||||
|
||||
if (channelIndex >= 0) {
|
||||
final channelState = ChannelState(channel: eventChannel);
|
||||
channels[channelIndex].state?.updateChannelState(channelState);
|
||||
controller.channels = updatedChannels;
|
||||
}
|
||||
|
||||
controller.channels = channels;
|
||||
}
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.channelHidden].
|
||||
///
|
||||
/// This event is fired when a channel is hidden.
|
||||
///
|
||||
/// By default, this removes the channel from the list of channels.
|
||||
void onChannelHidden(Event event, StreamChannelListController controller) {
|
||||
onChannelDeleted(event, controller);
|
||||
}
|
||||
|
||||
/// Handles [EventType.channelVisible] event.
|
||||
///
|
||||
/// This event is fired when a channel is made visible.
|
||||
///
|
||||
/// By default, this adds the channel to the list of channels.
|
||||
///
|
||||
/// ```dart
|
||||
/// StreamChannelListController(
|
||||
/// client: client,
|
||||
/// onChannelVisible: (event, controller) {
|
||||
/// // Do something
|
||||
/// },
|
||||
/// );
|
||||
/// ```
|
||||
void onChannelVisible(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) async {
|
||||
final channelId = event.channelId;
|
||||
final channelType = event.channelType;
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.channelTruncated].
|
||||
///
|
||||
/// This event is fired when a channel is truncated.
|
||||
///
|
||||
/// By default, this refreshes the whole channel list.
|
||||
void onChannelTruncated(Event event, StreamChannelListController controller) {
|
||||
controller.refresh();
|
||||
}
|
||||
|
||||
if (channelId == null || channelType == null) return;
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.channelUpdated].
|
||||
///
|
||||
/// This event is fired when a channel is updated.
|
||||
///
|
||||
/// By default, this updates the channel received in the event.
|
||||
void onChannelUpdated(Event event, StreamChannelListController controller) {
|
||||
final eventChannel = event.channel;
|
||||
if (eventChannel == null) return;
|
||||
|
||||
final channel = await controller.getChannel(
|
||||
id: channelId,
|
||||
type: channelType,
|
||||
);
|
||||
|
||||
final currentChannels = [...controller.currentItems];
|
||||
|
||||
final updatedChannels = [
|
||||
channel,
|
||||
...currentChannels..removeWhere((it) => it.cid == channel.cid),
|
||||
];
|
||||
|
||||
controller.channels = updatedChannels;
|
||||
}
|
||||
|
||||
/// Handles [EventType.connectionRecovered] event.
|
||||
///
|
||||
/// This event is fired when the client web-socket connection recovers.
|
||||
///
|
||||
/// By default, this refreshes the whole channel list.
|
||||
///
|
||||
/// ```dart
|
||||
/// StreamChannelListController(
|
||||
/// client: client,
|
||||
/// onConnectionRecovered: (event, controller) {
|
||||
/// // Do something
|
||||
/// },
|
||||
/// );
|
||||
/// ```
|
||||
void onConnectionRecovered(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
controller.refresh();
|
||||
}
|
||||
|
||||
/// Handles [EventType.messageNew] event.
|
||||
///
|
||||
/// This event is fired when a new message is created in one of the channels
|
||||
/// we are currently watching.
|
||||
///
|
||||
/// By default, this moves the channel to the top of the list.
|
||||
///
|
||||
/// ```dart
|
||||
/// StreamChannelListController(
|
||||
/// client: client,
|
||||
/// onMessageNew: (event, controller) {
|
||||
/// // Do something
|
||||
/// },
|
||||
/// );
|
||||
/// ```
|
||||
void onMessageNew(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
final channelCid = event.cid;
|
||||
if (channelCid == null) return;
|
||||
|
||||
final channels = [...controller.currentItems];
|
||||
|
||||
final channelIndex = channels.indexWhere((it) => it.cid == channelCid);
|
||||
if (channelIndex <= 0) return;
|
||||
|
||||
final channel = channels.removeAt(channelIndex);
|
||||
channels.insert(0, channel);
|
||||
|
||||
controller.channels = [...channels];
|
||||
}
|
||||
|
||||
/// Handles [EventType.notificationAddedToChannel] event.
|
||||
///
|
||||
/// This event is fired when a channel is added which we are not watching.
|
||||
///
|
||||
/// By default, this adds the channel and moves it to the top of list.
|
||||
///
|
||||
/// ```dart
|
||||
/// StreamChannelListController(
|
||||
/// client: client,
|
||||
/// onNotificationAddedToChannel: (event, controller) {
|
||||
/// // Do something
|
||||
/// },
|
||||
/// );
|
||||
/// ```
|
||||
void onNotificationAddedToChannel(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
onChannelVisible(event, controller);
|
||||
}
|
||||
|
||||
/// Handles [EventType.notificationMessageNew] event.
|
||||
///
|
||||
/// This event is fired when a new message is created in a channel which we are
|
||||
/// not currently watching.
|
||||
///
|
||||
/// By default, this adds the channel and moves it to the top of list.
|
||||
///
|
||||
/// ```dart
|
||||
/// StreamChannelListController(
|
||||
/// client: client,
|
||||
/// onNotificationMessageNew: (event, controller) {
|
||||
/// // Do something
|
||||
/// },
|
||||
/// );
|
||||
/// ```
|
||||
void onNotificationMessageNew(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
onChannelVisible(event, controller);
|
||||
}
|
||||
|
||||
/// Handles [EventType.notificationRemovedFromChannel] event.
|
||||
///
|
||||
/// This event is fired when a user is removed from a channel which we are
|
||||
/// not currently watching.
|
||||
///
|
||||
/// By default, this removes the event channel from the list.
|
||||
///
|
||||
/// ```dart
|
||||
/// StreamChannelListController(
|
||||
/// client: client,
|
||||
/// onNotificationRemovedFromChannel: (event, controller) {
|
||||
/// // Do something
|
||||
/// },
|
||||
/// );
|
||||
/// ```
|
||||
void onNotificationRemovedFromChannel(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
final channels = [...controller.currentItems];
|
||||
final updatedChannels = channels.where((it) => it.cid != event.channel?.cid);
|
||||
final listChanged = channels.length != updatedChannels.length;
|
||||
|
||||
if (!listChanged) return;
|
||||
|
||||
controller.channels = [...updatedChannels];
|
||||
}
|
||||
|
||||
/// Handles 'user.presence.changed' and [EventType.userUpdated] event.
|
||||
///
|
||||
/// This event is fired when a user's presence changes or gets updated.
|
||||
///
|
||||
/// By default, this updates the channel member with the event user.
|
||||
///
|
||||
/// ```dart
|
||||
/// StreamChannelListController(
|
||||
/// client: client,
|
||||
/// onUserPresenceChanged: (event, controller) {
|
||||
/// // Do something
|
||||
/// },
|
||||
/// );
|
||||
/// ```
|
||||
void onUserPresenceChanged(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
final user = event.user;
|
||||
if (user == null) return;
|
||||
|
||||
final channels = [...controller.currentItems];
|
||||
|
||||
final updatedChannels = channels.map((channel) {
|
||||
final members = [...channel.state!.members];
|
||||
final memberIndex = members.indexWhere(
|
||||
(it) => user.id == (it.userId ?? it.user?.id),
|
||||
final channels = [...controller.currentItems];
|
||||
final channelIndex = channels.indexWhere(
|
||||
(it) => it.cid == (event.cid ?? eventChannel.cid),
|
||||
);
|
||||
|
||||
if (memberIndex < 0) return channel;
|
||||
if (channelIndex >= 0) {
|
||||
final channelState = ChannelState(channel: eventChannel);
|
||||
channels[channelIndex].state?.updateChannelState(channelState);
|
||||
}
|
||||
|
||||
members[memberIndex] = members[memberIndex].copyWith(user: user);
|
||||
final updatedState = ChannelState(members: [...members]);
|
||||
channel.state!.updateChannelState(updatedState);
|
||||
controller.channels = channels;
|
||||
}
|
||||
|
||||
return channel;
|
||||
});
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.channelVisible].
|
||||
///
|
||||
/// This event is fired when a channel is made visible.
|
||||
///
|
||||
/// By default, this adds the channel to the list of channels.
|
||||
void onChannelVisible(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) async {
|
||||
final channelId = event.channelId;
|
||||
final channelType = event.channelType;
|
||||
|
||||
controller.channels = [...updatedChannels];
|
||||
if (channelId == null || channelType == null) return;
|
||||
|
||||
final channel = await controller.getChannel(
|
||||
id: channelId,
|
||||
type: channelType,
|
||||
);
|
||||
|
||||
final currentChannels = [...controller.currentItems];
|
||||
|
||||
final updatedChannels = [
|
||||
channel,
|
||||
...currentChannels..removeWhere((it) => it.cid == channel.cid),
|
||||
];
|
||||
|
||||
controller.channels = updatedChannels;
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.connectionRecovered].
|
||||
///
|
||||
/// This event is fired when the client web-socket connection recovers.
|
||||
///
|
||||
/// By default, this refreshes the whole channel list.
|
||||
void onConnectionRecovered(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
controller.refresh();
|
||||
}
|
||||
|
||||
/// Function which gets called for the event [EventType.messageNew].
|
||||
///
|
||||
/// This event is fired when a new message is created in one of the channels
|
||||
/// we are currently watching.
|
||||
///
|
||||
/// By default, this moves the channel to the top of the list.
|
||||
void onMessageNew(Event event, StreamChannelListController controller) {
|
||||
final channelCid = event.cid;
|
||||
if (channelCid == null) return;
|
||||
|
||||
final channels = [...controller.currentItems];
|
||||
|
||||
final channelIndex = channels.indexWhere((it) => it.cid == channelCid);
|
||||
if (channelIndex <= 0) return;
|
||||
|
||||
final channel = channels.removeAt(channelIndex);
|
||||
channels.insert(0, channel);
|
||||
|
||||
controller.channels = [...channels];
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.notificationAddedToChannel].
|
||||
///
|
||||
/// This event is fired when a channel is added which we are not watching.
|
||||
///
|
||||
/// By default, this adds the channel and moves it to the top of list.
|
||||
void onNotificationAddedToChannel(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
onChannelVisible(event, controller);
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.notificationMessageNew].
|
||||
///
|
||||
/// This event is fired when a new message is created in a channel which we are
|
||||
/// not currently watching.
|
||||
///
|
||||
/// By default, this adds the channel and moves it to the top of list.
|
||||
void onNotificationMessageNew(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
onChannelVisible(event, controller);
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// [EventType.notificationRemovedFromChannel].
|
||||
///
|
||||
/// This event is fired when a user is removed from a channel which we are
|
||||
/// not currently watching.
|
||||
///
|
||||
/// By default, this removes the event channel from the list.
|
||||
void onNotificationRemovedFromChannel(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
final channels = [...controller.currentItems];
|
||||
final updatedChannels =
|
||||
channels.where((it) => it.cid != event.channel?.cid);
|
||||
final listChanged = channels.length != updatedChannels.length;
|
||||
|
||||
if (!listChanged) return;
|
||||
|
||||
controller.channels = [...updatedChannels];
|
||||
}
|
||||
|
||||
/// Function which gets called for the event
|
||||
/// 'user.presence.changed' and [EventType.userUpdated].
|
||||
///
|
||||
/// This event is fired when a user's presence changes or gets updated.
|
||||
///
|
||||
/// By default, this updates the channel member with the event user.
|
||||
void onUserPresenceChanged(
|
||||
Event event,
|
||||
StreamChannelListController controller,
|
||||
) {
|
||||
final user = event.user;
|
||||
if (user == null) return;
|
||||
|
||||
final channels = [...controller.currentItems];
|
||||
|
||||
final updatedChannels = channels.map((channel) {
|
||||
final members = [...channel.state!.members];
|
||||
final memberIndex = members.indexWhere(
|
||||
(it) => user.id == (it.userId ?? it.user?.id),
|
||||
);
|
||||
|
||||
if (memberIndex < 0) return channel;
|
||||
|
||||
members[memberIndex] = members[memberIndex].copyWith(user: user);
|
||||
final updatedState = ChannelState(members: [...members]);
|
||||
channel.state!.updateChannelState(updatedState);
|
||||
|
||||
return channel;
|
||||
});
|
||||
|
||||
controller.channels = [...updatedChannels];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ export 'src/user_item.dart';
|
||||
export 'src/user_list_view.dart';
|
||||
export 'src/user_mention_tile.dart';
|
||||
export 'src/utils.dart';
|
||||
export 'src/v4/channel_list_view/channel_event_handlers.dart';
|
||||
export 'src/v4/channel_list_view/stream_channel_list_controller.dart';
|
||||
export 'src/v4/channel_list_view/stream_channel_list_event_handler.dart';
|
||||
export 'src/v4/channel_list_view/stream_channel_list_view.dart';
|
||||
export 'src/visible_footnote.dart';
|
||||
|
||||
Reference in New Issue
Block a user