Skip to content

Commit

Permalink
Merge pull request #775 from GetStream/feat/stream-channel-listview
Browse files Browse the repository at this point in the history
feat(ui): stream channel listview
  • Loading branch information
imtoori authored Mar 8, 2022
2 parents 165e7a5 + f8c0f80 commit 55b8729
Show file tree
Hide file tree
Showing 40 changed files with 3,253 additions and 249 deletions.
8 changes: 8 additions & 0 deletions packages/stream_chat/lib/src/client/channel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,14 @@ class ChannelClientState {
(watchers, users) => watchers!.map((e) => users[e.id] ?? e).toList(),
);

/// Channel member for the current user.
Member? get currentUserMember => members.firstWhereOrNull(
(m) => m.user?.id == _channel.client.state.currentUser?.id,
);

/// User role for the current user.
String? get currentUserRole => currentUserMember?.role;

/// Channel read list.
List<Read> get read => _channelState.read;

Expand Down
34 changes: 19 additions & 15 deletions packages/stream_chat_flutter/example/lib/split_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,34 @@ class _SplitViewState extends State<SplitView> {
);
}

class ChannelListPage extends StatelessWidget {
class ChannelListPage extends StatefulWidget {
const ChannelListPage({
Key? key,
this.onTap,
}) : super(key: key);

final void Function(Channel)? onTap;

@override
State<ChannelListPage> createState() => _ChannelListPageState();
}

class _ChannelListPageState extends State<ChannelListPage> {
late final _listController = StreamChannelListController(
client: StreamChat.of(context).client,
filter: Filter.in_(
'members',
[StreamChat.of(context).currentUser!.id],
),
sort: const [SortOption('last_message_at')],
limit: 20,
);

@override
Widget build(BuildContext context) => Scaffold(
body: ChannelsBloc(
child: ChannelListView(
onChannelTap: onTap != null
? (channel, _) {
onTap!(channel);
}
: null,
filter: Filter.in_(
'members',
[StreamChat.of(context).currentUser!.id],
),
sort: const [SortOption('last_message_at')],
limit: 20,
),
body: StreamChannelListView(
onChannelTap: widget.onTap,
controller: _listController,
),
);
}
Expand Down
25 changes: 11 additions & 14 deletions packages/stream_chat_flutter/example/lib/tutorial_part_1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,15 @@ class ChannelPage extends StatelessWidget {
}) : super(key: key);

@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return Scaffold(
appBar: const ChannelHeader(),
body: Column(
children: const <Widget>[
Expanded(
child: MessageListView(),
),
MessageInput(),
],
),
);
}
Widget build(BuildContext context) => Scaffold(
appBar: const ChannelHeader(),
body: Column(
children: const <Widget>[
Expanded(
child: MessageListView(),
),
MessageInput(),
],
),
);
}
77 changes: 47 additions & 30 deletions packages/stream_chat_flutter/example/lib/tutorial_part_2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,53 @@ class MyApp extends StatelessWidget {
client: client,
child: child,
),
home: const ChannelListPage(),
home: ChannelListPage(
client: client,
),
);
}
}

class ChannelListPage extends StatelessWidget {
class ChannelListPage extends StatefulWidget {
const ChannelListPage({
Key? key,
required this.client,
}) : super(key: key);

final StreamChatClient client;

@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return Scaffold(
body: ChannelsBloc(
child: ChannelListView(
filter: Filter.in_(
'members',
[StreamChat.of(context).currentUser!.id],
State<ChannelListPage> createState() => _ChannelListPageState();
}

class _ChannelListPageState extends State<ChannelListPage> {
late final _controller = StreamChannelListController(
client: widget.client,
filter: Filter.in_(
'members',
[StreamChat.of(context).currentUser!.id],
),
sort: const [SortOption('last_message_at')],
);

@override
Widget build(BuildContext context) => Scaffold(
body: RefreshIndicator(
onRefresh: _controller.refresh,
child: StreamChannelListView(
controller: _controller,
onChannelTap: (channel) => Navigator.push(
context,
MaterialPageRoute(
builder: (_) => StreamChannel(
channel: channel,
child: const ChannelPage(),
),
),
),
),
sort: const [SortOption('last_message_at')],
limit: 20,
channelWidget: const ChannelPage(),
),
),
);
}
);
}

class ChannelPage extends StatelessWidget {
Expand All @@ -97,18 +117,15 @@ class ChannelPage extends StatelessWidget {
}) : super(key: key);

@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return Scaffold(
appBar: const ChannelHeader(),
body: Column(
children: const <Widget>[
Expanded(
child: MessageListView(),
),
MessageInput(),
],
),
);
}
Widget build(BuildContext context) => Scaffold(
appBar: const ChannelHeader(),
body: Column(
children: const <Widget>[
Expanded(
child: MessageListView(),
),
MessageInput(),
],
),
);
}
59 changes: 39 additions & 20 deletions packages/stream_chat_flutter/example/lib/tutorial_part_3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,31 +68,49 @@ class MyApp extends StatelessWidget {
}
}

class ChannelListPage extends StatelessWidget {
class ChannelListPage extends StatefulWidget {
const ChannelListPage({
Key? key,
}) : super(key: key);

@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return Scaffold(
body: ChannelsBloc(
child: ChannelListView(
filter: Filter.in_(
'members',
[StreamChat.of(context).currentUser!.id],
),
channelPreviewBuilder: _channelPreviewBuilder,
// sort: [SortOption('last_message_at')],
limit: 20,
channelWidget: const ChannelPage(),
State<ChannelListPage> createState() => _ChannelListPageState();
}

class _ChannelListPageState extends State<ChannelListPage> {
late final _listController = StreamChannelListController(
client: StreamChat.of(context).client,
filter: Filter.in_(
'members',
[StreamChat.of(context).currentUser!.id],
),
sort: const [SortOption('last_message_at')],
limit: 20,
);

@override
Widget build(BuildContext context) => Scaffold(
body: StreamChannelListView(
controller: _listController,
itemBuilder: _channelPreviewBuilder,
onChannelTap: (channel) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => StreamChannel(
channel: channel,
child: const ChannelPage(),
),
),
);
},
),
),
);
}
);

Widget _channelPreviewBuilder(BuildContext context, Channel channel) {
Widget _channelPreviewBuilder(
BuildContext context,
Channel channel,
StreamChannelListTile defaultTile,
) {
final lastMessage = channel.state?.messages.reversed.firstWhereOrNull(
(message) => !message.isDeleted,
);
Expand All @@ -112,16 +130,17 @@ class ChannelListPage extends StatelessWidget {
),
);
},
leading: ChannelAvatar(
leading: StreamChannelAvatar(
channel: channel,
),
title: ChannelName(
title: StreamChannelName(
textStyle: ChannelPreviewTheme.of(context).titleStyle!.copyWith(
color: StreamChatTheme.of(context)
.colorTheme
.textHighEmphasis
.withOpacity(opacity),
),
channel: channel,
),
subtitle: Text(subtitle),
trailing: channel.state!.unreadCount > 0
Expand Down
75 changes: 43 additions & 32 deletions packages/stream_chat_flutter/example/lib/tutorial_part_4.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,42 @@ class MyApp extends StatelessWidget {
}
}

class ChannelListPage extends StatelessWidget {
class ChannelListPage extends StatefulWidget {
const ChannelListPage({
Key? key,
}) : super(key: key);

@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return Scaffold(
body: ChannelsBloc(
child: ChannelListView(
filter: Filter.in_(
'members',
[StreamChat.of(context).currentUser!.id],
),
sort: const [SortOption('last_message_at')],
limit: 20,
channelWidget: const ChannelPage(),
State<ChannelListPage> createState() => _ChannelListPageState();
}

class _ChannelListPageState extends State<ChannelListPage> {
late final _listController = StreamChannelListController(
client: StreamChat.of(context).client,
filter: Filter.in_(
'members',
[StreamChat.of(context).currentUser!.id],
),
sort: const [SortOption('last_message_at')],
limit: 20,
);

@override
Widget build(BuildContext context) => Scaffold(
body: StreamChannelListView(
controller: _listController,
onChannelTap: (channel) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => StreamChannel(
channel: channel,
child: const ChannelPage(),
),
),
);
},
),
),
);
}
);
}

class ChannelPage extends StatelessWidget {
Expand All @@ -83,24 +97,21 @@ class ChannelPage extends StatelessWidget {
}) : super(key: key);

@override
// ignore: prefer_expression_function_bodies
Widget build(BuildContext context) {
return Scaffold(
appBar: const ChannelHeader(),
body: Column(
children: <Widget>[
Expanded(
child: MessageListView(
threadBuilder: (_, parentMessage) => ThreadPage(
parent: parentMessage,
Widget build(BuildContext context) => Scaffold(
appBar: const ChannelHeader(),
body: Column(
children: <Widget>[
Expanded(
child: MessageListView(
threadBuilder: (_, parentMessage) => ThreadPage(
parent: parentMessage,
),
),
),
),
const MessageInput(),
],
),
);
}
const MessageInput(),
],
),
);
}

class ThreadPage extends StatelessWidget {
Expand Down
Loading

0 comments on commit 55b8729

Please sign in to comment.