perform heavy calls only first time

This commit is contained in:
Salvatore Giordano
2021-05-11 15:21:30 +02:00
parent 435a88cd1b
commit 5c04492176
6 changed files with 99 additions and 83 deletions
@@ -1087,8 +1087,6 @@ class _MessageListViewState extends State<MessageListView> {
_itemPositionListener = _itemPositionListener =
widget.itemPositionListener ?? ItemPositionsListener.create(); widget.itemPositionListener ?? ItemPositionsListener.create();
streamChannel = StreamChannel.of(context);
initialIndex = _initialIndex; initialIndex = _initialIndex;
initialAlignment = _initialAlignment; initialAlignment = _initialAlignment;
@@ -1100,25 +1098,27 @@ class _MessageListViewState extends State<MessageListView> {
void didChangeDependencies() { void didChangeDependencies() {
streamChannel = StreamChannel.of(context); streamChannel = StreamChannel.of(context);
_messageNewListener?.cancel(); if (_messageNewListener == null) {
_messageNewListener = _messageNewListener?.cancel();
streamChannel.channel.on(EventType.messageNew).listen((event) { _messageNewListener =
if (_upToDate) { streamChannel.channel.on(EventType.messageNew).listen((event) {
_bottomPaginationActive = false; if (_upToDate) {
_topPaginationActive = false; _bottomPaginationActive = false;
} _topPaginationActive = false;
if (event.message!.user!.id == }
streamChannel.channel.client.state.user!.id) { if (event.message!.user!.id ==
WidgetsBinding.instance!.addPostFrameCallback((_) { streamChannel.channel.client.state.user!.id) {
_scrollController?.jumpTo( WidgetsBinding.instance!.addPostFrameCallback((_) {
index: 0, _scrollController?.jumpTo(
); index: 0,
}); );
} });
}); }
});
if (_isThreadConversation) { if (_isThreadConversation) {
streamChannel.getReplies(widget.parentMessage!.id); streamChannel.getReplies(widget.parentMessage!.id);
}
} }
super.didChangeDependencies(); super.didChangeDependencies();
} }
@@ -178,19 +178,17 @@ class ChannelListCoreState extends State<ChannelListCore> {
if (_subscription == null) { if (_subscription == null) {
loadData(); loadData();
final client = _streamChatCoreState.client;
_subscription = client
.on(
EventType.connectionRecovered,
EventType.notificationAddedToChannel,
EventType.notificationMessageNew,
EventType.channelVisible,
)
.listen((event) => loadData());
} }
final client = _streamChatCoreState.client;
_subscription?.cancel();
_subscription = client
.on(
EventType.connectionRecovered,
EventType.notificationAddedToChannel,
EventType.notificationMessageNew,
EventType.channelVisible,
)
.listen((event) => loadData());
super.didChangeDependencies(); super.didChangeDependencies();
} }
@@ -149,59 +149,62 @@ class ChannelsBlocState extends State<ChannelsBloc>
_streamChatCoreState = StreamChatCore.of(context); _streamChatCoreState = StreamChatCore.of(context);
final client = _streamChatCoreState.client; final client = _streamChatCoreState.client;
_cancelSubscriptions(); if (_subscriptions.isEmpty) {
if (!widget.lockChannelsOrder) { if (!widget.lockChannelsOrder) {
_subscriptions.add(client _subscriptions.add(client
.on( .on(
EventType.messageNew, EventType.messageNew,
) )
.listen((e) { .listen((e) {
final newChannels = List<Channel>.from(channels ?? []); final newChannels = List<Channel>.from(channels ?? []);
final index = newChannels.indexWhere((c) => c.cid == e.cid); final index = newChannels.indexWhere((c) => c.cid == e.cid);
if (index != -1) { if (index != -1) {
if (index > 0) { if (index > 0) {
final channel = newChannels.removeAt(index); final channel = newChannels.removeAt(index);
newChannels.insert(0, channel); newChannels.insert(0, channel);
} }
} else if (widget.shouldAddChannel?.call(e) == true) { } else if (widget.shouldAddChannel?.call(e) == true) {
final hiddenIndex = _hiddenChannels.indexWhere((c) => c.cid == e.cid); final hiddenIndex =
if (hiddenIndex != -1) { _hiddenChannels.indexWhere((c) => c.cid == e.cid);
newChannels.insert(0, _hiddenChannels[hiddenIndex]); if (hiddenIndex != -1) {
_hiddenChannels.removeAt(hiddenIndex); newChannels.insert(0, _hiddenChannels[hiddenIndex]);
} else { _hiddenChannels.removeAt(hiddenIndex);
if (client.state.channels[e.cid] != null) { } else {
newChannels.insert(0, client.state.channels[e.cid]!); if (client.state.channels[e.cid] != null) {
newChannels.insert(0, client.state.channels[e.cid]!);
}
} }
} }
}
if (widget.channelsComparator != null) { if (widget.channelsComparator != null) {
newChannels.sort(widget.channelsComparator); newChannels.sort(widget.channelsComparator);
} }
_channelsController.add(newChannels);
}));
}
_subscriptions
..add(client.on(EventType.channelHidden).listen((event) async {
final newChannels = List<Channel>.from(channels ?? []);
final channelIndex = newChannels.indexWhere((c) => c.cid == event.cid);
if (channelIndex > -1) {
final channel = newChannels.removeAt(channelIndex);
_hiddenChannels.add(channel);
_channelsController.add(newChannels); _channelsController.add(newChannels);
} }));
})) }
..add(client
.on( _subscriptions
EventType.channelDeleted, ..add(client.on(EventType.channelHidden).listen((event) async {
EventType.notificationRemovedFromChannel, final newChannels = List<Channel>.from(channels ?? []);
) final channelIndex =
.listen((e) { newChannels.indexWhere((c) => c.cid == event.cid);
final channel = e.channel; if (channelIndex > -1) {
_channelsController.add(List.from( final channel = newChannels.removeAt(channelIndex);
(channels ?? [])..removeWhere((c) => c.cid == channel?.cid))); _hiddenChannels.add(channel);
})); _channelsController.add(newChannels);
}
}))
..add(client
.on(
EventType.channelDeleted,
EventType.notificationRemovedFromChannel,
)
.listen((e) {
final channel = e.channel;
_channelsController.add(List.from(
(channels ?? [])..removeWhere((c) => c.cid == channel?.cid)));
}));
}
super.didChangeDependencies(); super.didChangeDependencies();
} }
@@ -173,10 +173,13 @@ class MessageListCoreState extends State<MessageListCore> {
} }
} }
var _initialized = false;
@override @override
void didChangeDependencies() { void didChangeDependencies() {
_streamChannel = StreamChannel.of(context); _streamChannel = StreamChannel.of(context);
if (_isThreadConversation) { if (!_initialized && _isThreadConversation) {
_initialized = true;
_streamChannel.getReplies(widget.parentMessage!.id); _streamChannel.getReplies(widget.parentMessage!.id);
} }
super.didChangeDependencies(); super.didChangeDependencies();
@@ -107,10 +107,17 @@ class MessageSearchListCore extends StatefulWidget {
class MessageSearchListCoreState extends State<MessageSearchListCore> { class MessageSearchListCoreState extends State<MessageSearchListCore> {
late MessageSearchBlocState _messageSearchBloc; late MessageSearchBlocState _messageSearchBloc;
var _initialized = false;
@override @override
void didChangeDependencies() { void didChangeDependencies() {
_messageSearchBloc = MessageSearchBloc.of(context); _messageSearchBloc = MessageSearchBloc.of(context);
loadData();
if (!_initialized) {
loadData();
_initialized = true;
}
if (widget.messageSearchListController != null) { if (widget.messageSearchListController != null) {
widget.messageSearchListController!.loadData = loadData; widget.messageSearchListController!.loadData = loadData;
widget.messageSearchListController!.paginateData = paginateData; widget.messageSearchListController!.paginateData = paginateData;
@@ -122,9 +122,14 @@ class UserListCore extends StatefulWidget {
/// The current state of the [UserListCore]. /// The current state of the [UserListCore].
class UserListCoreState extends State<UserListCore> class UserListCoreState extends State<UserListCore>
with WidgetsBindingObserver { with WidgetsBindingObserver {
var _initialized = false;
@override @override
void didChangeDependencies() { void didChangeDependencies() {
loadData(); if (!_initialized) {
loadData();
_initialized = true;
}
if (widget.userListController != null) { if (widget.userListController != null) {
widget.userListController!.loadData = loadData; widget.userListController!.loadData = loadData;
widget.userListController!.paginateData = paginateData; widget.userListController!.paginateData = paginateData;