fix delete conversation logic

This commit is contained in:
Salvatore Giordano
2020-12-10 14:42:15 +01:00
parent b85ed04170
commit 0534488b94
5 changed files with 158 additions and 167 deletions
+36 -36
View File
@@ -69,40 +69,26 @@ class ChannelBottomSheet extends StatelessWidget {
), ),
), ),
Divider(), Divider(),
StreamBuilder<bool>(
stream: channel.isMutedStream,
initialData: channel.isMuted,
builder: (context, snapshot) {
return ListTile(
leading: StreamSvgIcon.mute(
size: 22,
color: StreamChatTheme.of(context).primaryIconTheme.color,
),
title: Text('Mute ${channel.isGroup ? 'group' : 'user'}'),
trailing: Switch(
onChanged: (bool muted) async {
if (muted) {
await channel.mute();
} else {
await channel.unmute();
}
},
value: snapshot.data,
),
);
}),
Divider(),
if (channel.isGroup && !channel.isDistinct) if (channel.isGroup && !channel.isDistinct)
ListTile( ListTile(
leading: StreamSvgIcon.userRemove( leading: StreamSvgIcon.userRemove(
size: 22, size: 24,
color: Colors.black, color: Color(0xff7A7A7A),
),
title: Text(
'Leave Group',
style: TextStyle(fontWeight: FontWeight.bold),
), ),
title: Text('Leave Group'),
onTap: () async { onTap: () async {
final confirm = await showConfirmationDialog( final confirm = await showConfirmationDialog(
context, context,
'Do you want to leave the group?', title: 'Leave Group',
okText: 'LEAVE',
question: 'Are you sure you want to leave this group?',
cancelText: 'CANCEL',
icon: StreamSvgIcon.userRemove(
color: Colors.red,
),
); );
if (confirm == true) { if (confirm == true) {
await channel await channel
@@ -111,11 +97,17 @@ class ChannelBottomSheet extends StatelessWidget {
} }
}, },
), ),
if (!channel.isGroup && !channel.isDistinct) if ([
'admin',
'owner',
].contains(channel.state.members
.firstWhere((m) => m.userId == channel.client.state.user.id,
orElse: () => null)
?.role))
ListTile( ListTile(
leading: Icon( leading: StreamSvgIcon.delete(
Icons.delete_outline,
color: Color(0xFFFF3742), color: Color(0xFFFF3742),
size: 24,
), ),
title: Text( title: Text(
'Delete chat', 'Delete chat',
@@ -124,14 +116,22 @@ class ChannelBottomSheet extends StatelessWidget {
), ),
), ),
onTap: () async { onTap: () async {
final confirm = await showConfirmationDialog( final res = await showConfirmationDialog(
context, context,
'Do you want to delete the chat?', title: 'Delete Conversation',
okText: 'DELETE',
question:
'Are you sure you want to delete this conversation?',
cancelText: 'CANCEL',
icon: StreamSvgIcon.delete(
color: Colors.red,
),
); );
if (confirm == true) { var channel = StreamChannel.of(context).channel;
await channel if (res == true) {
.removeMembers([StreamChat.of(context).user.id]); await channel.delete().then((value) {
Navigator.pop(context); Navigator.pop(context);
});
} }
}, },
), ),
+6 -2
View File
@@ -100,7 +100,7 @@ class ChannelHeader extends StatelessWidget implements PreferredSizeWidget {
child: Center( child: Center(
child: ChannelImage( child: ChannelImage(
onTap: onImageTap ?? onTap: onImageTap ??
() { () async {
if (channel.memberCount == 2 && channel.isDistinct) { if (channel.memberCount == 2 && channel.isDistinct) {
final currentUser = StreamChat.of(context).user; final currentUser = StreamChat.of(context).user;
final otherUser = channel.state.members.firstWhere( final otherUser = channel.state.members.firstWhere(
@@ -108,7 +108,7 @@ class ChannelHeader extends StatelessWidget implements PreferredSizeWidget {
orElse: () => null, orElse: () => null,
); );
if (otherUser != null) { if (otherUser != null) {
Navigator.push( final pop = await Navigator.push(
context, context,
MaterialPageRoute( MaterialPageRoute(
builder: (context) => StreamChannel( builder: (context) => StreamChannel(
@@ -119,6 +119,10 @@ class ChannelHeader extends StatelessWidget implements PreferredSizeWidget {
), ),
), ),
); );
if (pop == true) {
Navigator.pop(context);
}
} }
} }
}, },
+22 -33
View File
@@ -524,44 +524,33 @@ class _ChannelListViewState extends State<ChannelListView>
); );
}, },
), ),
IconSlideAction( if ([
color: backgroundColor, 'admin',
iconWidget: StreamSvgIcon.mute(), 'owner',
onTap: () async { ].contains(channel.state.members
if (!channel.isMuted) { .firstWhere(
await channel.mute(); (m) => m.userId == channel.client.state.user.id,
} else { orElse: () => null)
await channel.unmute(); ?.role))
}
},
),
if (channel.isGroup && !channel.isDistinct)
IconSlideAction( IconSlideAction(
color: backgroundColor, color: backgroundColor,
iconWidget: StreamSvgIcon.userRemove(), iconWidget: StreamSvgIcon.delete(
color: Color(0xFFFF3742),
),
onTap: () async { onTap: () async {
final confirm = await showConfirmationDialog( final res = await showConfirmationDialog(
context, context,
'Do you want to leave the group?', title: 'Delete Conversation',
okText: 'DELETE',
question:
'Are you sure you want to delete this conversation?',
cancelText: 'CANCEL',
icon: StreamSvgIcon.delete(
color: Color(0xFFFF3742),
),
); );
if (confirm == true) { if (res == true) {
await channel await channel.delete();
.removeMembers([StreamChat.of(context).user.id]);
}
},
),
if (!channel.isGroup && !channel.isDistinct)
IconSlideAction(
color: backgroundColor,
icon: Icons.delete_outline,
onTap: () async {
final confirm = await showConfirmationDialog(
context,
'Do you want to delete the chat?',
);
if (confirm == true) {
await channel
.removeMembers([StreamChat.of(context).user.id]);
} }
}, },
), ),
+25 -72
View File
@@ -19,6 +19,7 @@ class ChatInfoScreen extends StatefulWidget {
class _ChatInfoScreenState extends State<ChatInfoScreen> { class _ChatInfoScreenState extends State<ChatInfoScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final channel = StreamChannel.of(context).channel;
return Scaffold( return Scaffold(
backgroundColor: Color(0xFFe6e6e6), backgroundColor: Color(0xFFe6e6e6),
body: ListView( body: ListView(
@@ -31,7 +32,14 @@ class _ChatInfoScreenState extends State<ChatInfoScreen> {
SizedBox( SizedBox(
height: 8.0, height: 8.0,
), ),
_buildDeleteListTile(), if ([
'admin',
'owner',
].contains(channel.state.members
.firstWhere((m) => m.userId == channel.client.state.user.id,
orElse: () => null)
?.role))
_buildDeleteListTile(),
], ],
), ),
); );
@@ -212,78 +220,23 @@ class _ChatInfoScreenState extends State<ChatInfoScreen> {
); );
} }
void _showDeleteDialog() { void _showDeleteDialog() async {
final res = await showConfirmationDialog(
context,
title: 'Delete Conversation',
okText: 'DELETE',
question: 'Are you sure you want to delete this conversation?',
cancelText: 'CANCEL',
icon: StreamSvgIcon.delete(
color: Colors.red,
),
);
var channel = StreamChannel.of(context).channel; var channel = StreamChannel.of(context).channel;
if (res == true) {
showModalBottomSheet( await channel.delete().then((value) {
backgroundColor: Colors.white, Navigator.pop(context);
context: context, });
shape: RoundedRectangleBorder( }
borderRadius: BorderRadius.only(
topLeft: Radius.circular(16.0),
topRight: Radius.circular(16.0),
)),
builder: (context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
height: 26.0,
),
StreamSvgIcon.delete(
color: Colors.red,
),
SizedBox(
height: 26.0,
),
Text(
'Delete Conversation',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16.0),
),
SizedBox(
height: 7.0,
),
Text('Are you sure you want to delete this conversation?'),
SizedBox(
height: 36.0,
),
Container(
color: Color(0xffe6e6e6),
height: 1.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
FlatButton(
child: Text(
'CANCEL',
style: TextStyle(
color: Colors.black.withOpacity(0.5),
fontWeight: FontWeight.w400),
),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text(
'DELETE',
style: TextStyle(
color: Colors.red, fontWeight: FontWeight.w400),
),
onPressed: () {
channel.delete().then((value) {
Navigator.pop(context);
Navigator.pop(context);
Navigator.pop(context);
});
},
),
],
),
],
);
});
} }
Widget _buildConnectedTitleState() { Widget _buildConnectedTitleState() {
+69 -24
View File
@@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
import 'package:stream_chat/stream_chat.dart'; import 'package:stream_chat/stream_chat.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
import '../stream_chat_flutter.dart';
Future<void> launchURL(BuildContext context, String url) async { Future<void> launchURL(BuildContext context, String url) async {
if (await canLaunch(url)) { if (await canLaunch(url)) {
await launch(url); await launch(url);
@@ -15,33 +17,76 @@ Future<void> launchURL(BuildContext context, String url) async {
} }
Future<bool> showConfirmationDialog( Future<bool> showConfirmationDialog(
BuildContext context, BuildContext context, {
String title,
Widget icon,
String question, String question,
) { String okText,
return showDialog<bool>( String cancelText,
context: context, }) {
builder: (context) { return showModalBottomSheet(
return AlertDialog( backgroundColor: Colors.white,
title: Text(question), context: context,
actions: <Widget>[ shape: RoundedRectangleBorder(
FlatButton( borderRadius: BorderRadius.only(
child: Text('Ok'), topLeft: Radius.circular(16.0),
onPressed: () => Navigator.pop( topRight: Radius.circular(16.0),
context, )),
true, builder: (context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
height: 26.0,
), ),
), if (icon != null) icon,
FlatButton( SizedBox(
child: Text('Cancel'), height: 26.0,
onPressed: () => Navigator.pop(
context,
false,
), ),
), Text(
], title,
); style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16.0),
}, ),
); SizedBox(
height: 7.0,
),
Text(question),
SizedBox(
height: 36.0,
),
Container(
color: Color(0xffe6e6e6),
height: 1.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
FlatButton(
child: Text(
cancelText,
style: TextStyle(
color: Colors.black.withOpacity(0.5),
fontWeight: FontWeight.w400),
),
onPressed: () {
Navigator.of(context).pop();
},
),
FlatButton(
child: Text(
okText,
style: TextStyle(
color: Colors.red, fontWeight: FontWeight.w400),
),
onPressed: () {
Navigator.pop(context, true);
},
),
],
),
],
);
});
} }
/// Get random png with initials /// Get random png with initials