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 =
widget.itemPositionListener ?? ItemPositionsListener.create();
streamChannel = StreamChannel.of(context);
initialIndex = _initialIndex;
initialAlignment = _initialAlignment;
@@ -1100,25 +1098,27 @@ class _MessageListViewState extends State<MessageListView> {
void didChangeDependencies() {
streamChannel = StreamChannel.of(context);
_messageNewListener?.cancel();
_messageNewListener =
streamChannel.channel.on(EventType.messageNew).listen((event) {
if (_upToDate) {
_bottomPaginationActive = false;
_topPaginationActive = false;
}
if (event.message!.user!.id ==
streamChannel.channel.client.state.user!.id) {
WidgetsBinding.instance!.addPostFrameCallback((_) {
_scrollController?.jumpTo(
index: 0,
);
});
}
});
if (_messageNewListener == null) {
_messageNewListener?.cancel();
_messageNewListener =
streamChannel.channel.on(EventType.messageNew).listen((event) {
if (_upToDate) {
_bottomPaginationActive = false;
_topPaginationActive = false;
}
if (event.message!.user!.id ==
streamChannel.channel.client.state.user!.id) {
WidgetsBinding.instance!.addPostFrameCallback((_) {
_scrollController?.jumpTo(
index: 0,
);
});
}
});
if (_isThreadConversation) {
streamChannel.getReplies(widget.parentMessage!.id);
if (_isThreadConversation) {
streamChannel.getReplies(widget.parentMessage!.id);
}
}
super.didChangeDependencies();
}
@@ -178,19 +178,17 @@ class ChannelListCoreState extends State<ChannelListCore> {
if (_subscription == null) {
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();
}
@@ -149,59 +149,62 @@ class ChannelsBlocState extends State<ChannelsBloc>
_streamChatCoreState = StreamChatCore.of(context);
final client = _streamChatCoreState.client;
_cancelSubscriptions();
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 != -1) {
if (index > 0) {
final channel = newChannels.removeAt(index);
newChannels.insert(0, channel);
}
} else if (widget.shouldAddChannel?.call(e) == true) {
final hiddenIndex = _hiddenChannels.indexWhere((c) => c.cid == e.cid);
if (hiddenIndex != -1) {
newChannels.insert(0, _hiddenChannels[hiddenIndex]);
_hiddenChannels.removeAt(hiddenIndex);
} else {
if (client.state.channels[e.cid] != null) {
newChannels.insert(0, client.state.channels[e.cid]!);
if (_subscriptions.isEmpty) {
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 != -1) {
if (index > 0) {
final channel = newChannels.removeAt(index);
newChannels.insert(0, channel);
}
} else if (widget.shouldAddChannel?.call(e) == true) {
final hiddenIndex =
_hiddenChannels.indexWhere((c) => c.cid == e.cid);
if (hiddenIndex != -1) {
newChannels.insert(0, _hiddenChannels[hiddenIndex]);
_hiddenChannels.removeAt(hiddenIndex);
} else {
if (client.state.channels[e.cid] != null) {
newChannels.insert(0, client.state.channels[e.cid]!);
}
}
}
}
if (widget.channelsComparator != null) {
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);
if (widget.channelsComparator != null) {
newChannels.sort(widget.channelsComparator);
}
_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)));
}));
}));
}
_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);
}
}))
..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();
}
@@ -173,10 +173,13 @@ class MessageListCoreState extends State<MessageListCore> {
}
}
var _initialized = false;
@override
void didChangeDependencies() {
_streamChannel = StreamChannel.of(context);
if (_isThreadConversation) {
if (!_initialized && _isThreadConversation) {
_initialized = true;
_streamChannel.getReplies(widget.parentMessage!.id);
}
super.didChangeDependencies();
@@ -107,10 +107,17 @@ class MessageSearchListCore extends StatefulWidget {
class MessageSearchListCoreState extends State<MessageSearchListCore> {
late MessageSearchBlocState _messageSearchBloc;
var _initialized = false;
@override
void didChangeDependencies() {
_messageSearchBloc = MessageSearchBloc.of(context);
loadData();
if (!_initialized) {
loadData();
_initialized = true;
}
if (widget.messageSearchListController != null) {
widget.messageSearchListController!.loadData = loadData;
widget.messageSearchListController!.paginateData = paginateData;
@@ -122,9 +122,14 @@ class UserListCore extends StatefulWidget {
/// The current state of the [UserListCore].
class UserListCoreState extends State<UserListCore>
with WidgetsBindingObserver {
var _initialized = false;
@override
void didChangeDependencies() {
loadData();
if (!_initialized) {
loadData();
_initialized = true;
}
if (widget.userListController != null) {
widget.userListController!.loadData = loadData;
widget.userListController!.paginateData = paginateData;