< Summary

Class:ImageComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/Image/ImageComponentView.cs
Covered lines:69
Uncovered lines:15
Coverable lines:84
Total lines:214
Line coverage:82.1% (69 of 84)
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%5.024060%
SetFitParent(...)0%220100%
SetLoadingIndicatorVisible(...)0%110100%
OnImageObserverUpdated(...)0%5.055087.5%
ResizeFillParent()0%330100%
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;
 53357    internal ILazyTextureObserver imageObserver = new LazyTextureObserver();
 58    internal Vector2 lastParentSize;
 59
 59460    public override void Start() { imageObserver.AddListener(OnImageObserverUpdated); }
 61
 62    private void LateUpdate()
 63    {
 36164        if (model.fitParent && HasParentSizeChanged())
 21265            SetFitParent(model.fitParent);
 36166    }
 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    {
 78191        base.Dispose();
 92
 78193        imageObserver.RemoveListener(OnImageObserverUpdated);
 78194        Destroy(currentSprite);
 78195    }
 96
 97    public void SetImage(Sprite sprite)
 98    {
 2999        model.sprite = sprite;
 100
 29101        if (image == null)
 0102            return;
 103
 29104        image.sprite = sprite;
 29105        SetFitParent(model.fitParent);
 29106    }
 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    {
 4129        if (model.uri == uri)
 0130            return;
 131
 4132        model.uri = uri;
 133
 4134        if (!Application.isPlaying)
 0135            return;
 136
 4137        SetLoadingIndicatorVisible(true);
 4138        if (!string.IsNullOrEmpty(uri))
 4139            imageObserver.RefreshWithUri(uri);
 140        else
 0141            OnImageObserverUpdated(null);
 0142    }
 143
 144    public void SetFitParent(bool fitParent)
 145    {
 248146        model.fitParent = fitParent;
 147
 248148        if (fitParent)
 238149            ResizeFillParent();
 248150    }
 151
 152    public void SetLoadingIndicatorVisible(bool isVisible)
 153    {
 15154        image.enabled = !isVisible;
 15155        loadingIndicator.SetActive(isVisible);
 15156    }
 157
 158    internal void OnImageObserverUpdated(Texture texture)
 159    {
 1160        if (Application.isPlaying)
 1161            Destroy(currentSprite);
 162        else
 0163            DestroyImmediate(currentSprite);
 164
 1165        currentSprite = texture != null ? Sprite.Create((Texture2D)texture, new Rect(0, 0, texture.width, texture.height
 1166        SetImage(currentSprite);
 1167        SetLoadingIndicatorVisible(false);
 1168        OnLoaded?.Invoke(currentSprite);
 1169    }
 170
 171    internal void ResizeFillParent()
 172    {
 238173        RectTransform imageRectTransform = (RectTransform)image.transform;
 174
 238175        imageRectTransform.anchorMin = new Vector2(0.5f, 0.5f);
 238176        imageRectTransform.anchorMax = new Vector2(0.5f, 0.5f);
 238177        imageRectTransform.pivot = new Vector2(0.5f, 0.5f);
 238178        imageRectTransform.localPosition = Vector2.zero;
 179
 238180        if (transform.parent == null)
 1181            return;
 182
 237183        RectTransform parent = transform.parent as RectTransform;
 184
 185        float h, w;
 237186        h = parent.rect.height;
 237187        w = h * (image.mainTexture.width / (float)image.mainTexture.height);
 188
 237189        if ((parent.rect.width - w) > 0)
 190        {
 163191            w = parent.rect.width;
 163192            h = w * (image.mainTexture.height / (float)image.mainTexture.width);
 193        }
 194
 237195        imageRectTransform.sizeDelta = new Vector2(w, h);
 237196    }
 197
 198    internal bool HasParentSizeChanged()
 199    {
 258200        Transform imageParent = transform.parent;
 258201        if (imageParent != null)
 202        {
 258203            Vector2 currentParentSize = ((RectTransform)imageParent).rect.size;
 204
 258205            if (lastParentSize != currentParentSize)
 206            {
 212207                lastParentSize = currentParentSize;
 212208                return true;
 209            }
 210        }
 211
 46212        return false;
 213    }
 214}