< Summary

Class:ButtonDoubleClick
Assembly:HUDCommon
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/Common/ButtonDoubleClick.cs
Covered lines:25
Uncovered lines:4
Coverable lines:29
Total lines:66
Line coverage:86.2% (25 of 29)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:7
Method coverage:100% (7 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ButtonDoubleClick()0%110100%
Awake()0%110100%
OnEnable()0%330100%
OnDisable()0%330100%
DetectClicks()0%1312080.95%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/Common/ButtonDoubleClick.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System;
 3using System.Threading;
 4using UnityEngine;
 5using UnityEngine.UI;
 6
 7[RequireComponent(typeof(Button))]
 8public class ButtonDoubleClick : MonoBehaviour, IButtonDoubleClick
 9{
 10    public event Action OnClick;
 11    public event Action OnDoubleClick;
 6812    public bool AlwaysPerformSingleClick { get; set; } = false;
 13
 14    private Button button;
 6615    private CancellationTokenSource cts = new CancellationTokenSource();
 16
 17    private void Awake() =>
 6518        button = GetComponent<Button>();
 19
 20    private void OnEnable()
 21    {
 8222        cts?.Cancel();
 8223        cts?.Dispose();
 8224        cts = new CancellationTokenSource();
 8225        DetectClicks().Forget();
 8226    }
 27
 28    private void OnDisable()
 29    {
 8230        cts?.Cancel();
 8231        cts?.Dispose();
 8232        cts = null;
 8233    }
 34
 35    private async UniTaskVoid DetectClicks()
 36    {
 37        const int TIME_BETWEEN_CLICKS_MS = 200;
 8238        TimeoutController timeoutController = new TimeoutController(cts);
 8239        IAsyncClickEventHandler clickEventHandler = button.GetAsyncClickEventHandler(cts.Token);
 040        while (true)
 41        {
 25542            await clickEventHandler.OnClickAsync();
 43
 344            if(AlwaysPerformSingleClick)
 345                OnClick?.Invoke();
 46
 347            timeoutController.Reset();
 48
 49            try
 50            {
 951                await clickEventHandler.OnClickAsync().AttachExternalCancellation(timeoutController.Timeout(TIME_BETWEEN
 052            }
 353            catch (OperationCanceledException e)
 54            {
 355                if(cts.IsCancellationRequested)
 056                    throw;
 357            }
 358            if (timeoutController.IsTimeout())
 59            {
 360                OnClick?.Invoke();
 361                continue;
 62            }
 063            OnDoubleClick?.Invoke();
 64        }
 65    }
 66}