< Summary

Class:ImageComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/Image/ImageComponentView.cs
Covered lines:68
Uncovered lines:14
Coverable lines:82
Total lines:211
Line coverage:82.9% (68 of 82)
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%13.585030%
Dispose()0%110100%
SetImage(...)0%2.022083.33%
SetImage(...)0%3.13077.78%
SetImage(...)0%3.473062.5%
SetFitParent(...)0%220100%
SetLoadingIndicatorVisible(...)0%110100%
OnImageObserverUpdated(...)0%5.055087.5%
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 System.Collections;
 4using UnityEngine;
 5using UnityEngine.UI;
 6
 7public interface IImageComponentView
 8{
 9    /// <summary>
 10    /// It will be triggered when the sprite has been loaded.
 11    /// </summary>
 12    event Action<Sprite> OnLoaded;
 13
 14    /// <summary>
 15    /// Set an image directly from a sprite.
 16    /// </summary>
 17    /// <param name="sprite">A sprite.</param>
 18    void SetImage(Sprite sprite);
 19
 20    /// <summary>
 21    /// Set an image from a 2D texture,
 22    /// </summary>
 23    /// <param name="texture">2D texture.</param>
 24    void SetImage(Texture2D texture);
 25
 26    /// <summary>
 27    /// Set an image from an uri.
 28    /// </summary>
 29    /// <param name="uri">Url of the image.</param>
 30    void SetImage(string uri);
 31
 32    /// <summary>
 33    /// Resize the image size to fit into the parent.
 34    /// </summary>
 35    /// <param name="fitParent">True to fit the size.</param>
 36    void SetFitParent(bool fitParent);
 37
 38    /// <summary>
 39    /// Active or deactive the loading indicator.
 40    /// </summary>
 41    /// <param name="isVisible">True for showing the loading indicator and hiding the image.</param>
 42    void SetLoadingIndicatorVisible(bool isVisible);
 43}
 44
 45public class ImageComponentView : BaseComponentView, IImageComponentView, IComponentModelConfig
 46{
 47    [Header("Prefab References")]
 48    [SerializeField] internal Image image;
 49    [SerializeField] internal GameObject loadingIndicator;
 50
 51    [Header("Configuration")]
 52    [SerializeField] internal ImageComponentModel model;
 53
 54    public event Action<Sprite> OnLoaded;
 55
 56    internal Sprite currentSprite;
 110757    internal ILazyTextureObserver imageObserver = new LazyTextureObserver();
 58    internal Vector2 lastParentSize;
 59
 26060    public override void Start() { imageObserver.AddListener(OnImageObserverUpdated); }
 61
 62    private void LateUpdate()
 63    {
 22364        if (model.fitParent && HasParentSizeChanged())
 265            SetFitParent(model.fitParent);
 22366    }
 67
 68    public void Configure(BaseComponentModel newModel)
 69    {
 170        model = (ImageComponentModel)newModel;
 171        RefreshControl();
 172    }
 73
 74    public override void RefreshControl()
 75    {
 176        if (model == null)
 077            return;
 78
 179        if (model.sprite != null)
 180            SetImage(model.sprite);
 081        else if (model.texture != null)
 082            SetImage(model.texture);
 083        else if (!string.IsNullOrEmpty(model.uri))
 084            SetImage(model.uri);
 85        else
 086            SetImage(sprite: null);
 087    }
 88
 89    public override void Dispose()
 90    {
 112691        base.Dispose();
 92
 112693        imageObserver.RemoveListener(OnImageObserverUpdated);
 112694        Destroy(currentSprite);
 112695    }
 96
 97    public void SetImage(Sprite sprite)
 98    {
 4099        model.sprite = sprite;
 100
 40101        if (image == null)
 0102            return;
 103
 40104        image.sprite = sprite;
 40105        SetFitParent(model.fitParent);
 40106    }
 107
 108    public void SetImage(Texture2D texture)
 109    {
 5110        if (model.texture != texture)
 111        {
 5112            model.texture = texture;
 113
 5114            if (!Application.isPlaying)
 115            {
 0116                OnImageObserverUpdated(texture);
 0117                return;
 118            }
 119
 5120            SetLoadingIndicatorVisible(true);
 5121            imageObserver.RefreshWithTexture(texture);
 122        }
 123
 5124        SetFitParent(model.fitParent);
 5125    }
 126
 127    public void SetImage(string uri)
 128    {
 6129        model.uri = uri;
 130
 6131        if (!Application.isPlaying)
 0132            return;
 133
 6134        SetLoadingIndicatorVisible(true);
 6135        if (!string.IsNullOrEmpty(uri))
 6136            imageObserver.RefreshWithUri(uri);
 137        else
 0138            OnImageObserverUpdated(null);
 0139    }
 140
 141    public void SetFitParent(bool fitParent)
 142    {
 49143        model.fitParent = fitParent;
 144
 49145        if (fitParent)
 39146            ResizeFillParent();
 49147    }
 148
 149    public void SetLoadingIndicatorVisible(bool isVisible)
 150    {
 17151        image.enabled = !isVisible;
 17152        loadingIndicator.SetActive(isVisible);
 17153    }
 154
 155    internal void OnImageObserverUpdated(Texture texture)
 156    {
 1157        if (Application.isPlaying)
 1158            Destroy(currentSprite);
 159        else
 0160            DestroyImmediate(currentSprite);
 161
 1162        currentSprite = texture != null ? Sprite.Create((Texture2D)texture, new Rect(0, 0, texture.width, texture.height
 1163        SetImage(currentSprite);
 1164        SetLoadingIndicatorVisible(false);
 1165        OnLoaded?.Invoke(currentSprite);
 1166    }
 167
 168    internal void ResizeFillParent()
 169    {
 39170        RectTransform imageRectTransform = (RectTransform)image.transform;
 171
 39172        imageRectTransform.anchorMin = new Vector2(0.5f, 0.5f);
 39173        imageRectTransform.anchorMax = new Vector2(0.5f, 0.5f);
 39174        imageRectTransform.pivot = new Vector2(0.5f, 0.5f);
 39175        imageRectTransform.localPosition = Vector2.zero;
 176
 39177        if (transform.parent == null)
 1178            return;
 179
 38180        RectTransform parent = transform.parent as RectTransform;
 181
 182        float h, w;
 38183        h = parent.rect.height;
 38184        w = h * (image.mainTexture != null ? (image.mainTexture.width / (float)image.mainTexture.height) : 1);
 185
 38186        if ((parent.rect.width - w) > 0)
 187        {
 26188            w = parent.rect.width;
 26189            h = w * (image.mainTexture != null ? (image.mainTexture.height / (float)image.mainTexture.width) : 1);
 190        }
 191
 38192        imageRectTransform.sizeDelta = new Vector2(w, h);
 38193    }
 194
 195    internal bool HasParentSizeChanged()
 196    {
 58197        Transform imageParent = transform.parent;
 58198        if (imageParent != null)
 199        {
 58200            Vector2 currentParentSize = ((RectTransform)imageParent).rect.size;
 201
 58202            if (lastParentSize != currentParentSize)
 203            {
 2204                lastParentSize = currentParentSize;
 2205                return true;
 206            }
 207        }
 208
 56209        return false;
 210    }
 211}