< Summary

Class:DCL.AssetPromise_Font
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Font/AssetPromise_Font.cs
Covered lines:20
Uncovered lines:5
Coverable lines:25
Total lines:85
Line coverage:80% (20 of 25)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_Font(...)0%110100%
OnAfterLoadOrReuse()0%110100%
OnBeforeLoadOrReuse()0%110100%
OnCancelLoading()0%110100%
Cleanup()0%110100%
OnLoad(...)0%3.583060%
GetId()0%110100%
GetFontFromResources()0%6.976070%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Font/AssetPromise_Font.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using Cysharp.Threading.Tasks;
 5using DCL.Helpers;
 6using TMPro;
 7using UnityEngine;
 8
 9namespace DCL
 10{
 11    public class AssetPromise_Font : AssetPromise<Asset_Font>
 12    {
 13        private const string RESOURCE_FONT_FOLDER = "Fonts & Materials";
 14        private const string DEFAULT_SANS_SERIF_HEAVY = "Inter-Heavy SDF";
 15        private const string DEFAULT_SANS_SERIF_BOLD = "Inter-Bold SDF";
 16        private const string DEFAULT_SANS_SERIF_SEMIBOLD = "Inter-SemiBold SDF";
 17        private const string DEFAULT_SANS_SERIF = "Inter-Regular SDF";
 18
 4919        private readonly Dictionary<string, string> fontsMapping = new Dictionary<string, string>()
 20        {
 21            { "builtin:SF-UI-Text-Regular SDF", DEFAULT_SANS_SERIF },
 22            { "builtin:SF-UI-Text-Heavy SDF", DEFAULT_SANS_SERIF_HEAVY },
 23            { "builtin:SF-UI-Text-Semibold SDF", DEFAULT_SANS_SERIF_SEMIBOLD },
 24            { "builtin:LiberationSans SDF", "LiberationSans SDF" },
 25            { "SansSerif", DEFAULT_SANS_SERIF },
 26            { "SansSerif_Heavy", DEFAULT_SANS_SERIF_HEAVY },
 27            { "SansSerif_Bold", DEFAULT_SANS_SERIF_BOLD },
 28            { "SansSerif_SemiBold", DEFAULT_SANS_SERIF_SEMIBOLD },
 29        };
 30
 31        private string src;
 32        private Coroutine fontCoroutine;
 33
 14734        public AssetPromise_Font(string src) { this.src = src; }
 35
 4136        protected override void OnAfterLoadOrReuse() {  }
 37
 5138        protected override void OnBeforeLoadOrReuse() {  }
 39
 40        protected override void OnCancelLoading()
 41        {
 1042            CoroutineStarter.Stop(fontCoroutine);
 1043        }
 44
 45        public override void Cleanup()
 46        {
 3747            base.Cleanup();
 3748            CoroutineStarter.Stop(fontCoroutine);
 3749        }
 50
 51        protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail)
 52        {
 3053            if (fontsMapping.TryGetValue(src, out string fontResourceName))
 54            {
 3055                fontCoroutine = CoroutineStarter.Start(GetFontFromResources(OnSuccess,OnFail,fontResourceName));
 3056            }
 57            else
 58            {
 059                OnFail?.Invoke(new Exception("Font doesn't correspond with any know font"));
 60            }
 061        }
 62
 26063        public override object GetId() { return src; }
 64
 65        IEnumerator GetFontFromResources(Action OnSuccess, Action<Exception> OnFail, string fontResourceName)
 66        {
 3067            ResourceRequest request = Resources.LoadAsync($"{RESOURCE_FONT_FOLDER}/{fontResourceName}",
 68                typeof(TMP_FontAsset));
 69
 3070            yield return request;
 71
 2072            if (request.asset != null)
 73            {
 2074                asset.font = request.asset as TMP_FontAsset;
 2075                OnSuccess?.Invoke();
 2076            }
 77            else
 78            {
 079                string message = $"couldn't fetch font from resources {fontResourceName}";
 080                Debug.Log(message);
 081                OnFail?.Invoke(new Exception(message));
 82            }
 2083        }
 84    }
 85}