< Summary

Class:DCL.Components.GetAnchorPointsHandler
Assembly:DCL.ECSComponents.AvatarAttach
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AvatarAttach/AnchorPointsGetterHandlers/GetAnchorPointsHandler.cs
Covered lines:38
Uncovered lines:4
Coverable lines:42
Total lines:107
Line coverage:90.4% (38 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SearchAnchorPoints(...)0%4.324072.73%
CancelCurrentSearch()0%110100%
Dispose()0%110100%
CleanUp()0%220100%
GetHandler(...)0%220100%
OnAvatarFoundEvent(...)0%220100%
OnAvatarRemovedEvent(...)0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/AvatarAttach/AnchorPointsGetterHandlers/GetAnchorPointsHandler.cs

#LineLine coverage
 1using System;
 2
 3namespace DCL.Components
 4{
 5    public class GetAnchorPointsHandler : IDisposable
 6    {
 7        public event Action OnAvatarRemoved;
 8
 9        private Action<IAvatarAnchorPoints> onAvatarFound;
 10        private bool cleaned = false;
 11
 4512        private UserProfile ownPlayerProfile => UserProfile.GetOwnUserProfile();
 13
 1614        private IAnchorPointsGetterHandler ownPlayerAnchorPointsGetterHandler => new OwnPlayerGetAnchorPointsHandler();
 2715        private IAnchorPointsGetterHandler otherPlayerAnchorPointsGetterHandler => new OtherPlayerGetAnchorPointsHandler
 16        private IAnchorPointsGetterHandler currentAnchorPointsGetterHandler;
 17
 18        /// <summary>
 19        /// Search for an `avatarId` that could be the own player or any other player.
 20        /// If that `avatarId` is loaded it will call `onSuccess`, otherwise it will wait
 21        /// for that `avatarId` to be loaded
 22        /// </summary>
 23        public void SearchAnchorPoints(string avatarId, Action<IAvatarAnchorPoints> onSuccess, bool supportNullId = fals
 24        {
 1525            CleanUp();
 1526            cleaned = false;
 27
 1528            if (string.IsNullOrEmpty(avatarId) && !supportNullId)
 029                return;
 30
 1531            string ownUserId = ownPlayerProfile.userId;
 32
 1533            onAvatarFound = onSuccess;
 34
 35            void GetOwnProfileUpdated(UserProfile profile)
 36            {
 1537                ownPlayerProfile.OnUpdate -= GetOwnProfileUpdated;
 1538                ownUserId = profile.userId;
 1539                string targetId = string.IsNullOrEmpty(avatarId) ? ownUserId : avatarId;
 40
 1541                if (cleaned)
 42                {
 043                    return;
 44                }
 45
 1546                currentAnchorPointsGetterHandler = GetHandler(targetId, ownUserId);
 1547                currentAnchorPointsGetterHandler.OnAvatarFound += OnAvatarFoundEvent;
 1548                currentAnchorPointsGetterHandler.OnAvatarRemoved += OnAvatarRemovedEvent;
 1549                currentAnchorPointsGetterHandler.GetAnchorPoints(targetId);
 1550            }
 51
 1552            if (string.IsNullOrEmpty(ownUserId))
 53            {
 54                // Unsubscribe first in case of multiple calls.
 055                ownPlayerProfile.OnUpdate -= GetOwnProfileUpdated;
 056                ownPlayerProfile.OnUpdate += GetOwnProfileUpdated;
 57            }
 58            else
 59            {
 1560                GetOwnProfileUpdated(ownPlayerProfile);
 61            }
 1562        }
 63
 64        /// <summary>
 65        /// Cancel the active search/waiting of the `avatarId`
 66        /// </summary>
 67        public void CancelCurrentSearch()
 68        {
 2669            CleanUp();
 2670        }
 71
 72        public void Dispose()
 73        {
 1474            CleanUp();
 1475            ownPlayerAnchorPointsGetterHandler.Dispose();
 1476            otherPlayerAnchorPointsGetterHandler.Dispose();
 1477        }
 78
 79        private void CleanUp()
 80        {
 5581            onAvatarFound = null;
 5582            cleaned = true;
 83
 5584            if (currentAnchorPointsGetterHandler != null)
 85            {
 2186                currentAnchorPointsGetterHandler.CleanUp();
 2187                currentAnchorPointsGetterHandler.OnAvatarFound -= OnAvatarFoundEvent;
 2188                currentAnchorPointsGetterHandler.OnAvatarRemoved -= OnAvatarRemovedEvent;
 89            }
 5590        }
 91
 92        private IAnchorPointsGetterHandler GetHandler(string id, string ownPlayerId)
 93        {
 1594            return id == ownPlayerId ? ownPlayerAnchorPointsGetterHandler : otherPlayerAnchorPointsGetterHandler;
 95        }
 96
 97        private void OnAvatarFoundEvent(string id, IAvatarAnchorPoints anchorPoints)
 98        {
 1399            onAvatarFound?.Invoke(anchorPoints);
 12100        }
 101
 102        private void OnAvatarRemovedEvent(string id)
 103        {
 3104            OnAvatarRemoved?.Invoke();
 2105        }
 106    }
 107}