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