From 7c3390f8af3e739ab3294328f2a2404d6d82dd3c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 1 May 2019 19:29:46 +0200 Subject: [PATCH] Can remove a friend from the list of friends --- lib/helpers/friends_helper.dart | 11 ++++++ lib/ui/screens/friends_list_screen.dart | 46 ++++++++++++++++++++++--- lib/ui/tiles/accepted_friend_tile.dart | 33 ++++++++++++++++-- lib/utils/ui_utils.dart | 6 ++++ 4 files changed, 90 insertions(+), 6 deletions(-) diff --git a/lib/helpers/friends_helper.dart b/lib/helpers/friends_helper.dart index e58bc67..4b20558 100644 --- a/lib/helpers/friends_helper.dart +++ b/lib/helpers/friends_helper.dart @@ -51,4 +51,15 @@ class FriendsHelper { return response.code == 200; } + + /// Remove a friend from the list + Future removeFriend(int friendID) async { + final response = await APIRequest( + uri: "friends/remove", + needLogin: true, + args: {"friendID" : friendID.toString()} + ).exec(); + + return response.code == 200; + } } diff --git a/lib/ui/screens/friends_list_screen.dart b/lib/ui/screens/friends_list_screen.dart index 9914c69..5f0e58f 100644 --- a/lib/ui/screens/friends_list_screen.dart +++ b/lib/ui/screens/friends_list_screen.dart @@ -39,8 +39,10 @@ class _FriendsListScreenState extends SafeState { set loading(bool loading) => setState(() => _loading = loading); - void _gotError() => - error = _friendsList == null ? _ErrorsLevel.MAJOR : _ErrorsLevel.MINOR; + void _gotError() { + loading = false; + error = _friendsList == null ? _ErrorsLevel.MAJOR : _ErrorsLevel.MINOR; + } @override void didChangeDependencies() { @@ -109,6 +111,7 @@ class _FriendsListScreenState extends SafeState { ? AcceptedFriendTile( friend: _friendsList[i], user: _usersInfo.getUser(_friendsList[i].id), + onRequestDelete: _deleteFriend, ) : PendingFriendTile( friend: _friendsList[i], @@ -125,10 +128,45 @@ class _FriendsListScreenState extends SafeState { loading = true; if (!await _friendsHelper.respondRequest(f.id, accept)) - Scaffold.of(context).showSnackBar(SnackBar( - content: Text(tr("Could not respond to friendship request!")))); + showSimpleSnack(context, tr("Could not respond to friendship request!")); // Load the list of friends again _loadList(); } + + /// Handles deletion request of a friend + Future _deleteFriend(Friend f) async { + final choice = await showDialog( + context: context, + builder: (b) => AlertDialog( + title: Text(tr("Delete friend")), + content: Text(tr( + "Are you sure do you want to remove this friend from your list of friends ? A friendship request will have to be sent to get this user back to your list!")), + actions: [ + FlatButton( + onPressed: () => Navigator.pop(context, false), + child: Text(tr("Cancel").toUpperCase()), + ), + FlatButton( + onPressed: () => Navigator.pop(context, true), + child: Text( + tr("Confirm").toUpperCase(), + style: TextStyle(color: Colors.red), + ), + ), + ], + ), + ); + + if (choice == null || !choice) return; + + // Forward the request to the server + loading = true; + if (!await _friendsHelper.removeFriend(f.id)) + showSimpleSnack( + context, tr("Could not delete this person from your friends list!")); + + // Refresh list + _loadList(); + } } diff --git a/lib/ui/tiles/accepted_friend_tile.dart b/lib/ui/tiles/accepted_friend_tile.dart index 921325b..6967730 100644 --- a/lib/ui/tiles/accepted_friend_tile.dart +++ b/lib/ui/tiles/accepted_friend_tile.dart @@ -9,14 +9,23 @@ import 'package:flutter/material.dart'; /// /// @author Pierre HUBERT +enum _FriendMenuChoices { REMOVE } + +typedef OnRequestDeleteFriend = void Function(Friend); + class AcceptedFriendTile extends StatelessWidget { final Friend friend; final User user; + final OnRequestDeleteFriend onRequestDelete; const AcceptedFriendTile( - {Key key, @required this.friend, @required this.user}) + {Key key, + @required this.friend, + @required this.user, + @required this.onRequestDelete}) : assert(friend != null), assert(user != null), + assert(onRequestDelete != null), super(key: key); @override @@ -32,8 +41,28 @@ class AcceptedFriendTile extends StatelessWidget { style: TextStyle(color: Colors.green), ) : Text( - diffTimeFromNowToStr(friend.lastActive), + diffTimeFromNowToStr(friend.lastActive), ), + trailing: PopupMenuButton<_FriendMenuChoices>( + itemBuilder: (c) => [ + PopupMenuItem( + child: Text(tr("Remove")), + value: _FriendMenuChoices.REMOVE, + ) + ], + onSelected: _selectedMenuOption, + ), ); } + + void _selectedMenuOption(_FriendMenuChoices value) { + if (value == null) return; + + switch (value) { + //Delete friend + case _FriendMenuChoices.REMOVE: + onRequestDelete(friend); + break; + } + } } diff --git a/lib/utils/ui_utils.dart b/lib/utils/ui_utils.dart index e72ee61..a999780 100644 --- a/lib/utils/ui_utils.dart +++ b/lib/utils/ui_utils.dart @@ -60,3 +60,9 @@ void showImageFullScreen(BuildContext context, String url) { })); } + + +/// Show simple snack +void showSimpleSnack(BuildContext context, String message) { + Scaffold.of(context).showSnackBar(SnackBar(content: Text(message))); +} \ No newline at end of file