From 9392d150c068b44148f13695d2729357db1004e6 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Thu, 6 May 2021 13:25:41 +0200 Subject: [PATCH] [core]: move .of calls to didChangeDependencies --- .../lib/src/channel_list_core.dart | 79 +++++++++++-------- .../lib/src/channels_bloc.dart | 76 ++++++++++-------- .../lib/src/message_list_core.dart | 26 ++++-- .../lib/src/message_search_bloc.dart | 19 +++-- .../lib/src/message_search_list_core.dart | 48 +++++------ .../lib/src/stream_channel.dart | 11 ++- .../lib/src/stream_chat_core.dart | 10 +-- .../lib/src/user_list_core.dart | 2 +- .../lib/src/users_bloc.dart | 19 +++-- 9 files changed, 170 insertions(+), 120 deletions(-) diff --git a/packages/stream_chat_flutter_core/lib/src/channel_list_core.dart b/packages/stream_chat_flutter_core/lib/src/channel_list_core.dart index 8ead2637..e327f0ba 100644 --- a/packages/stream_chat_flutter_core/lib/src/channel_list_core.dart +++ b/packages/stream_chat_flutter_core/lib/src/channel_list_core.dart @@ -119,12 +119,11 @@ class ChannelListCore extends StatefulWidget { /// The current state of the [ChannelListCore]. class ChannelListCoreState extends State { - @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> _buildListView( ChannelsBlocState channelsBlocState, @@ -147,36 +146,42 @@ class ChannelListCoreState extends State { ); /// Fetches initial channels and updates the widget - Future loadData() { - final channelsBloc = ChannelsBloc.of(context); - return channelsBloc.queryChannels( - filter: widget.filter, - sortOptions: widget.sort, - paginationParams: widget.pagination, - options: widget.options, - ); - } + Future 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 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 paginateData() => _channelsBloc.queryChannels( + filter: widget.filter, + sortOptions: widget.sort, + paginationParams: widget.pagination.copyWith( + offset: _channelsBloc.channels?.length ?? 0, + ), + options: widget.options, + ); - late StreamSubscription _subscription; + StreamSubscription? _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 { ) .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 { 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(); } } diff --git a/packages/stream_chat_flutter_core/lib/src/channels_bloc.dart b/packages/stream_chat_flutter_core/lib/src/channels_bloc.dart index ce17235a..966a3fe5 100644 --- a/packages/stream_chat_flutter_core/lib/src/channels_bloc.dart +++ b/packages/stream_chat_flutter_core/lib/src/channels_bloc.dart @@ -50,17 +50,20 @@ class ChannelsBloc extends StatefulWidget { streamChatState = context.findAncestorStateOfType(); - 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 with AutomaticKeepAliveClientMixin { + late final StreamChatCoreState _streamChatCoreState; + @override Widget build(BuildContext context) { super.build(context); @@ -86,6 +89,8 @@ class ChannelsBlocState extends State bool _paginationEnded = false; + final List _subscriptions = []; + /// Calls [client.queryChannels] updating [queryChannelsLoading] stream Future queryChannels({ Filter? filter, @@ -93,7 +98,7 @@ class ChannelsBlocState extends State PaginationParams paginationParams = const PaginationParams(limit: 30), Map? 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 } } - final List _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 })); } - _subscriptions.add(client.on(EventType.channelHidden).listen((event) async { - final newChannels = List.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.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; } diff --git a/packages/stream_chat_flutter_core/lib/src/message_list_core.dart b/packages/stream_chat_flutter_core/lib/src/message_list_core.dart index e1fed859..74c6c1ea 100644 --- a/packages/stream_chat_flutter_core/lib/src/message_list_core.dart +++ b/packages/stream_chat_flutter_core/lib/src/message_list_core.dart @@ -109,7 +109,7 @@ class MessageListCore extends StatefulWidget { /// The current state of the [MessageListCore]. class MessageListCoreState extends State { - late StreamChannelState _streamChannel; + late final StreamChannelState _streamChannel; bool get _upToDate => _streamChannel.channel.state?.isUpToDate ?? true; @@ -174,18 +174,34 @@ class MessageListCoreState extends State { } @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 diff --git a/packages/stream_chat_flutter_core/lib/src/message_search_bloc.dart b/packages/stream_chat_flutter_core/lib/src/message_search_bloc.dart index 6c9be4f0..c98c470e 100644 --- a/packages/stream_chat_flutter_core/lib/src/message_search_bloc.dart +++ b/packages/stream_chat_flutter_core/lib/src/message_search_bloc.dart @@ -29,17 +29,20 @@ class MessageSearchBloc extends StatefulWidget { state = context.findAncestorStateOfType(); - 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 with AutomaticKeepAliveClientMixin { + late final StreamChatCoreState _streamChatCoreState; + /// The current messages list List? get messageResponses => _messageResponses.value; @@ -64,7 +67,7 @@ class MessageSearchBlocState extends State 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 return widget.child; } + @override + void didChangeDependencies() { + _streamChatCoreState = StreamChatCore.of(context); + super.didChangeDependencies(); + } + @override void dispose() { _messageResponses.close(); diff --git a/packages/stream_chat_flutter_core/lib/src/message_search_list_core.dart b/packages/stream_chat_flutter_core/lib/src/message_search_list_core.dart index cb8c9387..5c661ca1 100644 --- a/packages/stream_chat_flutter_core/lib/src/message_search_list_core.dart +++ b/packages/stream_chat_flutter_core/lib/src/message_search_list_core.dart @@ -105,21 +105,21 @@ class MessageSearchListCore extends StatefulWidget { /// The current state of the [MessageSearchListCore]. class MessageSearchListCoreState extends State { + 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>( @@ -140,30 +140,24 @@ class MessageSearchListCoreState extends State { ); /// Fetches initial messages and updates the widget - Future 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 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 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 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) { diff --git a/packages/stream_chat_flutter_core/lib/src/stream_channel.dart b/packages/stream_chat_flutter_core/lib/src/stream_channel.dart index a6fd83b2..2760d9cc 100644 --- a/packages/stream_chat_flutter_core/lib/src/stream_channel.dart +++ b/packages/stream_chat_flutter_core/lib/src/stream_channel.dart @@ -47,13 +47,12 @@ class StreamChannel extends StatefulWidget { streamChannelState = context.findAncestorStateOfType(); - 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 diff --git a/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart b/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart index a72425ed..89d4853a 100644 --- a/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart +++ b/packages/stream_chat_flutter_core/lib/src/stream_chat_core.dart @@ -70,12 +70,12 @@ class StreamChatCore extends StatefulWidget { streamChatState = context.findAncestorStateOfType(); - 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!; } } diff --git a/packages/stream_chat_flutter_core/lib/src/user_list_core.dart b/packages/stream_chat_flutter_core/lib/src/user_list_core.dart index 694c52bc..d608ddc4 100644 --- a/packages/stream_chat_flutter_core/lib/src/user_list_core.dart +++ b/packages/stream_chat_flutter_core/lib/src/user_list_core.dart @@ -124,12 +124,12 @@ class UserListCoreState extends State with WidgetsBindingObserver { @override void didChangeDependencies() { - super.didChangeDependencies(); loadData(); if (widget.userListController != null) { widget.userListController!.loadData = loadData; widget.userListController!.paginateData = paginateData; } + super.didChangeDependencies(); } @override diff --git a/packages/stream_chat_flutter_core/lib/src/users_bloc.dart b/packages/stream_chat_flutter_core/lib/src/users_bloc.dart index 9b33f75c..f19efa8d 100644 --- a/packages/stream_chat_flutter_core/lib/src/users_bloc.dart +++ b/packages/stream_chat_flutter_core/lib/src/users_bloc.dart @@ -30,11 +30,12 @@ class UsersBloc extends StatefulWidget { state = context.findAncestorStateOfType(); - 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 /// The stream notifying the state of queryUsers call Stream 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 Map? 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 } } + @override + void didChangeDependencies() { + _streamChatCore = StreamChatCore.of(context); + super.didChangeDependencies(); + } + @override Widget build(BuildContext context) { super.build(context);