From ed9f5e396ce4237a0e57e0fca75a56015d9f13d7 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 13 Mar 2021 12:07:16 +0100 Subject: [PATCH] Delete conversation => leave conversation --- lib/models/conversation.dart | 3 ++ lib/ui/screens/conversations_list_screen.dart | 37 +++++-------------- lib/ui/tiles/conversation_tile.dart | 18 ++++----- 3 files changed, 22 insertions(+), 36 deletions(-) diff --git a/lib/models/conversation.dart b/lib/models/conversation.dart index 220be60..e5b847a 100644 --- a/lib/models/conversation.dart +++ b/lib/models/conversation.dart @@ -50,6 +50,9 @@ class Conversation extends SerializableElement { /// 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; diff --git a/lib/ui/screens/conversations_list_screen.dart b/lib/ui/screens/conversations_list_screen.dart index e1d4991..253189f 100644 --- a/lib/ui/screens/conversations_list_screen.dart +++ b/lib/ui/screens/conversations_list_screen.dart @@ -123,30 +123,13 @@ class _ConversationScreenState extends SafeState { } /// Handle conversation deletion request - Future _requestDeleteConversation(Conversation conversation) async { - final result = await showDialog( - 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: [ - 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 _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 { 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 { _openConversation(c.id); }, onRequestUpdate: _updateConversation, - onRequestDelete: _requestDeleteConversation, + onRequestLeave: _requestLeaveConversation, ); }, itemCount: _list.length, diff --git a/lib/ui/tiles/conversation_tile.dart b/lib/ui/tiles/conversation_tile.dart index c2b69fd..39454e4 100644 --- a/lib/ui/tiles/conversation_tile.dart +++ b/lib/ui/tiles/conversation_tile.dart @@ -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; } }