< Summary

Class:VoiceChatWindowComponentView
Assembly:VoiceChatHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/VoiceChatHUD/VoiceChatWindowComponentView.cs
Covered lines:99
Uncovered lines:23
Coverable lines:122
Total lines:298
Line coverage:81.1% (99 of 122)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
VoiceChatWindowComponentView()0%110100%
Awake()0%110100%
Start()0%2100%
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.024088.89%
SetPlayerBlocked(...)0%220100%
SetPlayerAsFriend(...)0%220100%
SetPlayerAsJoined(...)0%220100%
AddOrUpdatePlayer(...)0%3.093078.57%
RemoveUser(...)0%3.13077.78%
GetUserTalkingByIndex(...)0%2100%
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
 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 override void Start()
 60    {
 061        base.Start();
 62
 063        AllowUsersOptionChanged(true, VoiceChatAllow.ALL_USERS.ToString(), ALLOW_USERS_TITLE_ALL);
 064    }
 65
 66    public void Configure(BaseComponentModel newModel)
 67    {
 168        model = (VoiceChatWindowComponentModel)newModel;
 169        RefreshControl();
 170    }
 71
 72    public override void RefreshControl()
 73    {
 174        if (model == null)
 075            return;
 76
 177        SetNumberOfPlayers(model.numberOfPlayers);
 178        SetAsJoined(model.isJoined);
 179    }
 80
 281    public override void Show(bool instant = false) { gameObject.SetActive(true); }
 82
 83    public override void Hide(bool instant = false)
 84    {
 185        contextMenuPanel.Hide();
 186        gameObject.SetActive(false);
 187    }
 88
 89    public void SetNumberOfPlayers(int numPlayers)
 90    {
 591        model.numberOfPlayers = numPlayers;
 92
 593        if (playersText != null)
 94        {
 595            playersText.text = $"PLAYERS ({numPlayers})";
 596            playersText.gameObject.SetActive(numPlayers > 0);
 97        }
 98
 599        if (emptyListGameObject != null)
 5100            emptyListGameObject.SetActive(numPlayers == 0);
 5101    }
 102
 103    public void SetAsJoined(bool isJoined)
 104    {
 3105        model.isJoined = isJoined;
 106
 3107        if (joinButton != null)
 3108            joinButton.gameObject.SetActive(!isJoined);
 109
 3110        if (leaveButton != null)
 3111            leaveButton.gameObject.SetActive(isJoined);
 112
 3113        using (var iterator = currentPlayers.GetEnumerator())
 114        {
 12115            while (iterator.MoveNext())
 116            {
 9117                iterator.Current.Value.SetAsJoined(isJoined);
 118            }
 3119        }
 3120    }
 121
 4122    public void SelectAllowUsersOption(int optionIndex) { allowUsersDropdown.GetOption(optionIndex).isOn = true; }
 123
 124    public void SetPlayerMuted(string userId, bool isMuted)
 125    {
 2126        if (!currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView))
 0127            return;
 128
 2129        elementView.SetAsMuted(isMuted);
 2130    }
 131
 132    public void SetPlayerRecording(string userId, bool isRecording)
 133    {
 6134        if (!currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView))
 0135            return;
 136
 6137        elementView.SetAsTalking(isRecording);
 138
 6139        if (isRecording)
 140        {
 3141            if (!usersTalking.Contains(userId))
 3142                usersTalking.Add(userId);
 3143        }
 144        else
 145        {
 3146            usersTalking.Remove(userId);
 147        }
 3148    }
 149
 150    public void SetPlayerBlocked(string userId, bool isBlocked)
 151    {
 6152        if (currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView))
 6153            elementView.SetAsBlocked(isBlocked);
 6154    }
 155
 156    public void SetPlayerAsFriend(string userId, bool isFriend)
 157    {
 6158        currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView playerView);
 159
 6160        if (playerView != null)
 6161            playerView.SetAsFriend(isFriend);
 6162    }
 163
 164    public void SetPlayerAsJoined(string userId, bool isJoined)
 165    {
 6166        currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView playerView);
 167
 6168        if (playerView != null)
 6169            playerView.SetAsJoined(isJoined);
 6170    }
 171
 172    public void AddOrUpdatePlayer(UserProfile otherProfile)
 173    {
 1174        if (currentPlayers.ContainsKey(otherProfile.userId))
 0175            return;
 176
 1177        VoiceChatPlayerComponentView elementView = null;
 178
 1179        if (playersPool.Count > 0)
 180        {
 0181            elementView = playersPool.Dequeue();
 0182        }
 183        else
 184        {
 1185            elementView = Instantiate(playerPrefab, usersContainer);
 1186            elementView.OnContextMenuOpen += OpenContextMenu;
 1187            elementView.OnMuteUser += MuteUser;
 188        }
 189
 1190        elementView.Configure(new VoiceChatPlayerComponentModel
 191        {
 192            userId = otherProfile.userId,
 193            userImageUrl = otherProfile.face256SnapshotURL,
 194            userName = otherProfile.userName,
 195            isMuted = false,
 196            isTalking = false,
 197            isBlocked = false,
 198            isFriend = false,
 199            isJoined = false,
 200            isBackgroundHover = false
 201        });
 202
 1203        elementView.SetActive(true);
 1204        currentPlayers.Add(otherProfile.userId, elementView);
 1205        SetNumberOfPlayers(currentPlayers.Count);
 1206    }
 207
 208    public void RemoveUser(string userId)
 209    {
 1210        if (!currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView))
 0211            return;
 212
 1213        if (!elementView)
 0214            return;
 215
 1216        playersPool.Enqueue(elementView);
 1217        currentPlayers.Remove(userId);
 1218        SetNumberOfPlayers(currentPlayers.Count);
 219
 1220        elementView.SetActive(false);
 1221    }
 222
 0223    public string GetUserTalkingByIndex(int index) { return usersTalking[index]; }
 224
 225    public override void Dispose()
 226    {
 98227        closeButton.onClick.RemoveAllListeners();
 98228        joinButton.onClick.RemoveAllListeners();
 98229        leaveButton.onClick.RemoveAllListeners();
 98230        allowUsersDropdown.OnOptionSelectionChanged -= AllowUsersOptionChanged;
 98231        muteAllToggle.OnSelectedChanged -= OnMuteAllToggleChanged;
 232
 98233        currentPlayers.Clear();
 98234        playersPool.Clear();
 235
 98236        base.Dispose();
 98237    }
 238
 239    internal void ConfigureAllowUsersFilter()
 240    {
 52241        allowUsersDropdown.SetOptions(new List<ToggleComponentModel>
 242        {
 243            new ToggleComponentModel
 244            {
 245                isOn = true,
 246                id = VoiceChatAllow.ALL_USERS.ToString(),
 247                text = ALLOW_USERS_TITLE_ALL,
 248                isTextActive = true,
 249                changeTextColorOnSelect = true
 250            },
 251            new ToggleComponentModel
 252            {
 253                isOn = false,
 254                id = VoiceChatAllow.VERIFIED_ONLY.ToString(),
 255                text = ALLOW_USERS_TITLE_REGISTERED,
 256                isTextActive = true,
 257                changeTextColorOnSelect = true
 258            },
 259            new ToggleComponentModel
 260            {
 261                isOn = false,
 262                id = VoiceChatAllow.FRIENDS_ONLY.ToString(),
 263                text = ALLOW_USERS_TITLE_FRIENDS,
 264                isTextActive = true,
 265                changeTextColorOnSelect = true
 266            }
 267        });
 52268    }
 269
 270    internal void AllowUsersOptionChanged(bool isOn, string optionId, string optionName)
 271    {
 5272        if (!isOn)
 2273            return;
 274
 3275        allowUsersDropdown.SetTitle(optionName);
 3276        OnAllowUsersFilterChange?.Invoke(optionId);
 1277    }
 278
 4279    internal void OnMuteAllToggleChanged(bool isOn, string id, string text) { OnMuteAll?.Invoke(isOn); }
 280
 4281    internal void MuteUser(string userId, bool isMute) { OnMuteUser?.Invoke(userId, isMute); }
 282
 283    internal void OpenContextMenu(string userId)
 284    {
 0285        currentPlayers.TryGetValue(userId, out VoiceChatPlayerComponentView elementView);
 286
 0287        if (elementView != null)
 0288            elementView.DockAndOpenUserContextMenu(contextMenuPanel);
 0289    }
 290
 291    internal static VoiceChatWindowComponentView Create()
 292    {
 0293        VoiceChatWindowComponentView voiceChatWindowComponentView = Instantiate(Resources.Load<GameObject>("SocialBarV1/
 0294        voiceChatWindowComponentView.name = "_VoiceChatHUD";
 295
 0296        return voiceChatWindowComponentView;
 297    }
 298}