1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 04:49:21 +00:00

Delete conversation => leave conversation

This commit is contained in:
Pierre HUBERT 2021-03-13 12:07:16 +01:00
parent 4614f3ae2e
commit ed9f5e396c
3 changed files with 22 additions and 36 deletions

View File

@ -50,6 +50,9 @@ class Conversation extends SerializableElement<Conversation> {
/// Check out whether current user of the application is an admin
bool get isAdmin => membership.isAdmin;
/// Check if current user is the last admin of the conversation
bool get isLastAdmin => isAdmin && adminsID.length == 1;
/// Check it current user is following the conversation or not
bool get following => membership.following;

View File

@ -123,30 +123,13 @@ class _ConversationScreenState extends SafeState<ConversationsListScreen> {
}
/// Handle conversation deletion request
Future<void> _requestDeleteConversation(Conversation conversation) async {
final result = await showDialog<bool>(
context: context,
builder: (c) {
return AlertDialog(
title: Text(tr("Delete conversation")),
content: Text(tr(
"Do you really want to remove this conversation from your list of conversations ? If you are the owner of this conversation, it will be completely deleted!")),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.pop(context, false),
child: Text(tr("cancel").toUpperCase()),
),
FlatButton(
onPressed: () => Navigator.pop(context, true),
child: Text(
tr("delete").toUpperCase(),
style: TextStyle(color: Colors.red),
),
)
],
);
},
);
Future<void> _requestLeaveConversation(Conversation conversation) async {
final result = await showConfirmDialog(
context: context,
message: conversation.isLastAdmin
? tr(
"Do you really want to leave this conversation ? As you are its last admin, it will be completely deleted!")
: tr("Do you really want to leave this conversation ?"));
if (result == null || !result) return;
@ -154,9 +137,9 @@ class _ConversationScreenState extends SafeState<ConversationsListScreen> {
try {
await _conversationsHelper.deleteConversation(conversation.id);
} catch (e, s) {
print("Failed to delete conversation! $e => $s");
print("Failed to leave conversation! $e => $s");
Scaffold.of(context).showSnackBar(
SnackBar(content: Text(tr("Could not delete the conversation!"))));
SnackBar(content: Text(tr("Could not leave the conversation!"))));
}
// Reload the list of conversations
@ -191,7 +174,7 @@ class _ConversationScreenState extends SafeState<ConversationsListScreen> {
_openConversation(c.id);
},
onRequestUpdate: _updateConversation,
onRequestDelete: _requestDeleteConversation,
onRequestLeave: _requestLeaveConversation,
);
},
itemCount: _list.length,

View File

@ -13,17 +13,17 @@ import 'package:flutter/material.dart';
/// @author Pierre HUBERT
typedef OpenConversationCallback = void Function(Conversation);
typedef RequestDeleteConversationCallback = void Function(Conversation);
typedef RequestLeaveConversationCallback = void Function(Conversation);
typedef RequestUpdateConversationCallback = void Function(Conversation);
enum _PopupMenuChoices { UPDATE, DELETE }
enum _PopupMenuChoices { UPDATE, LEAVE }
class ConversationTile extends StatelessWidget {
final Conversation conversation;
final UsersList usersList;
final OpenConversationCallback onOpen;
final RequestUpdateConversationCallback onRequestUpdate;
final RequestDeleteConversationCallback onRequestDelete;
final RequestLeaveConversationCallback onRequestLeave;
const ConversationTile({
Key key,
@ -31,12 +31,12 @@ class ConversationTile extends StatelessWidget {
@required this.usersList,
@required this.onOpen,
@required this.onRequestUpdate,
@required this.onRequestDelete,
@required this.onRequestLeave,
}) : assert(conversation != null),
assert(usersList != null),
assert(onOpen != null),
assert(onRequestUpdate != null),
assert(onRequestDelete != null),
assert(onRequestLeave != null),
super(key: key);
_buildSubInformation(IconData icon, String content) {
@ -110,8 +110,8 @@ class ConversationTile extends StatelessWidget {
value: _PopupMenuChoices.UPDATE,
),
PopupMenuItem(
child: Text(tr("Delete")),
value: _PopupMenuChoices.DELETE,
child: Text(tr("Leave")),
value: _PopupMenuChoices.LEAVE,
)
]).then(_conversationMenuCallback);
},
@ -140,8 +140,8 @@ class ConversationTile extends StatelessWidget {
onRequestUpdate(conversation);
break;
case _PopupMenuChoices.DELETE:
onRequestDelete(conversation);
case _PopupMenuChoices.LEAVE:
onRequestLeave(conversation);
break;
}
}