Skip to content

Commit

Permalink
Merge pull request #514 from GetStream/feat/listControllers
Browse files Browse the repository at this point in the history
feat: list controllers
  • Loading branch information
imtoori authored Jun 29, 2021
2 parents 5cb87d2 + ba927bf commit ef4aada
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 13 deletions.
11 changes: 10 additions & 1 deletion packages/stream_chat_flutter/lib/src/channel_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class ChannelListView extends StatefulWidget {
this.onMoreDetailsPressed,
this.onDeletePressed,
this.swipeActions,
this.channelListController,
}) : super(key: key);

/// If true a default swipe to action behaviour will be added to this widget
Expand Down Expand Up @@ -187,14 +188,22 @@ class ChannelListView extends StatefulWidget {
/// List of actions for slidable
final List<SwipeAction>? swipeActions;

/// A [ChannelListController] allows reloading and pagination.
/// Use [ChannelListController.loadData] and
/// [ChannelListController.paginateData] respectively for reloading and
/// pagination.
final ChannelListController? channelListController;

@override
_ChannelListViewState createState() => _ChannelListViewState();
}

class _ChannelListViewState extends State<ChannelListView> {
final _slideController = SlidableController();

final _channelListController = ChannelListController();
late final _defaultController = ChannelListController();
ChannelListController get _channelListController =>
widget.channelListController ?? _defaultController;

@override
Widget build(BuildContext context) {
Expand Down
9 changes: 8 additions & 1 deletion packages/stream_chat_flutter/lib/src/message_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class MessageListView extends StatefulWidget {
this.usernameBuilder,
this.showFloatingDateDivider = true,
this.threadSeparatorBuilder,
this.messageListController,
}) : super(key: key);

/// Function used to build a custom message widget
Expand Down Expand Up @@ -296,6 +297,10 @@ class MessageListView extends StatefulWidget {
/// Builder used to build the thread separator in case it's a thread view
final WidgetBuilder? threadSeparatorBuilder;

/// A [MessageListController] allows pagination.
/// Use [ChannelListController.paginateData] pagination.
final MessageListController? messageListController;

@override
_MessageListViewState createState() => _MessageListViewState();
}
Expand Down Expand Up @@ -347,7 +352,9 @@ class _MessageListViewState extends State<MessageListView> {

bool _inBetweenList = false;

final MessageListController _messageListController = MessageListController();
late final _defaultController = MessageListController();
MessageListController get _messageListController =>
widget.messageListController ?? _defaultController;

@override
Widget build(BuildContext context) => MessageListCore(
Expand Down
12 changes: 10 additions & 2 deletions packages/stream_chat_flutter/lib/src/message_search_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class MessageSearchListView extends StatefulWidget {
this.errorBuilder,
this.loadingBuilder,
this.childBuilder,
this.messageSearchListController,
}) : super(key: key);

/// Message String to search on
Expand Down Expand Up @@ -127,13 +128,20 @@ class MessageSearchListView extends StatefulWidget {
/// The builder that will be used in case of loading
final WidgetBuilder? loadingBuilder;

/// A [MessageSearchListController] allows reloading and pagination.
/// Use [MessageSearchListController.loadData] and
/// [MessageSearchListController.paginateData] respectively for reloading and
/// pagination.
final MessageSearchListController? messageSearchListController;

@override
_MessageSearchListViewState createState() => _MessageSearchListViewState();
}

class _MessageSearchListViewState extends State<MessageSearchListView> {
final MessageSearchListController _messageSearchListController =
MessageSearchListController();
late final _defaultController = MessageSearchListController();
MessageSearchListController get _messageSearchListController =>
widget.messageSearchListController ?? _defaultController;

@override
Widget build(BuildContext context) => MessageSearchListCore(
Expand Down
10 changes: 9 additions & 1 deletion packages/stream_chat_flutter/lib/src/user_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class UserListView extends StatefulWidget {
this.emptyBuilder,
this.loadingBuilder,
this.listBuilder,
this.userListController,
}) : assert(
crossAxisCount == 1 || groupAlphabetically == false,
'Cannot group alphabetically when crossAxisCount > 1',
Expand Down Expand Up @@ -140,6 +141,11 @@ class UserListView extends StatefulWidget {
/// The builder used when the channel list is empty.
final WidgetBuilder? emptyBuilder;

/// A [UserListController] allows reloading and pagination.
/// Use [UserListController.loadData] and [UserListController.paginateData]
/// respectively for reloading and pagination.
final UserListController? userListController;

@override
_UserListViewState createState() => _UserListViewState();
}
Expand All @@ -148,7 +154,9 @@ class _UserListViewState extends State<UserListView>
with WidgetsBindingObserver {
bool get _isListView => widget.crossAxisCount == 1;

final UserListController _userListController = UserListController();
late final _defaultController = UserListController();
UserListController get _userListController =>
widget.userListController ?? _defaultController;

@override
Widget build(BuildContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,24 @@ class MessageSearchListCoreState extends State<MessageSearchListCore> {
if (newMessageSearchBloc != _messageSearchBloc) {
_messageSearchBloc = newMessageSearchBloc;
loadData();
if (widget.messageSearchListController != null) {
widget.messageSearchListController!.loadData = loadData;
widget.messageSearchListController!.paginateData = paginateData;
}
}

super.didChangeDependencies();
}

void _setupController() {
if (widget.messageSearchListController != null) {
widget.messageSearchListController!.loadData = loadData;
widget.messageSearchListController!.paginateData = paginateData;
}
}

@override
void initState() {
super.initState();
_setupController();
}

@override
Widget build(BuildContext context) => _buildListView(_messageSearchBloc!);

Expand Down Expand Up @@ -176,6 +185,11 @@ class MessageSearchListCoreState extends State<MessageSearchListCore> {
oldWidget.paginationParams?.toJson().toString()) {
loadData();
}

if (widget.messageSearchListController !=
oldWidget.messageSearchListController) {
_setupController();
}
}
}

Expand Down
21 changes: 17 additions & 4 deletions packages/stream_chat_flutter_core/lib/src/user_list_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,23 @@ class UserListCoreState extends State<UserListCore>
if (newUsersBloc != _usersBloc) {
_usersBloc = newUsersBloc;
loadData();
if (widget.userListController != null) {
widget.userListController!.loadData = loadData;
widget.userListController!.paginateData = paginateData;
}
}
super.didChangeDependencies();
}

@override
void initState() {
super.initState();
_setupController();
}

void _setupController() {
if (widget.userListController != null) {
widget.userListController!.loadData = loadData;
widget.userListController!.paginateData = paginateData;
}
}

@override
Widget build(BuildContext context) => _buildListView();

Expand Down Expand Up @@ -212,6 +221,10 @@ class UserListCoreState extends State<UserListCore>
oldWidget.pagination?.toJson().toString()) {
loadData();
}

if (widget.userListController != oldWidget.userListController) {
_setupController();
}
}
}

Expand Down

0 comments on commit ef4aada

Please sign in to comment.