< Summary

Class:UserContextMenu
Assembly:UserContextMenu
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/UserContextMenu/Scripts/UserContextMenu.cs
Covered lines:116
Uncovered lines:42
Coverable lines:158
Total lines:410
Line coverage:73.4% (116 of 158)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UserContextMenu()0%110100%
Show(...)0%110100%
Show(...)0%4.034087.5%
SetConfirmationDialog(...)0%2100%
Hide()0%110100%
Awake()0%220100%
Update()0%2100%
OnDisable()0%220100%
OnPassportButtonPressed()0%220100%
OnReportUserButtonPressed()0%220100%
OnDeleteUserButtonPressed()0%5.935066.67%
OnAddFriendButtonPressed()0%30500%
OnCancelFriendRequestButtonPressed()0%12300%
OnMessageButtonPressed()0%12300%
OnBlockUserButtonPressed()0%3.023087.5%
UpdateBlockButton()0%330100%
HideIfClickedOutside()0%12300%
ProcessActiveElements(...)0%110100%
Setup(...)0%12.1212090.48%
SetupFriendship(...)0%14.5813078.95%
OnFriendActionUpdate(...)0%7.047090.91%
OnValidate()0%2.062075%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/UserContextMenu/Scripts/UserContextMenu.cs

#LineLine coverage
 1using DCL.Interface;
 2using TMPro;
 3using UnityEngine;
 4using UnityEngine.UI;
 5
 6/// <summary>
 7/// Contextual menu with different options about an user.
 8/// </summary>
 9[RequireComponent(typeof(RectTransform))]
 10public class UserContextMenu : MonoBehaviour
 11{
 12    internal const string CURRENT_PLAYER_ID = "CurrentPlayerInfoCardId";
 13
 14    const string BLOCK_BTN_BLOCK_TEXT = "Block";
 15    const string BLOCK_BTN_UNBLOCK_TEXT = "Unblock";
 16    const string DELETE_MSG_PATTERN = "Are you sure you want to delete {0} as a friend?";
 17
 18    [System.Flags]
 19    public enum MenuConfigFlags
 20    {
 21        Name = 1,
 22        Friendship = 2,
 23        Message = 4,
 24        Passport = 8,
 25        Block = 16,
 26        Report = 32
 27    }
 28
 29    const MenuConfigFlags headerFlags = MenuConfigFlags.Name | MenuConfigFlags.Friendship;
 30    const MenuConfigFlags usesFriendsApiFlags = MenuConfigFlags.Friendship | MenuConfigFlags.Message;
 31
 32    [Header("Optional: Set Confirmation Dialog")]
 33    [SerializeField] internal UserContextConfirmationDialog confirmationDialog;
 34
 35    [Header("Enable Actions")]
 14536    [SerializeField] internal MenuConfigFlags menuConfigFlags = MenuConfigFlags.Passport | MenuConfigFlags.Block | MenuC
 37
 38    [Header("Containers")]
 39    [SerializeField] internal GameObject headerContainer;
 40    [SerializeField] internal GameObject bodyContainer;
 41    [SerializeField] internal GameObject friendshipContainer;
 42    [SerializeField] internal GameObject friendAddContainer;
 43    [SerializeField] internal GameObject friendRemoveContainer;
 44    [SerializeField] internal GameObject friendRequestedContainer;
 45
 46    [Header("Texts")]
 47    [SerializeField] internal TextMeshProUGUI userName;
 48    [SerializeField] internal TextMeshProUGUI blockText;
 49
 50    [Header("Buttons")]
 51    [SerializeField] internal Button passportButton;
 52    [SerializeField] internal Button blockButton;
 53    [SerializeField] internal Button reportButton;
 54    [SerializeField] internal Button addFriendButton;
 55    [SerializeField] internal Button cancelFriendButton;
 56    [SerializeField] internal Button deleteFriendButton;
 57    [SerializeField] internal Button messageButton;
 58
 59    public static event System.Action<string> OnOpenPrivateChatRequest;
 60
 061    public bool isVisible => gameObject.activeSelf;
 62
 63    public event System.Action OnShowMenu;
 64    public event System.Action<string> OnPassport;
 65    public event System.Action<string> OnReport;
 66    public event System.Action<string, bool> OnBlock;
 67    public event System.Action<string> OnUnfriend;
 68    public event System.Action<string> OnAddFriend;
 69    public event System.Action<string> OnCancelFriend;
 70    public event System.Action<string> OnMessage;
 71
 72    private static StringVariable currentPlayerId = null;
 73    private RectTransform rectTransform;
 74    private string userId;
 75    private bool isBlocked;
 76    private MenuConfigFlags currentConfigFlags;
 77    private IConfirmationDialog currentConfirmationDialog;
 78
 79    /// <summary>
 80    /// Show context menu
 81    /// </summary>
 82    /// <param name="userId"> user id</param>
 1683    public void Show(string userId) { Show(userId, menuConfigFlags); }
 84
 85    /// <summary>
 86    /// Show context menu
 87    /// </summary>
 88    /// <param name="userId"> user id</param>
 89    /// <param name="configFlags">set buttons to enable in menu</param>
 90    public void Show(string userId, MenuConfigFlags configFlags)
 91    {
 1092        this.userId = userId;
 1093        ProcessActiveElements(configFlags);
 1094        Setup(userId, configFlags);
 1095        if (currentConfirmationDialog == null && confirmationDialog != null)
 96        {
 097            SetConfirmationDialog(confirmationDialog);
 98        }
 1099        gameObject.SetActive(true);
 10100        OnShowMenu?.Invoke();
 1101    }
 102
 103    /// <summary>
 104    /// Set confirmation popup to reference use
 105    /// </summary>
 106    /// <param name="confirmationPopup">confirmation popup reference</param>
 0107    public void SetConfirmationDialog(IConfirmationDialog confirmationPopup) { this.currentConfirmationDialog = confirma
 108
 109    /// <summary>
 110    /// Hides the context menu.
 111    /// </summary>
 64112    public void Hide() { gameObject.SetActive(false); }
 113
 114    private void Awake()
 115    {
 11116        if (!currentPlayerId)
 117        {
 1118            currentPlayerId = Resources.Load<StringVariable>(CURRENT_PLAYER_ID);
 119        }
 120
 11121        rectTransform = GetComponent<RectTransform>();
 122
 11123        passportButton.onClick.AddListener(OnPassportButtonPressed);
 11124        blockButton.onClick.AddListener(OnBlockUserButtonPressed);
 11125        reportButton.onClick.AddListener(OnReportUserButtonPressed);
 11126        deleteFriendButton.onClick.AddListener(OnDeleteUserButtonPressed);
 11127        addFriendButton.onClick.AddListener(OnAddFriendButtonPressed);
 11128        cancelFriendButton.onClick.AddListener(OnCancelFriendRequestButtonPressed);
 11129        messageButton.onClick.AddListener(OnMessageButtonPressed);
 11130    }
 131
 0132    private void Update() { HideIfClickedOutside(); }
 133
 134    private void OnDisable()
 135    {
 11136        if (FriendsController.i)
 137        {
 11138            FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate;
 139        }
 11140    }
 141
 142    private void OnPassportButtonPressed()
 143    {
 2144        OnPassport?.Invoke(userId);
 2145        currentPlayerId.Set(userId);
 2146        Hide();
 147
 2148        AudioScriptableObjects.dialogOpen.Play(true);
 2149    }
 150
 151    private void OnReportUserButtonPressed()
 152    {
 2153        OnReport?.Invoke(userId);
 2154        WebInterface.SendReportPlayer(userId);
 2155        Hide();
 2156    }
 157
 158    private void OnDeleteUserButtonPressed()
 159    {
 1160        OnUnfriend?.Invoke(userId);
 1161        if (currentConfirmationDialog != null)
 162        {
 0163            currentConfirmationDialog.SetText(string.Format(DELETE_MSG_PATTERN, UserProfileController.userProfilesCatalo
 0164            currentConfirmationDialog.Show(() =>
 165            {
 0166                FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 167                {
 168                    userId = userId,
 169                    action = FriendshipAction.DELETED
 170                });
 171
 0172                WebInterface.UpdateFriendshipStatus(
 173                    new FriendsController.FriendshipUpdateStatusMessage()
 174                    {
 175                        action = FriendshipAction.DELETED,
 176                        userId = userId
 177                    });
 0178            });
 179        }
 1180        Hide();
 1181    }
 182
 183    private void OnAddFriendButtonPressed()
 184    {
 0185        OnAddFriend?.Invoke(userId);
 186
 0187        if (!FriendsController.i)
 188        {
 0189            return;
 190        }
 191
 192        // NOTE: if we don't add this, the friend request has strange behaviors
 0193        UserProfileController.i.AddUserProfileToCatalog(new UserProfileModel()
 194        {
 195            userId = userId,
 196            name = UserProfileController.userProfilesCatalog.Get(userId)?.userName
 197        });
 198
 0199        FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 200        {
 201            userId = userId,
 202            action = FriendshipAction.REQUESTED_TO
 203        });
 204
 0205        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 206        {
 207            userId = userId, action = FriendshipAction.REQUESTED_TO
 208        });
 0209    }
 210
 211    private void OnCancelFriendRequestButtonPressed()
 212    {
 0213        OnCancelFriend?.Invoke(userId);
 214
 0215        if (!FriendsController.i)
 216        {
 0217            return;
 218        }
 219
 0220        FriendsController.i.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 221        {
 222            userId = userId,
 223            action = FriendshipAction.CANCELLED
 224        });
 225
 0226        WebInterface.UpdateFriendshipStatus(new FriendsController.FriendshipUpdateStatusMessage()
 227        {
 228            userId = userId, action = FriendshipAction.CANCELLED
 229        });
 0230    }
 231
 232    private void OnMessageButtonPressed()
 233    {
 0234        OnMessage?.Invoke(userId);
 0235        OnOpenPrivateChatRequest?.Invoke(userId);
 0236        Hide();
 0237    }
 238
 239    private void OnBlockUserButtonPressed()
 240    {
 1241        bool blockUser = !isBlocked;
 1242        OnBlock?.Invoke(userId, blockUser);
 1243        if (blockUser)
 244        {
 1245            WebInterface.SendBlockPlayer(userId);
 1246        }
 247        else
 248        {
 0249            WebInterface.SendUnblockPlayer(userId);
 250        }
 1251        Hide();
 1252    }
 253
 16254    private void UpdateBlockButton() { blockText.text = isBlocked ? BLOCK_BTN_UNBLOCK_TEXT : BLOCK_BTN_BLOCK_TEXT; }
 255
 256    private void HideIfClickedOutside()
 257    {
 0258        if (Input.GetMouseButtonDown(0) &&
 259            !RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition))
 260        {
 0261            Hide();
 262        }
 0263    }
 264
 265    private void ProcessActiveElements(MenuConfigFlags flags)
 266    {
 88267        headerContainer.SetActive((flags & headerFlags) != 0);
 88268        userName.gameObject.SetActive((flags & MenuConfigFlags.Name) != 0);
 88269        friendshipContainer.SetActive((flags & MenuConfigFlags.Friendship) != 0);
 88270        deleteFriendButton.gameObject.SetActive((flags & MenuConfigFlags.Friendship) != 0);
 88271        passportButton.gameObject.SetActive((flags & MenuConfigFlags.Passport) != 0);
 88272        blockButton.gameObject.SetActive((flags & MenuConfigFlags.Block) != 0);
 88273        reportButton.gameObject.SetActive((flags & MenuConfigFlags.Report) != 0);
 88274        messageButton.gameObject.SetActive((flags & MenuConfigFlags.Message) != 0);
 88275    }
 276
 277    private void Setup(string userId, MenuConfigFlags configFlags)
 278    {
 10279        this.userId = userId;
 280
 10281        UserProfile profile = UserProfileController.userProfilesCatalog.Get(userId);
 10282        bool userHasWallet = profile?.hasConnectedWeb3 ?? false;
 283
 10284        if (!userHasWallet || !UserProfile.GetOwnUserProfile().hasConnectedWeb3)
 285        {
 2286            configFlags &= ~usesFriendsApiFlags;
 287        }
 288
 10289        this.currentConfigFlags = configFlags;
 10290        ProcessActiveElements(configFlags);
 291
 10292        if ((configFlags & MenuConfigFlags.Block) != 0)
 293        {
 8294            isBlocked = UserProfile.GetOwnUserProfile().blocked.Contains(userId);
 8295            UpdateBlockButton();
 296        }
 10297        if ((configFlags & MenuConfigFlags.Name) != 0)
 298        {
 8299            string name = profile?.userName;
 8300            userName.text = name;
 301        }
 10302        if ((configFlags & usesFriendsApiFlags) != 0 && FriendsController.i)
 303        {
 8304            if (FriendsController.i.friends.TryGetValue(userId, out FriendsController.UserStatus status))
 305            {
 0306                SetupFriendship(status.friendshipStatus);
 0307            }
 308            else
 309            {
 8310                SetupFriendship(FriendshipStatus.NONE);
 311            }
 8312            FriendsController.i.OnUpdateFriendship -= OnFriendActionUpdate;
 8313            FriendsController.i.OnUpdateFriendship += OnFriendActionUpdate;
 314        }
 10315    }
 316
 317    private void SetupFriendship(FriendshipStatus friendshipStatus)
 318    {
 14319        bool friendshipEnabled = (currentConfigFlags & MenuConfigFlags.Friendship) != 0;
 14320        bool messageEnabled = (currentConfigFlags & MenuConfigFlags.Message) != 0;
 321
 14322        if (friendshipStatus == FriendshipStatus.FRIEND)
 323        {
 2324            if (friendshipEnabled)
 325            {
 1326                friendAddContainer.SetActive(false);
 1327                friendRemoveContainer.SetActive(true);
 1328                friendRequestedContainer.SetActive(false);
 1329                deleteFriendButton.gameObject.SetActive(true);
 330            }
 2331            if (messageEnabled)
 332            {
 1333                messageButton.gameObject.SetActive(true);
 334            }
 1335        }
 12336        else if (friendshipStatus == FriendshipStatus.REQUESTED_TO)
 337        {
 2338            if (friendshipEnabled)
 339            {
 1340                friendAddContainer.SetActive(false);
 1341                friendRemoveContainer.SetActive(false);
 1342                friendRequestedContainer.SetActive(true);
 1343                deleteFriendButton.gameObject.SetActive(false);
 344            }
 2345            if (messageEnabled)
 346            {
 1347                messageButton.gameObject.SetActive(false);
 348            }
 1349        }
 10350        else if (friendshipStatus == FriendshipStatus.NONE)
 351        {
 10352            if (friendshipEnabled)
 353            {
 8354                friendAddContainer.SetActive(true);
 8355                friendRemoveContainer.SetActive(false);
 8356                friendRequestedContainer.SetActive(false);
 8357                deleteFriendButton.gameObject.SetActive(false);
 358            }
 10359            if (messageEnabled)
 360            {
 8361                messageButton.gameObject.SetActive(false);
 362            }
 8363        }
 0364        else if (friendshipStatus == FriendshipStatus.REQUESTED_FROM)
 365        {
 0366            if (friendshipEnabled)
 367            {
 0368                friendAddContainer.SetActive(true);
 0369                friendRemoveContainer.SetActive(false);
 0370                friendRequestedContainer.SetActive(false);
 0371                deleteFriendButton.gameObject.SetActive(false);
 372            }
 0373            if (messageEnabled)
 374            {
 0375                messageButton.gameObject.SetActive(false);
 376            }
 377        }
 4378    }
 379
 380    private void OnFriendActionUpdate(string userId, FriendshipAction action)
 381    {
 6382        if (this.userId != userId)
 383        {
 0384            return;
 385        }
 386
 6387        if (action == FriendshipAction.APPROVED)
 388        {
 2389            SetupFriendship(FriendshipStatus.FRIEND);
 2390        }
 4391        else if (action == FriendshipAction.REQUESTED_TO)
 392        {
 2393            SetupFriendship(FriendshipStatus.REQUESTED_TO);
 2394        }
 2395        else if (action == FriendshipAction.DELETED || action == FriendshipAction.CANCELLED || action == FriendshipActio
 396        {
 2397            SetupFriendship(FriendshipStatus.NONE);
 398        }
 2399    }
 400
 401#if UNITY_EDITOR
 402    //This is just to process buttons and container visibility on editor
 403    private void OnValidate()
 404    {
 68405        if (headerContainer == null)
 0406            return;
 68407        ProcessActiveElements(menuConfigFlags);
 68408    }
 409#endif
 410}