< Summary

Class:DateSeparatorEntry
Assembly:ChatHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ChatWidgetHUD/DateSeparatorEntry.cs
Covered lines:14
Uncovered lines:52
Coverable lines:66
Total lines:161
Line coverage:21.2% (14 of 66)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
Populate(...)0%110100%
SetFadeout(...)0%6200%
DeactivatePreview()0%20400%
FadeOut()0%12300%
ActivatePreview()0%12300%
ActivatePreviewInstantly()0%6200%
DeactivatePreviewInstantly()0%6200%
GetDateFormat(...)0%440100%
GetDateTimeFromUnixTimestampMilliseconds(...)0%110100%
InterpolatePreviewColor()0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/ChatWidgetHUD/DateSeparatorEntry.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Globalization;
 4using TMPro;
 5using UnityEngine;
 6using UnityEngine.UI;
 7
 8/// <summary>
 9/// Special type of entry to be used as date separator in chat conversations.
 10/// </summary>
 11public class DateSeparatorEntry : ChatEntry
 12{
 13    [SerializeField] internal TextMeshProUGUI title;
 14
 15    [Header("Preview Mode")]
 16    [SerializeField] private Image previewBackgroundImage;
 17    [SerializeField] private Color previewBackgroundColor;
 18    [SerializeField] private Color previewFontColor;
 19
 20    private DateTime timestamp;
 21    private ChatEntryModel chatEntryModel;
 22    private Color originalBackgroundColor;
 23    private Color originalFontColor;
 24
 025    public override ChatEntryModel Model => chatEntryModel;
 26
 27    private void Awake()
 28    {
 229        originalBackgroundColor = previewBackgroundImage.color;
 230        originalFontColor = title.color;
 231    }
 32
 33    public override void Populate(ChatEntryModel model)
 34    {
 235        chatEntryModel = model;
 236        title.text = GetDateFormat(GetDateTimeFromUnixTimestampMilliseconds(model.timestamp));
 237    }
 38
 39    public override void SetFadeout(bool enabled)
 40    {
 041        if (!enabled)
 42        {
 043            group.alpha = 1;
 044            fadeEnabled = false;
 045            return;
 46        }
 47
 048        fadeEnabled = true;
 049    }
 50
 51    public override void DeactivatePreview()
 52    {
 053        if (!gameObject.activeInHierarchy)
 54        {
 055            previewBackgroundImage.color = originalBackgroundColor;
 056            title.color = originalFontColor;
 057            return;
 58        }
 59
 060        if (previewInterpolationRoutine != null)
 061            StopCoroutine(previewInterpolationRoutine);
 62
 063        if (previewInterpolationAlphaRoutine != null)
 064            StopCoroutine(previewInterpolationAlphaRoutine);
 65
 066        group.alpha = 1;
 067        previewInterpolationRoutine =
 68            StartCoroutine(InterpolatePreviewColor(originalBackgroundColor, originalFontColor, 0.5f));
 069    }
 70
 71    public override void FadeOut()
 72    {
 073        if (!gameObject.activeInHierarchy)
 74        {
 075            group.alpha = 0;
 076            return;
 77        }
 78
 079        if (previewInterpolationAlphaRoutine != null)
 080            StopCoroutine(previewInterpolationAlphaRoutine);
 81
 082        previewInterpolationAlphaRoutine = StartCoroutine(InterpolateAlpha(0, 0.5f));
 083    }
 84
 85    public override void ActivatePreview()
 86    {
 087        if (!gameObject.activeInHierarchy)
 88        {
 089            ActivatePreviewInstantly();
 090            return;
 91        }
 92
 093        if (previewInterpolationRoutine != null)
 094            StopCoroutine(previewInterpolationRoutine);
 95
 096        previewInterpolationRoutine = StartCoroutine(InterpolatePreviewColor(previewBackgroundColor, previewFontColor, 0
 97
 098        previewInterpolationAlphaRoutine = StartCoroutine(InterpolateAlpha(1, 0.5f));
 099    }
 100
 101    public override void ActivatePreviewInstantly()
 102    {
 0103        if (previewInterpolationRoutine != null)
 0104            StopCoroutine(previewInterpolationRoutine);
 105
 0106        previewBackgroundImage.color = previewBackgroundColor;
 0107        title.color = previewFontColor;
 0108    }
 109
 110    public override void DeactivatePreviewInstantly()
 111    {
 0112        if (previewInterpolationRoutine != null)
 0113            StopCoroutine(previewInterpolationRoutine);
 114
 0115        previewBackgroundImage.color = originalBackgroundColor;
 0116        title.color = originalFontColor;
 0117    }
 118
 119    private string GetDateFormat(DateTime date)
 120    {
 2121        string result = string.Empty;
 122
 2123        if (date.Year == DateTime.Now.Year &&
 124            date.Month == DateTime.Now.Month &&
 125            date.Day == DateTime.Now.Day)
 126        {
 1127            result = "Today";
 1128        }
 129        else
 130        {
 1131            result = date.ToString("D", DateTimeFormatInfo.InvariantInfo);
 132        }
 133
 2134        return result;
 135    }
 136
 137    private DateTime GetDateTimeFromUnixTimestampMilliseconds(ulong milliseconds)
 138    {
 2139        DateTime result = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
 2140        return result.AddMilliseconds(milliseconds);
 141    }
 142
 143    private IEnumerator InterpolatePreviewColor(Color backgroundColor, Color fontColor, float duration)
 144    {
 0145        var t = 0f;
 146
 0147        while (t < duration)
 148        {
 0149            t += Time.deltaTime;
 150
 0151            previewBackgroundImage.color = Color.Lerp(previewBackgroundImage.color, backgroundColor, t / duration);
 0152            title.color = Color.Lerp(title.color, fontColor, t / duration);
 153
 0154            yield return null;
 155        }
 156
 0157        previewBackgroundImage.color = backgroundColor;
 0158        title.color = fontColor;
 0159    }
 160
 161}