Skip to content

Commit e474e88

Browse files
committed
ADD: Fluxor state manager
UPDATE: replace contexts with fluxor stores, rename label display components
1 parent 3a700db commit e474e88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+491
-437
lines changed

Src/Apps/Desktop/ScalesDesktop/MauiProgram.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Append.Blazor.Printing;
2+
using Fluxor;
23
using MauiPageFullScreen;
34
using Microsoft.Extensions.Configuration;
45
using Microsoft.Extensions.Logging;
@@ -29,15 +30,16 @@ public static MauiAppBuilder CreateMauiApp()
2930
builder.ApplyRefitConfigurations();
3031

3132
builder.Services.AddScoped<IPrintingService, PrintingService>();
33+
builder.Services.AddFluxor(options =>
34+
{
35+
options.WithLifetime(StoreLifetime.Singleton);
36+
options.ScanAssemblies(typeof(MauiProgram).Assembly);
37+
});
3238

3339
builder.Services.AddSingleton<PalletDocumentGenerator>();
34-
3540
builder.Services.AddSingleton<ScalesService>();
3641
builder.Services.AddSingleton<PrinterService>();
3742

38-
builder.Services.AddSingleton<LabelContext>();
39-
builder.Services.AddSingleton<PalletContext>();
40-
4143
return builder;
4244
}
4345
}

Src/Apps/Desktop/ScalesDesktop/ScalesDesktop.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
2828
<ApplicationVersion>1</ApplicationVersion>
2929

30-
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.19041.0</SupportedOSPlatformVersion>
31-
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.19041.0</TargetPlatformMinVersion>
30+
<SupportedOSPlatformVersion>10.0.19041.0</SupportedOSPlatformVersion>
31+
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
3232

3333
<AppxPackageSigningTimestampDigestAlgorithm>SHA256</AppxPackageSigningTimestampDigestAlgorithm>
3434
<PackageCertificateThumbprint>60D25C0106434F55B5A2D2089C7009A542A4C854</PackageCertificateThumbprint>
@@ -45,6 +45,7 @@
4545
<ItemGroup>
4646
<PackageReference Include="Append.Blazor.Printing" Version="6.3.0" />
4747
<PackageReference Include="FluentValidation" Version="11.9.2" />
48+
<PackageReference Include="Fluxor.Blazor.Web" Version="6.0.0" />
4849
<PackageReference Include="FullScreenStatus.Maui" Version="1.0.5" />
4950
<PackageReference Include="itext7" Version="8.0.4" />
5051
<PackageReference Include="itext7.bouncy-castle-adapter" Version="8.0.4" />

Src/Apps/Desktop/ScalesDesktop/Source/App/MainLayout.razor

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@using ScalesDesktop.Source.Features.ErrorBoundaries
2+
@using Fluxor.Blazor.Web
23
@inherits LayoutComponentBase
34

45
<ArmErrorBoundary>
@@ -10,3 +11,4 @@
1011
<FluentDialogProvider />
1112
<FluentTooltipProvider />
1213
<FluentToastProvider Position="@ToastPosition.TopCenter" />
14+
<StoreInitializer />
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
@using ScalesDesktop.Source.Shared.Services.Stores
12
@using TscZebra.Plugin.Abstractions.Enums
2-
@implements IDisposable
3+
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
34

45
<Button
56
Size="ButtonSize.Icon"
@@ -12,25 +13,23 @@
1213
</Button>
1314

1415
@code {
15-
1616
# region Injects
1717

18+
[Inject] private IState<PrinterState> PrinterState { get; set; } = default!;
1819
[Inject] private PrinterService PrinterService { get; set; } = default!;
1920
[Inject] private IToastService ToastService { get; set; } = default!;
2021
[Inject] private IStringLocalizer<ApplicationResources> Localizer { get; set; } = default!;
2122

2223
# endregion
2324

24-
protected override void OnInitialized() => PrinterService.StatusChanged += StateHasChanged;
25-
26-
private string GetIconStyle => PrinterService.Status switch
25+
private string GetIconStyle => PrinterState.Value.Status switch
2726
{
2827
PrinterStatus.Disconnected => "text-red-500",
2928
PrinterStatus.Busy or PrinterStatus.Ready => "text-green-500",
3029
_ => "text-amber-500"
3130
};
3231

33-
private string GetIndicatorStyle => PrinterService.Status switch
32+
private string GetIndicatorStyle => PrinterState.Value.Status switch
3433
{
3534
PrinterStatus.Disconnected => "bg-red-500 animate-ping",
3635
PrinterStatus.Busy or PrinterStatus.Ready => "bg-green-500",
@@ -39,8 +38,8 @@
3938

4039
private async Task PrintStatusMessage()
4140
{
42-
PrinterStatus printerStatus = await PrinterService.GetStatusAsync();
43-
switch (printerStatus)
41+
await PrinterService.RequestStatusAsync();
42+
switch (PrinterState.Value.Status)
4443
{
4544
case PrinterStatus.Disconnected:
4645
ToastService.ShowError(Localizer["PrinterStatusDisconnected"]);
@@ -49,7 +48,7 @@
4948
ToastService.ShowSuccess(Localizer["PrinterStatusReady"]);
5049
break;
5150
default:
52-
ToastService.ShowWarning(printerStatus switch
51+
ToastService.ShowWarning(PrinterState.Value.Status switch
5352
{
5453
PrinterStatus.Paused => Localizer["PrinterStatusPaused"],
5554
PrinterStatus.HeadOpen => Localizer["PrinterStatusHeadOpen"],
@@ -60,7 +59,4 @@
6059
break;
6160
}
6261
}
63-
64-
65-
public void Dispose() => PrinterService.StatusChanged -= StateHasChanged;
6662
}

Src/Apps/Desktop/ScalesDesktop/Source/Features/DeviceStatusIcons/ScaleStatusIcon.razor

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
@using MassaK.Plugin.Abstractions.Enums
2-
@implements IDisposable
2+
@using ScalesDesktop.Source.Shared.Services.Stores
3+
4+
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
35

46
<Button
57
Size="ButtonSize.Icon"
@@ -12,21 +14,23 @@
1214
</Button>
1315

1416
@code {
15-
[Inject] private ScalesService ScalesService { get; set; } = default!;
17+
# region Injects
18+
1619
[Inject] private IToastService ToastService { get; set; } = default!;
1720
[Inject] private IStringLocalizer<ApplicationResources> Localizer { get; set; } = default!;
21+
[Inject] private IState<ScalesState> ScalesState { get; set; } = default!;
1822

19-
protected override void OnInitialized() => ScalesService.StatusChanged += StateHasChanged;
23+
# endregion
2024

21-
private string GetIconStyle => ScalesService.Status switch
25+
private string GetIconStyle => ScalesState.Value.Status switch
2226
{
2327
MassaKStatus.Disabled => "text-muted-foreground",
2428
MassaKStatus.Detached => "text-red-500",
2529
MassaKStatus.Initializing => "text-amber-500",
2630
_ => "text-green-500"
2731
};
2832

29-
private string GetIndicatorStyle => ScalesService.Status switch
33+
private string GetIndicatorStyle => ScalesState.Value.Status switch
3034
{
3135
MassaKStatus.Disabled => "bg-muted-foreground",
3236
MassaKStatus.Detached => "bg-red-500 animate-ping",
@@ -36,8 +40,7 @@
3640

3741
private void PrintStatusMessage()
3842
{
39-
MassaKStatus scalesStatus = ScalesService.Status;
40-
switch (scalesStatus)
43+
switch (ScalesState.Value.Status)
4144
{
4245
case MassaKStatus.Detached:
4346
ToastService.ShowError(Localizer["ScalesStatusDetached"]);
@@ -55,6 +58,4 @@
5558
throw new ArgumentOutOfRangeException();
5659
}
5760
}
58-
59-
public void Dispose() => ScalesService.StatusChanged -= StateHasChanged;
6061
}

Src/Apps/Desktop/ScalesDesktop/Source/Features/ErrorBoundaries/PrinterErrorBoundary.razor

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
@using ScalesDesktop.Source.Shared.Services.Stores
12
@using TscZebra.Plugin.Abstractions.Enums
2-
@implements IDisposable
33

4-
@if (PrinterService.Status is PrinterStatus.Disconnected)
4+
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
5+
6+
@if (PrinterState.Value.Status is PrinterStatus.Disconnected)
57
{
68
<div class="flex flex-col size-full items-center justify-center">
79
<ExclamationTriangleIcon class="size-16 stroke-[0.9] text-red-500" />
@@ -23,14 +25,14 @@ else
2325
@ChildContent
2426
}
2527

26-
2728
@code {
29+
# region Injects
30+
2831
[Inject] private IStringLocalizer<ApplicationResources> Localizer { get; set; } = default!;
2932
[Inject] private PrinterService PrinterService { get; set; } = default!;
33+
[Inject] private IState<PrinterState> PrinterState { get; set; } = default!;
3034

31-
[Parameter] public RenderFragment? ChildContent { get; set; }
32-
33-
protected override void OnInitialized() => PrinterService.StatusChanged += StateHasChanged;
35+
# endregion
3436

35-
public void Dispose() => PrinterService.StatusChanged -= StateHasChanged;
37+
[Parameter] public RenderFragment? ChildContent { get; set; }
3638
}

Src/Apps/Desktop/ScalesDesktop/Source/Features/ErrorBoundaries/ScalesErrorBoundary.razor

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
@using MassaK.Plugin.Abstractions.Enums
2-
@implements IDisposable
2+
@using ScalesDesktop.Source.Shared.Services.Stores
33

4-
@if (ScalesService.Status is MassaKStatus.Detached or MassaKStatus.Disabled)
4+
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
5+
6+
@if (ScalesState.Value.Status is MassaKStatus.Detached or MassaKStatus.Disabled)
57
{
68
<div class="flex flex-col size-full items-center justify-center">
79
<ExclamationTriangleIcon class="size-16 stroke-[0.9] text-red-500" />
@@ -17,12 +19,12 @@ else
1719

1820

1921
@code {
20-
[Inject] private IStringLocalizer<ApplicationResources> Localizer { get; set; } = default!;
21-
[Inject] private ScalesService ScalesService { get; set; } = default!;
22+
# region Injects
2223

23-
[Parameter] public RenderFragment? ChildContent { get; set; }
24+
[Inject] private IStringLocalizer<ApplicationResources> Localizer { get; set; } = default!;
25+
[Inject] private IState<ScalesState> ScalesState { get; set; } = default!;
2426

25-
protected override void OnInitialized() => ScalesService.StatusChanged += StateHasChanged;
27+
# endregion
2628

27-
public void Dispose() => ScalesService.StatusChanged -= StateHasChanged;
29+
[Parameter] public RenderFragment? ChildContent { get; set; }
2830
}

Src/Apps/Desktop/ScalesDesktop/Source/Features/LabelPrint.razor

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
2+
13
<Button
24
Size="ButtonSize.Full"
35
OnClick="@PrintLabel"

Src/Apps/Desktop/ScalesDesktop/Source/Features/LabelPrint.razor.cs

+22-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
using Fluxor;
2+
using Fluxor.Blazor.Web.Components;
13
using MassaK.Plugin.Abstractions.Enums;
4+
using ScalesDesktop.Source.Shared.Models;
5+
using ScalesDesktop.Source.Shared.Services.Stores;
26
using TscZebra.Plugin.Abstractions.Enums;
37
using TscZebra.Plugin.Abstractions.Exceptions;
48
using Ws.Desktop.Models;
@@ -9,35 +13,30 @@
913

1014
namespace ScalesDesktop.Source.Features;
1115

12-
public sealed partial class LabelPrint : ComponentBase, IAsyncDisposable
16+
public sealed partial class LabelPrint : FluxorComponent
1317
{
1418
# region Injects
1519

1620
[Inject] private IStringLocalizer<WsDataResources> WsDataLocalizer { get; set; } = default!;
1721
[Inject] private IStringLocalizer<ApplicationResources> Localizer { get; set; } = default!;
1822
[Inject] private IToastService ToastService { get; set; } = default!;
1923
[Inject] private PrinterService PrinterService { get; set; } = default!;
20-
[Inject] private ScalesService ScalesService { get; set; } = default!;
21-
[Inject] private LabelContext LabelContext { get; set; } = default!;
2224
[Inject] private IJSRuntime JsRuntime { get; set; } = default!;
2325
[Inject] private IDesktopApi DesktopApi { get; set; } = default!;
2426
[Inject] private ArmApi ArmApi { get; set; } = default!;
27+
[Inject] private IState<PrinterState> PrinterState { get; set; } = default!;
28+
[Inject] private IState<WeightState> WeightState { get; set; } = default!;
29+
[Inject] private IState<ScalesState> ScalesState { get; set; } = default!;
30+
[Inject] private IState<PluState> PluState { get; set; } = default!;
2531

2632
#endregion
2733

2834
[Parameter, EditorRequired] public ArmValue Arm { get; set; } = default!;
35+
[Parameter, EditorRequired] public WeightKneadingModel KneadingModel { get; set; } = default!;
2936

3037
private bool IsButtonClicked { get; set; }
31-
3238
private const int ButtonCooldownDelay = 500;
3339

34-
protected override void OnInitialized()
35-
{
36-
PrinterService.StatusChanged += StateHasChanged;
37-
ScalesService.StatusChanged += StateHasChanged;
38-
ScalesService.WeightChanged += StateHasChanged;
39-
}
40-
4140
protected override async Task OnAfterRenderAsync(bool firstRender)
4241
{
4342
if (!firstRender) return;
@@ -68,15 +67,15 @@ private async Task PrintLabelAsync()
6867

6968
CreateWeightLabelDto createDto = new()
7069
{
71-
Kneading = LabelContext.KneadingModel.KneadingCount,
72-
ProductDt = GetProductDt(LabelContext.KneadingModel.ProductDate),
73-
WeightNet = LabelContext.KneadingModel.NetWeight,
74-
WeightTare = LabelContext.Plu?.TareWeight ?? 0
70+
Kneading = KneadingModel.KneadingCount,
71+
ProductDt = GetProductDt(KneadingModel.ProductDate),
72+
WeightNet = KneadingModel.NetWeight,
73+
WeightTare = PluState.Value.Plu?.TareWeight ?? 0
7574
};
7675

7776
try
7877
{
79-
WeightLabel label = await DesktopApi.CreatePluWeightLabel(Arm.Id, LabelContext.Plu!.Id, createDto);
78+
WeightLabel label = await DesktopApi.CreatePluWeightLabel(Arm.Id, PluState.Value.Plu!.Id, createDto);
8079
ArmApi.UpdateArmCounter(label.ArmCounter);
8180
await PrinterService.PrintZplAsync(label.Zpl);
8281
}
@@ -109,21 +108,21 @@ private async Task PrintLabelAsync()
109108

110109
private async Task<bool> ValidatePrinterStatus()
111110
{
112-
PrinterStatus printerStatus = await PrinterService.GetStatusAsync();
113-
if (printerStatus is PrinterStatus.Ready or PrinterStatus.Busy) return true;
111+
await PrinterService.RequestStatusAsync();
112+
if (PrinterState.Value.Status is PrinterStatus.Ready or PrinterStatus.Busy) return true;
114113
PrintPrinterStatusMessage();
115114
return false;
116115
}
117116

118117
private bool ValidateScalesStatus()
119118
{
120-
if (!ScalesService.IsStable)
119+
if (!WeightState.Value.IsStable)
121120
{
122121
ToastService.ShowWarning(Localizer["ScalesStatusUnstable"]);
123122
return false;
124123
}
125124

126-
if (LabelContext.KneadingModel.NetWeight >= 0) return true;
125+
if (KneadingModel.NetWeight >= 0) return true;
127126
ToastService.ShowWarning(Localizer["ScalesStatusTooLight"]);
128127
return false;
129128
}
@@ -132,7 +131,7 @@ private static DateTime GetProductDt(DateTime time) =>
132131
new(time.Year, time.Month, time.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
133132

134133
private void PrintPrinterStatusMessage() =>
135-
ToastService.ShowWarning(PrinterService.Status switch
134+
ToastService.ShowWarning(PrinterState.Value.Status switch
136135
{
137136
PrinterStatus.Disconnected => Localizer["PrinterStatusIsForceDisconnected"],
138137
PrinterStatus.Paused => Localizer["PrinterStatusPaused"],
@@ -143,16 +142,10 @@ private void PrintPrinterStatusMessage() =>
143142
});
144143

145144
private bool GetPrintLabelDisabledStatus() =>
146-
LabelContext.Plu == null || ScalesService.Status != MassaKStatus.Ready;
145+
PluState.Value.Plu == null || ScalesState.Value.Status != MassaKStatus.Ready;
147146

148-
# region Event Subscribe and Unsubscribe
149-
150-
public async ValueTask DisposeAsync()
147+
public new async ValueTask DisposeAsync()
151148
{
152-
PrinterService.StatusChanged -= StateHasChanged;
153-
ScalesService.StatusChanged -= StateHasChanged;
154-
ScalesService.WeightChanged -= StateHasChanged;
155-
156149
try
157150
{
158151
await JsRuntime.InvokeVoidAsync("unsubscribeMiddleMouseClickEvent");
@@ -162,6 +155,4 @@ public async ValueTask DisposeAsync()
162155
// pass
163156
}
164157
}
165-
166-
# endregion
167158
}

0 commit comments

Comments
 (0)