< Summary

Class:DCL.Components.DCLAvatarTexture
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Textures/DCLAvatarTexture.cs
Covered lines:0
Uncovered lines:27
Coverable lines:27
Total lines:111
Line coverage:0% (0 of 27)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GetDataFromJSON(...)0%2100%
DCLAvatarTexture()0%2100%
ApplyChanges()0%1101000%
GetAvatarUrls()0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Textures/DCLAvatarTexture.cs

#LineLine coverage
 1using DCL.Components;
 2using DCL.Controllers;
 3using DCL.Models;
 4using System;
 5using System.Collections;
 6using DCL.Helpers;
 7using UnityEngine;
 8using UnityEngine.Networking;
 9using System.Collections.Generic;
 10using DCL;
 11
 12namespace DCL.Components
 13{
 14    public class DCLAvatarTexture : DCLTexture
 15    {
 16
 17        [System.Serializable]
 18        public class ProfileRequestData
 19        {
 20            [System.Serializable]
 21            public class Avatars
 22            {
 23                public Avatar avatar;
 24            }
 25
 26            [System.Serializable]
 27            public class Avatar
 28            {
 29                public Snapshots snapshots;
 30            }
 31
 32            [System.Serializable]
 33            public class Snapshots
 34            {
 35                public string face;
 36                public string face128;
 37                public string face256;
 38                public string body;
 39            }
 40
 41            public Avatars[] avatars;
 42        }
 43
 44        [System.Serializable]
 45        public class AvatarModel : DCLTexture.Model
 46        {
 47            public string userId;
 048            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<AvatarModel>(json); }
 49        }
 50
 051        public DCLAvatarTexture() {
 052            model = new AvatarModel();
 053        }
 54
 55        public override IEnumerator ApplyChanges(BaseModel newModel)
 56        {
 057            yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get());
 58
 59            //If the scene creates and destroy the component before our renderer has been turned on bad things happen!
 60            //TODO: Analyze if we can catch this upstream and stop the IEnumerator
 061            if (isDisposed)
 062                yield break;
 63
 064            AvatarModel model = (AvatarModel) newModel;
 65
 066            if (texture == null && !string.IsNullOrEmpty(model.userId))
 67            {
 068                string textureUrl = string.Empty;
 069                string sourceUrl = Environment.i.platform.serviceProviders.catalyst.lambdasUrl + "/profiles?id=" + model
 70
 71                // The sourceUrl request should return an array, with an object
 72                //      the object has `timerstamp` and `avatars`, `avatars` is an array
 73                //      we only request a single avatar so with length=1
 74                //      avatars[0] has the avatar and we have to access to
 75                //      avatars[0].avatar.snapshots, and the links are
 76                //      face,face128,face256 and body
 77
 78                // TODO: check if this user data already exists to avoid this fetch.
 079                yield return GetAvatarUrls(sourceUrl, (faceUrl) =>
 80                {
 081                    textureUrl = faceUrl;
 082                });
 83
 084                if (!string.IsNullOrEmpty(textureUrl))
 85                {
 086                    model.src = textureUrl;
 087                    yield return base.ApplyChanges(model);
 88                }
 089            }
 090        }
 91
 92        private static IEnumerator GetAvatarUrls(string url, Action<string> onURLSuccess)
 93        {
 094            yield return Environment.i.platform.webRequest.Get(
 95                url: url,
 96                downloadHandler: new DownloadHandlerBuffer(),
 97                timeout: 10,
 98                disposeOnCompleted: false,
 99                OnFail: (webRequest) =>
 100                {
 0101                    Debug.LogWarning($"Request error! profile data couldn't be fetched! -- {webRequest.webRequest.error}
 0102                },
 103                OnSuccess: (webRequest) =>
 104                {
 0105                    ProfileRequestData[] data = DCL.Helpers.Utils.ParseJsonArray<ProfileRequestData[]>(webRequest.webReq
 0106                    string face256Url = data[0]?.avatars[0]?.avatar.snapshots.face256;
 0107                    onURLSuccess?.Invoke(face256Url);
 0108                });
 0109        }
 110    }
 111}