< 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
Covered methods:10
Total methods:10
Method coverage:100% (10 of 10)

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
 4212        private UserProfile ownPlayerProfile => UserProfile.GetOwnUserProfile();
 13
 1514        private IAnchorPointsGetterHandler ownPlayerAnchorPointsGetterHandler => new OwnPlayerGetAnchorPointsHandler();
 2515        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        {
 1425            CleanUp();
 1426            cleaned = false;
 27
 1428            if (string.IsNullOrEmpty(avatarId) && !supportNullId)
 029                return;
 30
 1431            string ownUserId = ownPlayerProfile.userId;
 32
 1433            onAvatarFound = onSuccess;
 34
 35            void GetOwnProfileUpdated(UserProfile profile)
 36            {
 1437                ownPlayerProfile.OnUpdate -= GetOwnProfileUpdated;
 1438                ownUserId = profile.userId;
 1439                string targetId = string.IsNullOrEmpty(avatarId) ? ownUserId : avatarId;
 40
 1441                if (cleaned)
 42                {
 043                    return;
 44                }
 45
 1446                currentAnchorPointsGetterHandler = GetHandler(targetId, ownUserId);
 1447                currentAnchorPointsGetterHandler.OnAvatarFound += OnAvatarFoundEvent;
 1448                currentAnchorPointsGetterHandler.OnAvatarRemoved += OnAvatarRemovedEvent;
 1449                currentAnchorPointsGetterHandler.GetAnchorPoints(targetId);
 1450            }
 51
 1452            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            {
 1460                GetOwnProfileUpdated(ownPlayerProfile);
 61            }
 1462        }
 63
 64        /// <summary>
 65        /// Cancel the active search/waiting of the `avatarId`
 66        /// </summary>
 67        public void CancelCurrentSearch()
 68        {
 2469            CleanUp();
 2470        }
 71
 72        public void Dispose()
 73        {
 1374            CleanUp();
 1375            ownPlayerAnchorPointsGetterHandler.Dispose();
 1376            otherPlayerAnchorPointsGetterHandler.Dispose();
 1377        }
 78
 79        private void CleanUp()
 80        {
 5181            onAvatarFound = null;
 5182            cleaned = true;
 83
 5184            if (currentAnchorPointsGetterHandler != null)
 85            {
 1986                currentAnchorPointsGetterHandler.CleanUp();
 1987                currentAnchorPointsGetterHandler.OnAvatarFound -= OnAvatarFoundEvent;
 1988                currentAnchorPointsGetterHandler.OnAvatarRemoved -= OnAvatarRemovedEvent;
 89            }
 5190        }
 91
 92        private IAnchorPointsGetterHandler GetHandler(string id, string ownPlayerId)
 93        {
 1494            return id == ownPlayerId ? ownPlayerAnchorPointsGetterHandler : otherPlayerAnchorPointsGetterHandler;
 95        }
 96
 97        private void OnAvatarFoundEvent(string id, IAvatarAnchorPoints anchorPoints)
 98        {
 1299            onAvatarFound?.Invoke(anchorPoints);
 11100        }
 101
 102        private void OnAvatarRemovedEvent(string id)
 103        {
 3104            OnAvatarRemoved?.Invoke();
 2105        }
 106    }
 107}