< Summary

Class:ImageComponentView
Assembly:UIComponents
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/UIComponents/Scripts/Components/Image/ImageComponentView.cs
Covered lines:21
Uncovered lines:29
Coverable lines:50
Total lines:146
Line coverage:42% (21 of 50)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ImageComponentView()0%110100%
PostInitialization()0%110100%
Configure(...)0%110100%
RefreshControl()0%13.585030%
Dispose()0%110100%
SetImage(...)0%2.032080%
SetImage(...)0%6200%
SetImage(...)0%6200%
SetLoadingIndicatorVisible(...)0%110100%
OnImageObserverUpdated(...)0%12300%

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    /// Fill the model and updates the image with this data.
 15    /// </summary>
 16    /// <param name="model">Data to configure the image.</param>
 17    void Configure(ImageComponentModel model);
 18
 19    /// <summary>
 20    /// Set an image directly from a sprite.
 21    /// </summary>
 22    /// <param name="sprite">A sprite.</param>
 23    void SetImage(Sprite sprite);
 24
 25    /// <summary>
 26    /// Set an image from a 2D texture,
 27    /// </summary>
 28    /// <param name="texture">2D texture.</param>
 29    void SetImage(Texture2D texture);
 30
 31    /// <summary>
 32    /// Set an image from an uri.
 33    /// </summary>
 34    /// <param name="uri">Url of the image.</param>
 35    void SetImage(string uri);
 36
 37    /// <summary>
 38    /// Active or deactive the loading indicator.
 39    /// </summary>
 40    /// <param name="isVisible">True for showing the loading indicator and hiding the image.</param>
 41    void SetLoadingIndicatorVisible(bool isVisible);
 42}
 43
 44public class ImageComponentView : BaseComponentView, IImageComponentView
 45{
 46    [Header("Prefab References")]
 47    [SerializeField] internal Image image;
 48    [SerializeField] internal GameObject loadingIndicator;
 49
 50    [Header("Configuration")]
 51    [SerializeField] internal ImageComponentModel model;
 52
 53    public event Action<Sprite> OnLoaded;
 54
 55    internal Sprite currentSprite;
 46256    internal ILazyTextureObserver imageObserver = new LazyTextureObserver();
 57
 58    public override void PostInitialization()
 59    {
 6360        imageObserver.AddListener(OnImageObserverUpdated);
 6361        Configure(model);
 6362    }
 63
 64    public void Configure(ImageComponentModel model)
 65    {
 6466        this.model = model;
 6467        RefreshControl();
 6468    }
 69
 70    public override void RefreshControl()
 71    {
 6572        if (model == null)
 073            return;
 74
 6575        if (model.sprite != null)
 6576            SetImage(model.sprite);
 077        else if (model.texture != null)
 078            SetImage(model.texture);
 079        else if (!string.IsNullOrEmpty(model.uri))
 080            SetImage(model.uri);
 81        else
 082            SetImage(sprite: null);
 083    }
 84
 85    public override void Dispose()
 86    {
 9987        base.Dispose();
 88
 9989        imageObserver.RemoveListener(OnImageObserverUpdated);
 9990        Destroy(currentSprite);
 9991    }
 92
 93    public void SetImage(Sprite sprite)
 94    {
 8695        model.sprite = sprite;
 96
 8697        if (image == null)
 098            return;
 99
 86100        image.sprite = sprite;
 86101    }
 102
 103    public void SetImage(Texture2D texture)
 104    {
 0105        model.texture = texture;
 106
 0107        if (!Application.isPlaying)
 108        {
 0109            OnImageObserverUpdated(texture);
 0110            return;
 111        }
 112
 0113        imageObserver.RefreshWithTexture(texture);
 0114        SetLoadingIndicatorVisible(true);
 0115    }
 116
 117    public void SetImage(string uri)
 118    {
 0119        model.uri = uri;
 120
 0121        if (!Application.isPlaying)
 0122            return;
 123
 0124        imageObserver.RefreshWithUri(uri);
 0125        SetLoadingIndicatorVisible(true);
 0126    }
 127
 128    public void SetLoadingIndicatorVisible(bool isVisible)
 129    {
 4130        image.enabled = !isVisible;
 4131        loadingIndicator.SetActive(isVisible);
 4132    }
 133
 134    internal void OnImageObserverUpdated(Texture texture)
 135    {
 0136        if (Application.isPlaying)
 0137            Destroy(currentSprite);
 138        else
 0139            DestroyImmediate(currentSprite);
 140
 0141        currentSprite = Sprite.Create((Texture2D)texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5
 0142        SetImage(currentSprite);
 0143        SetLoadingIndicatorVisible(false);
 0144        OnLoaded?.Invoke(currentSprite);
 0145    }
 146}