Skip to content

Commit

Permalink
RX State all new State handling
Browse files Browse the repository at this point in the history
Stategroup + Naming
  • Loading branch information
Bernhard Straub committed Apr 17, 2024
1 parent 6c58561 commit c1a98cb
Show file tree
Hide file tree
Showing 47 changed files with 325 additions and 265 deletions.
27 changes: 15 additions & 12 deletions RxBlazorLightCore/Core/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public interface IState<T> : IStateInformation

public interface IStateCommand : IStateInformation
{
public void Execute(Action executeDelegate);
public void Execute(Action executeCallback);
}

public interface IStateCommandAsync : IStateCommand
Expand All @@ -61,24 +61,27 @@ public interface IStateCommandAsync : IStateCommand
public CancellationToken CancellationToken { get; }
public Guid? ChangeCallerID { get; }
public void NotifyChanging();
public Task ExecuteAsync(Func<IStateCommandAsync, Task> executeDelegateAsync, bool deferredNotification = false, Guid? changeCallerID = null);
public Task ExecuteAsync(Func<IStateCommandAsync, Task> executeCallbackAsync, bool deferredNotification = false, Guid? changeCallerID = null);
public void Cancel();
}


public interface IStateGroup<T> : IStateInformation
public interface IStateGroupBase<T> : IStateInformation
{
public T Value { get; }
public T? Value { get; }
public T[] Items { get; }
public bool ItemDisabled(int index);
public void ChangeValue(T value, Action<T, T>? changingDelegate = null);
public void Update(T value);

[MemberNotNullWhen(true, nameof(Value))]
public bool HasValue();
}

public interface IStateGroupAsync<T> : IStateInformation
public interface IStateGroup<T> : IStateGroupBase<T>
{
public T Value { get; }
public T[] Items { get; }
public bool ItemDisabled(int index);
public Task ChangeValueAsync(T value, Func<T, T, Task>? changingDelegateAsync = null);
public void ChangeValue(T value, Action<T?, T>? changingCallback = null);
}

public interface IStateGroupAsync<T> : IStateGroupBase<T>
{
public Task ChangeValueAsync(T value, Func<T?, T, Task>? changingCallbackAsync = null);
}
}
8 changes: 4 additions & 4 deletions RxBlazorLightCore/Core/RxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public static IStateCommandAsync CreateStateCommandAsync(this RxBLService servic
return StateCommandAsync.Create(service, canCancel);
}

public static IStateGroup<T> CreateStateGroup<T>(this RxBLService service, T[] items, T value, Func<int, bool>? itemDisabledDelegate = null)
public static IStateGroup<T> CreateStateGroup<T>(this RxBLService service, T[] items, T? value = default)
{
return StateGroup<T>.Create(service, items, value, itemDisabledDelegate);
return StateGroup<T>.Create(service, items, value);
}

public static IStateGroupAsync<T> CreateStateGroupAsync<T>(this RxBLService service, T[] items, T value, Func<int, bool>? itemDisabledDelegate = null)
public static IStateGroupAsync<T> CreateStateGroupAsync<T>(this RxBLService service, T[] items, T? value = default)
{
return StateGroupAsync<T>.Create(service, items, value, itemDisabledDelegate);
return StateGroupAsync<T>.Create(service, items, value);
}

public static bool Changing(this IStateInformation state)
Expand Down
74 changes: 35 additions & 39 deletions RxBlazorLightCore/Core/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ protected StateCommand(RxBLService service) : base(service)
{
}

public void Execute(Action changeDelegate)
public void Execute(Action changeCallback)
{
try
{
changeDelegate();
changeCallback();
PhaseChanged(true);
}
catch (Exception ex)
Expand Down Expand Up @@ -142,15 +142,15 @@ protected StateCommandAsync(RxBLService service, bool canCancel) : base(service)
}


public async Task ExecuteAsync(Func<IStateCommandAsync, Task> changeDelegateAsync, bool deferredNotification = false, Guid? changeCallerID = null)
public async Task ExecuteAsync(Func<IStateCommandAsync, Task> changeCallbackAsync, bool deferredNotification = false, Guid? changeCallerID = null)
{
try
{
ChangeCallerID = changeCallerID;

ResetCancellationToken();
PhaseChanged(false, !deferredNotification);
await changeDelegateAsync(this);
await changeCallbackAsync(this);
PhaseChanged(true);
}
catch (Exception ex)
Expand Down Expand Up @@ -188,35 +188,46 @@ public static IStateCommandAsync Create(RxBLService service, bool canCancel)
}
}

public class StateGroup<T> : StateBase, IStateGroup<T>
public class StateGroupBase<T> : StateBase, IStateGroupBase<T>
{
public T Value { get; private set; }
public T? Value { get; protected set; }
public T[] Items => _items;

private readonly T[] _items;
private readonly Func<int, bool>? _itemDisabledDelegate;

protected StateGroup(RxBLService service, T value, T[] items, Func<int, bool>? itemDisabledDelegate) :
protected StateGroupBase(RxBLService service, T? value, T[] items) :
base(service)
{
Value = value;
_items = items;
}

_itemDisabledDelegate = itemDisabledDelegate;
public void Update(T value)
{
Value = value;
}

public bool ItemDisabled(int index)
[MemberNotNullWhen(true, nameof(Value))]
public bool HasValue()
{
return Value is not null;
}
}

public class StateGroup<T> : StateGroupBase<T>, IStateGroup<T>
{
protected StateGroup(RxBLService service, T? value, T[] items) :
base(service, value, items)
{
return _itemDisabledDelegate is not null && _itemDisabledDelegate(index);
}

public void ChangeValue(T value, Action<T, T>? changingDelegate)
public void ChangeValue(T value, Action<T?, T>? changingCallback)
{
try
{
if (changingDelegate is not null)
if (changingCallback is not null)
{
changingDelegate(Value, value);
changingCallback(Value, value);
}
Value = value;
PhaseChanged(true);
Expand All @@ -227,42 +238,27 @@ public void ChangeValue(T value, Action<T, T>? changingDelegate)
}
}

public static IStateGroup<T> Create(RxBLService service, T[] items, T value, Func<int, bool>? itemDisabledDelegate = null)
public static IStateGroup<T> Create(RxBLService service, T[] items, T? value = default)
{
return new StateGroup<T>(service, value, items, itemDisabledDelegate);
return new StateGroup<T>(service, value, items);
}
}

public class StateGroupAsync<T> : StateBase, IStateGroupAsync<T>
public class StateGroupAsync<T> : StateGroupBase<T>, IStateGroupAsync<T>
{
public T Value { get; private set; }
public T[] Items => _items;

private readonly T[] _items;
private readonly Func<int, bool>? _itemDisabledDelegate;

protected StateGroupAsync(RxBLService service, T value, T[] items, Func<int, bool>? itemDisabledDelegate) :
base(service)
{
Value = value;
_items = items;

_itemDisabledDelegate = itemDisabledDelegate;
}

public bool ItemDisabled(int index)
protected StateGroupAsync(RxBLService service, T? value, T[] items) :
base(service, value, items)
{
return _itemDisabledDelegate is not null && _itemDisabledDelegate(index);
}

public async Task ChangeValueAsync(T value, Func<T, T, Task>? changingDelegateAsync)
public async Task ChangeValueAsync(T value, Func<T?, T, Task>? changingCallbackAsync)
{
try
{
PhaseChanged(false);
if (changingDelegateAsync is not null)
if (changingCallbackAsync is not null)
{
await changingDelegateAsync(Value, value);
await changingCallbackAsync(Value, value);
}
Value = value;
PhaseChanged(true);
Expand All @@ -273,9 +269,9 @@ public async Task ChangeValueAsync(T value, Func<T, T, Task>? changingDelegateAs
}
}

public static IStateGroupAsync<T> Create(RxBLService service, T[] items, T value, Func<int, bool>? itemDisabledDelegate = null)
public static IStateGroupAsync<T> Create(RxBLService service, T[] items, T? value = default)
{
return new StateGroupAsync<T>(service, value, items, itemDisabledDelegate);
return new StateGroupAsync<T>(service, value, items);
}
}
}
4 changes: 2 additions & 2 deletions RxBlazorLightCoreTestBase/ServiceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public ServiceFixture()

CRUDListCommand = this.CreateStateCommandAsync(true);
CRUDDictCommand = this.CreateStateCommandAsync(true);
EnumStateGroup = this.CreateStateGroup([TestEnum.ONE, TestEnum.TWO, TestEnum.THREE], TestEnum.ONE, i => i == 1);
EnumStateGroupAsync = this.CreateStateGroupAsync([TestEnum.ONE, TestEnum.TWO, TestEnum.THREE], TestEnum.ONE, i => i == 1);
EnumStateGroup = this.CreateStateGroup([TestEnum.ONE, TestEnum.TWO, TestEnum.THREE], TestEnum.ONE);
EnumStateGroupAsync = this.CreateStateGroupAsync([TestEnum.ONE, TestEnum.TWO, TestEnum.THREE], TestEnum.ONE);
}
}
}
9 changes: 0 additions & 9 deletions RxBlazorLightCoreTests/StateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,6 @@ public void TestStateGroup()
fixture.EnumStateGroup.ChangeValue(TestEnum.THREE);
while (!done) ;
Assert.Equal(TestEnum.THREE, fixture.EnumStateGroup.Value);

Assert.True(fixture.EnumStateGroup.ItemDisabled(1));

Assert.Equal(1, stateChangeCount);
}

Expand Down Expand Up @@ -656,9 +653,6 @@ public void TestStateGroupSync()
while (!done) ;
Assert.Equal(TestEnum.THREE, fixture.EnumStateGroup.Value);
Assert.Equal(TestEnum.ONE, fixture.EnumStateGroupOldValue);

Assert.True(fixture.EnumStateGroup.ItemDisabled(1));

Assert.Equal(1, stateChangeCount);
}

Expand Down Expand Up @@ -689,9 +683,6 @@ public async Task TestStateGroupAsync()
while (!done) ;
Assert.Equal(TestEnum.THREE, fixture.EnumStateGroupAsync.Value);
Assert.Equal(TestEnum.ONE, fixture.EnumStateGroupAsyncOldValue);

Assert.True(fixture.EnumStateGroupAsync.ItemDisabled(1));

Assert.Equal(2, stateChangeCount);
}
}
Expand Down
20 changes: 10 additions & 10 deletions RxMudBlazorLight/ButtonBase/ButtonRx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static ButtonRx Create(MBButtonType type, Func<Task<bool>>? confirmExecut

[MemberNotNull(nameof(OnClick))]
[MemberNotNull(nameof(OnTouch))]
public void SetParameter(IStateCommand stateCommand, Action executeCommand, Func<bool>? canChange)
public void SetParameter(IStateCommand stateCommand, Action executeCallback, Func<bool>? canChangeCallback)
{
VerifyButtonParameters();

Expand All @@ -37,15 +37,15 @@ public void SetParameter(IStateCommand stateCommand, Action executeCommand, Func
ChildContent = stateCommand.Changing() && _hasProgress ? RenderProgress() : _buttonChildContent;
}

OnClick = EventCallback.Factory.Create<MouseEventArgs>(this, () => stateCommand.Execute(executeCommand));
OnTouch = EventCallback.Factory.Create<TouchEventArgs>(this, () => stateCommand.Execute(executeCommand));
OnClick = EventCallback.Factory.Create<MouseEventArgs>(this, () => stateCommand.Execute(executeCallback));
OnTouch = EventCallback.Factory.Create<TouchEventArgs>(this, () => stateCommand.Execute(executeCallback));

Disabled = stateCommand.Changing() || (canChange is not null && !canChange());
Disabled = stateCommand.Changing() || (canChangeCallback is not null && !canChangeCallback());
}

[MemberNotNull(nameof(OnClick))]
[MemberNotNull(nameof(OnTouch))]
public void SetParameter(IStateCommandAsync stateCommand, Func<IStateCommandAsync, Task> executeCommandAsync, Func<bool>? canChange, bool deferredNotification)
public void SetParameter(IStateCommandAsync stateCommand, Func<IStateCommandAsync, Task> executeAsyncCallback, Func<bool>? canChangeCallback, bool deferredNotification)
{
VerifyButtonParametersAsync(stateCommand);

Expand Down Expand Up @@ -99,22 +99,22 @@ public void SetParameter(IStateCommandAsync stateCommand, Func<IStateCommandAsyn
Color = _buttonColor;

OnClick = EventCallback.Factory.Create<MouseEventArgs>(this, () =>
ExecuteStateCommandAsync(stateCommand, executeCommandAsync, deferredNotification));
ExecuteStateCommandAsync(stateCommand, executeAsyncCallback, deferredNotification));
OnTouch = EventCallback.Factory.Create<TouchEventArgs>(this, () =>
ExecuteStateCommandAsync(stateCommand, executeCommandAsync, deferredNotification));
ExecuteStateCommandAsync(stateCommand, executeAsyncCallback, deferredNotification));

Disabled = canChange is not null && !canChange();
Disabled = canChangeCallback is not null && !canChangeCallback();
}

OnClick ??= EventCallback.Factory.Create<MouseEventArgs>(this, _ => { });
OnTouch ??= EventCallback.Factory.Create<TouchEventArgs>(this, _ => { });
}

private async Task ExecuteStateCommandAsync(IStateCommandAsync stateCommand, Func<IStateCommandAsync, Task> executeCommandAsync, bool deferredNotification)
private async Task ExecuteStateCommandAsync(IStateCommandAsync stateCommand, Func<IStateCommandAsync, Task> executeAsyncCallback, bool deferredNotification)
{
if (_confirmExecutionAsync is null || await _confirmExecutionAsync())
{
await stateCommand.ExecuteAsync(executeCommandAsync, deferredNotification, _id);
await stateCommand.ExecuteAsync(executeAsyncCallback, deferredNotification, _id);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions RxMudBlazorLight/Buttons/MudButtonAsyncRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
public required IStateCommandAsync StateCommand { get; init; }

[Parameter, EditorRequired]
public required Func<IStateCommandAsync, Task> ExecuteCommandAsync { get; init; }
public required Func<IStateCommandAsync, Task> ExecuteAsyncCallback { get; init; }

[Parameter]
public Func<bool>? CanChange { get; init; }
public Func<bool>? CanChangeCallback { get; init; }

[Parameter]
public Func<Task<bool>>? ConfirmExecutionAsync { get; init; }
Expand Down Expand Up @@ -38,7 +38,7 @@
protected override void OnParametersSet()
{
ArgumentNullException.ThrowIfNull(_buttonRx);
_buttonRx.SetParameter(StateCommand, ExecuteCommandAsync, CanChange, DeferredNotification);
_buttonRx.SetParameter(StateCommand, ExecuteAsyncCallback, CanChangeCallback, DeferredNotification);

ChildContent = _buttonRx.ChildContent;
Color = _buttonRx.Color;
Expand Down
6 changes: 3 additions & 3 deletions RxMudBlazorLight/Buttons/MudButtonRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
public required IStateCommand StateCommand { get; init; }

[Parameter, EditorRequired]
public required Action ExecuteCommand { get; init; }
public required Action ExecuteCallback { get; init; }

[Parameter]
public Func<bool>? CanChange { get; init; }
public Func<bool>? CanChangeCallback { get; init; }

[Parameter]
public Func<Task<bool>>? ConfirmExecutionAsync { get; init; }
Expand All @@ -28,7 +28,7 @@
protected override void OnParametersSet()
{
ArgumentNullException.ThrowIfNull(_buttonRx);
_buttonRx.SetParameter(StateCommand, ExecuteCommand, CanChange);
_buttonRx.SetParameter(StateCommand, ExecuteCallback, CanChangeCallback);

OnClick = (EventCallback<MouseEventArgs>)_buttonRx.OnClick;
Disabled = _buttonRx.Disabled;
Expand Down
2 changes: 1 addition & 1 deletion RxMudBlazorLight/Dialogs/DialogAsyncRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<DialogActions>
<MudButton Disabled=@CanNotCancel() Variant="Variant.Filled" Color=@(SuccessOnConfirm ? Color.Error : Color.Success) OnClick="Cancel">Cancel</MudButton>
<MudButtonAsyncRx @ref=_buttonRef Variant="Variant.Filled" Color=@(SuccessOnConfirm ? Color.Success : Color.Error)
StateCommand=@StateCommand ExecuteCommandAsync=@ExecuteCommandAsync CanChange=@CanChange HasProgress=@HasProgress CancelText=@CancelText CancelColor=@CancelColor>
StateCommand=@StateCommand ExecuteAsyncCallback=@ExecuteAsyncCallback CanChangeCallback=@CanChangeCallback HasProgress=@HasProgress CancelText=@CancelText CancelColor=@CancelColor>
@ConfirmButton
</MudButtonAsyncRx>
</DialogActions>
Expand Down
Loading

0 comments on commit c1a98cb

Please sign in to comment.