Add custom sort support for offline channels
Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
@@ -106,7 +106,7 @@ class ChannelListCore extends StatefulWidget {
|
||||
/// Sorting is based on field and direction, multiple sorting options can be provided.
|
||||
/// You can sort based on last_updated, last_message_at, updated_at, created_at or member_count.
|
||||
/// Direction can be ascending or descending.
|
||||
final List<SortOption> sort;
|
||||
final List<SortOption<ChannelModel>> sort;
|
||||
|
||||
/// Pagination parameters
|
||||
/// limit: the number of channels to return (max is 30)
|
||||
@@ -118,8 +118,7 @@ class ChannelListCore extends StatefulWidget {
|
||||
_ChannelListCoreState createState() => _ChannelListCoreState();
|
||||
}
|
||||
|
||||
class _ChannelListCoreState extends State<ChannelListCore>
|
||||
with WidgetsBindingObserver {
|
||||
class _ChannelListCoreState extends State<ChannelListCore> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final channelsBloc = ChannelsBloc.of(context);
|
||||
@@ -133,34 +132,21 @@ class _ChannelListCoreState extends State<ChannelListCore>
|
||||
return StreamBuilder<List<Channel>>(
|
||||
stream: channelsBlocState.channelsStream,
|
||||
builder: (context, snapshot) {
|
||||
var child;
|
||||
if (snapshot.hasError) {
|
||||
child = _buildErrorWidget(
|
||||
snapshot,
|
||||
context,
|
||||
channelsBlocState,
|
||||
);
|
||||
} else if (!snapshot.hasData) {
|
||||
child = _buildLoadingWidget();
|
||||
} else {
|
||||
final channels = snapshot.data;
|
||||
|
||||
child = widget.emptyBuilder(context);
|
||||
|
||||
if (channels.isNotEmpty) {
|
||||
return widget.listBuilder(context, channels);
|
||||
}
|
||||
return _buildErrorWidget(snapshot, context, channelsBlocState);
|
||||
}
|
||||
|
||||
return child;
|
||||
if (!snapshot.hasData) {
|
||||
return widget.loadingBuilder(context);
|
||||
}
|
||||
final channels = snapshot.data;
|
||||
if (channels.isEmpty) {
|
||||
return widget.emptyBuilder(context);
|
||||
}
|
||||
return widget.listBuilder(context, channels);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildLoadingWidget() {
|
||||
return widget.loadingBuilder(context);
|
||||
}
|
||||
|
||||
Widget _buildErrorWidget(
|
||||
AsyncSnapshot<List<Channel>> snapshot,
|
||||
BuildContext context,
|
||||
@@ -197,39 +183,21 @@ class _ChannelListCoreState extends State<ChannelListCore>
|
||||
);
|
||||
}
|
||||
|
||||
StreamSubscription _subscription;
|
||||
StreamSubscription<Event> _subscription;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
|
||||
final channelsBloc = ChannelsBloc.of(context);
|
||||
channelsBloc.queryChannels(
|
||||
filter: widget.filter,
|
||||
sortOptions: widget.sort,
|
||||
paginationParams: widget.pagination,
|
||||
options: widget.options,
|
||||
);
|
||||
|
||||
loadData();
|
||||
final client = StreamChatCore.of(context).client;
|
||||
|
||||
_subscription = client
|
||||
.on(
|
||||
EventType.connectionRecovered,
|
||||
EventType.notificationAddedToChannel,
|
||||
EventType.notificationMessageNew,
|
||||
EventType.channelVisible,
|
||||
)
|
||||
.listen((event) {
|
||||
channelsBloc.queryChannels(
|
||||
filter: widget.filter,
|
||||
sortOptions: widget.sort,
|
||||
paginationParams: widget.pagination,
|
||||
options: widget.options,
|
||||
);
|
||||
});
|
||||
EventType.connectionRecovered,
|
||||
EventType.notificationAddedToChannel,
|
||||
EventType.notificationMessageNew,
|
||||
EventType.channelVisible,
|
||||
)
|
||||
.listen((event) => loadData());
|
||||
|
||||
if (widget.channelListController != null) {
|
||||
widget.channelListController.loadData = loadData;
|
||||
@@ -246,20 +214,13 @@ class _ChannelListCoreState extends State<ChannelListCore>
|
||||
widget.pagination?.toJson()?.toString() !=
|
||||
oldWidget.pagination?.toJson()?.toString() ||
|
||||
widget.options?.toString() != oldWidget.options?.toString()) {
|
||||
final channelsBloc = ChannelsBloc.of(context);
|
||||
channelsBloc.queryChannels(
|
||||
filter: widget.filter,
|
||||
sortOptions: widget.sort,
|
||||
paginationParams: widget.pagination,
|
||||
options: widget.options,
|
||||
);
|
||||
loadData();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_subscription.cancel();
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ class ChannelsBlocState extends State<ChannelsBloc>
|
||||
/// Calls [client.queryChannels] updating [queryChannelsLoading] stream
|
||||
Future<void> queryChannels({
|
||||
Map<String, dynamic> filter,
|
||||
List<SortOption> sortOptions,
|
||||
List<SortOption<ChannelModel>> sortOptions,
|
||||
PaginationParams paginationParams,
|
||||
Map<String, dynamic> options,
|
||||
bool onlyOffline = false,
|
||||
@@ -102,21 +102,21 @@ class ChannelsBlocState extends State<ChannelsBloc>
|
||||
paginationParams.offset == null ||
|
||||
paginationParams.offset == 0;
|
||||
final oldChannels = List<Channel>.from(channels ?? []);
|
||||
final _channels = await client.queryChannels(
|
||||
await for (final channels in client.queryChannels(
|
||||
filter: filter,
|
||||
sort: sortOptions,
|
||||
options: options,
|
||||
paginationParams: paginationParams,
|
||||
onlyOffline: onlyOffline,
|
||||
);
|
||||
|
||||
if (clear) {
|
||||
_channelsController.add(_channels);
|
||||
} else {
|
||||
final l = oldChannels + _channels;
|
||||
_channelsController.add(l);
|
||||
preferOffline: onlyOffline,
|
||||
)) {
|
||||
if (clear) {
|
||||
_channelsController.add(channels);
|
||||
} else {
|
||||
final l = oldChannels + channels;
|
||||
_channelsController.add(l);
|
||||
}
|
||||
_queryChannelsLoadingController.sink.add(false);
|
||||
}
|
||||
_queryChannelsLoadingController.sink.add(false);
|
||||
} catch (err, stackTrace) {
|
||||
print(err);
|
||||
print(stackTrace);
|
||||
|
||||
Reference in New Issue
Block a user