< Summary

Class:VoiceChatWindowComponentView
Assembly:VoiceChatHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/VoiceChatHUD/VoiceChatWindowComponentView.cs
Covered lines:100
Uncovered lines:18
Coverable lines:118
Total lines:293
Line coverage:84.7% (100 of 118)
Covered branches:0
Total branches:0

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.13077.78%
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
 5141    private readonly Queue<VoiceChatPlayerComponentView> playersPool = new Queue<VoiceChatPlayerComponentView>();
 5142    internal Dictionary<string, VoiceChatPlayerComponentView> currentPlayers = new Dictionary<string, VoiceChatPlayerCom
 5143    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    {
 6127        if (!currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView))
 0128            return;
 129
 6130        elementView.SetAsTalking(isRecording);
 131
 6132        if (isRecording)
 133        {
 3134            if (!usersTalking.Contains(userId))
 3135                usersTalking.Add(userId);
 136        }
 137        else
 138        {
 3139            usersTalking.Remove(userId);
 140        }
 3141    }
 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        currentPlayers.Remove(userId);
 1211        SetNumberOfPlayers(currentPlayers.Count);
 212
 1213        elementView.SetActive(false);
 1214    }
 215
 1216    public string GetUserTalkingByIndex(int index) { return usersTalking[index]; }
 217
 218    public override void Dispose()
 219    {
 98220        closeButton.onClick.RemoveAllListeners();
 98221        joinButton.onClick.RemoveAllListeners();
 98222        leaveButton.onClick.RemoveAllListeners();
 98223        allowUsersDropdown.OnOptionSelectionChanged -= AllowUsersOptionChanged;
 98224        muteAllToggle.OnSelectedChanged -= OnMuteAllToggleChanged;
 225
 98226        currentPlayers.Clear();
 98227        playersPool.Clear();
 228
 98229        base.Dispose();
 98230    }
 231
 232    internal void ConfigureAllowUsersFilter()
 233    {
 52234        allowUsersDropdown.SetOptions(new List<ToggleComponentModel>
 235        {
 236            new ToggleComponentModel
 237            {
 238                isOn = true,
 239                id = VoiceChatAllow.ALL_USERS.ToString(),
 240                text = ALLOW_USERS_TITLE_ALL,
 241                isTextActive = true,
 242                changeTextColorOnSelect = true
 243            },
 244            new ToggleComponentModel
 245            {
 246                isOn = false,
 247                id = VoiceChatAllow.VERIFIED_ONLY.ToString(),
 248                text = ALLOW_USERS_TITLE_REGISTERED,
 249                isTextActive = true,
 250                changeTextColorOnSelect = true
 251            },
 252            new ToggleComponentModel
 253            {
 254                isOn = false,
 255                id = VoiceChatAllow.FRIENDS_ONLY.ToString(),
 256                text = ALLOW_USERS_TITLE_FRIENDS,
 257                isTextActive = true,
 258                changeTextColorOnSelect = true
 259            }
 260        });
 261
 52262        allowUsersDropdown.SetTitle(ALLOW_USERS_TITLE_ALL);
 52263    }
 264
 265    internal void AllowUsersOptionChanged(bool isOn, string optionId, string optionName)
 266    {
 5267        if (!isOn)
 2268            return;
 269
 3270        allowUsersDropdown.SetTitle(optionName);
 3271        OnAllowUsersFilterChange?.Invoke(optionId);
 1272    }
 273
 4274    internal void OnMuteAllToggleChanged(bool isOn, string id, string text) { OnMuteAll?.Invoke(isOn); }
 275
 4276    internal void MuteUser(string userId, bool isMute) { OnMuteUser?.Invoke(userId, isMute); }
 277
 278    internal void OpenContextMenu(string userId)
 279    {
 0280        currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView);
 281
 0282        if (elementView != null)
 0283            elementView.DockAndOpenUserContextMenu(contextMenuPanel);
 0284    }
 285
 286    internal static VoiceChatWindowComponentView Create()
 287    {
 0288        VoiceChatWindowComponentView voiceChatWindowComponentView = Instantiate(Resources.Load<GameObject>("SocialBarV1/
 0289        voiceChatWindowComponentView.name = "_VoiceChatHUD";
 290
 0291        return voiceChatWindowComponentView;
 292    }
 293}