Skip to content

Commit

Permalink
RX State all new State handling
Browse files Browse the repository at this point in the history
Add Verification and renamed Scope
  • Loading branch information
Bernhard Straub committed Mar 23, 2024
1 parent 131e169 commit 5121f64
Show file tree
Hide file tree
Showing 34 changed files with 204 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@namespace RxBlazorLightCore
@typeparam TScope where TScope : IRxBLScope
@typeparam TScope where TScope : IRxBLStateScope
@typeparam TService where TService : IRxBLService

<CascadingValue Value=@Scope>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

namespace RxBlazorLightCore
{
public sealed partial class RxBLServiceScope<TScope, TService> : ComponentBase, IDisposable
public sealed partial class RxBLStateScope<TScope, TService> : ComponentBase, IDisposable
{
[Inject]
public required TService Service { get; init; }

[Parameter, EditorRequired]
public required Func<IRxBLScope> ScopeFactory { get; init; }
public required Func<IRxBLStateScope> ScopeFactory { get; init; }

[Parameter]
public required RenderFragment ChildContent { get; init; }
Expand Down
3 changes: 2 additions & 1 deletion RxBlazorLightCore/Core/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ public enum ChangeReason

public readonly record struct ServiceChangeReason(Guid ServiceID, Guid StateID, ChangeReason Reason);
public readonly record struct ServiceException(Exception Exception, Guid ID);
public readonly record struct StateValidation(string Message, bool Error);

public interface IRxBLScope : IDisposable, IObservable<ServiceChangeReason>
public interface IRxBLStateScope : IDisposable
{
public ValueTask OnContextReadyAsync();
}
Expand Down
7 changes: 1 addition & 6 deletions RxBlazorLightCore/Core/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace RxBlazorLightCore
{
public class RxBLScope<T>(T service) : IRxBLScope where T : IRxBLService
public class RxBLStateScope<T>(T service) : IRxBLStateScope where T : IRxBLService
{
protected T Service => service;

Expand All @@ -22,11 +22,6 @@ public void Dispose()
protected virtual void Dispose(bool disposing)
{
}

public IDisposable Subscribe(IObserver<ServiceChangeReason> observer)
{
return Service.Subscribe(observer);
}
}

public class RxBLService : IRxBLService
Expand Down
2 changes: 1 addition & 1 deletion RxBlazorLightCore/Core/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public async Task ChangeAsync(Func<IStateAsync<T>, Task> changeDelegateAsync, bo
ChangeCallerID = changeCallerID;

ResetCancellationToken();
PhaseChanged(false, notify);
PhaseChanged(false, false);
await changeDelegateAsync(this);
PhaseChanged(true, notify);
}
Expand Down
2 changes: 2 additions & 0 deletions RxBlazorLightCore/RxBlazorLightCore.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=component/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
2 changes: 1 addition & 1 deletion RxMudBlazorLight/ButtonBase/ButtonBaseRx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class ButtonBaseRx<T>
protected readonly Color? _cancelColor;
protected readonly string? _cancelText;
protected readonly bool _hasProgress;
protected readonly Guid _id = Guid.NewGuid();
internal readonly Guid _id = Guid.NewGuid();

private enum IconForState
{
Expand Down
3 changes: 2 additions & 1 deletion RxMudBlazorLight/ButtonBase/MudFabAsyncBaseRx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override void OnParametersSet()
{
ArgumentNullException.ThrowIfNull(_buttonRx);
_buttonRx.SetParameter(State, _changeStateAsync, _changeStateAsyncCancel, CanChange, DeferredNotification);

var parameters = _buttonRx.GetFabParameters(State, StartIcon, EndIcon, Label, IconVariant, _changeStateAsyncCancel is not null, _forceBadge);

StartIcon = parameters.StartIcon;
Expand All @@ -52,6 +52,7 @@ protected override void OnParametersSet()

Color = _buttonRx.Color;
OnClick = (EventCallback<MouseEventArgs>)_buttonRx.OnClick;

Disabled = _buttonRx.Disabled;

base.OnParametersSet();
Expand Down
7 changes: 7 additions & 0 deletions RxMudBlazorLight/Inputs/MudAutocompleteRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
Disabled = (CanChange is not null && !State.CanChange(CanChange));
Value = State.Value;
Text = Value?.ToString();
if (Validation is not null)
{
var validate = (Func<IState<T>, StateValidation>)Validation;
var validation = validate(State);
ErrorText = validation.Message;
Error = validation.Error;
}

base.OnParametersSet();
}
Expand Down
7 changes: 7 additions & 0 deletions RxMudBlazorLight/Inputs/MudCheckBoxRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
{
Disabled = (CanChange is not null && !State.CanChange(CanChange));
Value = State.Value;
if (Validation is not null)
{
var validate = (Func<IState<bool>, StateValidation>)Validation;
var validation = validate(State);
ErrorText = validation.Message;
Error = validation.Error;
}

base.OnParametersSet();
}
Expand Down
7 changes: 7 additions & 0 deletions RxMudBlazorLight/Inputs/MudDatePickerRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
{
Disabled = (CanChange is not null && !State.CanChange(CanChange));
Date = State.Value;
if (Validation is not null)
{
var validate = (Func<IState<DateTime>, StateValidation>)Validation;
var validation = validate(State);
ErrorText = validation.Message;
Error = validation.Error;
}

base.OnParametersSet();
}
Expand Down
7 changes: 7 additions & 0 deletions RxMudBlazorLight/Inputs/MudNumericFieldRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
Disabled = (CanChange is not null && !State.CanChange(CanChange));
Value = State.Value;
Text = Value?.ToString();
if (Validation is not null)
{
var validate = (Func<IState<T>, StateValidation>)Validation;
var validation = validate(State);
ErrorText = validation.Message;
Error = validation.Error;
}

base.OnParametersSet();
}
Expand Down
7 changes: 7 additions & 0 deletions RxMudBlazorLight/Inputs/MudSwitchRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
{
Disabled = (CanChange is not null && !State.CanChange(CanChange));
Value = State.Value;
if (Validation is not null)
{
var validate = (Func<IState<bool>, StateValidation>)Validation;
var validation = validate(State);
ErrorText = validation.Message;
Error = validation.Error;
}

base.OnParametersSet();
}
Expand Down
7 changes: 7 additions & 0 deletions RxMudBlazorLight/Inputs/MudTextFieldRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
Disabled = (CanChange is not null && !State.CanChange(CanChange));
Value = State.Value;
Text = Value?.ToString();
if (Validation is not null)
{
var validate = (Func<IState<T>, StateValidation>)Validation;
var validation = validate(State);
ErrorText = validation.Message;
Error = validation.Error;
}

base.OnParametersSet();
}
Expand Down
7 changes: 7 additions & 0 deletions RxMudBlazorLight/Inputs/MudTimePickerRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
{
Disabled = (CanChange is not null && !State.CanChange(CanChange));
Time = State.Value;
if (Validation is not null)
{
var validate = (Func<IState<TimeSpan>, StateValidation>)Validation;
var validation = validate(State);
ErrorText = validation.Message;
Error = validation.Error;
}

base.OnParametersSet();
}
Expand Down
9 changes: 8 additions & 1 deletion RxMudBlazorLight/Inputs/Radio/MudRadioGroupAsyncRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,16 @@
protected override void OnParametersSet()
{
Disabled = (CanChange is not null && !StateGroupAsync.CanChange(CanChange)) || StateGroupAsync.Changing();
if (StateGroupAsync.Done() && StateGroupAsync.HasValue())
if (StateGroupAsync.Done())
{
Value = StateGroupAsync.Value;
if (Validation is not null)
{
var validate = (Func<IStateAsync<T>, StateValidation>)Validation;
var validation = validate(StateGroupAsync);
ErrorText = validation.Message;
Error = validation.Error;
}
}

base.OnParametersSet();
Expand Down
7 changes: 7 additions & 0 deletions RxMudBlazorLight/Inputs/Radio/MudRadioGroupRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@
{
Disabled = (CanChange is not null && !StateGroup.CanChange(CanChange));
Value = StateGroup.Value;
if (Validation is not null)
{
var validate = (Func<IState<T>, StateValidation>)Validation;
var validation = validate(StateGroup);
ErrorText = validation.Message;
Error = validation.Error;
}

base.OnParametersSet();
}
Expand Down
11 changes: 9 additions & 2 deletions RxMudBlazorLight/Inputs/Select/MudSelectAsyncRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,17 @@
protected override void OnParametersSet()
{
Disabled = (CanChange is not null && !StateGroupAsync.CanChange(CanChange)) || StateGroupAsync.Changing();
if (StateGroupAsync.Done() && StateGroupAsync.HasValue())
if (StateGroupAsync.Done())
{
Value = StateGroupAsync.Value;
Text = Value.ToString();
Text = Value?.ToString();
if (Validation is not null)
{
var validate = (Func<IStateAsync<T>, StateValidation>)Validation;
var validation = validate(StateGroupAsync);
ErrorText = validation.Message;
Error = validation.Error;
}
}

base.OnParametersSet();
Expand Down
7 changes: 7 additions & 0 deletions RxMudBlazorLight/Inputs/Select/MudSelectRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@
Disabled = (CanChange is not null && !StateGroup.CanChange(CanChange));
Value = StateGroup.Value;
Text = Value?.ToString();
if (Validation is not null)
{
var validate = (Func<IState<T>, StateValidation>)Validation;
var validation = validate(StateGroup);
ErrorText = validation.Message;
Error = validation.Error;
}

base.OnParametersSet();
}
Expand Down
2 changes: 1 addition & 1 deletion RxMudBlazorLight/RxMudBlazorLight.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageTags>Blazor,MudBlazor,Rx,Reactive</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageOutputPath>..\Nuget</PackageOutputPath>
<PackageVersion>0.6.6</PackageVersion>
<PackageVersion>0.6.7</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions RxMudBlazorLightSample/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<RxBLServiceContext />

<MudLayout>
<RxBLServiceScope TScope=@TimerService.TimerScope TService=@TimerService ScopeFactory=@TimerService.CreateScope>
<RxBLStateScope TScope=@TimerService.TimerStateScope TService=@TimerService ScopeFactory=@TimerService.CreateScope>
<MudAppBar Elevation="0">
<MudIconButton Icon="@Icons.Material.Filled.Menu" Color="Color.Inherit" Edge="Edge.Start" OnClick="@((e) => DrawerToggle())" />
<MudSpacer />
Expand All @@ -23,7 +23,7 @@
@Body
</MudContainer>
</MudMainContent>
</RxBLServiceScope>
</RxBLStateScope>
</MudLayout>

@code {
Expand Down
2 changes: 1 addition & 1 deletion RxMudBlazorLightTestBase/CRUD/CRUDItemAddOrUpdate.razor
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ else

if (!result.Canceled)
{
var scope = (CrudService.CrudItemScope)result.Data;
var scope = (CrudService.CrudItemInput)result.Data;
await scope.SubmitAsync();
}
}
Expand Down
24 changes: 12 additions & 12 deletions RxMudBlazorLightTestBase/CRUD/CRUDItemDialog.razor
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
@inherits RxBLServiceSubscriber<CrudService>

<MudDialog>
<MudDialog ContentStyle="width: 400px; height: 400px">
<DialogContent>
<EditForm Model=@_scope OnValidSubmit="OnValidSubmit">
<MudCard>
<MudCardContent>
<MudTextFieldRx State=@_scope.Text AutoFocus="true" Immediate="true" Label="ToDo" />
<MudDatePickerRx State=@_scope.DueDateDate CanChange=@_scope.CanUpdateDueDate Label="Due Date" Editable="true" />
<MudTimePickerRx State=@_scope.DueDateTime CanChange=@_scope.CanUpdateTime PickerVariant="PickerVariant.Dialog" />
</MudCardContent>
<MudCardActions>
<MudCard>
<MudCardContent>
<MudTextFieldRx State=@_scope.Text Validation=@CrudService.CrudItemInput.ValidateText AutoFocus="true" Immediate="true" Label="ToDo" />
<MudDatePickerRx State=@_scope.DueDateDate Validation=@CrudService.CrudItemInput.ValidateDueDate CanChange=@_scope.CanUpdateDueDate Label="Due Date" Editable="true" />
<MudTimePickerRx PickerVariant=@PickerVariant.Dialog State=@_scope.DueDateTime Validation=@_scope.ValidateDueDateTime CanChange=@_scope.CanUpdateTime />
</MudCardContent>
<MudCardActions>
<MudButton Disabled=@(!_scope.CanSubmit()) ButtonType="ButtonType.Submit" Variant="Variant.Filled" Color=@Color.Success Class="ml-auto">Save</MudButton>
</MudCardActions>
</MudCard>
</MudCardActions>
</MudCard>
</EditForm>
</DialogContent>
</MudDialog>
Expand All @@ -28,11 +28,11 @@
void Cancel() => MudDialog?.Cancel();

[NotNull]
private CrudService.CrudItemScope? _scope { get; set; }
private CrudService.CrudItemInput? _scope { get; set; }

protected override void OnInitialized()
{
_scope = Service.CreateItemScope(Item);
_scope = Service.CreateItemInput(Item);
ArgumentNullException.ThrowIfNull(_scope);

MudDialog.Options.CloseButton = true;
Expand Down
10 changes: 6 additions & 4 deletions RxMudBlazorLightTestBase/CRUD/CRUDTable.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
<MudSelectRx StateGroup=@Service.CRUDDBRoleGroup CanChange=Service.CanChangeRole FullWidth=@false />
<CRUDItemAddOrUpdate AddMode=@true Item=@null />
<MudFabAsyncRx Color="Color.Error" StartIcon="@Icons.Material.Filled.AutoDelete" Size="Size.Small" ConfirmExecutionAsync=@(() => ConfirmDelete(DeleteType.Completed))
State=@Service.CRUDItemDB HasProgress=@true CanChange=@Service.CanRemoveCompletedCRUDItems ChangeStateAsync=@(CrudService.RemoveCompletedCRUDItems()) />
State=@Service.CRUDItemDB CanChange=@Service.CanRemoveCompletedCRUDItems ChangeStateAsync=@(CrudService.RemoveCompletedCRUDItems()) />
<MudFabAsyncRx Color="Color.Error" StartIcon="@Icons.Material.Filled.DeleteForever" Size="Size.Small" ConfirmExecutionAsync=@(() => ConfirmDelete(DeleteType.All))
State=@Service.CRUDItemDB HasProgress=@true CanChange=@CrudService.CanRemoveAllCRUDItems ChangeStateAsync=@(CrudService.RemoveAllCRUDItems()) />
State=@Service.CRUDItemDB CanChange=@Service.CanRemoveAllCRUDItems ChangeStateAsync=@(CrudService.RemoveAllCRUDItems()) />
</MudStack>
</CardHeaderActions>
</MudCardHeader>
Expand Down Expand Up @@ -55,13 +55,15 @@
<MudTd DataLabel="ToDo"><MudText Style=@ColorForItem(context)>@context.Text</MudText></MudTd>
<MudTd DataLabel="DueDate"><MudText Style=@ColorForItem(context)>@DateTimeForItem(context)</MudText></MudTd>
<MudTd DataLabel="Completed" Style="text-align:center">
<MudIconButtonAsyncRx Icon=@(context.Completed ? Icons.Material.Filled.CheckBox : Icons.Material.Filled.CheckBoxOutlineBlank) State=@Service.CRUDItemDB CanChange=@Service.CanToggleCRUDItemCompleted ChangeStateAsync=@(CrudService.ToggleCRUDItemCompletedAsync(context)) />
<MudIconButtonAsyncCancelRx Icon=@(context.Completed ? Icons.Material.Filled.CheckBox : Icons.Material.Filled.CheckBoxOutlineBlank) State=@Service.CRUDItemDB
CanChange=@Service.CanToggleCRUDItemCompleted ChangeStateAsync=@(CrudService.ToggleCRUDItemCompletedAsync(context)) />
</MudTd>
<MudTd DataLabel="Edit" Style="text-align:center">
<CRUDItemAddOrUpdate AddMode=@false Item=@context />
</MudTd>
<MudTd DataLabel="DeleteItem" Style="text-align:center">
<MudIconButtonAsyncRx Color="Color.Error" Icon=@Icons.Material.Filled.Delete State=@Service.CRUDItemDB HasProgress=@true ConfirmExecutionAsync=@(() => ConfirmDelete(DeleteType.One)) ChangeStateAsync=@(CrudService.RemoveCRUDItem(context)) />
<MudIconButtonAsyncRx Color="Color.Error" Icon=@Icons.Material.Filled.Delete State=@Service.CRUDItemDB CanChange=@Service.CanRemoveCRUDItem
ConfirmExecutionAsync=@(() => ConfirmDelete(DeleteType.One)) ChangeStateAsync=@(CrudService.RemoveCRUDItem(context)) />
</MudTd>
</RowTemplate>
</MudTable>
Expand Down
8 changes: 4 additions & 4 deletions RxMudBlazorLightTestBase/Components/ButtonTest.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<MudText Typo="Typo.h3" GutterBottom="true">@Service.StateInfo?.State</MudText>

<TimerComponent Name="Gobal" />
<RxBLServiceScope TScope=@TimerService.TimerScope TService=@TimerService ScopeFactory=@TimerService.CreateScope>
<RxBLStateScope TScope=@TimerService.TimerStateScope TService=@TimerService ScopeFactory=@TimerService.CreateScope>
<TimerComponent Name="ButtonTest" />
</RxBLServiceScope>
</RxBLStateScope>

<MudText Class="mb-4">Current count: @Service.CountState.Value</MudText>
<MudText Class="mb-4">Current count Async: @Service.CountStateAsync.Value</MudText>
Expand Down Expand Up @@ -43,9 +43,9 @@
<MudFabAsyncCancelRx Color="Color.Secondary" EndIcon="@Icons.Material.Filled.Add" State=@Service.CountStateAsync ChangeStateAsync=@DoPrepareAddAsync DeferredNotification=@true CancelText="Cancel Add" CancelColor=@Color.Error Label="AddAsync 2" />
</MudStack>

<RxBLServiceScope TScope=@TestService.Scope TService=@TestService ScopeFactory=@Service.CreateScope>
<RxBLStateScope TScope=@TestService.Scope TService=@TestService ScopeFactory=@Service.CreateScope>
<IconButtons />
</RxBLServiceScope>
</RxBLStateScope>

<MudStack Row=@true AlignItems=@AlignItems.Center>
<MudSwitchRx State=@Service.AddMode CanChange=@Service.AddModeCanChange Label="Switch to Add mode" />
Expand Down
Loading

0 comments on commit 5121f64

Please sign in to comment.