< Summary

Information
Class: Utility.Components.Stopwatch.Stopwatch
Assembly: Utility
File(s): /home/runner/work/Utility-Blazor/Utility-Blazor/src/Utility/Components/Stopwatch/Stopwatch.razor
Tag: 231_14069517506
Line coverage
0%
Covered lines: 0
Uncovered lines: 43
Coverable lines: 43
Total lines: 109
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Input()100%210%
get_TimerOut()100%210%
.ctor()100%210%
StartStop()0%620%
Reset()100%210%
Start(...)0%620%
Stop()100%210%
OnInitialized()100%210%
OnTimedEvent()0%620%
Dispose()100%210%

File(s)

/home/runner/work/Utility-Blazor/Utility-Blazor/src/Utility/Components/Stopwatch/Stopwatch.razor

#LineLine coverage
 1@using System.Timers
 2@implements IDisposable
 3
 4<div class="container">
 5    <div class="row">
 6        <div class="col">
 7            <h2><i class="fas fa-stopwatch"></i> Stopwatch</h2>
 8        </div>
 9    </div>
 10
 11    <div class="row">
 12        <div class="col">
 13            <input id="input" name="input" class="form-control" @bind="Input" placeholder="00:00" />
 14        </div>
 15    </div>
 16
 17    <div class="row">
 18        <div class="col">
 19            <button id="btnStartStop" name="btnStartStop" class="btn btn-success" @onclick="StartStop"><i class="fas fa-
 20            <button id="btnReset" name="btnReset" class="btn btn-info float-right" @onclick="Reset"><i class="fas fa-syn
 21        </div>
 22    </div>
 23
 24    @* <div class="row"> *@
 25    @*     <div class="col"> *@
 26    @*         <p>Inspired from <a href="" target="_blank"></a></p> *@
 27    @*     </div> *@
 28    @* </div> *@
 29
 30</div>
 31
 32@code {
 33    [Parameter]
 034    public string? Input { get; set; }
 35
 36    [Parameter]
 037    public EventCallback TimerOut { get; set; }
 38
 039    private Timer timer = null!;
 040    private int secondsToRun = 0;
 41
 42    // protected override void OnInitialized()
 43    // {
 44    //     // Input = "00:00";
 45    //     timer = new System.Threading.Timer(async _ =>  // async void
 46    //     {
 47    //         Input =
 48    //         // we need StateHasChanged() because this is an async void handler
 49    //         // we need to Invoke it because we could be on the wrong Thread
 50    //         await InvokeAsync(StateHasChanged);
 51    //     }, null, 0, 1000);
 52    // }
 53
 54    void StartStop()
 055    {
 056        if (string.IsNullOrEmpty(Input)) return;
 057        Start(10);
 058    }
 59
 60    void Reset()
 061    {
 062        Start(600);
 063    }
 64
 65    public void Start(int secondsToRun)
 066    {
 067        if (secondsToRun > 0)
 068        {
 069            Input = TimeSpan.FromSeconds(secondsToRun).ToString(@"mm\:ss");
 070            StateHasChanged();
 071            timer.Start();
 072        }
 073    }
 74
 75    public void Stop()
 076    {
 077        timer.Stop();
 078    }
 79
 80    protected override void OnInitialized()
 081    {
 082        timer = new Timer(1000);
 083        timer.Elapsed += OnTimedEvent;
 084        timer.AutoReset = true;
 085    }
 86
 87    private async void OnTimedEvent(object? sender, ElapsedEventArgs e)
 088    {
 089        secondsToRun--;
 90
 091        await InvokeAsync(() =>
 092        {
 093            Input = TimeSpan.FromSeconds(secondsToRun).ToString(@"mm\:ss");
 094            StateHasChanged();
 095        });
 96
 097        if (secondsToRun <= 0)
 098        {
 099            timer.Stop();
 0100            await TimerOut.InvokeAsync();
 0101        }
 0102    }
 103
 104    public void Dispose()
 0105    {
 0106        timer.Dispose();
 0107    }
 108
 109}