1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 12:59: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 /// Check out whether current user of the application is an admin
bool get isAdmin => membership.isAdmin; 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 /// Check it current user is following the conversation or not
bool get following => membership.following; bool get following => membership.following;

View File

@ -123,30 +123,13 @@ class _ConversationScreenState extends SafeState<ConversationsListScreen> {
} }
/// Handle conversation deletion request /// Handle conversation deletion request
Future<void> _requestDeleteConversation(Conversation conversation) async { Future<void> _requestLeaveConversation(Conversation conversation) async {
final result = await showDialog<bool>( final result = await showConfirmDialog(
context: context, context: context,
builder: (c) { message: conversation.isLastAdmin
return AlertDialog( ? tr(
title: Text(tr("Delete conversation")), "Do you really want to leave this conversation ? As you are its last admin, it will be completely deleted!")
content: Text(tr( : tr("Do you really want to leave this conversation ?"));
"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),
),
)
],
);
},
);
if (result == null || !result) return; if (result == null || !result) return;
@ -154,9 +137,9 @@ class _ConversationScreenState extends SafeState<ConversationsListScreen> {
try { try {
await _conversationsHelper.deleteConversation(conversation.id); await _conversationsHelper.deleteConversation(conversation.id);
} catch (e, s) { } catch (e, s) {
print("Failed to delete conversation! $e => $s"); print("Failed to leave conversation! $e => $s");
Scaffold.of(context).showSnackBar( 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 // Reload the list of conversations
@ -191,7 +174,7 @@ class _ConversationScreenState extends SafeState<ConversationsListScreen> {
_openConversation(c.id); _openConversation(c.id);
}, },
onRequestUpdate: _updateConversation, onRequestUpdate: _updateConversation,
onRequestDelete: _requestDeleteConversation, onRequestLeave: _requestLeaveConversation,
); );
}, },
itemCount: _list.length, itemCount: _list.length,

View File

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