< Summary

Class:ImageComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/Image/ImageComponentView.cs
Covered lines:85
Uncovered lines:10
Coverable lines:95
Total lines:236
Line coverage:89.4% (85 of 95)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ImageComponentView()0%110100%
Start()0%110100%
LateUpdate()0%330100%
Configure(...)0%110100%
RefreshControl()0%5.515072.73%
Dispose()0%110100%
SetImage(...)0%3.023087.5%
SetImage(...)0%2.042077.78%
SetImage(...)0%5.095084.62%
SetLastUriRequestCached(...)0%2100%
SetFitParent(...)0%220100%
SetLoadingIndicatorVisible(...)0%110100%
OnImageObserverUpdated(...)0%5.025090%
ResizeFillParent()0%770100%
HasParentSizeChanged()0%330100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/Image/ImageComponentView.cs

#LineLine coverage
 1using DCL.Helpers;
 2using System;
 3using UnityEngine;
 4using UnityEngine.UI;
 5
 6public interface IImageComponentView
 7{
 8    /// <summary>
 9    /// It will be triggered when the sprite has been loaded.
 10    /// </summary>
 11    event Action<Sprite> OnLoaded;
 12
 13    /// <summary>
 14    /// Set an image directly from a sprite.
 15    /// </summary>
 16    /// <param name="sprite">A sprite.</param>
 17    void SetImage(Sprite sprite, bool cleanLastLoadedUri = true);
 18
 19    /// <summary>
 20    /// Set an image from a 2D texture,
 21    /// </summary>
 22    /// <param name="texture">2D texture.</param>
 23    void SetImage(Texture2D texture);
 24
 25    /// <summary>
 26    /// Set an image from an uri.
 27    /// </summary>
 28    /// <param name="uri">Url of the image.</param>
 29    void SetImage(string uri);
 30
 31    /// <summary>
 32    /// Indicates if we want to cache the last uri request.
 33    /// </summary>
 34    /// <param name="isEnabled">True for caching the last uri request.</param>
 35    void SetLastUriRequestCached(bool isEnabled);
 36
 37    /// <summary>
 38    /// Resize the image size to fit into the parent.
 39    /// </summary>
 40    /// <param name="fitParent">True to fit the size.</param>
 41    void SetFitParent(bool fitParent);
 42
 43    /// <summary>
 44    /// Active or deactive the loading indicator.
 45    /// </summary>
 46    /// <param name="isVisible">True for showing the loading indicator and hiding the image.</param>
 47    void SetLoadingIndicatorVisible(bool isVisible);
 48}
 49
 50public class ImageComponentView : BaseComponentView, IImageComponentView, IComponentModelConfig<ImageComponentModel>
 51{
 52    [Header("Prefab References")]
 53    [SerializeField] internal Image image;
 54    [SerializeField] internal GameObject loadingIndicator;
 55
 56    [Header("Configuration")]
 57    [SerializeField] internal ImageComponentModel model;
 58
 59    public event Action<Sprite> OnLoaded;
 60
 61    internal Sprite currentSprite;
 324562    internal ILazyTextureObserver imageObserver = new LazyTextureObserver();
 63    internal Vector2 lastParentSize;
 64    internal string currentUriLoading = null;
 65    internal string lastLoadedUri = null;
 66
 58667    public override void Start() { imageObserver.AddListener(OnImageObserverUpdated); }
 68
 69    private void LateUpdate()
 70    {
 84871        if (model.fitParent && HasParentSizeChanged())
 272            SetFitParent(model.fitParent);
 84873    }
 74
 75    public virtual void Configure(ImageComponentModel newModel)
 76    {
 2277        model = newModel;
 2278        RefreshControl();
 2279    }
 80
 81    public override void RefreshControl()
 82    {
 2283        if (model == null)
 084            return;
 85
 2286        SetLastUriRequestCached(model.lastUriCached);
 2287        if (model.sprite != null)
 188            SetImage(model.sprite);
 2189        else if (model.texture != null)
 090            SetImage(model.texture);
 2191        else if (!string.IsNullOrEmpty(model.uri))
 092            SetImage(model.uri);
 93        else
 2194            SetImage(sprite: null);
 2195    }
 96
 97    public override void Dispose()
 98    {
 429599        base.Dispose();
 100
 4295101        currentUriLoading = null;
 4295102        lastLoadedUri = null;
 4295103        imageObserver.RemoveListener(OnImageObserverUpdated);
 4295104        Destroy(currentSprite);
 4295105    }
 106
 107    public void SetImage(Sprite sprite, bool cleanLastLoadedUri = true)
 108    {
 3876109        model.sprite = sprite;
 110
 3876111        if (image == null)
 0112            return;
 113
 3876114        image.sprite = sprite;
 115
 3876116        if (cleanLastLoadedUri)
 3874117            lastLoadedUri = null;
 118
 3876119        SetFitParent(model.fitParent);
 3876120    }
 121
 122    public void SetImage(Texture2D texture)
 123    {
 5124        model.texture = texture;
 125
 5126        if (!Application.isPlaying)
 127        {
 0128            OnImageObserverUpdated(texture);
 0129            return;
 130        }
 131
 5132        SetLoadingIndicatorVisible(true);
 5133        imageObserver.RefreshWithTexture(texture);
 134
 5135        lastLoadedUri = null;
 5136        SetFitParent(model.fitParent);
 5137    }
 138
 139    public virtual void SetImage(string uri)
 140    {
 17141        if (model.lastUriCached && uri == lastLoadedUri)
 0142            return;
 143
 17144        model.uri = uri;
 145
 17146        if (!Application.isPlaying)
 0147            return;
 148
 17149        SetLoadingIndicatorVisible(true);
 17150        if (!string.IsNullOrEmpty(uri))
 151        {
 16152            currentUriLoading = uri;
 16153            imageObserver.RefreshWithUri(uri);
 16154        }
 155        else
 156        {
 1157            lastLoadedUri = null;
 1158            OnImageObserverUpdated(null);
 159        }
 1160    }
 161
 0162    public void SetLastUriRequestCached(bool isEnabled) { model.lastUriCached = isEnabled; }
 163
 164    public void SetFitParent(bool fitParent)
 165    {
 3885166        model.fitParent = fitParent;
 167
 3885168        if (fitParent)
 37169            ResizeFillParent();
 3885170    }
 171
 172    public void SetLoadingIndicatorVisible(bool isVisible)
 173    {
 29174        image.enabled = !isVisible;
 29175        loadingIndicator.SetActive(isVisible);
 29176    }
 177
 178    internal void OnImageObserverUpdated(Texture2D texture)
 179    {
 2180        if (Application.isPlaying)
 2181            Destroy(currentSprite);
 182        else
 0183            DestroyImmediate(currentSprite);
 184
 2185        currentSprite = texture != null ? Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vect
 2186        SetImage(currentSprite, false);
 2187        SetLoadingIndicatorVisible(false);
 2188        lastLoadedUri = currentUriLoading;
 2189        currentUriLoading = null;
 2190        OnLoaded?.Invoke(currentSprite);
 1191    }
 192
 193    internal void ResizeFillParent()
 194    {
 37195        RectTransform imageRectTransform = (RectTransform)image.transform;
 196
 37197        imageRectTransform.anchorMin = new Vector2(0.5f, 0.5f);
 37198        imageRectTransform.anchorMax = new Vector2(0.5f, 0.5f);
 37199        imageRectTransform.pivot = new Vector2(0.5f, 0.5f);
 37200        imageRectTransform.localPosition = Vector2.zero;
 201
 37202        if (transform.parent == null)
 1203            return;
 204
 36205        RectTransform parent = transform.parent as RectTransform;
 206
 207        float h, w;
 36208        h = parent.rect.height;
 36209        w = h * (image.mainTexture != null ? (image.mainTexture.width / (float)image.mainTexture.height) : 1);
 210
 36211        if ((parent.rect.width - w) > 0)
 212        {
 24213            w = parent.rect.width;
 24214            h = w * (image.mainTexture != null ? (image.mainTexture.height / (float)image.mainTexture.width) : 1);
 215        }
 216
 36217        imageRectTransform.sizeDelta = new Vector2(w, h);
 36218    }
 219
 220    internal bool HasParentSizeChanged()
 221    {
 58222        Transform imageParent = transform.parent;
 58223        if (imageParent != null)
 224        {
 58225            Vector2 currentParentSize = ((RectTransform)imageParent).rect.size;
 226
 58227            if (lastParentSize != currentParentSize)
 228            {
 2229                lastParentSize = currentParentSize;
 2230                return true;
 231            }
 232        }
 233
 56234        return false;
 235    }
 236}