1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 06:53:23 +00:00

Can set to follow a friend

This commit is contained in:
Pierre HUBERT 2019-05-01 19:46:13 +02:00
parent 7c3390f8af
commit eba12736e9
3 changed files with 56 additions and 8 deletions

View File

@ -52,6 +52,20 @@ class FriendsHelper {
return response.code == 200; return response.code == 200;
} }
/// Update following status for a friend
Future<bool> setFollowing(int friendID, bool follow) async {
final response = await APIRequest(
uri: "friends/setFollowing",
needLogin: true,
args: {
"friendID" : friendID.toString(),
"follow": follow.toString()
}
).exec();
return response.code == 200;
}
/// Remove a friend from the list /// Remove a friend from the list
Future<bool> removeFriend(int friendID) async { Future<bool> removeFriend(int friendID) async {
final response = await APIRequest( final response = await APIRequest(

View File

@ -111,6 +111,7 @@ class _FriendsListScreenState extends SafeState<FriendsListScreen> {
? AcceptedFriendTile( ? AcceptedFriendTile(
friend: _friendsList[i], friend: _friendsList[i],
user: _usersInfo.getUser(_friendsList[i].id), user: _usersInfo.getUser(_friendsList[i].id),
onSetFollowing: _setFollowingFriend,
onRequestDelete: _deleteFriend, onRequestDelete: _deleteFriend,
) )
: PendingFriendTile( : PendingFriendTile(
@ -134,6 +135,18 @@ class _FriendsListScreenState extends SafeState<FriendsListScreen> {
_loadList(); _loadList();
} }
/// Update following status of a friend
Future<void> _setFollowingFriend(Friend friend, bool follow) async {
loading = true;
if(!await _friendsHelper.setFollowing(friend.id, follow))
showSimpleSnack(context, tr("Could not update following status!"));
_loadList();
}
/// Handles deletion request of a friend /// Handles deletion request of a friend
Future<void> _deleteFriend(Friend f) async { Future<void> _deleteFriend(Friend f) async {
final choice = await showDialog<bool>( final choice = await showDialog<bool>(

View File

@ -9,23 +9,27 @@ import 'package:flutter/material.dart';
/// ///
/// @author Pierre HUBERT /// @author Pierre HUBERT
enum _FriendMenuChoices { REMOVE } enum _FriendMenuChoices { REMOVE, TOGGLE_FOLLOWING }
typedef OnRequestDeleteFriend = void Function(Friend); typedef OnRequestDeleteFriend = void Function(Friend);
typedef OnSetFollowing = void Function(Friend, bool);
class AcceptedFriendTile extends StatelessWidget { class AcceptedFriendTile extends StatelessWidget {
final Friend friend; final Friend friend;
final User user; final User user;
final OnRequestDeleteFriend onRequestDelete; final OnRequestDeleteFriend onRequestDelete;
final OnSetFollowing onSetFollowing;
const AcceptedFriendTile( const AcceptedFriendTile({
{Key key, Key key,
@required this.friend, @required this.friend,
@required this.user, @required this.user,
@required this.onRequestDelete}) @required this.onRequestDelete,
: assert(friend != null), @required this.onSetFollowing,
}) : assert(friend != null),
assert(user != null), assert(user != null),
assert(onRequestDelete != null), assert(onRequestDelete != null),
assert(onSetFollowing != null),
super(key: key); super(key: key);
@override @override
@ -45,10 +49,22 @@ class AcceptedFriendTile extends StatelessWidget {
), ),
trailing: PopupMenuButton<_FriendMenuChoices>( trailing: PopupMenuButton<_FriendMenuChoices>(
itemBuilder: (c) => [ itemBuilder: (c) => [
PopupMenuItem(
child: Row(
children: <Widget>[
Icon(friend.following
? Icons.check_box
: Icons.check_box_outline_blank),
Container(width: 10.0),
Text(friend.following ? tr("Following") : tr("Follow")),
],
),
value: _FriendMenuChoices.TOGGLE_FOLLOWING,
),
PopupMenuItem( PopupMenuItem(
child: Text(tr("Remove")), child: Text(tr("Remove")),
value: _FriendMenuChoices.REMOVE, value: _FriendMenuChoices.REMOVE,
) ),
], ],
onSelected: _selectedMenuOption, onSelected: _selectedMenuOption,
), ),
@ -59,6 +75,11 @@ class AcceptedFriendTile extends StatelessWidget {
if (value == null) return; if (value == null) return;
switch (value) { switch (value) {
//Toggle following status
case _FriendMenuChoices.TOGGLE_FOLLOWING:
onSetFollowing(friend, !friend.following);
break;
//Delete friend //Delete friend
case _FriendMenuChoices.REMOVE: case _FriendMenuChoices.REMOVE:
onRequestDelete(friend); onRequestDelete(friend);