< 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:36
Uncovered lines:5
Coverable lines:41
Total lines:104
Line coverage:87.8% (36 of 41)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
SearchAnchorPoints(...)0%3.183072.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
 014        private IAnchorPointsGetterHandler ownPlayerAnchorPointsGetterHandler => new OwnPlayerGetAnchorPointsHandler();
 2815        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)
 24        {
 1525            CleanUp();
 1526            cleaned = false;
 27
 1528            if (string.IsNullOrEmpty(avatarId))
 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;
 39
 1540                if (cleaned)
 41                {
 042                    return;
 43                }
 44
 1545                currentAnchorPointsGetterHandler = GetHandler(avatarId, ownUserId);
 1546                currentAnchorPointsGetterHandler.OnAvatarFound += OnAvatarFoundEvent;
 1547                currentAnchorPointsGetterHandler.OnAvatarRemoved += OnAvatarRemovedEvent;
 1548                currentAnchorPointsGetterHandler.GetAnchorPoints(avatarId);
 1549            }
 50
 1551            if (string.IsNullOrEmpty(ownUserId))
 52            {
 053                ownPlayerProfile.OnUpdate += GetOwnProfileUpdated;
 054            }
 55            else
 56            {
 1557                GetOwnProfileUpdated(ownPlayerProfile);
 58            }
 1559        }
 60
 61        /// <summary>
 62        /// Cancel the active search/waiting of the `avatarId`
 63        /// </summary>
 64        public void CancelCurrentSearch()
 65        {
 2866            CleanUp();
 2867        }
 68
 69        public void Dispose()
 70        {
 1571            CleanUp();
 1572            ownPlayerAnchorPointsGetterHandler.Dispose();
 1573            otherPlayerAnchorPointsGetterHandler.Dispose();
 1574        }
 75
 76        private void CleanUp()
 77        {
 5878            onAvatarFound = null;
 5879            cleaned = true;
 80
 5881            if (currentAnchorPointsGetterHandler != null)
 82            {
 2183                currentAnchorPointsGetterHandler.CleanUp();
 2184                currentAnchorPointsGetterHandler.OnAvatarFound -= OnAvatarFoundEvent;
 2185                currentAnchorPointsGetterHandler.OnAvatarRemoved -= OnAvatarRemovedEvent;
 86            }
 5887        }
 88
 89        private IAnchorPointsGetterHandler GetHandler(string id, string ownPlayerId)
 90        {
 1591            return id == ownPlayerId ? ownPlayerAnchorPointsGetterHandler : otherPlayerAnchorPointsGetterHandler;
 92        }
 93
 94        private void OnAvatarFoundEvent(string id, IAvatarAnchorPoints anchorPoints)
 95        {
 1396            onAvatarFound?.Invoke(anchorPoints);
 1297        }
 98
 99        private void OnAvatarRemovedEvent(string id)
 100        {
 3101            OnAvatarRemoved?.Invoke();
 2102        }
 103    }
 104}