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

Can delete a conversation from the list of conversations

This commit is contained in:
Pierre HUBERT 2019-05-01 10:52:10 +02:00
parent fcc8c2faa4
commit db02a41b75
3 changed files with 80 additions and 2 deletions

View File

@ -61,6 +61,19 @@ class ConversationsHelper {
return response.code == 200; return response.code == 200;
} }
/// Delete a conversation specified by its [id]
Future<bool> deleteConversation(int id) async {
final response = await APIRequest(
uri: "conversations/delete",
needLogin: true,
args: {
"conversationID": id.toString(),
},
).exec();
return response.code == 200;
}
/// Download the list of conversations from the server /// Download the list of conversations from the server
Future<ConversationsList> downloadList() async { Future<ConversationsList> downloadList() async {
final response = final response =

View File

@ -2,6 +2,7 @@ import 'package:comunic/enums/load_error_level.dart';
import 'package:comunic/helpers/conversations_helper.dart'; import 'package:comunic/helpers/conversations_helper.dart';
import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/conversations_list.dart'; import 'package:comunic/lists/conversations_list.dart';
import 'package:comunic/models/conversation.dart';
import 'package:comunic/ui/routes/conversation_route.dart'; import 'package:comunic/ui/routes/conversation_route.dart';
import 'package:comunic/ui/routes/create_conversation_route.dart'; import 'package:comunic/ui/routes/create_conversation_route.dart';
import 'package:comunic/ui/tiles/conversation_tile.dart'; import 'package:comunic/ui/tiles/conversation_tile.dart';
@ -105,6 +106,43 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
.push(MaterialPageRoute(builder: (c) => CreateConversationRoute())); .push(MaterialPageRoute(builder: (c) => CreateConversationRoute()));
} }
/// Handles 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),
),
)
],
);
},
);
if (result == null || !result) return;
// Request the conversation to be deleted now
if (!await _conversationsHelper.deleteConversation(conversation.id))
Scaffold.of(context).showSnackBar(
SnackBar(content: Text(tr("Could not delete the conversation!"))));
// Reload the list of conversations
_loadConversationsList(false);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_error == LoadErrorLevel.MAJOR) return _buildErrorCard(); if (_error == LoadErrorLevel.MAJOR) return _buildErrorCard();
@ -128,6 +166,7 @@ class _ConversationScreenState extends State<ConversationsListScreen> {
onOpen: (c) { onOpen: (c) {
_openConversation(context, c.id); _openConversation(context, c.id);
}, },
onRequestDelete: _requestDeleteConversation,
); );
}, },
itemCount: _list.length, itemCount: _list.length,

View File

@ -10,20 +10,26 @@ 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);
enum _PopupMenuChoices { DELETE }
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 RequestDeleteConversationCallback onRequestDelete;
const ConversationTile( const ConversationTile(
{Key key, {Key key,
@required this.conversation, @required this.conversation,
@required this.usersList, @required this.usersList,
@required this.onOpen}) @required this.onOpen,
@required this.onRequestDelete})
: assert(conversation != null), : assert(conversation != null),
assert(usersList != null), assert(usersList != null),
assert(onOpen != null), assert(onOpen != null),
assert(onRequestDelete != null),
super(key: key); super(key: key);
_buildSubInformation(IconData icon, String content) { _buildSubInformation(IconData icon, String content) {
@ -80,6 +86,26 @@ class ConversationTile extends StatelessWidget {
), ),
], ],
), ),
// Trailing information
trailing: PopupMenuButton<_PopupMenuChoices>(
itemBuilder: (b) => <PopupMenuEntry<_PopupMenuChoices>>[
PopupMenuItem(
child: Text(tr("Delete")),
value: _PopupMenuChoices.DELETE,
)
],
onSelected: _conversationMenuCallback,
),
); );
} }
/// Method called each time an option of the menu is selected
void _conversationMenuCallback(_PopupMenuChoices c) {
switch (c) {
case _PopupMenuChoices.DELETE:
onRequestDelete(conversation);
break;
}
}
} }