swap user list with message list and create channel with draft: true

This commit is contained in:
Salvatore Giordano
2020-11-20 18:03:33 +01:00
parent 5352c2ac4b
commit 682c0b3e10
6 changed files with 339 additions and 235 deletions
+17 -4
View File
@@ -22,7 +22,7 @@ class ChipsInputTextField<T> extends StatefulWidget {
this.focusNode, this.focusNode,
this.onChipAdded, this.onChipAdded,
this.onChipRemoved, this.onChipRemoved,
this.hint = 'Type a name or group', this.hint = 'Type a name',
}) : super(key: key); }) : super(key: key);
@override @override
@@ -61,7 +61,16 @@ class ChipInputTextFieldState<T> extends State<ChipsInputTextField<T>> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Material( return GestureDetector(
onTap: _pauseItemAddition
? () {
setState(() {
_pauseItemAddition = false;
widget.focusNode?.requestFocus();
});
}
: null,
child: Material(
elevation: 1, elevation: 1,
color: Colors.white, color: Colors.white,
child: Container( child: Container(
@@ -122,11 +131,14 @@ class ChipInputTextFieldState<T> extends State<ChipsInputTextField<T>> {
alignment: Alignment.bottomCenter, alignment: Alignment.bottomCenter,
child: IconButton( child: IconButton(
icon: Icon( icon: Icon(
_chips.isEmpty ? StreamIcons.user : StreamIcons.user_add, _chips.isEmpty
? StreamIcons.user
: StreamIcons.user_add,
color: Colors.black.withOpacity(0.5), color: Colors.black.withOpacity(0.5),
size: 24, size: 24,
), ),
onPressed: !_pauseItemAddition ? null : resumeItemAddition, onPressed:
!_pauseItemAddition ? null : resumeItemAddition,
alignment: Alignment.topRight, alignment: Alignment.topRight,
visualDensity: VisualDensity.compact, visualDensity: VisualDensity.compact,
padding: const EdgeInsets.all(0), padding: const EdgeInsets.all(0),
@@ -142,6 +154,7 @@ class ChipInputTextFieldState<T> extends State<ChipsInputTextField<T>> {
), ),
), ),
), ),
),
); );
} }
} }
+88 -22
View File
@@ -111,6 +111,7 @@ class ChannelListPage extends StatelessWidget {
), ),
ListTile( ListTile(
onTap: () { onTap: () {
Navigator.pop(context);
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute(builder: (_) => NewChatScreen()), MaterialPageRoute(builder: (_) => NewChatScreen()),
@@ -126,6 +127,7 @@ class ChannelListPage extends StatelessWidget {
), ),
ListTile( ListTile(
onTap: () { onTap: () {
Navigator.pop(context);
Navigator.push( Navigator.push(
context, context,
MaterialPageRoute(builder: (_) => NewGroupChatScreen()), MaterialPageRoute(builder: (_) => NewGroupChatScreen()),
@@ -177,7 +179,10 @@ class ChannelListPage extends StatelessWidget {
filter: { filter: {
'members': { 'members': {
'\$in': [user.id], '\$in': [user.id],
} },
'draft': {
r'$ne': true,
},
}, },
options: { options: {
'presence': true, 'presence': true,
@@ -283,12 +288,17 @@ class _NewChatScreenState extends State<NewChatScreen> {
final _selectedUsers = <User>{}; final _selectedUsers = <User>{};
final _searchFocusNode = FocusNode();
final _messageInputFocusNode = FocusNode();
bool _isSearchActive = false; bool _isSearchActive = false;
Channel channel; Channel channel;
Timer _debounce; Timer _debounce;
bool _showUserList = true;
void _userNameListener() { void _userNameListener() {
if (_debounce?.isActive ?? false) _debounce.cancel(); if (_debounce?.isActive ?? false) _debounce.cancel();
_debounce = Timer(const Duration(milliseconds: 350), () { _debounce = Timer(const Duration(milliseconds: 350), () {
@@ -305,10 +315,50 @@ class _NewChatScreenState extends State<NewChatScreen> {
super.initState(); super.initState();
channel = StreamChat.of(context).client.channel('messaging'); channel = StreamChat.of(context).client.channel('messaging');
_controller = TextEditingController()..addListener(_userNameListener); _controller = TextEditingController()..addListener(_userNameListener);
_searchFocusNode.addListener(() async {
if (_searchFocusNode.hasFocus && !_showUserList) {
if (channel.extraData['draft'] == true) {
await channel.stopWatching();
channel.dispose();
channel.client.state.channels.remove(channel.cid);
}
setState(() {
_showUserList = true;
});
}
});
_messageInputFocusNode.addListener(() async {
if (_messageInputFocusNode.hasFocus && _selectedUsers.isNotEmpty) {
final chatState = StreamChat.of(context);
channel = chatState.client.channel(
'messaging',
extraData: {
'members': [
..._selectedUsers.map((e) => e.id),
chatState.user.id,
],
'draft': true,
},
);
if (!chatState.client.state.channels.containsKey(channel.cid)) {
await channel.watch();
}
setState(() {
_showUserList = false;
});
}
});
} }
@override @override
void dispose() { void dispose() {
_searchFocusNode.dispose();
_messageInputFocusNode.dispose();
_controller?.clear(); _controller?.clear();
_controller?.removeListener(_userNameListener); _controller?.removeListener(_userNameListener);
_controller?.dispose(); _controller?.dispose();
@@ -335,14 +385,13 @@ class _NewChatScreenState extends State<NewChatScreen> {
body: StreamChannel( body: StreamChannel(
showLoading: false, showLoading: false,
channel: channel, channel: channel,
child: UsersBloc(
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
ChipsInputTextField<User>( ChipsInputTextField<User>(
key: _chipInputTextFieldStateKey, key: _chipInputTextFieldStateKey,
controller: _controller, controller: _controller,
focusNode: FocusNode(), focusNode: _searchFocusNode,
chipBuilder: (context, user) { chipBuilder: (context, user) {
return Stack( return Stack(
alignment: AlignmentDirectional.centerStart, alignment: AlignmentDirectional.centerStart,
@@ -410,6 +459,7 @@ class _NewChatScreenState extends State<NewChatScreen> {
), ),
), ),
), ),
if (_showUserList)
Container( Container(
width: double.maxFinite, width: double.maxFinite,
decoration: BoxDecoration( decoration: BoxDecoration(
@@ -439,6 +489,8 @@ class _NewChatScreenState extends State<NewChatScreen> {
), ),
), ),
Expanded( Expanded(
child: _showUserList
? UsersBloc(
child: UserListView( child: UserListView(
selectedUsers: _selectedUsers, selectedUsers: _selectedUsers,
groupAlphabetically: _isSearchActive ? false : true, groupAlphabetically: _isSearchActive ? false : true,
@@ -459,7 +511,10 @@ class _NewChatScreenState extends State<NewChatScreen> {
if (_userNameQuery.isNotEmpty) if (_userNameQuery.isNotEmpty)
'name': { 'name': {
r'$autocomplete': _userNameQuery, r'$autocomplete': _userNameQuery,
} },
'id': {
r'$ne': StreamChat.of(context).user.id,
},
}, },
sort: [ sort: [
SortOption( SortOption(
@@ -487,7 +542,8 @@ class _NewChatScreenState extends State<NewChatScreen> {
color: Colors.grey, color: Colors.grey,
), ),
), ),
Text('No user matches these keywords...'), Text(
'No user matches these keywords...'),
], ],
), ),
), ),
@@ -497,19 +553,31 @@ class _NewChatScreenState extends State<NewChatScreen> {
); );
}, },
), ),
)
: MessageListView(),
), ),
MessageInput( MessageInput(
preMessageSending: (message) async { focusNode: _messageInputFocusNode,
channel.extraData = { onMessageSent: (m) {
'members': [ if (!m.isEphemeral) {
..._selectedUsers.map((e) => e.id), _updateChannelAndNavigate(context);
channel.client.state.user.id, } else {
], channel.on('message.new').first.then((_) {
}; _updateChannelAndNavigate(context);
await channel.watch(); });
return message; }
}, },
onMessageSent: (_) { ),
],
),
),
);
}
void _updateChannelAndNavigate(BuildContext context) {
channel.update({
'draft': false,
});
Navigator.pushReplacement( Navigator.pushReplacement(
context, context,
MaterialPageRoute( MaterialPageRoute(
@@ -521,13 +589,6 @@ class _NewChatScreenState extends State<NewChatScreen> {
}, },
), ),
); );
},
),
],
),
),
),
);
} }
} }
@@ -761,6 +822,9 @@ class _NewGroupChatScreenState extends State<NewGroupChatScreen> {
if (_userNameQuery.isNotEmpty) if (_userNameQuery.isNotEmpty)
'name': { 'name': {
r'$autocomplete': _userNameQuery, r'$autocomplete': _userNameQuery,
},
'id': {
r'$ne': StreamChat.of(context).user.id,
} }
}, },
sort: [ sort: [
@@ -832,10 +896,12 @@ class _GroupChatDetailsScreenState extends State<GroupChatDetailsScreen> {
void _groupNameListener() { void _groupNameListener() {
final name = _groupNameController.text; final name = _groupNameController.text;
if (mounted) {
setState(() { setState(() {
_isGroupNameEmpty = name.isEmpty; _isGroupNameEmpty = name.isEmpty;
}); });
} }
}
@override @override
void initState() { void initState() {
+1 -1
View File
@@ -1,6 +1,6 @@
name: example name: example
description: A new Flutter project. description: A new Flutter project.
version: 1.0.61+63 version: 1.0.62+64
environment: environment:
sdk: ">=2.2.2 <3.0.0" sdk: ">=2.2.2 <3.0.0"
+7 -1
View File
@@ -104,6 +104,7 @@ class MessageInput extends StatefulWidget {
this.actions, this.actions,
this.actionsLocation = ActionsLocation.left, this.actionsLocation = ActionsLocation.left,
this.attachmentThumbnailBuilders, this.attachmentThumbnailBuilders,
this.focusNode,
}) : super(key: key); }) : super(key: key);
/// Message to edit /// Message to edit
@@ -149,6 +150,9 @@ class MessageInput extends StatefulWidget {
/// Map that defines a thumbnail builder for an attachment type /// Map that defines a thumbnail builder for an attachment type
final Map<String, AttachmentThumbnailBuilder> attachmentThumbnailBuilders; final Map<String, AttachmentThumbnailBuilder> attachmentThumbnailBuilders;
/// The focus node associated to the TextField
final FocusNode focusNode;
@override @override
MessageInputState createState() => MessageInputState(); MessageInputState createState() => MessageInputState();
@@ -169,10 +173,10 @@ class MessageInput extends StatefulWidget {
class MessageInputState extends State<MessageInput> { class MessageInputState extends State<MessageInput> {
final List<_SendingAttachment> _attachments = []; final List<_SendingAttachment> _attachments = [];
final _focusNode = FocusNode();
final List<User> _mentionedUsers = []; final List<User> _mentionedUsers = [];
final _imagePicker = ImagePicker(); final _imagePicker = ImagePicker();
FocusNode _focusNode;
bool _inputEnabled = true; bool _inputEnabled = true;
bool _messageIsPresent = false; bool _messageIsPresent = false;
bool _animateContainer = true; bool _animateContainer = true;
@@ -1708,6 +1712,8 @@ class MessageInputState extends State<MessageInput> {
void initState() { void initState() {
super.initState(); super.initState();
_focusNode = widget.focusNode ?? FocusNode();
_emojiNames = Emoji.all().map((e) => e.name); _emojiNames = Emoji.all().map((e) => e.name);
if (!kIsWeb) { if (!kIsWeb) {
+19
View File
@@ -180,7 +180,26 @@ class _MessageListViewState extends State<MessageListView> {
? streamChannel.channel.state.threads[widget.parentMessage.id] ? streamChannel.channel.state.threads[widget.parentMessage.id]
: streamChannel.channel.state.messages, : streamChannel.channel.state.messages,
builder: (context, snapshot) { builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(),
);
}
final messages = snapshot.data?.reversed?.toList() ?? []; final messages = snapshot.data?.reversed?.toList() ?? [];
if (messages.isEmpty) {
return Center(
child: Text(
'No chats here yet...',
style: TextStyle(
fontSize: 12,
color: Colors.black.withOpacity(.5),
),
),
);
}
return Stack( return Stack(
alignment: Alignment.center, alignment: Alignment.center,
children: [ children: [
+3 -3
View File
@@ -4,9 +4,9 @@ import 'package:stream_chat_flutter/src/utils.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart'; import 'package:stream_chat_flutter/stream_chat_flutter.dart';
class UrlAttachment extends StatelessWidget { class UrlAttachment extends StatelessWidget {
Attachment urlAttachment; final Attachment urlAttachment;
String hostDisplayName; final String hostDisplayName;
EdgeInsets textPadding; final EdgeInsets textPadding;
UrlAttachment({ UrlAttachment({
@required this.urlAttachment, @required this.urlAttachment,