< Summary

Class:DCL.Components.DCLAvatarTexture
Assembly:DCL.Components.Texture
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:109
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 face256;
 36                public string body;
 37            }
 38
 39            public Avatars[] avatars;
 40        }
 41
 42        [System.Serializable]
 43        public class AvatarModel : DCLTexture.Model
 44        {
 45            public string userId;
 046            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<AvatarModel>(json); }
 47        }
 48
 049        public DCLAvatarTexture() {
 050            model = new AvatarModel();
 051        }
 52
 53        public override IEnumerator ApplyChanges(BaseModel newModel)
 54        {
 055            yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get());
 56
 57            //If the scene creates and destroy the component before our renderer has been turned on bad things happen!
 58            //TODO: Analyze if we can catch this upstream and stop the IEnumerator
 059            if (isDisposed)
 060                yield break;
 61
 062            AvatarModel model = (AvatarModel) newModel;
 63
 064            if (texture == null && !string.IsNullOrEmpty(model.userId))
 65            {
 066                string textureUrl = string.Empty;
 067                string sourceUrl = Environment.i.platform.serviceProviders.catalyst.lambdasUrl + "/profiles?id=" + model
 68
 69                // The sourceUrl request should return an array, with an object
 70                //      the object has `timerstamp` and `avatars`, `avatars` is an array
 71                //      we only request a single avatar so with length=1
 72                //      avatars[0] has the avatar and we have to access to
 73                //      avatars[0].avatar.snapshots, and the links are
 74                //      face,face128,face256 and body
 75
 76                // TODO: check if this user data already exists to avoid this fetch.
 077                yield return GetAvatarUrls(sourceUrl, (faceUrl) =>
 78                {
 079                    textureUrl = faceUrl;
 080                });
 81
 082                if (!string.IsNullOrEmpty(textureUrl))
 83                {
 084                    model.src = textureUrl;
 085                    yield return base.ApplyChanges(model);
 86                }
 087            }
 088        }
 89
 90        private static IEnumerator GetAvatarUrls(string url, Action<string> onURLSuccess)
 91        {
 092            yield return Environment.i.platform.webRequest.Get(
 93                url: url,
 94                downloadHandler: new DownloadHandlerBuffer(),
 95                timeout: 10,
 96                disposeOnCompleted: false,
 97                OnFail: (webRequest) =>
 98                {
 099                    Debug.LogWarning($"Request error! profile data couldn't be fetched! -- {webRequest.webRequest.error}
 0100                },
 101                OnSuccess: (webRequest) =>
 102                {
 0103                    ProfileRequestData[] data = DCL.Helpers.Utils.ParseJsonArray<ProfileRequestData[]>(webRequest.webReq
 0104                    string face256Url = data[0]?.avatars[0]?.avatar.snapshots.face256;
 0105                    onURLSuccess?.Invoke(face256Url);
 0106                });
 0107        }
 108    }
 109}