return a stream from querychannels and add channelsBloc to handle channelListview
This commit is contained in:
+22
-11
@@ -63,17 +63,28 @@ class ChannelListPage extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: ChannelListView(
|
||||
filter: {
|
||||
'members': {
|
||||
'\$in': [StreamChat.of(context).user.id],
|
||||
}
|
||||
},
|
||||
sort: [SortOption('last_message_at')],
|
||||
pagination: PaginationParams(
|
||||
limit: 20,
|
||||
),
|
||||
channelWidget: ChannelPage(),
|
||||
body: PageView(
|
||||
children: <Widget>[
|
||||
ChannelListView(
|
||||
filter: {
|
||||
'members': {
|
||||
'\$in': [StreamChat.of(context).user.id],
|
||||
}
|
||||
},
|
||||
sort: [SortOption('last_message_at')],
|
||||
pagination: PaginationParams(
|
||||
limit: 20,
|
||||
),
|
||||
channelWidget: ChannelPage(),
|
||||
),
|
||||
ChannelListView(
|
||||
sort: [SortOption('last_message_at')],
|
||||
pagination: PaginationParams(
|
||||
limit: 20,
|
||||
),
|
||||
channelWidget: ChannelPage(),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:stream_chat/stream_chat.dart';
|
||||
import 'package:stream_chat_flutter/src/channels_bloc.dart';
|
||||
|
||||
import '../stream_chat_flutter.dart';
|
||||
import 'channel_preview.dart';
|
||||
@@ -99,15 +100,16 @@ class ChannelListView extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _ChannelListViewState extends State<ChannelListView>
|
||||
with WidgetsBindingObserver {
|
||||
with WidgetsBindingObserver, AutomaticKeepAliveClientMixin {
|
||||
final ScrollController _scrollController = ScrollController();
|
||||
ChannelsBloc channelsBloc;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final streamChat = StreamChat.of(context);
|
||||
super.build(context);
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
return streamChat.queryChannels(
|
||||
return channelsBloc.queryChannels(
|
||||
filter: widget.filter,
|
||||
sortOptions: widget.sort,
|
||||
paginationParams: widget.pagination,
|
||||
@@ -115,7 +117,7 @@ class _ChannelListViewState extends State<ChannelListView>
|
||||
);
|
||||
},
|
||||
child: StreamBuilder<List<Channel>>(
|
||||
stream: streamChat.channelsStream,
|
||||
stream: channelsBloc.channelsStream,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
if (snapshot.error is Error) {
|
||||
@@ -163,7 +165,7 @@ class _ChannelListViewState extends State<ChannelListView>
|
||||
),
|
||||
FlatButton(
|
||||
onPressed: () {
|
||||
streamChat.queryChannels(
|
||||
channelsBloc.queryChannels(
|
||||
filter: widget.filter,
|
||||
sortOptions: widget.sort,
|
||||
paginationParams: widget.pagination,
|
||||
@@ -215,7 +217,7 @@ class _ChannelListViewState extends State<ChannelListView>
|
||||
if (i < channels.length) {
|
||||
final channel = channels[i];
|
||||
|
||||
final channelClient = streamChat.channels.firstWhere(
|
||||
final channelClient = channelsBloc.channels.firstWhere(
|
||||
(c) => c.cid == channel.cid,
|
||||
orElse: () => null,
|
||||
);
|
||||
@@ -285,7 +287,7 @@ class _ChannelListViewState extends State<ChannelListView>
|
||||
|
||||
Widget _buildQueryProgressIndicator(context, StreamChatState streamChat) {
|
||||
return StreamBuilder<bool>(
|
||||
stream: streamChat.queryChannelsLoading,
|
||||
stream: channelsBloc.queryChannelsLoading,
|
||||
initialData: false,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
@@ -320,11 +322,11 @@ class _ChannelListViewState extends State<ChannelListView>
|
||||
void _listenChannelPagination(StreamChatState streamChat) {
|
||||
if (_scrollController.position.maxScrollExtent ==
|
||||
_scrollController.offset) {
|
||||
streamChat.queryChannels(
|
||||
channelsBloc.queryChannels(
|
||||
filter: widget.filter,
|
||||
sortOptions: widget.sort,
|
||||
paginationParams: widget.pagination.copyWith(
|
||||
offset: streamChat.channels.length,
|
||||
offset: channelsBloc.channels.length,
|
||||
),
|
||||
options: widget.options,
|
||||
);
|
||||
@@ -334,11 +336,12 @@ class _ChannelListViewState extends State<ChannelListView>
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final streamChat = StreamChat.of(context);
|
||||
|
||||
channelsBloc = ChannelsBloc(streamChat.client);
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
|
||||
final streamChat = StreamChat.of(context);
|
||||
streamChat.queryChannels(
|
||||
channelsBloc.queryChannels(
|
||||
filter: widget.filter,
|
||||
sortOptions: widget.sort,
|
||||
paginationParams: widget.pagination,
|
||||
@@ -353,7 +356,7 @@ class _ChannelListViewState extends State<ChannelListView>
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
StreamChat.of(context).queryChannels(
|
||||
channelsBloc.queryChannels(
|
||||
filter: widget.filter,
|
||||
sortOptions: widget.sort,
|
||||
paginationParams: widget.pagination,
|
||||
@@ -366,6 +369,10 @@ class _ChannelListViewState extends State<ChannelListView>
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
channelsBloc.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
bool get wantKeepAlive => true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:stream_chat/stream_chat.dart';
|
||||
|
||||
class ChannelsBloc {
|
||||
final Client client;
|
||||
|
||||
ChannelsBloc(this.client);
|
||||
|
||||
/// The current channel list
|
||||
List<Channel> get channels => _channelsController.value;
|
||||
|
||||
/// The current channel list as a stream
|
||||
Stream<List<Channel>> get channelsStream => _channelsController.stream;
|
||||
|
||||
final BehaviorSubject<bool> _queryChannelsLoadingController =
|
||||
BehaviorSubject.seeded(false);
|
||||
|
||||
final BehaviorSubject<List<Channel>> _channelsController =
|
||||
BehaviorSubject.seeded([]);
|
||||
|
||||
/// The stream notifying the state of queryChannel call
|
||||
Stream<bool> get queryChannelsLoading =>
|
||||
_queryChannelsLoadingController.stream;
|
||||
|
||||
/// Calls [client.queryChannels] updating [queryChannelsLoading] stream
|
||||
Future<void> queryChannels({
|
||||
Map<String, dynamic> filter,
|
||||
List<SortOption> sortOptions,
|
||||
PaginationParams paginationParams,
|
||||
Map<String, dynamic> options,
|
||||
bool onlyOffline = false,
|
||||
}) async {
|
||||
if (_queryChannelsLoadingController.value == true) {
|
||||
return;
|
||||
}
|
||||
_queryChannelsLoadingController.sink.add(true);
|
||||
|
||||
try {
|
||||
final clear = paginationParams == null ||
|
||||
paginationParams.offset == null ||
|
||||
paginationParams.offset == 0;
|
||||
final oldChannels = List<Channel>.from(channels);
|
||||
client
|
||||
.queryChannels(
|
||||
filter: filter,
|
||||
sort: sortOptions,
|
||||
options: options,
|
||||
paginationParams: paginationParams,
|
||||
onlyOffline: onlyOffline,
|
||||
)
|
||||
.listen((channels) {
|
||||
if (clear) {
|
||||
_channelsController.add(channels);
|
||||
} else {
|
||||
final l = oldChannels + channels;
|
||||
_channelsController.add(l);
|
||||
}
|
||||
}, onDone: () {
|
||||
_queryChannelsLoadingController.sink.add(false);
|
||||
}, onError: (err, stackTrace) {
|
||||
_queryChannelsLoadingController.addError(err, stackTrace);
|
||||
});
|
||||
} catch (err, stackTrace) {
|
||||
_queryChannelsLoadingController.addError(err, stackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_channelsController.close();
|
||||
_queryChannelsLoadingController.close();
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:rxdart/rxdart.dart';
|
||||
import 'package:stream_chat/stream_chat.dart';
|
||||
import 'package:stream_chat_flutter/src/stream_chat_theme.dart';
|
||||
|
||||
@@ -59,15 +58,12 @@ class StreamChat extends StatefulWidget {
|
||||
}
|
||||
}
|
||||
|
||||
class StreamChatState extends State<StreamChat>
|
||||
with WidgetsBindingObserver, AutomaticKeepAliveClientMixin {
|
||||
final List<StreamSubscription> _subscriptions = [];
|
||||
class StreamChatState extends State<StreamChat> with WidgetsBindingObserver {
|
||||
Client get client => widget.client;
|
||||
final GlobalKey<NavigatorState> _navigatorKey = GlobalKey<NavigatorState>();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
super.build(context);
|
||||
final theme = _getTheme(context, widget.streamChatThemeData);
|
||||
return StreamChatTheme(
|
||||
data: theme,
|
||||
@@ -179,46 +175,6 @@ class StreamChatState extends State<StreamChat>
|
||||
/// The current user as a stream
|
||||
Stream<User> get userStream => widget.client.state.userStream;
|
||||
|
||||
/// The current channel list
|
||||
List<Channel> get channels => client.state.channels;
|
||||
|
||||
/// The current channel list as a stream
|
||||
Stream<List<Channel>> get channelsStream => client.state.channelsStream;
|
||||
|
||||
final BehaviorSubject<bool> _queryChannelsLoadingController =
|
||||
BehaviorSubject.seeded(false);
|
||||
|
||||
/// The stream notifying the state of queryChannel call
|
||||
Stream<bool> get queryChannelsLoading =>
|
||||
_queryChannelsLoadingController.stream;
|
||||
|
||||
/// Calls [client.queryChannels] updating [queryChannelsLoading] stream
|
||||
Future<void> queryChannels({
|
||||
Map<String, dynamic> filter,
|
||||
List<SortOption> sortOptions,
|
||||
PaginationParams paginationParams,
|
||||
Map<String, dynamic> options,
|
||||
bool onlyOffline = false,
|
||||
}) async {
|
||||
if (_queryChannelsLoadingController.value == true) {
|
||||
return;
|
||||
}
|
||||
_queryChannelsLoadingController.sink.add(true);
|
||||
|
||||
try {
|
||||
await widget.client.queryChannels(
|
||||
filter: filter,
|
||||
sort: sortOptions,
|
||||
options: options,
|
||||
paginationParams: paginationParams,
|
||||
onlyOffline: onlyOffline,
|
||||
);
|
||||
_queryChannelsLoadingController.sink.add(false);
|
||||
} catch (err, stackTrace) {
|
||||
_queryChannelsLoadingController.addError(err, stackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -239,12 +195,7 @@ class StreamChatState extends State<StreamChat>
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_subscriptions.forEach((s) => s.cancel());
|
||||
_queryChannelsLoadingController.close();
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
bool get wantKeepAlive => true;
|
||||
}
|
||||
|
||||
+1
-1
@@ -17,7 +17,7 @@ dependencies:
|
||||
url_launcher: ^5.4.2
|
||||
video_player: ^0.10.8+1
|
||||
chewie: ^0.9.10
|
||||
file_picker: ^1.5.1
|
||||
file_picker: ^1.6.0
|
||||
image_picker: ^0.6.4
|
||||
flutter_keyboard_visibility: ^0.7.0
|
||||
stream_chat:
|
||||
|
||||
Reference in New Issue
Block a user