Skip to content

Commit

Permalink
Refactor demo app to fix analyzer issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hmG3 committed Mar 13, 2024
1 parent cd4723b commit 1c61c2f
Show file tree
Hide file tree
Showing 21 changed files with 384 additions and 392 deletions.
2 changes: 1 addition & 1 deletion src/TaTooIne.Demo/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</Router>
15 changes: 0 additions & 15 deletions src/TaTooIne.Demo/DemoIndicators/IndicatorChart.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,3 @@
@inject IJSRuntime JsRuntime

<section id="chart" @ref="_chartCanvas"></section>

@code {
private ElementReference _chartCanvas;

[Parameter]
public Function Function { get; set; }

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JsRuntime.InvokeVoidAsync("SiteJsInterop.setupChart", _chartCanvas);
}
}
}
21 changes: 21 additions & 0 deletions src/TaTooIne.Demo/DemoIndicators/IndicatorChart.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using TALib;

namespace TaTooIne.Demo.DemoIndicators;

public partial class IndicatorChart
{
private ElementReference _chartCanvas;

[Parameter]
public required Function Function { get; set; }

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await JsRuntime.InvokeVoidAsync("SiteJsInterop.setupChart", _chartCanvas);
}
}
}
59 changes: 18 additions & 41 deletions src/TaTooIne.Demo/DemoIndicators/IndicatorData.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<section id="calculation">
@if (State == null)
{
<p><em>Loading...</em></p>
<p>
<em>Loading...</em>
</p>
}
else
{
Expand All @@ -23,47 +25,22 @@
</tr>
</thead>
<tbody>
@foreach (var row in _transposedState)
{
<tr>
<td>@row.First().Time.ToShortDateString()</td>
@foreach (var cell in row)
{
<td>
@if (cell != null)
{
@($"{cell.Value:F2}")
}
</td>
}
</tr>
}
@foreach (var row in _transposedState)
{
<tr>
<td>@row.First().Time.ToShortDateString()</td>

Check warning on line 31 in src/TaTooIne.Demo/DemoIndicators/IndicatorData.razor

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
@foreach (var cell in row)
{
<td>
@if (cell != null)
{
@($"{cell.Value:F2}")
}
</td>
}
</tr>
}
</tbody>
</table>
}
</section>

@code {
[Parameter]
public CalculatedState State { get; set; }

private IEnumerable<IEnumerable<Line>> _transposedState;

protected override void OnParametersSet()
{
if (State != null)
{
_transposedState = Transpose(State.InputValues.Concat(State.OutputValues));
}
}

private static IEnumerable<IEnumerable<T>> Transpose<T>(IEnumerable<IEnumerable<T>> @this)
{
var enumerators = @this.Select(t => t.GetEnumerator()).Where(e => e.MoveNext());

while (enumerators.Any()) {
yield return enumerators.Select(e => e.Current);
enumerators = enumerators.Where(e => e.MoveNext());
}
}
}
39 changes: 39 additions & 0 deletions src/TaTooIne.Demo/DemoIndicators/IndicatorData.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.AspNetCore.Components;
using TaTooIne.Demo.Models;

namespace TaTooIne.Demo.DemoIndicators;

public partial class IndicatorData
{
[Parameter]
public CalculatedState? State { get; set; }

private IEnumerable<IEnumerable<Line?>> _transposedState = null!;

protected override void OnParametersSet()
{
if (State != null)
{
_transposedState = Transpose(State.InputValues.Concat(State.OutputValues));
}
}

private static IEnumerable<IEnumerable<T?>> Transpose<T>(IEnumerable<IEnumerable<T?>> source)
{
// ReSharper disable once NotDisposedResourceIsReturned
var enumerators = source.Select(e => e.GetEnumerator()).Where(e => e.MoveNext()).ToList();

try
{
while (enumerators.Count != 0)
{
yield return enumerators.Select(e => e.Current);
enumerators = enumerators.Where(e => e.MoveNext()).ToList();
}
}
finally
{
enumerators.ForEach(e => e.Dispose());
}
}
}
37 changes: 8 additions & 29 deletions src/TaTooIne.Demo/DemoIndicators/IndicatorsIndex.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@
</button>
<div class="docs-toc-wrap">
<ul id="docs-toc" class="docs-toc">
@for (var i = 0; i < _groups.Capacity; i++)
@for (var i = 0; i < _groups.Count - 1; i++)
{
var closureI = i;
<li class="toc-item has-children @NavMenuCssClass(closureI)">
<button type="button" class="button-icon" @onclick="() => ToggleActive(closureI)">
var localI = i;
<li class="toc-item has-children @NavMenuCssClass(localI)">
<button type="button" class="button-icon" @onclick="() => ToggleActive(localI)">
@_groups[i].Key
</button>
<button type="button" class="submenu-toggle" @onclick="() => ToggleActive(closureI)">
<button type="button" class="submenu-toggle" @onclick="() => ToggleActive(localI)">
<span class="screen-reader-text">Submenu</span>
<span class="icon-angle-right" aria-hidden="true"></span>
</button>
<ul class="toc-submenu">
@foreach (var ind in _groups[i])
{
<li class="toc-item">
<button type="button" class="button-icon" @onclick="() => OnIndicatorSelect.InvokeAsync(ind)" @onclick:stopPropagation="true">
<button type="button" class="button-icon"
@onclick="() => OnIndicatorSelect.InvokeAsync(ind)"
@onclick:stopPropagation="true">
@ind.Name
</button>
</li>
Expand All @@ -36,26 +38,3 @@
</div>
</div>
</nav>

@code {
private bool _navMenuOpened;
private List<IGrouping<string, Function>> _groups;
private int ActiveGroupId { get; set; } = -1;

[Parameter] public EventCallback<Function> OnIndicatorSelect { get; set; }

protected override void OnInitialized()
{
_groups = new Functions().Where(g => g.Group != "Math Transform").GroupBy(f => f.Group).ToList();
}

private string NavMenuCssClass(int groupId) => ActiveGroupId == groupId ? "active" : null;

private void ToggleActive(int groupId) => ActiveGroupId = ActiveGroupId == groupId ? -1 : groupId;

private async Task ToggleNavMenuAsync()
{
_navMenuOpened = !_navMenuOpened;
await JsRuntime.InvokeVoidAsync("SiteJsInterop.collapseNavMenu", _navMenuOpened, "toc");
}
}
31 changes: 31 additions & 0 deletions src/TaTooIne.Demo/DemoIndicators/IndicatorsIndex.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using TALib;

namespace TaTooIne.Demo.DemoIndicators;

public partial class IndicatorsIndex
{
private bool _navMenuOpened;
private List<IGrouping<string, Function>> _groups = null!;

private int ActiveGroupId { get; set; } = -1;

[Parameter]
public EventCallback<Function> OnIndicatorSelect { get; set; }

protected override void OnInitialized()
{
_groups = Functions.All.Where(g => g.Group != "Math Transform").GroupBy(f => f.Group).ToList();

Check failure on line 19 in src/TaTooIne.Demo/DemoIndicators/IndicatorsIndex.razor.cs

View workflow job for this annotation

GitHub Actions / build

'Functions' does not contain a definition for 'All'
}

private string? NavMenuCssClass(int groupId) => ActiveGroupId == groupId ? "active" : null;

private void ToggleActive(int groupId) => ActiveGroupId = ActiveGroupId == groupId ? -1 : groupId;

private async Task ToggleNavMenuAsync()
{
_navMenuOpened = !_navMenuOpened;
await JsRuntime.InvokeVoidAsync("SiteJsInterop.collapseNavMenu", _navMenuOpened, "toc");
}
}
19 changes: 8 additions & 11 deletions src/TaTooIne.Demo/Models/CalculatedState.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using System.Collections.Generic;
namespace TaTooIne.Demo.Models;

namespace TaTooIne.Demo.Models
public sealed class CalculatedState
{
public class CalculatedState
{
public string FuncDescription { get; set; }
public required string FuncDescription { get; init; }

public IReadOnlyCollection<string> InputNames { get; set; }
public required IReadOnlyCollection<string> InputNames { get; init; }

public IReadOnlyCollection<string> OutputNames { get; set; }
public required IReadOnlyCollection<string> OutputNames { get; init; }

public IReadOnlyCollection<IReadOnlyCollection<Line>> InputValues { get; set; }
public required IReadOnlyCollection<IReadOnlyCollection<Line?>> InputValues { get; init; }

public IReadOnlyCollection<IReadOnlyCollection<Line>> OutputValues { get; set; }
}
}
public required List<List<Line?>> OutputValues { get; init; }
}
19 changes: 8 additions & 11 deletions src/TaTooIne.Demo/Models/Candle.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using System;
namespace TaTooIne.Demo.Models;

namespace TaTooIne.Demo.Models
public sealed class Candle
{
public sealed class Candle
{
public DateTime Time { get; set; }
public DateTime Time { get; init; }

public decimal Open { get; set; }
public double Open { get; init; }

public decimal High { get; set; }
public double High { get; init; }

public decimal Low { get; set; }
public double Low { get; init; }

public decimal Close { get; set; }
public double Close { get; init; }

public decimal Volume { get; set; }
}
public double Volume { get; init; }
}
26 changes: 12 additions & 14 deletions src/TaTooIne.Demo/Models/CandleCsvMapping.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
using System;
using TinyCsvParser.Mapping;
using TinyCsvParser.Mapping;

namespace TaTooIne.Demo.Models
namespace TaTooIne.Demo.Models;

public class CandleCsvMapping : CsvMapping<Candle>
{
public class CandleCsvMapping : CsvMapping<Candle>
public CandleCsvMapping()
{
public CandleCsvMapping()
{
MapProperty(0, x => x.Time, new SpecifiedKindDateTimeConverter(DateTimeKind.Utc));
MapProperty(1, x => x.Open);
MapProperty(2, x => x.High);
MapProperty(3, x => x.Low);
MapProperty(4, x => x.Close);
MapProperty(5, x => x.Volume);
}
MapProperty(0, x => x.Time, new SpecifiedKindDateTimeConverter(DateTimeKind.Utc));
MapProperty(1, x => x.Open);
MapProperty(2, x => x.High);
MapProperty(3, x => x.Low);
MapProperty(4, x => x.Close);
MapProperty(5, x => x.Volume);
}
}
}
11 changes: 4 additions & 7 deletions src/TaTooIne.Demo/Models/Line.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
namespace TaTooIne.Demo.Models;

namespace TaTooIne.Demo.Models
public sealed record Line
{
public sealed class Line
{
public DateTime Time { get; set; }
public required DateTime Time { get; init; }

public decimal Value { get; set; }
}
public required double Value { get; init; }
}
Loading

0 comments on commit 1c61c2f

Please sign in to comment.