fix channellistview loading when client is not initialized

This commit is contained in:
Salvatore Giordano
2020-09-16 10:35:28 +02:00
parent ef51103b81
commit bd22ee0fd6
2 changed files with 30 additions and 17 deletions
+16 -4
View File
@@ -205,8 +205,20 @@ class _ChannelListViewState extends State<ChannelListView>
} }
if (!snapshot.hasData) { if (!snapshot.hasData) {
return Center( return LayoutBuilder(
child: CircularProgressIndicator(), builder: (context, viewportConstraints) {
return SingleChildScrollView(
physics: AlwaysScrollableScrollPhysics(),
child: ConstrainedBox(
constraints: BoxConstraints(
minHeight: viewportConstraints.maxHeight,
),
child: Center(
child: CircularProgressIndicator(),
),
),
);
},
); );
} }
@@ -374,7 +386,7 @@ class _ChannelListViewState extends State<ChannelListView>
filter: widget.filter, filter: widget.filter,
sortOptions: widget.sort, sortOptions: widget.sort,
paginationParams: widget.pagination.copyWith( paginationParams: widget.pagination.copyWith(
offset: channelsProvider.channels.length, offset: channelsProvider.channels?.length ?? 0,
), ),
options: widget.options, options: widget.options,
); );
@@ -426,7 +438,7 @@ class _ChannelListViewState extends State<ChannelListView>
void didUpdateWidget(ChannelListView oldWidget) { void didUpdateWidget(ChannelListView oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
if (widget.filter != oldWidget.filter || if (widget.filter?.toString() != oldWidget.filter?.toString() ||
widget.sort != oldWidget.sort || widget.sort != oldWidget.sort ||
widget.pagination != oldWidget.pagination || widget.pagination != oldWidget.pagination ||
widget.options != oldWidget.options) { widget.options != oldWidget.options) {
+14 -13
View File
@@ -56,8 +56,7 @@ class ChannelsBlocState extends State<ChannelsBloc>
final BehaviorSubject<bool> _queryChannelsLoadingController = final BehaviorSubject<bool> _queryChannelsLoadingController =
BehaviorSubject.seeded(false); BehaviorSubject.seeded(false);
final BehaviorSubject<List<Channel>> _channelsController = final BehaviorSubject<List<Channel>> _channelsController = BehaviorSubject();
BehaviorSubject.seeded([]);
/// The stream notifying the state of queryChannel call /// The stream notifying the state of queryChannel call
Stream<bool> get queryChannelsLoading => Stream<bool> get queryChannelsLoading =>
@@ -73,7 +72,10 @@ class ChannelsBlocState extends State<ChannelsBloc>
Map<String, dynamic> options, Map<String, dynamic> options,
bool onlyOffline = false, bool onlyOffline = false,
}) async { }) async {
if (_queryChannelsLoadingController.value == true) { final client = StreamChat.of(context).client;
if (client.state?.user == null ||
_queryChannelsLoadingController.value == true) {
return; return;
} }
_queryChannelsLoadingController.sink.add(true); _queryChannelsLoadingController.sink.add(true);
@@ -82,16 +84,15 @@ class ChannelsBlocState extends State<ChannelsBloc>
final clear = paginationParams == null || final clear = paginationParams == null ||
paginationParams.offset == null || paginationParams.offset == null ||
paginationParams.offset == 0; paginationParams.offset == 0;
final oldChannels = List<Channel>.from(channels); final oldChannels = List<Channel>.from(channels ?? []);
StreamChat.of(context) client
.client
.queryChannels( .queryChannels(
filter: filter, filter: filter,
sort: sortOptions, sort: sortOptions,
options: options, options: options,
paginationParams: paginationParams, paginationParams: paginationParams,
onlyOffline: onlyOffline, onlyOffline: onlyOffline,
) )
.listen((channels) { .listen((channels) {
if (clear) { if (clear) {
_channelsController.add(channels); _channelsController.add(channels);
@@ -139,7 +140,7 @@ class ChannelsBlocState extends State<ChannelsBloc>
} }
_subscriptions.add(client.on(EventType.channelHidden).listen((event) async { _subscriptions.add(client.on(EventType.channelHidden).listen((event) async {
final newChannels = List<Channel>.from(channels); final newChannels = List<Channel>.from(channels ?? []);
final channelIndex = newChannels.indexWhere((c) => c.cid == event.cid); final channelIndex = newChannels.indexWhere((c) => c.cid == event.cid);
if (channelIndex > -1) { if (channelIndex > -1) {
final channel = newChannels.removeAt(channelIndex); final channel = newChannels.removeAt(channelIndex);