update didchangedependencies

This commit is contained in:
Salvatore Giordano
2021-05-12 11:50:06 +02:00
parent 19ce7ff756
commit a7f00b3d85
6 changed files with 88 additions and 102 deletions
@@ -274,15 +274,15 @@ class _MessageListViewState extends State<MessageListView> {
bool _showScrollToBottom = false; bool _showScrollToBottom = false;
late final ItemPositionsListener _itemPositionListener; late final ItemPositionsListener _itemPositionListener;
int? _messageListLength; int? _messageListLength;
late StreamChannelState streamChannel; StreamChannelState? streamChannel;
int? get _initialIndex { int? get _initialIndex {
if (widget.initialScrollIndex != null) return widget.initialScrollIndex; if (widget.initialScrollIndex != null) return widget.initialScrollIndex;
if (streamChannel.initialMessageId != null) { if (streamChannel!.initialMessageId != null) {
final messages = streamChannel.channel.state!.messages; final messages = streamChannel!.channel.state!.messages;
final totalMessages = messages.length; final totalMessages = messages.length;
final messageIndex = final messageIndex =
messages.indexWhere((e) => e.id == streamChannel.initialMessageId); messages.indexWhere((e) => e.id == streamChannel!.initialMessageId);
final index = totalMessages - messageIndex; final index = totalMessages - messageIndex;
if (index != 0) return index - 1; if (index != 0) return index - 1;
return index; return index;
@@ -295,9 +295,9 @@ class _MessageListViewState extends State<MessageListView> {
return 0; return 0;
} }
bool _isInitialMessage(String id) => streamChannel.initialMessageId == id; bool _isInitialMessage(String id) => streamChannel!.initialMessageId == id;
bool get _upToDate => streamChannel.channel.state!.isUpToDate; bool get _upToDate => streamChannel!.channel.state!.isUpToDate;
bool get _isThreadConversation => widget.parentMessage != null; bool get _isThreadConversation => widget.parentMessage != null;
@@ -506,13 +506,13 @@ class _MessageListViewState extends State<MessageListView> {
} }
if (i == messages.length + 1) { if (i == messages.length + 1) {
return _buildLoadingIndicator( return _buildLoadingIndicator(
streamChannel, streamChannel!,
QueryDirection.top, QueryDirection.top,
); );
} }
if (i == 0) { if (i == 0) {
return _buildLoadingIndicator( return _buildLoadingIndicator(
streamChannel, streamChannel!,
QueryDirection.bottom, QueryDirection.bottom,
); );
} }
@@ -525,7 +525,7 @@ class _MessageListViewState extends State<MessageListView> {
context, context,
message, message,
messages, messages,
streamChannel, streamChannel!,
); );
} else if (i == messages.length - 1) { } else if (i == messages.length - 1) {
messageWidget = _buildTopMessage( messageWidget = _buildTopMessage(
@@ -605,8 +605,8 @@ class _MessageListViewState extends State<MessageListView> {
Widget _buildScrollToBottom() => StreamBuilder<Tuple2<bool, int>>( Widget _buildScrollToBottom() => StreamBuilder<Tuple2<bool, int>>(
stream: Rx.combineLatest2( stream: Rx.combineLatest2(
streamChannel.channel.state!.isUpToDateStream, streamChannel!.channel.state!.isUpToDateStream,
streamChannel.channel.state!.unreadCountStream, streamChannel!.channel.state!.unreadCountStream,
(bool isUpToDate, int unreadCount) => Tuple2(isUpToDate, unreadCount), (bool isUpToDate, int unreadCount) => Tuple2(isUpToDate, unreadCount),
), ),
builder: (_, snapshot) { builder: (_, snapshot) {
@@ -622,8 +622,8 @@ class _MessageListViewState extends State<MessageListView> {
} }
final unreadCount = snapshot.data!.item2; final unreadCount = snapshot.data!.item2;
final showUnreadCount = unreadCount > 0 && final showUnreadCount = unreadCount > 0 &&
streamChannel.channel.state!.members.any((e) => streamChannel!.channel.state!.members.any((e) =>
e.userId == streamChannel.channel.client.state.user!.id); e.userId == streamChannel!.channel.client.state.user!.id);
final chatThemeData = StreamChatTheme.of(context); final chatThemeData = StreamChatTheme.of(context);
return Positioned( return Positioned(
bottom: 8, bottom: 8,
@@ -637,12 +637,12 @@ class _MessageListViewState extends State<MessageListView> {
backgroundColor: chatThemeData.colorTheme.white, backgroundColor: chatThemeData.colorTheme.white,
onPressed: () { onPressed: () {
if (unreadCount > 0) { if (unreadCount > 0) {
streamChannel.channel.markRead(); streamChannel!.channel.markRead();
} }
if (!_upToDate) { if (!_upToDate) {
_bottomPaginationActive = false; _bottomPaginationActive = false;
_topPaginationActive = false; _topPaginationActive = false;
streamChannel.reloadChannel(); streamChannel!.reloadChannel();
} else { } else {
setState(() => _showScrollToBottom = false); setState(() => _showScrollToBottom = false);
_scrollController!.scrollTo( _scrollController!.scrollTo(
@@ -888,7 +888,7 @@ class _MessageListViewState extends State<MessageListView> {
); );
} }
final channel = streamChannel.channel; final channel = streamChannel!.channel;
final readList = channel.state?.read?.where((read) { final readList = channel.state?.read?.where((read) {
if (read.user.id == userId) return false; if (read.user.id == userId) return false;
return read.lastRead.isAfter(message.createdAt) || return read.lastRead.isAfter(message.createdAt) ||
@@ -962,7 +962,7 @@ class _MessageListViewState extends State<MessageListView> {
if (messages.map((e) => e.id).contains(quotedMessageId)) { if (messages.map((e) => e.id).contains(quotedMessageId)) {
scrollToIndex(); scrollToIndex();
} else { } else {
await streamChannel.loadChannelAtMessage(quotedMessageId).then((_) { await streamChannel!.loadChannelAtMessage(quotedMessageId).then((_) {
WidgetsBinding.instance!.addPostFrameCallback((_) { WidgetsBinding.instance!.addPostFrameCallback((_) {
if (messages.map((e) => e.id).contains(quotedMessageId)) { if (messages.map((e) => e.id).contains(quotedMessageId)) {
scrollToIndex(); scrollToIndex();
@@ -1093,20 +1093,22 @@ class _MessageListViewState extends State<MessageListView> {
@override @override
void didChangeDependencies() { void didChangeDependencies() {
streamChannel = StreamChannel.of(context); final newStreamChannel = StreamChannel.of(context);
if (_messageNewListener == null) { if (newStreamChannel != streamChannel) {
streamChannel = newStreamChannel;
_messageNewListener?.cancel();
initialIndex = _initialIndex; initialIndex = _initialIndex;
initialAlignment = _initialAlignment; initialAlignment = _initialAlignment;
_messageNewListener = _messageNewListener =
streamChannel.channel.on(EventType.messageNew).listen((event) { streamChannel!.channel.on(EventType.messageNew).listen((event) {
if (_upToDate) { if (_upToDate) {
_bottomPaginationActive = false; _bottomPaginationActive = false;
_topPaginationActive = false; _topPaginationActive = false;
} }
if (event.message!.user!.id == if (event.message!.user!.id ==
streamChannel.channel.client.state.user!.id) { streamChannel!.channel.client.state.user!.id) {
WidgetsBinding.instance!.addPostFrameCallback((_) { WidgetsBinding.instance!.addPostFrameCallback((_) {
_scrollController?.jumpTo( _scrollController?.jumpTo(
index: 0, index: 0,
@@ -1116,9 +1118,10 @@ class _MessageListViewState extends State<MessageListView> {
}); });
if (_isThreadConversation) { if (_isThreadConversation) {
streamChannel.getReplies(widget.parentMessage!.id); streamChannel!.getReplies(widget.parentMessage!.id);
} }
} }
super.didChangeDependencies(); super.didChangeDependencies();
} }
@@ -1137,12 +1140,12 @@ class _MessageListViewState extends State<MessageListView> {
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (_) => StreamBuilder<Message>( builder: (_) => StreamBuilder<Message>(
stream: streamChannel.channel.state!.messagesStream.map( stream: streamChannel!.channel.state!.messagesStream.map(
(messages) => (messages) =>
messages!.firstWhere((m) => m.id == message.id)), messages!.firstWhere((m) => m.id == message.id)),
initialData: message, initialData: message,
builder: (_, snapshot) => StreamChannel( builder: (_, snapshot) => StreamChannel(
channel: streamChannel.channel, channel: streamChannel!.channel,
child: widget.threadBuilder!(context, snapshot.data), child: widget.threadBuilder!(context, snapshot.data),
), ),
), ),
@@ -1155,7 +1158,7 @@ class _MessageListViewState extends State<MessageListView> {
@override @override
void dispose() { void dispose() {
if (!_upToDate) { if (!_upToDate) {
streamChannel.reloadChannel(); streamChannel!.reloadChannel();
} }
_messageNewListener?.cancel(); _messageNewListener?.cancel();
super.dispose(); super.dispose();
@@ -120,7 +120,7 @@ class ChannelListCore extends StatefulWidget {
/// The current state of the [ChannelListCore]. /// The current state of the [ChannelListCore].
class ChannelListCoreState extends State<ChannelListCore> { class ChannelListCoreState extends State<ChannelListCore> {
late ChannelsBlocState _channelsBloc; late ChannelsBlocState _channelsBloc;
late StreamChatCoreState _streamChatCoreState; StreamChatCoreState? _streamChatCoreState;
@override @override
Widget build(BuildContext context) => _buildListView(_channelsBloc); Widget build(BuildContext context) => _buildListView(_channelsBloc);
@@ -174,11 +174,13 @@ class ChannelListCoreState extends State<ChannelListCore> {
@override @override
void didChangeDependencies() { void didChangeDependencies() {
_channelsBloc = ChannelsBloc.of(context); _channelsBloc = ChannelsBloc.of(context);
_streamChatCoreState = StreamChatCore.of(context); final newStreamChatCoreState = StreamChatCore.of(context);
if (_subscription == null) { if (newStreamChatCoreState != _streamChatCoreState) {
_streamChatCoreState = newStreamChatCoreState;
loadData(); loadData();
final client = _streamChatCoreState.client; final client = _streamChatCoreState!.client;
_subscription?.cancel();
_subscription = client _subscription = client
.on( .on(
EventType.connectionRecovered, EventType.connectionRecovered,
@@ -62,7 +62,7 @@ class ChannelsBloc extends StatefulWidget {
/// The current state of the [ChannelsBloc]. /// The current state of the [ChannelsBloc].
class ChannelsBlocState extends State<ChannelsBloc> class ChannelsBlocState extends State<ChannelsBloc>
with AutomaticKeepAliveClientMixin { with AutomaticKeepAliveClientMixin {
late StreamChatCoreState _streamChatCoreState; StreamChatCoreState? _streamChatCoreState;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@@ -98,7 +98,7 @@ class ChannelsBlocState extends State<ChannelsBloc>
PaginationParams paginationParams = const PaginationParams(limit: 30), PaginationParams paginationParams = const PaginationParams(limit: 30),
Map<String, dynamic>? options, Map<String, dynamic>? options,
}) async { }) async {
final client = _streamChatCoreState.client; final client = _streamChatCoreState!.client;
final clear = paginationParams.offset == 0; final clear = paginationParams.offset == 0;
@@ -146,10 +146,13 @@ class ChannelsBlocState extends State<ChannelsBloc>
@override @override
void didChangeDependencies() { void didChangeDependencies() {
_streamChatCoreState = StreamChatCore.of(context); final newStreamChatCoreState = StreamChatCore.of(context);
final client = _streamChatCoreState.client;
if (_subscriptions.isEmpty) { if (newStreamChatCoreState != _streamChatCoreState) {
_streamChatCoreState = newStreamChatCoreState;
final client = _streamChatCoreState!.client;
_cancelSubscriptions();
if (!widget.lockChannelsOrder) { if (!widget.lockChannelsOrder) {
_subscriptions.add(client _subscriptions.add(client
.on( .on(
@@ -109,23 +109,23 @@ class MessageListCore extends StatefulWidget {
/// The current state of the [MessageListCore]. /// The current state of the [MessageListCore].
class MessageListCoreState extends State<MessageListCore> { class MessageListCoreState extends State<MessageListCore> {
late StreamChannelState _streamChannel; StreamChannelState? _streamChannel;
bool get _upToDate => _streamChannel.channel.state?.isUpToDate ?? true; bool get _upToDate => _streamChannel!.channel.state?.isUpToDate ?? true;
bool get _isThreadConversation => widget.parentMessage != null; bool get _isThreadConversation => widget.parentMessage != null;
OwnUser? get _currentUser => _streamChannel.channel.client.state.user; OwnUser? get _currentUser => _streamChannel!.channel.client.state.user;
var _messages = <Message>[]; var _messages = <Message>[];
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final messagesStream = _isThreadConversation final messagesStream = _isThreadConversation
? _streamChannel.channel.state?.threadsStream ? _streamChannel!.channel.state?.threadsStream
.where((threads) => threads.containsKey(widget.parentMessage!.id)) .where((threads) => threads.containsKey(widget.parentMessage!.id))
.map((threads) => threads[widget.parentMessage!.id]) .map((threads) => threads[widget.parentMessage!.id])
: _streamChannel.channel.state?.messagesStream; : _streamChannel!.channel.state?.messagesStream;
bool defaultFilter(Message m) { bool defaultFilter(Message m) {
final isMyMessage = m.user?.id == _currentUser?.id; final isMyMessage = m.user?.id == _currentUser?.id;
@@ -167,21 +167,23 @@ class MessageListCoreState extends State<MessageListCore> {
QueryDirection direction = QueryDirection.top, QueryDirection direction = QueryDirection.top,
}) { }) {
if (!_isThreadConversation) { if (!_isThreadConversation) {
return _streamChannel.queryMessages(direction: direction); return _streamChannel!.queryMessages(direction: direction);
} else { } else {
return _streamChannel.getReplies(widget.parentMessage!.id); return _streamChannel!.getReplies(widget.parentMessage!.id);
} }
} }
var _initialized = false;
@override @override
void didChangeDependencies() { void didChangeDependencies() {
_streamChannel = StreamChannel.of(context); final newStreamChannel = StreamChannel.of(context);
if (!_initialized && _isThreadConversation) {
_initialized = true; if (newStreamChannel != _streamChannel) {
_streamChannel.getReplies(widget.parentMessage!.id); _streamChannel = newStreamChannel;
if (_isThreadConversation) {
_streamChannel!.getReplies(widget.parentMessage!.id);
}
} }
super.didChangeDependencies(); super.didChangeDependencies();
} }
@@ -210,7 +212,7 @@ class MessageListCoreState extends State<MessageListCore> {
@override @override
void dispose() { void dispose() {
if (!_upToDate) { if (!_upToDate) {
_streamChannel.reloadChannel(); _streamChannel!.reloadChannel();
} }
super.dispose(); super.dispose();
} }
@@ -105,28 +105,22 @@ class MessageSearchListCore extends StatefulWidget {
/// The current state of the [MessageSearchListCore]. /// The current state of the [MessageSearchListCore].
class MessageSearchListCoreState extends State<MessageSearchListCore> { class MessageSearchListCoreState extends State<MessageSearchListCore> {
late MessageSearchBlocState _messageSearchBloc; MessageSearchBlocState? _messageSearchBloc;
var _initialized = false;
@override @override
void didChangeDependencies() { void didChangeDependencies() {
_messageSearchBloc = MessageSearchBloc.of(context); final newMessageSearchBloc = MessageSearchBloc.of(context);
if (!_initialized) { if (newMessageSearchBloc != _messageSearchBloc) {
_messageSearchBloc = newMessageSearchBloc;
loadData(); loadData();
_initialized = true;
} }
if (widget.messageSearchListController != null) {
widget.messageSearchListController!.loadData = loadData;
widget.messageSearchListController!.paginateData = paginateData;
}
super.didChangeDependencies(); super.didChangeDependencies();
} }
@override @override
Widget build(BuildContext context) => _buildListView(_messageSearchBloc); Widget build(BuildContext context) => _buildListView(_messageSearchBloc!);
Widget _buildListView(MessageSearchBlocState messageSearchBloc) => Widget _buildListView(MessageSearchBlocState messageSearchBloc) =>
StreamBuilder<List<GetMessageResponse>>( StreamBuilder<List<GetMessageResponse>>(
@@ -147,7 +141,7 @@ class MessageSearchListCoreState extends State<MessageSearchListCore> {
); );
/// Fetches initial messages and updates the widget /// Fetches initial messages and updates the widget
Future<void> loadData() => _messageSearchBloc.search( Future<void> loadData() => _messageSearchBloc!.search(
filter: widget.filters, filter: widget.filters,
sort: widget.sortOptions, sort: widget.sortOptions,
query: widget.messageQuery, query: widget.messageQuery,
@@ -156,11 +150,11 @@ class MessageSearchListCoreState extends State<MessageSearchListCore> {
); );
/// Fetches more messages with updated pagination and updates the widget /// Fetches more messages with updated pagination and updates the widget
Future<void> paginateData() => _messageSearchBloc.search( Future<void> paginateData() => _messageSearchBloc!.search(
filter: widget.filters, filter: widget.filters,
sort: widget.sortOptions, sort: widget.sortOptions,
pagination: widget.paginationParams!.copyWith( pagination: widget.paginationParams!.copyWith(
offset: _messageSearchBloc.messageResponses?.length ?? 0, offset: _messageSearchBloc!.messageResponses?.length ?? 0,
), ),
query: widget.messageQuery, query: widget.messageQuery,
messageFilter: widget.messageFilters, messageFilter: widget.messageFilters,
@@ -122,34 +122,25 @@ 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; UsersBlocState? _usersBloc;
@override @override
void didChangeDependencies() { void didChangeDependencies() {
if (!_initialized) { final newUsersBloc = UsersBloc.of(context);
if (newUsersBloc != _usersBloc) {
_usersBloc = newUsersBloc;
loadData(); loadData();
_initialized = true;
}
if (widget.userListController != null) {
widget.userListController!.loadData = loadData;
widget.userListController!.paginateData = paginateData;
} }
super.didChangeDependencies(); super.didChangeDependencies();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) => _buildListView();
final _usersBloc = UsersBloc.of(context);
return _buildListView(_usersBloc);
}
bool get _isListAlreadySorted => bool get _isListAlreadySorted =>
widget.sort?.any((e) => e.field == 'name' && e.direction == 1) ?? false; widget.sort?.any((e) => e.field == 'name' && e.direction == 1) ?? false;
Stream<List<ListItem>> _buildUserStream( Stream<List<ListItem>> _buildUserStream() => _usersBloc!.usersStream.map(
UsersBlocState usersBlocState,
) =>
usersBlocState.usersStream.map(
(users) { (users) {
if (widget.groupAlphabetically) { if (widget.groupAlphabetically) {
var temp = users; var temp = users;
@@ -174,11 +165,8 @@ class UserListCoreState extends State<UserListCore>
}, },
); );
StreamBuilder<List<ListItem>> _buildListView( StreamBuilder<List<ListItem>> _buildListView() => StreamBuilder(
UsersBlocState usersBlocState, stream: _buildUserStream(),
) =>
StreamBuilder(
stream: _buildUserStream(usersBlocState),
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.hasError) { if (snapshot.hasError) {
return widget.errorBuilder(snapshot.error!); return widget.errorBuilder(snapshot.error!);
@@ -195,28 +183,22 @@ class UserListCoreState extends State<UserListCore>
); );
// ignore: public_member_api_docs // ignore: public_member_api_docs
Future<void> loadData() { Future<void> loadData() => _usersBloc!.queryUsers(
final _usersBloc = UsersBloc.of(context); filter: widget.filter,
return _usersBloc.queryUsers( sort: widget.sort,
filter: widget.filter, pagination: widget.pagination,
sort: widget.sort, options: widget.options,
pagination: widget.pagination, );
options: widget.options,
);
}
// ignore: public_member_api_docs // ignore: public_member_api_docs
Future<void> paginateData() { Future<void> paginateData() => _usersBloc!.queryUsers(
final _usersBloc = UsersBloc.of(context); filter: widget.filter,
return _usersBloc.queryUsers( sort: widget.sort,
filter: widget.filter, pagination: widget.pagination!.copyWith(
sort: widget.sort, offset: _usersBloc!.users?.length ?? 0,
pagination: widget.pagination!.copyWith( ),
offset: _usersBloc.users?.length ?? 0, options: widget.options,
), );
options: widget.options,
);
}
@override @override
void didUpdateWidget(UserListCore oldWidget) { void didUpdateWidget(UserListCore oldWidget) {