< Summary

Class:VoiceChatWindowComponentView
Assembly:VoiceChatHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/VoiceChatHUD/VoiceChatWindowComponentView.cs
Covered lines:101
Uncovered lines:18
Coverable lines:119
Total lines:294
Line coverage:84.8% (101 of 119)
Covered branches:0
Total branches:0
Covered methods:22
Total methods:28
Method coverage:78.5% (22 of 28)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
VoiceChatWindowComponentView()0%110100%
Awake()0%110100%
Configure(...)0%110100%
RefreshControl()0%2.032080%
Show(...)0%110100%
Hide(...)0%110100%
SetNumberOfPlayers(...)0%330100%
SetAsJoined(...)0%440100%
SelectAllowUsersOption(...)0%110100%
SetPlayerMuted(...)0%2.062075%
SetPlayerRecording(...)0%4.034087.5%
SetPlayerBlocked(...)0%220100%
SetPlayerAsFriend(...)0%220100%
SetPlayerAsJoined(...)0%220100%
AddOrUpdatePlayer(...)0%3.033084.62%
RemoveUser(...)0%3.073080%
GetUserTalkingByIndex(...)0%110100%
Dispose()0%110100%
ConfigureAllowUsersFilter()0%110100%
AllowUsersOptionChanged(...)0%330100%
OnMuteAllToggleChanged(...)0%220100%
MuteUser(...)0%220100%
OpenContextMenu(...)0%6200%
Create()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/VoiceChatHUD/VoiceChatWindowComponentView.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using TMPro;
 4using UnityEngine;
 5using static DCL.SettingsCommon.GeneralSettings;
 6
 7public class VoiceChatWindowComponentView : BaseComponentView, IVoiceChatWindowComponentView, IComponentModelConfig<Voic
 8{
 9    private const string ALLOW_USERS_TITLE_ALL = "All";
 10    private const string ALLOW_USERS_TITLE_REGISTERED = "Verified Users";
 11    private const string ALLOW_USERS_TITLE_FRIENDS = "Friends";
 12
 13    [Header("Prefab References")]
 14    [SerializeField] internal ButtonComponentView closeButton;
 15    [SerializeField] internal ButtonComponentView joinButton;
 16    [SerializeField] internal ButtonComponentView leaveButton;
 17    [SerializeField] internal TMP_Text playersText;
 18    [SerializeField] internal DropdownComponentView allowUsersDropdown;
 19    [SerializeField] internal ToggleComponentView muteAllToggle;
 20    [SerializeField] internal GameObject emptyListGameObject;
 21    [SerializeField] internal ButtonComponentView goToCrowdButton;
 22    [SerializeField] internal VoiceChatPlayerComponentView playerPrefab;
 23    [SerializeField] internal Transform usersContainer;
 24    [SerializeField] internal UserContextMenu contextMenuPanel;
 25
 26    [Header("Configuration")]
 27    [SerializeField] internal VoiceChatWindowComponentModel model;
 28
 29    public event Action OnClose;
 30    public event Action<bool> OnJoinVoiceChat;
 31    public event Action<string> OnAllowUsersFilterChange;
 32    public event Action OnGoToCrowd;
 33    public event Action<bool> OnMuteAll;
 34    public event Action<string, bool> OnMuteUser;
 35
 036    public RectTransform Transform => (RectTransform)transform;
 037    public bool isMuteAllOn => muteAllToggle.isOn;
 038    public int numberOfPlayers => currentPlayers.Count;
 039    public int numberOfPlayersTalking => usersTalking.Count;
 40
 5041    private readonly Queue<VoiceChatPlayerComponentView> playersPool = new Queue<VoiceChatPlayerComponentView>();
 5042    internal Dictionary<string, VoiceChatPlayerComponentView> currentPlayers = new Dictionary<string, VoiceChatPlayerCom
 5043    internal List<string> usersTalking = new List<string>();
 44
 45    public override void Awake()
 46    {
 4947        base.Awake();
 48
 4949        closeButton.onClick.AddListener(() => OnClose?.Invoke());
 4950        joinButton.onClick.AddListener(() => OnJoinVoiceChat?.Invoke(true));
 4951        leaveButton.onClick.AddListener(() => OnJoinVoiceChat?.Invoke(false));
 4952        goToCrowdButton.onClick.AddListener(() => OnGoToCrowd?.Invoke());
 4953        allowUsersDropdown.OnOptionSelectionChanged += AllowUsersOptionChanged;
 4954        muteAllToggle.OnSelectedChanged += OnMuteAllToggleChanged;
 55
 4956        ConfigureAllowUsersFilter();
 4957    }
 58
 59    public void Configure(VoiceChatWindowComponentModel newModel)
 60    {
 161        model = newModel;
 162        RefreshControl();
 163    }
 64
 65    public override void RefreshControl()
 66    {
 167        if (model == null)
 068            return;
 69
 170        SetNumberOfPlayers(model.numberOfPlayers);
 171        SetAsJoined(model.isJoined);
 172    }
 73
 274    public override void Show(bool instant = false) { gameObject.SetActive(true); }
 75
 76    public override void Hide(bool instant = false)
 77    {
 178        contextMenuPanel.Hide();
 179        gameObject.SetActive(false);
 180    }
 81
 82    public void SetNumberOfPlayers(int numPlayers)
 83    {
 584        model.numberOfPlayers = numPlayers;
 85
 586        if (playersText != null)
 87        {
 588            playersText.text = $"PLAYERS ({numPlayers})";
 589            playersText.gameObject.SetActive(numPlayers > 0);
 90        }
 91
 592        if (emptyListGameObject != null)
 593            emptyListGameObject.SetActive(numPlayers == 0);
 594    }
 95
 96    public void SetAsJoined(bool isJoined)
 97    {
 398        model.isJoined = isJoined;
 99
 3100        if (joinButton != null)
 3101            joinButton.gameObject.SetActive(!isJoined);
 102
 3103        if (leaveButton != null)
 3104            leaveButton.gameObject.SetActive(isJoined);
 105
 3106        using (var iterator = currentPlayers.GetEnumerator())
 107        {
 12108            while (iterator.MoveNext())
 109            {
 9110                iterator.Current.Value.SetAsJoined(isJoined);
 111            }
 3112        }
 3113    }
 114
 4115    public void SelectAllowUsersOption(int optionIndex) { allowUsersDropdown.GetOption(optionIndex).isOn = true; }
 116
 117    public void SetPlayerMuted(string userId, bool isMuted)
 118    {
 2119        if (!currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView))
 0120            return;
 121
 2122        elementView.SetAsMuted(isMuted);
 2123    }
 124
 125    public void SetPlayerRecording(string userId, bool isRecording)
 126    {
 7127        if (!currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView))
 0128            return;
 129
 7130        elementView.SetAsTalking(isRecording);
 131
 7132        if (isRecording)
 133        {
 3134            if (!usersTalking.Contains(userId))
 3135                usersTalking.Add(userId);
 136        }
 137        else
 138        {
 4139            usersTalking.Remove(userId);
 140        }
 4141    }
 142
 143    public void SetPlayerBlocked(string userId, bool isBlocked)
 144    {
 6145        if (currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView))
 6146            elementView.SetAsBlocked(isBlocked);
 6147    }
 148
 149    public void SetPlayerAsFriend(string userId, bool isFriend)
 150    {
 6151        currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView playerView);
 152
 6153        if (playerView != null)
 6154            playerView.SetAsFriend(isFriend);
 6155    }
 156
 157    public void SetPlayerAsJoined(string userId, bool isJoined)
 158    {
 6159        currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView playerView);
 160
 6161        if (playerView != null)
 6162            playerView.SetAsJoined(isJoined);
 6163    }
 164
 165    public void AddOrUpdatePlayer(UserProfile otherProfile)
 166    {
 1167        if (currentPlayers.ContainsKey(otherProfile.userId))
 0168            return;
 169
 1170        VoiceChatPlayerComponentView elementView = null;
 171
 1172        if (playersPool.Count > 0)
 173        {
 0174            elementView = playersPool.Dequeue();
 175        }
 176        else
 177        {
 1178            elementView = Instantiate(playerPrefab, usersContainer);
 1179            elementView.OnContextMenuOpen += OpenContextMenu;
 1180            elementView.OnMuteUser += MuteUser;
 181        }
 182
 1183        elementView.Configure(new VoiceChatPlayerComponentModel
 184        {
 185            userId = otherProfile.userId,
 186            userImageUrl = otherProfile.face256SnapshotURL,
 187            userName = otherProfile.userName,
 188            isMuted = false,
 189            isTalking = false,
 190            isBlocked = false,
 191            isFriend = false,
 192            isJoined = false,
 193            isBackgroundHover = false
 194        });
 195
 1196        elementView.SetActive(true);
 1197        currentPlayers.Add(otherProfile.userId, elementView);
 1198        SetNumberOfPlayers(currentPlayers.Count);
 1199    }
 200
 201    public void RemoveUser(string userId)
 202    {
 1203        if (!currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView))
 0204            return;
 205
 1206        if (!elementView)
 0207            return;
 208
 1209        playersPool.Enqueue(elementView);
 1210        SetPlayerRecording(userId, false);
 1211        currentPlayers.Remove(userId);
 1212        SetNumberOfPlayers(currentPlayers.Count);
 213
 1214        elementView.SetActive(false);
 1215    }
 216
 1217    public string GetUserTalkingByIndex(int index) { return usersTalking[index]; }
 218
 219    public override void Dispose()
 220    {
 98221        closeButton.onClick.RemoveAllListeners();
 98222        joinButton.onClick.RemoveAllListeners();
 98223        leaveButton.onClick.RemoveAllListeners();
 98224        allowUsersDropdown.OnOptionSelectionChanged -= AllowUsersOptionChanged;
 98225        muteAllToggle.OnSelectedChanged -= OnMuteAllToggleChanged;
 226
 98227        currentPlayers.Clear();
 98228        playersPool.Clear();
 229
 98230        base.Dispose();
 98231    }
 232
 233    internal void ConfigureAllowUsersFilter()
 234    {
 52235        allowUsersDropdown.SetOptions(new List<ToggleComponentModel>
 236        {
 237            new ToggleComponentModel
 238            {
 239                isOn = true,
 240                id = VoiceChatAllow.ALL_USERS.ToString(),
 241                text = ALLOW_USERS_TITLE_ALL,
 242                isTextActive = true,
 243                changeTextColorOnSelect = true
 244            },
 245            new ToggleComponentModel
 246            {
 247                isOn = false,
 248                id = VoiceChatAllow.VERIFIED_ONLY.ToString(),
 249                text = ALLOW_USERS_TITLE_REGISTERED,
 250                isTextActive = true,
 251                changeTextColorOnSelect = true
 252            },
 253            new ToggleComponentModel
 254            {
 255                isOn = false,
 256                id = VoiceChatAllow.FRIENDS_ONLY.ToString(),
 257                text = ALLOW_USERS_TITLE_FRIENDS,
 258                isTextActive = true,
 259                changeTextColorOnSelect = true
 260            }
 261        });
 262
 52263        allowUsersDropdown.SetTitle(ALLOW_USERS_TITLE_ALL);
 52264    }
 265
 266    internal void AllowUsersOptionChanged(bool isOn, string optionId, string optionName)
 267    {
 5268        if (!isOn)
 2269            return;
 270
 3271        allowUsersDropdown.SetTitle(optionName);
 3272        OnAllowUsersFilterChange?.Invoke(optionId);
 1273    }
 274
 4275    internal void OnMuteAllToggleChanged(bool isOn, string id, string text) { OnMuteAll?.Invoke(isOn); }
 276
 4277    internal void MuteUser(string userId, bool isMute) { OnMuteUser?.Invoke(userId, isMute); }
 278
 279    internal void OpenContextMenu(string userId)
 280    {
 0281        currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView);
 282
 0283        if (elementView != null)
 0284            elementView.DockAndOpenUserContextMenu(contextMenuPanel);
 0285    }
 286
 287    internal static VoiceChatWindowComponentView Create()
 288    {
 0289        VoiceChatWindowComponentView voiceChatWindowComponentView = Instantiate(Resources.Load<GameObject>("SocialBarV1/
 0290        voiceChatWindowComponentView.name = "_VoiceChatHUD";
 291
 0292        return voiceChatWindowComponentView;
 293    }
 294}