< Summary

Class:UsersAroundListHUDController
Assembly:UsersAroundListHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/UsersAroundListHUD/UsersAroundListHUDController.cs
Covered lines:64
Uncovered lines:44
Coverable lines:108
Total lines:250
Line coverage:59.2% (64 of 108)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
UsersAroundListHUDController()0%110100%
UsersAroundListHUDController(...)0%2100%
Dispose()0%44093.75%
SetVisibility(...)0%110100%
SetButtonView(...)0%110100%
SetUsersMuted(...)0%6200%
SetUserRecording(...)0%2100%
Initialize(...)0%110100%
OnOtherPlayersStatusAdded(...)0%7.297081.82%
OnOtherPlayerStatusRemoved(...)0%330100%
ToggleVisibility()0%2100%
OnMuteUser(...)0%20400%
OnMuteUsers(...)0%12300%
OnMuteAll(...)0%6200%
OnGoToCrowd()0%2100%
OnListOpen()0%2.52050%
OnRendererStateChanged(...)0%12300%
OnUserProfileUpdate(...)0%20400%
ReportMuteStatuses()0%3.213071.43%
MuteStateUpdateRoutine()0%12300%
ToggleUsersCount(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/UsersAroundListHUD/UsersAroundListHUDController.cs

#LineLine coverage
 1using System.Collections;
 2using System.Collections.Generic;
 3using DCL;
 4using DCL.Interface;
 5using UnityEngine;
 6
 7public class UsersAroundListHUDController : IHUD
 8{
 9    const float MUTE_STATUS_UPDATE_INTERVAL = 0.3f;
 10
 11    internal IUsersAroundListHUDButtonView usersButtonView;
 12    internal IUsersAroundListHUDListView usersListView;
 13
 14    private bool isVisible = false;
 415    private readonly HashSet<string> trackedUsersHashSet = new HashSet<string>();
 1716    private UserProfile profile => UserProfile.GetOwnUserProfile();
 17
 418    private readonly List<string> usersToMute = new List<string>();
 419    private readonly List<string> usersToUnmute = new List<string>();
 20    private bool isMuteAll = false;
 21    private Coroutine updateMuteStatusRoutine = null;
 22
 1623    private BaseDictionary<string, Player> otherPlayers => DataStore.i.player.otherPlayers;
 24
 25    public event System.Action OnOpen;
 26
 427    public UsersAroundListHUDController()
 28    {
 429        UsersAroundListHUDListView view = Object.Instantiate(Resources.Load<GameObject>("UsersAroundListHUD")).GetCompon
 430        view.name = "_UsersAroundListHUD";
 431        view.gameObject.SetActive(false);
 432        Initialize(view);
 433    }
 34
 035    public UsersAroundListHUDController(IUsersAroundListHUDListView usersListView) { Initialize(usersListView); }
 36
 37    /// <summary>
 38    /// Dispose HUD controller
 39    /// </summary>
 40    public void Dispose()
 41    {
 442        ReportMuteStatuses();
 43
 444        if (updateMuteStatusRoutine != null)
 45        {
 046            CoroutineStarter.Stop(updateMuteStatusRoutine);
 47        }
 48
 449        otherPlayers.OnAdded -= OnOtherPlayersStatusAdded;
 450        otherPlayers.OnRemoved -= OnOtherPlayerStatusRemoved;
 51
 452        CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChanged;
 453        profile.OnUpdate -= OnUserProfileUpdate;
 54
 455        if (usersListView != null)
 56        {
 457            usersListView.OnRequestMuteUser -= OnMuteUser;
 458            usersListView.OnRequestMuteGlobal -= OnMuteAll;
 459            usersListView.OnGoToCrowdPressed -= OnGoToCrowd;
 460            usersListView.OnOpen -= OnListOpen;
 61
 462            usersListView.Dispose();
 63        }
 64
 465        if (usersButtonView != null)
 66        {
 167            usersButtonView.OnClick -= ToggleVisibility;
 68        }
 469    }
 70
 71    /// <summary>
 72    /// Set HUD's visibility
 73    /// </summary>
 74    /// <param name="visible"></param>
 75    public void SetVisibility(bool visible)
 76    {
 177        isVisible = visible;
 178        usersListView.SetVisibility(visible);
 179    }
 80
 81    /// <summary>
 82    /// Set button to toggle HUD visibility and display users count
 83    /// </summary>
 84    /// <param name="view">Button view</param>
 85    public void SetButtonView(IUsersAroundListHUDButtonView view)
 86    {
 187        usersButtonView = view;
 188        usersButtonView.OnClick += ToggleVisibility;
 189    }
 90
 91    /// <summary>
 92    /// Set mute status for users' id
 93    /// </summary>
 94    /// <param name="usersId">Array of user ids</param>
 95    /// <param name="isMuted">Set if users should be mute or unmute</param>
 96    public void SetUsersMuted(string[] usersId, bool isMuted)
 97    {
 098        for (int i = 0; i < usersId.Length; i++)
 99        {
 0100            usersListView.SetUserMuted(usersId[i], isMuted);
 101        }
 0102    }
 103
 104    /// <summary>
 105    /// Set user status as "talking"
 106    /// </summary>
 107    /// <param name="userId">User's id</param>
 108    /// <param name="isRecording">Set user status as "talking" or "not talking"</param>
 0109    public void SetUserRecording(string userId, bool isRecording) { usersListView.SetUserRecording(userId, isRecording);
 110
 111    void Initialize(IUsersAroundListHUDListView view)
 112    {
 4113        usersListView = view;
 114
 4115        usersListView.OnRequestMuteUser += OnMuteUser;
 4116        usersListView.OnRequestMuteGlobal += OnMuteAll;
 4117        usersListView.OnGoToCrowdPressed += OnGoToCrowd;
 4118        usersListView.OnOpen += OnListOpen;
 119
 4120        otherPlayers.OnAdded += OnOtherPlayersStatusAdded;
 4121        otherPlayers.OnRemoved += OnOtherPlayerStatusRemoved;
 122
 4123        CommonScriptableObjects.rendererState.OnChange += OnRendererStateChanged;
 4124        profile.OnUpdate += OnUserProfileUpdate;
 4125    }
 126
 127    void OnOtherPlayersStatusAdded(string userId, Player player)
 128    {
 3129        usersListView.AddOrUpdatePlayer(player);
 130
 3131        if (!trackedUsersHashSet.Contains(userId))
 132        {
 3133            trackedUsersHashSet.Add(userId);
 134
 3135            bool isMuted = profile.muted.Contains(userId);
 3136            bool isBlocked = profile.blocked != null ? profile.blocked.Contains(userId) : false;
 137
 3138            usersListView.SetUserMuted(userId, isMuted);
 3139            usersListView.SetUserBlocked(userId, isBlocked);
 140
 3141            if (isMuteAll && !isMuted)
 142            {
 0143                OnMuteUser(userId, true);
 144            }
 145        }
 146
 3147        usersButtonView?.SetUsersCount(trackedUsersHashSet.Count);
 0148    }
 149
 150    void OnOtherPlayerStatusRemoved(string userId, Player player)
 151    {
 2152        if (trackedUsersHashSet.Contains(userId))
 153        {
 2154            trackedUsersHashSet.Remove(userId);
 2155            usersButtonView?.SetUsersCount(trackedUsersHashSet.Count);
 156        }
 2157        usersListView.RemoveUser(userId);
 2158    }
 159
 160    void ToggleVisibility()
 161    {
 0162        bool setVisible = !isVisible;
 0163        SetVisibility(setVisible);
 0164    }
 165
 166    void OnMuteUser(string userId, bool mute)
 167    {
 0168        var list = mute ? usersToMute : usersToUnmute;
 0169        list.Add(userId);
 170
 0171        if (updateMuteStatusRoutine == null)
 172        {
 0173            updateMuteStatusRoutine = CoroutineStarter.Start(MuteStateUpdateRoutine());
 174        }
 0175    }
 176
 177    void OnMuteUsers(IEnumerable<string> usersId, bool mute)
 178    {
 0179        using (var iterator = usersId.GetEnumerator())
 180        {
 0181            while (iterator.MoveNext())
 182            {
 0183                OnMuteUser(iterator.Current, mute);
 184            }
 0185        }
 0186    }
 187
 188    void OnMuteAll(bool mute)
 189    {
 0190        isMuteAll = mute;
 191
 0192        if (mute)
 193        {
 0194            usersToUnmute.Clear();
 0195        }
 196        else
 197        {
 0198            usersToMute.Clear();
 199        }
 0200        OnMuteUsers(trackedUsersHashSet, mute);
 0201    }
 202
 0203    void OnGoToCrowd() { WebInterface.GoToCrowd(); }
 204
 1205    void OnListOpen() { OnOpen?.Invoke(); }
 206
 207    private void OnRendererStateChanged(bool isEnable, bool prevState)
 208    {
 0209        if (isEnable || !isVisible)
 0210            return;
 211
 0212        SetVisibility(false);
 0213    }
 214
 215    private void OnUserProfileUpdate(UserProfile profile)
 216    {
 0217        using (var iterator = trackedUsersHashSet.GetEnumerator())
 218        {
 0219            while (iterator.MoveNext())
 220            {
 0221                usersListView.SetUserBlocked(iterator.Current, profile.blocked != null ? profile.blocked.Contains(iterat
 222            }
 0223        }
 0224    }
 225
 226    void ReportMuteStatuses()
 227    {
 4228        if (usersToUnmute.Count > 0)
 229        {
 0230            WebInterface.SetMuteUsers(usersToUnmute.ToArray(), false);
 231        }
 4232        if (usersToMute.Count > 0)
 233        {
 0234            WebInterface.SetMuteUsers(usersToMute.ToArray(), true);
 235        }
 4236        usersToUnmute.Clear();
 4237        usersToMute.Clear();
 4238    }
 239
 240    IEnumerator MuteStateUpdateRoutine()
 241    {
 0242        yield return WaitForSecondsCache.Get(MUTE_STATUS_UPDATE_INTERVAL);
 0243        ReportMuteStatuses();
 0244        updateMuteStatusRoutine = null;
 0245    }
 246    public void ToggleUsersCount(bool isEnabled)
 247    {
 1248        usersButtonView.ToggleUsersCount(isEnabled);
 1249    }
 250}