[core]: move .of calls to didChangeDependencies

This commit is contained in:
Salvatore Giordano
2021-05-06 13:25:41 +02:00
parent b3ec54ae48
commit 9392d150c0
9 changed files with 170 additions and 120 deletions
@@ -119,12 +119,11 @@ class ChannelListCore extends StatefulWidget {
/// The current state of the [ChannelListCore].
class ChannelListCoreState extends State<ChannelListCore> {
@override
Widget build(BuildContext context) {
final channelsBloc = ChannelsBloc.of(context);
late final ChannelsBlocState _channelsBloc;
late final StreamChatCoreState _streamChatCoreState;
return _buildListView(channelsBloc);
}
@override
Widget build(BuildContext context) => _buildListView(_channelsBloc);
StreamBuilder<List<Channel>> _buildListView(
ChannelsBlocState channelsBlocState,
@@ -147,36 +146,42 @@ class ChannelListCoreState extends State<ChannelListCore> {
);
/// Fetches initial channels and updates the widget
Future<void> loadData() {
final channelsBloc = ChannelsBloc.of(context);
return channelsBloc.queryChannels(
filter: widget.filter,
sortOptions: widget.sort,
paginationParams: widget.pagination,
options: widget.options,
);
}
Future<void> loadData() => _channelsBloc.queryChannels(
filter: widget.filter,
sortOptions: widget.sort,
paginationParams: widget.pagination,
options: widget.options,
);
/// Fetches more channels with updated pagination and updates the widget
Future<void> paginateData() {
final channelsBloc = ChannelsBloc.of(context);
return channelsBloc.queryChannels(
filter: widget.filter,
sortOptions: widget.sort,
paginationParams: widget.pagination.copyWith(
offset: channelsBloc.channels?.length ?? 0,
),
options: widget.options,
);
}
Future<void> paginateData() => _channelsBloc.queryChannels(
filter: widget.filter,
sortOptions: widget.sort,
paginationParams: widget.pagination.copyWith(
offset: _channelsBloc.channels?.length ?? 0,
),
options: widget.options,
);
late StreamSubscription<Event> _subscription;
StreamSubscription<Event>? _subscription;
@override
void initState() {
super.initState();
loadData();
final client = StreamChatCore.of(context).client;
_setupController();
}
@override
void didChangeDependencies() {
_channelsBloc = ChannelsBloc.of(context);
_streamChatCoreState = StreamChatCore.of(context);
if (_subscription == null) {
loadData();
}
final client = _streamChatCoreState.client;
_subscription?.cancel();
_subscription = client
.on(
EventType.connectionRecovered,
@@ -186,10 +191,7 @@ class ChannelListCoreState extends State<ChannelListCore> {
)
.listen((event) => loadData());
if (widget.channelListController != null) {
widget.channelListController!.loadData = loadData;
widget.channelListController!.paginateData = paginateData;
}
super.didChangeDependencies();
}
@override
@@ -203,11 +205,22 @@ class ChannelListCoreState extends State<ChannelListCore> {
oldWidget.pagination.toJson().toString()) {
loadData();
}
if (widget.channelListController != oldWidget.channelListController) {
_setupController();
}
}
void _setupController() {
if (widget.channelListController != null) {
widget.channelListController!.loadData = loadData;
widget.channelListController!.paginateData = paginateData;
}
}
@override
void dispose() {
_subscription.cancel();
_subscription?.cancel();
super.dispose();
}
}
@@ -50,17 +50,20 @@ class ChannelsBloc extends StatefulWidget {
streamChatState = context.findAncestorStateOfType<ChannelsBlocState>();
if (streamChatState == null) {
throw Exception('You must have a ChannelsBloc widget as ancestor');
}
assert(
streamChatState != null,
'You must have a ChannelsBloc widget as ancestor',
);
return streamChatState;
return streamChatState!;
}
}
/// The current state of the [ChannelsBloc].
class ChannelsBlocState extends State<ChannelsBloc>
with AutomaticKeepAliveClientMixin {
late final StreamChatCoreState _streamChatCoreState;
@override
Widget build(BuildContext context) {
super.build(context);
@@ -86,6 +89,8 @@ class ChannelsBlocState extends State<ChannelsBloc>
bool _paginationEnded = false;
final List<StreamSubscription> _subscriptions = [];
/// Calls [client.queryChannels] updating [queryChannelsLoading] stream
Future<void> queryChannels({
Filter? filter,
@@ -93,7 +98,7 @@ class ChannelsBlocState extends State<ChannelsBloc>
PaginationParams paginationParams = const PaginationParams(limit: 30),
Map<String, dynamic>? options,
}) async {
final client = StreamChatCore.of(context).client;
final client = _streamChatCoreState.client;
final clear = paginationParams.offset == 0;
@@ -139,14 +144,12 @@ class ChannelsBlocState extends State<ChannelsBloc>
}
}
final List<StreamSubscription> _subscriptions = [];
@override
void initState() {
super.initState();
final client = StreamChatCore.of(context).client;
void didChangeDependencies() {
_streamChatCoreState = StreamChatCore.of(context);
final client = _streamChatCoreState.client;
_cancelSubscriptions();
if (!widget.lockChannelsOrder) {
_subscriptions.add(client
.on(
@@ -179,37 +182,44 @@ class ChannelsBlocState extends State<ChannelsBloc>
}));
}
_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);
}
}));
// ignore: cascade_invocations
_subscriptions.add(client
.on(
EventType.channelDeleted,
EventType.notificationRemovedFromChannel,
)
.listen((e) {
// ignore: cascade_invocations
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();
}
@override
void dispose() {
_channelsController.close();
_queryChannelsLoadingController.close();
_subscriptions.forEach((s) => s.cancel());
_cancelSubscriptions();
super.dispose();
}
void _cancelSubscriptions() {
_subscriptions
..forEach((s) => s.cancel())
..clear();
}
@override
bool get wantKeepAlive => true;
}
@@ -109,7 +109,7 @@ class MessageListCore extends StatefulWidget {
/// The current state of the [MessageListCore].
class MessageListCoreState extends State<MessageListCore> {
late StreamChannelState _streamChannel;
late final StreamChannelState _streamChannel;
bool get _upToDate => _streamChannel.channel.state?.isUpToDate ?? true;
@@ -174,18 +174,34 @@ class MessageListCoreState extends State<MessageListCore> {
}
@override
void initState() {
void didChangeDependencies() {
_streamChannel = StreamChannel.of(context);
if (_isThreadConversation) {
_streamChannel.getReplies(widget.parentMessage!.id);
}
super.didChangeDependencies();
}
@override
void didUpdateWidget(covariant MessageListCore oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.messageListController != oldWidget.messageListController) {
_setupController();
}
}
@override
void initState() {
_setupController();
super.initState();
}
void _setupController() {
if (widget.messageListController != null) {
widget.messageListController!.paginateData = paginateData;
}
super.initState();
}
@override
@@ -29,17 +29,20 @@ class MessageSearchBloc extends StatefulWidget {
state = context.findAncestorStateOfType<MessageSearchBlocState>();
if (state == null) {
throw Exception('You must have a MessageSearchBloc widget as ancestor');
}
assert(
state != null,
'You must have a MessageSearchBloc widget as ancestor',
);
return state;
return state!;
}
}
/// The current state of the [MessageSearchBloc]
class MessageSearchBlocState extends State<MessageSearchBloc>
with AutomaticKeepAliveClientMixin {
late final StreamChatCoreState _streamChatCoreState;
/// The current messages list
List<GetMessageResponse>? get messageResponses => _messageResponses.value;
@@ -64,7 +67,7 @@ class MessageSearchBlocState extends State<MessageSearchBloc>
String? query,
PaginationParams? pagination,
}) async {
final client = StreamChatCore.of(context).client;
final client = _streamChatCoreState.client;
if (_queryMessagesLoadingController.value == true) return;
@@ -109,6 +112,12 @@ class MessageSearchBlocState extends State<MessageSearchBloc>
return widget.child;
}
@override
void didChangeDependencies() {
_streamChatCoreState = StreamChatCore.of(context);
super.didChangeDependencies();
}
@override
void dispose() {
_messageResponses.close();
@@ -105,21 +105,21 @@ class MessageSearchListCore extends StatefulWidget {
/// The current state of the [MessageSearchListCore].
class MessageSearchListCoreState extends State<MessageSearchListCore> {
late final MessageSearchBlocState messageSearchBloc;
@override
void didChangeDependencies() {
super.didChangeDependencies();
messageSearchBloc = MessageSearchBloc.of(context);
loadData();
if (widget.messageSearchListController != null) {
widget.messageSearchListController!.loadData = loadData;
widget.messageSearchListController!.paginateData = paginateData;
}
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
final messageSearchBloc = MessageSearchBloc.of(context);
return _buildListView(messageSearchBloc);
}
Widget build(BuildContext context) => _buildListView(messageSearchBloc);
Widget _buildListView(MessageSearchBlocState messageSearchBloc) =>
StreamBuilder<List<GetMessageResponse>>(
@@ -140,30 +140,24 @@ class MessageSearchListCoreState extends State<MessageSearchListCore> {
);
/// Fetches initial messages and updates the widget
Future<void> loadData() {
final messageSearchBloc = MessageSearchBloc.of(context);
return messageSearchBloc.search(
filter: widget.filters,
sort: widget.sortOptions,
query: widget.messageQuery,
pagination: widget.paginationParams,
messageFilter: widget.messageFilters,
);
}
Future<void> loadData() => messageSearchBloc.search(
filter: widget.filters,
sort: widget.sortOptions,
query: widget.messageQuery,
pagination: widget.paginationParams,
messageFilter: widget.messageFilters,
);
/// Fetches more messages with updated pagination and updates the widget
Future<void> paginateData() {
final messageSearchBloc = MessageSearchBloc.of(context);
return messageSearchBloc.search(
filter: widget.filters,
sort: widget.sortOptions,
pagination: widget.paginationParams!.copyWith(
offset: messageSearchBloc.messageResponses?.length ?? 0,
),
query: widget.messageQuery,
messageFilter: widget.messageFilters,
);
}
Future<void> paginateData() => messageSearchBloc.search(
filter: widget.filters,
sort: widget.sortOptions,
pagination: widget.paginationParams!.copyWith(
offset: messageSearchBloc.messageResponses?.length ?? 0,
),
query: widget.messageQuery,
messageFilter: widget.messageFilters,
);
@override
void didUpdateWidget(MessageSearchListCore oldWidget) {
@@ -47,13 +47,12 @@ class StreamChannel extends StatefulWidget {
streamChannelState = context.findAncestorStateOfType<StreamChannelState>();
if (streamChannelState == null) {
throw Exception(
'You must have a StreamChannel widget at the top of your widget tree',
);
}
assert(
streamChannelState != null,
'You must have a StreamChannel widget at the top of your widget tree',
);
return streamChannelState;
return streamChannelState!;
}
@override
@@ -70,12 +70,12 @@ class StreamChatCore extends StatefulWidget {
streamChatState = context.findAncestorStateOfType<StreamChatCoreState>();
if (streamChatState == null) {
throw Exception(
'You must have a StreamChat widget at the top of your widget tree');
}
assert(
streamChatState != null,
'You must have a StreamChat widget at the top of your widget tree',
);
return streamChatState;
return streamChatState!;
}
}
@@ -124,12 +124,12 @@ class UserListCoreState extends State<UserListCore>
with WidgetsBindingObserver {
@override
void didChangeDependencies() {
super.didChangeDependencies();
loadData();
if (widget.userListController != null) {
widget.userListController!.loadData = loadData;
widget.userListController!.paginateData = paginateData;
}
super.didChangeDependencies();
}
@override
@@ -30,11 +30,12 @@ class UsersBloc extends StatefulWidget {
state = context.findAncestorStateOfType<UsersBlocState>();
if (state == null) {
throw Exception('You must have a UsersBloc widget as ancestor');
}
assert(
state != null,
'You must have a UsersBloc widget as ancestor',
);
return state;
return state!;
}
}
@@ -54,6 +55,8 @@ class UsersBlocState extends State<UsersBloc>
/// The stream notifying the state of queryUsers call
Stream<bool> get queryUsersLoading => _queryUsersLoadingController.stream;
late final StreamChatCoreState _streamChatCore;
/// The Query Users method allows you to search for users and see if they are
/// online/offline.
/// [API Reference](https://getstream.io/chat/docs/flutter-dart/query_users/?language=dart)
@@ -63,7 +66,7 @@ class UsersBlocState extends State<UsersBloc>
Map<String, dynamic>? options,
PaginationParams? pagination,
}) async {
final client = StreamChatCore.of(context).client;
final client = _streamChatCore.client;
if (_queryUsersLoadingController.value == true) return;
@@ -101,6 +104,12 @@ class UsersBlocState extends State<UsersBloc>
}
}
@override
void didChangeDependencies() {
_streamChatCore = StreamChatCore.of(context);
super.didChangeDependencies();
}
@override
Widget build(BuildContext context) {
super.build(context);