From a89b857f4d4c8400e80d60c0d42c848e6582329d Mon Sep 17 00:00:00 2001 From: Bernhard Straub Date: Wed, 27 Mar 2024 10:08:31 +0100 Subject: [PATCH] RX State all new State handling Introduce Commands again --- RxBlazorLightCore/Core/Interfaces.cs | 36 ++- RxBlazorLightCore/Core/RxExtensions.cs | 27 +- RxBlazorLightCore/Core/ServiceExtensions.cs | 3 - RxBlazorLightCore/Core/State.cs | 109 +++++--- RxBlazorLightCoreTest/Program.cs | 47 ++-- .../RxBlazorLightCoreTest.csproj | 2 +- RxBlazorLightCoreTest/TestService.cs | 70 +++-- .../ServiceFixture.State.cs | 57 ++-- RxBlazorLightCoreTestBase/ServiceFixture.cs | 38 +-- RxBlazorLightCoreTests/StateTests.cs | 243 ++++++++++-------- RxMudBlazorLight/ButtonBase/ButtonBaseRx.cs | 18 +- RxMudBlazorLight/ButtonBase/ButtonRx.cs | 50 ++-- .../ButtonBase/MudButtonAsyncBaseRx.cs | 16 +- .../ButtonBase/MudFabAsyncBaseRx.cs | 18 +- .../ButtonBase/MudIconButtonAsyncBaseRx.cs | 18 +- .../Buttons/MudButtonAsyncCancelRx.razor | 5 +- .../Buttons/MudButtonAsyncRx.razor | 5 +- RxMudBlazorLight/Buttons/MudButtonRx.razor | 13 +- RxMudBlazorLight/Dialogs/DialogAsyncRx.razor | 9 +- .../Dialogs/DialogAsyncRx.razor.cs | 42 +-- RxMudBlazorLight/Dialogs/DialogRx.razor | 7 +- RxMudBlazorLight/Dialogs/DialogRx.razor.cs | 35 ++- .../FabButtons/MudFabAsyncCancelRx.razor | 9 +- .../FabButtons/MudFabAsyncRx.razor | 9 +- RxMudBlazorLight/FabButtons/MudFabRx.razor | 15 +- .../MudIconButtonAsyncCancelRx.razor | 9 +- .../IconButtons/MudIconButtonAsyncRx.razor | 9 +- .../IconButtons/MudIconButtonRx.razor | 15 +- .../Inputs/MudAutocompleteRx.razor | 10 +- RxMudBlazorLight/Inputs/MudCheckBoxRx.razor | 10 +- RxMudBlazorLight/Inputs/MudDatePickerRx.razor | 10 +- .../Inputs/MudNumericFieldRx.razor | 10 +- RxMudBlazorLight/Inputs/MudRatingRx.razor | 6 +- RxMudBlazorLight/Inputs/MudSliderRx.razor | 6 +- RxMudBlazorLight/Inputs/MudSwitchRx.razor | 10 +- RxMudBlazorLight/Inputs/MudTextFieldRx.razor | 10 +- RxMudBlazorLight/Inputs/MudTimePickerRx.razor | 10 +- .../Inputs/Radio/MudRadioGroupAsyncRx.razor | 17 +- .../Inputs/Radio/MudRadioGroupRx.razor | 17 +- .../Inputs/Select/MudSelectAsyncRx.razor | 31 ++- .../Inputs/Select/MudSelectRx.razor | 41 ++- .../Menus/MudMenuItemAsyncRx.razor | 13 +- RxMudBlazorLight/Menus/MudMenuItemRx.razor | 13 +- .../MudToggleIconButtonRx.razor | 6 +- .../CRUD/CRUDItemAddOrUpdate.razor | 6 +- RxMudBlazorLightTestBase/CRUD/CRUDTable.razor | 10 +- .../Components/AsyncButtons.razor | 56 ++-- .../Components/ButtonTest.razor | 55 ++-- .../Components/ColorsScopedGroup.razor | 2 +- .../Components/DebugComponent.razor | 10 +- .../Components/IconButtons.razor | 15 +- .../Components/InputTest.razor | 25 +- .../Components/SimpleState.razor | 6 +- .../Components/TestPlayground.razor | 10 +- .../Service/CrudService.cs | 67 +++-- .../Service/StateService.cs | 26 +- .../Service/TestService.State.cs | 49 ++-- .../Service/TestService.cs | 64 +++-- .../Service/TimerService.cs | 5 +- 59 files changed, 825 insertions(+), 735 deletions(-) diff --git a/RxBlazorLightCore/Core/Interfaces.cs b/RxBlazorLightCore/Core/Interfaces.cs index 1b0c335..9cdaf27 100644 --- a/RxBlazorLightCore/Core/Interfaces.cs +++ b/RxBlazorLightCore/Core/Interfaces.cs @@ -35,42 +35,54 @@ public enum StatePhase EXCEPTION } - public interface IStateBase + public interface IState { public T Value { get; set; } [MemberNotNullWhen(true, nameof(Value))] public bool HasValue(); - public void NotifyChanging(); - public StatePhase Phase { get; } public Guid ID { get; } + } + public interface IStateCommandBase + { + public StatePhase Phase { get; } + public Guid ID { get; } public Guid? ChangeCallerID { get; } } - public interface IState : IStateBase + public interface IStateCommandAsyncBase { - public bool CanChange(Func, bool> canChangeDelegate); + public void NotifyChanging(); + } - public void Change(Action> changeDelegate); + public interface IStateCommand : IStateCommandBase + { + public void Change(); + + public void Change(Action changeDelegate); } - public interface IStateAsync : IStateBase + public interface IStateCommandAsync : IStateCommand, IStateCommandAsyncBase { - public bool CanChange(Func, bool> canChangeDelegate); - public Task ChangeAsync(Func, Task> changeDelegateAsync, bool notifyChanging = false, Guid? changeCallerID = null); - public Task ChangeAsync(Func, CancellationToken, Task> changeDelegateAsync, bool notifyChanging = true, Guid? changeCallerID = null); + public Task ChangeAsync(Func changeDelegateAsync, bool notifyChanging = true, Guid? changeCallerID = null); + public Task ChangeAsync(Func changeDelegateAsync, bool notifyChanging = true, Guid? changeCallerID = null); public void Cancel(); } - public interface IStateGroup : IState + + public interface IStateGroup : IStateCommand { + public T Value { get; set; } + public T[] Items { get; } public bool ItemDisabled(int index); } - public interface IStateGroupAsync : IStateAsync + public interface IStateGroupAsync : IStateCommandAsync { + public T Value { get; set; } + public T[] Items { get; } public bool ItemDisabled(int index); } diff --git a/RxBlazorLightCore/Core/RxExtensions.cs b/RxBlazorLightCore/Core/RxExtensions.cs index ff95988..4e2ab42 100644 --- a/RxBlazorLightCore/Core/RxExtensions.cs +++ b/RxBlazorLightCore/Core/RxExtensions.cs @@ -8,42 +8,47 @@ public static IState CreateState(this RxBLService service, T value) return State.Create(service, value); } - public static IStateAsync CreateStateAsync(this RxBLService service, T value) + public static IStateCommand CreateStateCommand(this RxBLService service) { - return StateAsync.Create(service, value); + return StateCommand.Create(service); } - public static IStateGroup CreateStateGroup(this RxBLService service, T[] items, T inititalItem, Func? itemDisabledDelegate = null) + public static IStateCommandAsync CreateStateCommandAsync(this RxBLService service) { - return StateGroup.Create(service, items, inititalItem, itemDisabledDelegate); + return StateCommandAsync.Create(service); } - public static IStateGroupAsync CreateStateGroupAsync(this RxBLService service, T[] items, T inititalItem, Func? itemDisabledDelegate = null) + public static IStateGroup CreateStateGroup(this RxBLService service, T[] items, T value, Func? itemDisabledDelegate = null) { - return StateGroupAsync.Create(service, items, inititalItem, itemDisabledDelegate); + return StateGroup.Create(service, items, value, itemDisabledDelegate); } - public static bool Changing(this IStateBase state) + public static IStateGroupAsync CreateStateGroupAsync(this RxBLService service, T[] items, T value, Func? itemDisabledDelegate = null) + { + return StateGroupAsync.Create(service, items, value, itemDisabledDelegate); + } + + public static bool Changing(this IStateCommandBase state) { return state.Phase is StatePhase.CHANGING; } - public static bool Changed(this IStateBase state) + public static bool Changed(this IStateCommandBase state) { return state.Phase is StatePhase.CHANGED; } - public static bool Canceled(this IStateBase state) + public static bool Canceled(this IStateCommandBase state) { return state.Phase is StatePhase.CANCELED; } - public static bool Exception(this IStateBase state) + public static bool Exception(this IStateCommandBase state) { return state.Phase is StatePhase.EXCEPTION; } - public static bool Done(this IStateBase state) + public static bool Done(this IStateCommandBase state) { return state.Phase is StatePhase.CHANGED || state.Phase is StatePhase.CANCELED || diff --git a/RxBlazorLightCore/Core/ServiceExtensions.cs b/RxBlazorLightCore/Core/ServiceExtensions.cs index 491230e..54118bf 100644 --- a/RxBlazorLightCore/Core/ServiceExtensions.cs +++ b/RxBlazorLightCore/Core/ServiceExtensions.cs @@ -1,8 +1,5 @@  -using Microsoft.AspNetCore.Components; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using System.Reactive.Linq; namespace RxBlazorLightCore { diff --git a/RxBlazorLightCore/Core/State.cs b/RxBlazorLightCore/Core/State.cs index 5cc4775..e248ec9 100644 --- a/RxBlazorLightCore/Core/State.cs +++ b/RxBlazorLightCore/Core/State.cs @@ -2,9 +2,8 @@ namespace RxBlazorLightCore { - public class StateBase : IStateBase + public class StateBase : IStateCommandBase, IStateCommandAsyncBase { - public T Value { get; set; } public StatePhase Phase { get; private set; } = StatePhase.CHANGED; public Guid ID { get; } = Guid.NewGuid(); public Guid? ChangeCallerID { get; protected set; } @@ -12,18 +11,11 @@ public class StateBase : IStateBase protected readonly RxBLService _service; private bool _notifyChanging = true; - protected StateBase(RxBLService service, T value) + protected StateBase(RxBLService service) { - Value = value; _service = service; } - [MemberNotNullWhen(true, nameof(Value))] - public bool HasValue() - { - return Value is not null; - } - public void NotifyChanging() { if (!_notifyChanging) @@ -68,21 +60,51 @@ protected void PhaseChanged(bool changed, bool notify = true, Exception? excepti } } - public class State : StateBase, IState + public class State : StateBase, IState + { + public T Value + { + get => _value; + set + { + _value = value; + PhaseChanged(true); + } + } + + private T _value; + + protected State(RxBLService service, T value) : base(service) + { + _value = value; + } + + [MemberNotNullWhen(true, nameof(Value))] + public bool HasValue() + { + return Value is not null; + } + + public static IState Create(RxBLService service, T value) + { + return new State(service, value); + } + } + + public class StateCommandBase : StateBase { - protected State(RxBLService service, T value) : base(service, value) { } + protected StateCommandBase(RxBLService service) : base(service) { } - public bool CanChange(Func, bool> canChangeDelegate) + public void Change() { - return canChangeDelegate(this); + PhaseChanged(true); } - public void Change(Action> changeDelegate) + public void Change(Action changeDelegate) { try { - PhaseChanged(false); - changeDelegate(this); + changeDelegate(); PhaseChanged(true); } catch (Exception ex) @@ -90,27 +112,28 @@ public void Change(Action> changeDelegate) PhaseChanged(true, true, ex); } } + } - public static IState Create(RxBLService service, T value) + public class StateCommand : StateCommandBase, IStateCommand + { + protected StateCommand(RxBLService service) : base(service) { } + + public static IStateCommand Create(RxBLService service) { - return new State(service, value); + return new StateCommand(service); } } - public class StateAsync : StateBase, IStateAsync + public class StateCommandAsync : StateCommandBase, IStateCommandAsync { private CancellationTokenSource _cancellationTokenSource = new(); - protected StateAsync(RxBLService service, T value) : base(service, value) { } + protected StateCommandAsync(RxBLService service) : base(service) { } private bool _canCancel; - public bool CanChange(Func, bool> canChangeDelegate) - { - return canChangeDelegate(this); - } - public async Task ChangeAsync(Func, CancellationToken, Task> changeDelegateAsync, bool notifyChanging, Guid? changeCallerID = null) + public async Task ChangeAsync(Func changeDelegateAsync, bool notifyChanging, Guid? changeCallerID = null) { try { @@ -119,7 +142,7 @@ public async Task ChangeAsync(Func, CancellationToken, Task> chan ResetCancellationToken(); _canCancel = true; PhaseChanged(false, notifyChanging); - await changeDelegateAsync(this, _cancellationTokenSource.Token); + await changeDelegateAsync(_cancellationTokenSource.Token); PhaseChanged(true); } catch (Exception ex) @@ -133,7 +156,7 @@ public async Task ChangeAsync(Func, CancellationToken, Task> chan } } - public async Task ChangeAsync(Func, Task> changeDelegateAsync, bool notifyChanging, Guid? changeCallerID = null) + public async Task ChangeAsync(Func changeDelegateAsync, bool notifyChanging, Guid? changeCallerID = null) { try { @@ -141,7 +164,7 @@ public async Task ChangeAsync(Func, Task> changeDelegateAsync, bo ResetCancellationToken(); PhaseChanged(false, notifyChanging); - await changeDelegateAsync(this); + await changeDelegateAsync(); PhaseChanged(true); } catch (Exception ex) @@ -170,22 +193,24 @@ private void ResetCancellationToken() } } - public static IStateAsync Create(RxBLService service, T value) + public static IStateCommandAsync Create(RxBLService service) { - return new StateAsync(service, value); + return new StateCommandAsync(service); } } - public class StateGroup : State, IStateGroup + public class StateGroup : StateCommand, IStateGroup, IStateCommand { + public T Value { get; set; } public T[] Items => _items; private readonly T[] _items; private readonly Func? _itemDisabledDelegate; - protected StateGroup(RxBLService service, T inititalItem, T[] items, Func? itemDisabledDelegate) : - base(service, inititalItem) + protected StateGroup(RxBLService service, T value, T[] items, Func? itemDisabledDelegate) : + base(service) { + Value = value; _items = items; _itemDisabledDelegate = itemDisabledDelegate; } @@ -195,22 +220,24 @@ public bool ItemDisabled(int index) return _itemDisabledDelegate is not null && _itemDisabledDelegate(index); } - public static IStateGroup Create(RxBLService service, T[] items, T inititalItem, Func? itemDisabledDelegate = null) + public static IStateGroup Create(RxBLService service, T[] items, T value, Func? itemDisabledDelegate = null) { - return new StateGroup(service, inititalItem, items, itemDisabledDelegate); + return new StateGroup(service, value, items, itemDisabledDelegate); } } - public class StateGroupAsync : StateAsync, IStateGroupAsync + public class StateGroupAsync : StateCommandAsync, IStateGroupAsync, IStateCommandAsync { + public T Value { get; set; } public T[] Items => _items; private readonly T[] _items; private readonly Func? _itemDisabledDelegate; - protected StateGroupAsync(RxBLService service, T inititalItem, T[] items, Func? itemDisabledDelegate) : - base(service, inititalItem) + protected StateGroupAsync(RxBLService service, T value, T[] items, Func? itemDisabledDelegate) : + base(service) { + Value = value; _items = items; _itemDisabledDelegate = itemDisabledDelegate; } @@ -220,9 +247,9 @@ public bool ItemDisabled(int index) return _itemDisabledDelegate is not null && _itemDisabledDelegate(index); } - public static IStateGroupAsync Create(RxBLService service, T[] items, T inititalItem, Func? itemDisabledDelegate = null) + public static IStateGroupAsync Create(RxBLService service, T[] items, T value, Func? itemDisabledDelegate = null) { - return new StateGroupAsync(service, inititalItem, items, itemDisabledDelegate); + return new StateGroupAsync(service, value, items, itemDisabledDelegate); } } } \ No newline at end of file diff --git a/RxBlazorLightCoreTest/Program.cs b/RxBlazorLightCoreTest/Program.cs index f7ccd4a..0eb4466 100644 --- a/RxBlazorLightCoreTest/Program.cs +++ b/RxBlazorLightCoreTest/Program.cs @@ -7,42 +7,45 @@ TestService testService = new(); -testService.Subscribe(p => -{ - Console.WriteLine(p.ToString()); - if (testService.CounterAsync.Phase is StatePhase.CHANGING) - { - testService.CounterAsync.Cancel(); - } -}); - testService.Subscribe(cr => { Console.WriteLine(cr.StateID); + Console.WriteLine(testService.Counter.Value); + Console.WriteLine(testService.CounterCommandResult); + Console.WriteLine(testService.StringList.FirstOrDefault()); + Console.WriteLine(testService.NullString); - if (testService.CounterAsync.Phase is StatePhase.CANCELED) + if (testService.CounterCommandAsync.Phase is StatePhase.CHANGING) + { + testService.CounterCommandAsync.Cancel(); + } + + if (testService.CounterCommandAsync.Phase is StatePhase.CANCELED) { done = true; } }); -testService.Counter.Change(TestService.Increment); -testService.Counter.Change(TestService.Increment); +testService.Counter.Value = 1; +testService.Counter.Value = 2; + +testService.CounterCommand.Change(testService.Increment); +testService.CounterCommand.Change(testService.Increment); -testService.Counter.Change(TestService.Add(10)); -testService.StringList.Change(TestService.AddString("Test")); -testService.NullString.Change(s => s.Value = "TestNotNull"); +testService.CounterCommand.Change(testService.Add(10)); +testService.StringListCommand.Change(testService.AddString("Test")); +testService.StringCommand.Change(testService.SetString("TestNotNull")); -await testService.CounterAsync.ChangeAsync(TestService.AddAsync(10)); +await testService.CounterCommandAsync.ChangeAsync(testService.AddAsync(10)); -await testService.CounterAsync.ChangeAsync(TestService.IncrementAsync); +await testService.CounterCommandAsync.ChangeAsync(testService.IncrementAsync); -await testService.CounterAsync.ChangeAsync(TestService.AddAsyncLR(10)); +await testService.CounterCommandAsync.ChangeAsync(testService.AddAsyncCancel(10)); -var ccCounter = testService.Counter.CanChange(TestService.CanChangeT(1)); -ccCounter = testService.Counter.CanChange(TestService.CanChangeT(100)); -var ccCounterAsync = testService.CounterAsync.CanChange(TestService.CanChangeNV); -var ccNullString = testService.NullString.CanChange(TestService.CanChangeS("TestNotNull")); +var ccCounter = testService.CanChangeT(1); +ccCounter = testService.CanChangeT(100); +var ccCounterAsync = testService.CanChangeNV; +var ccNullString = testService.CanChangeS("TestNotNull"); /*ServiceFixture fixture = new(); diff --git a/RxBlazorLightCoreTest/RxBlazorLightCoreTest.csproj b/RxBlazorLightCoreTest/RxBlazorLightCoreTest.csproj index 6927b96..654ff78 100644 --- a/RxBlazorLightCoreTest/RxBlazorLightCoreTest.csproj +++ b/RxBlazorLightCoreTest/RxBlazorLightCoreTest.csproj @@ -8,7 +8,7 @@ - + diff --git a/RxBlazorLightCoreTest/TestService.cs b/RxBlazorLightCoreTest/TestService.cs index fd20755..6cec7f5 100644 --- a/RxBlazorLightCoreTest/TestService.cs +++ b/RxBlazorLightCoreTest/TestService.cs @@ -6,55 +6,69 @@ namespace RxBlazorLightCoreTest internal class TestService : RxBLService { public IState Counter { get; } - public IStateAsync CounterAsync { get; } - public IState> StringList { get; } - public IState NullString { get; } + + public int CounterCommandResult { get; private set; } + public IStateCommand CounterCommand { get; } + public IStateCommandAsync CounterCommandAsync { get; } + + + public IList StringList { get; } + public IStateCommand StringListCommand { get; } + + public string? NullString { get; private set; } + public IStateCommand StringCommand { get; } public TestService() { Counter = this.CreateState(0); - CounterAsync = this.CreateStateAsync(10); - StringList = this.CreateState>([]); - NullString = this.CreateState(null); + CounterCommand = this.CreateStateCommand(); + CounterCommandAsync = this.CreateStateCommandAsync(); + + StringListCommand = this.CreateStateCommand(); + StringCommand = this.CreateStateCommand(); + + StringList = []; } - public static Func, bool> CanChangeNV => s => s.Value < 20; - public static Func, bool> CanChangeT(int threshold) => s => s.Value < threshold; - public static Func, bool> CanChangeS(string compare) => s => compare != s.Value; + public bool CanChangeNV => CounterCommandResult < 20; + public bool CanChangeT(int threshold) => CounterCommandResult < threshold; + public bool CanChangeS(string compare) => NullString != compare; - public static Action> Increment => s => s.Value++; + public Action Increment => () => CounterCommandResult++; - public static Action>> AddString(string value) + public Action AddString(string value) => () => { - return s => s.Value.Add(value); - } + StringList.Add(value); + }; - public static Action> Add(int value) + public Action SetString(string value) => () => { - return s => s.Value += value; - } + NullString = value; + }; - public static Func, Task> IncrementAsync => async s => + public Action Add(int value) => () => { - await Task.Delay(1000); - s.Value++; + CounterCommandResult += value; }; - public static Func, Task> AddAsync(int value) + public async Task IncrementAsync() { - return async s => - { - await Task.Delay(1000); - s.Value += value; - }; + await Task.Delay(1000); + CounterCommandResult++; } - public static Func, CancellationToken, Task> AddAsyncLR(int value) + public Func AddAsync(int value) => async () => + { + await Task.Delay(1000); + CounterCommandResult += value; + }; + + public Func AddAsyncCancel(int value) { - return async (s, ct) => + return async ct => { await Task.Delay(1000, ct); - s.Value += value; + CounterCommandResult = value; }; } } diff --git a/RxBlazorLightCoreTestBase/ServiceFixture.State.cs b/RxBlazorLightCoreTestBase/ServiceFixture.State.cs index 9fb3c32..b6df879 100644 --- a/RxBlazorLightCoreTestBase/ServiceFixture.State.cs +++ b/RxBlazorLightCoreTestBase/ServiceFixture.State.cs @@ -1,40 +1,41 @@  -using RxBlazorLightCore; - namespace RxBlazorLightCoreTestBase { public partial class ServiceFixture { - public static Func, bool> CanChangeNotZero => s => s.Value > 0; - public static Func, bool> CanChangeBelow(int upperBound) => s => s.Value < upperBound; + public Func CanChangeNotZero => () => IntStateResult > 0; + public Func CanChangeBelow(int upperBound) => () => IntStateResult < upperBound; + + public Action Reset => () => IntStateResult = 0; + public Func ResetAsync => () => { IntStateResult = 0; return Task.CompletedTask; }; - public static Action> Increment => s => s.Value++; + public Action Increment => () => IntStateResult++; - public static Action> Add(int value) + public Action Add(int value) { - return s => s.Value += value; + return () => IntStateResult += value; } - public static Func, Task> AddAsync(int value) + public Func AddAsync(int value) { - return async s => + return async () => { - if (s.Value > 0) + if (IntStateResult > 0) { throw new InvalidOperationException("AddAsync"); } await Task.Delay(1000); - s.Value += value; + IntStateResult += value; }; } - public static Func, CancellationToken, Task> MultiplyAsync(int value) + public Func MultiplyAsync(int value) { - return async (s, ct) => + return async ct => { await Task.Delay(1000, ct); - s.Value *= value; + IntStateResult *= value; }; } @@ -46,32 +47,32 @@ public enum CMD_CRUD CLEAR } - public static Func>, CancellationToken, Task> ChangeCrudListAsync((CMD_CRUD CMD, CRUDTest? ITEM) value) + public Func ChangeCrudListAsync((CMD_CRUD CMD, CRUDTest? ITEM) value) { - return async (s, ct) => + return async ct => { if (value.CMD is CMD_CRUD.ADD) { ArgumentNullException.ThrowIfNull(value.ITEM); - s.Value.Add(value.ITEM); + CRUDList.Add(value.ITEM); } else if (value.CMD is CMD_CRUD.UPDATE) { ArgumentNullException.ThrowIfNull(value.ITEM); - var item = s.Value.Where(i => i.Id == value.ITEM.Id).FirstOrDefault(); + var item = CRUDList.Where(i => i.Id == value.ITEM.Id).FirstOrDefault(); ArgumentNullException.ThrowIfNull(item); - s.Value.Remove(item); - s.Value.Add(value.ITEM); + CRUDList.Remove(item); + CRUDList.Add(value.ITEM); } else if (value.CMD is CMD_CRUD.DELETE) { ArgumentNullException.ThrowIfNull(value.ITEM); - s.Value.Remove(value.ITEM); + CRUDList.Remove(value.ITEM); } else if (value.CMD is CMD_CRUD.CLEAR) { - s.Value.Clear(); + CRUDList.Clear(); } else { @@ -82,30 +83,30 @@ public static Func>, CancellationToken, Task> Change }; } - public static Func>, CancellationToken, Task> ChangeCrudDictAsync((CMD_CRUD CMD, Guid? ID, CRUDTest? ITEM) value) + public Func ChangeCrudDictAsync((CMD_CRUD CMD, Guid? ID, CRUDTest? ITEM) value) { - return async (s, ct) => + return async ct => { if (value.CMD is CMD_CRUD.ADD) { ArgumentNullException.ThrowIfNull(value.ITEM); ArgumentNullException.ThrowIfNull(value.ID); - s.Value.Add(value.ID.Value, value.ITEM); + CRUDDict.Add(value.ID.Value, value.ITEM); } else if (value.CMD is CMD_CRUD.UPDATE) { ArgumentNullException.ThrowIfNull(value.ITEM); ArgumentNullException.ThrowIfNull(value.ID); - s.Value[value.ID.Value] = value.ITEM; + CRUDDict[value.ID.Value] = value.ITEM; } else if (value.CMD is CMD_CRUD.DELETE) { ArgumentNullException.ThrowIfNull(value.ID); - s.Value.Remove(value.ID.Value); + CRUDDict.Remove(value.ID.Value); } else if (value.CMD is CMD_CRUD.CLEAR) { - s.Value.Clear(); + CRUDDict.Clear(); } else { diff --git a/RxBlazorLightCoreTestBase/ServiceFixture.cs b/RxBlazorLightCoreTestBase/ServiceFixture.cs index b25badb..bb0c1bd 100644 --- a/RxBlazorLightCoreTestBase/ServiceFixture.cs +++ b/RxBlazorLightCoreTestBase/ServiceFixture.cs @@ -1,5 +1,8 @@  using RxBlazorLightCore; +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("RxBlazorLightCoreTests")] namespace RxBlazorLightCoreTestBase { @@ -8,39 +11,38 @@ public record CRUDTest(string Item, Guid Id); public enum TestEnum { ONE, - TWO, + TWO, THREE } public partial class ServiceFixture : RxBLService { - public IState ObservableIntState { get; } public IState IntState { get; } - public IStateAsync IntStateAsync { get; } - public IStateAsync> CRUDListState { get; } - public IStateAsync> CRUDDictState { get; } + public int IntStateResult { get; internal set; } + public IStateCommand IntCommand { get; } + public IStateCommandAsync IntCommandAsync { get; } - public IStateGroup EnumStateGroup { get; } - public IStateGroupAsync EnumStateGroupLR { get; } + public List CRUDList { get; } = []; + public IStateCommandAsync CRUDListCommand { get; } - public string Test { get; private set; } = string.Empty; + public Dictionary CRUDDict { get; } = []; + public IStateCommandAsync CRUDDictCommand { get; } + + public IStateGroup EnumStateGroup { get; } + public IStateGroupAsync EnumStateGroupAsync { get; } public ServiceFixture() { - ObservableIntState = this.CreateState(0); IntState = this.CreateState(-1); - IntStateAsync = this.CreateStateAsync(10); - CRUDListState = this.CreateStateAsync>([]); - CRUDDictState = this.CreateStateAsync>(new Dictionary()); - EnumStateGroup = this.CreateStateGroup([TestEnum.ONE, TestEnum.TWO, TestEnum.THREE], TestEnum.ONE, i => i == 1); - EnumStateGroupLR = this.CreateStateGroupAsync([TestEnum.ONE, TestEnum.TWO, TestEnum.THREE], TestEnum.ONE, i => i == 1); - } + IntCommand = this.CreateStateCommand(); + IntCommandAsync = this.CreateStateCommandAsync(); - public void ClearTest() - { - Test = string.Empty; + CRUDListCommand = this.CreateStateCommandAsync(); + CRUDDictCommand = this.CreateStateCommandAsync(); + 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); } } } diff --git a/RxBlazorLightCoreTests/StateTests.cs b/RxBlazorLightCoreTests/StateTests.cs index 1e42a10..bd8d045 100644 --- a/RxBlazorLightCoreTests/StateTests.cs +++ b/RxBlazorLightCoreTests/StateTests.cs @@ -3,8 +3,6 @@ using Xunit.Abstractions; using System.Reactive.Concurrency; using System.Reactive.Linq; -using Microsoft.VisualStudio.TestPlatform.Utilities; -using System.Reactive; namespace RxBlazorLightCoreTests { @@ -12,6 +10,31 @@ public class StateTests(ITestOutputHelper output) { private readonly ITestOutputHelper _output = output; + [Fact] + public void TestIntState() + { + ServiceFixture fixture = new(); + var stateChangeCount = 0; + + fixture.Subscribe(sc => + { + _output.WriteLine($"Value {fixture.IntState.Value}, CC {stateChangeCount} Reason {sc.Reason}, ID {sc.StateID}, VPID {fixture.IntState.ID}"); + + if (sc.StateID == fixture.IntState.ID) + { + stateChangeCount++; + } + }); + + fixture.IntState.Value = 0; + Assert.Equal(0, fixture.IntState.Value); + + fixture.IntState.Value++; + Assert.Equal(1, fixture.IntState.Value); + + Assert.Equal(2, stateChangeCount); + } + [Fact] public void TestIncrement() { @@ -21,30 +44,30 @@ public void TestIncrement() fixture.Subscribe(sc => { - if (sc.StateID == fixture.IntState.ID) + if (sc.StateID == fixture.IntCommand.ID) { stateChangeCount++; } - _output.WriteLine($"Done {fixture.IntState.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.IntState.Phase}, ID {sc.StateID}, VPID {fixture.IntState.ID}"); + _output.WriteLine($"Done {fixture.IntCommand.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.IntCommand.Phase}, ID {sc.StateID}, VPID {fixture.IntCommand.ID}"); - if (fixture.IntState.Done()) + if (fixture.IntCommand.Done()) { done = true; } }); - fixture.IntState.Change(s => s.Value = 0); + fixture.IntCommand.Change(fixture.Reset); while (!done) ; - Assert.False(fixture.IntState.CanChange(ServiceFixture.CanChangeNotZero)); - Assert.Equal(0, fixture.IntState.Value); + Assert.False(fixture.CanChangeNotZero()); + Assert.Equal(0, fixture.IntStateResult); - fixture.IntState.Change(ServiceFixture.Increment); + fixture.IntCommand.Change(fixture.Increment); while (!done) ; - Assert.True(fixture.IntState.CanChange(ServiceFixture.CanChangeNotZero)); + Assert.True(fixture.CanChangeNotZero()); - Assert.Equal(1, fixture.IntState.Value); - Assert.Equal(4, stateChangeCount); + Assert.Equal(1, fixture.IntStateResult); + Assert.Equal(2, stateChangeCount); } [Fact] @@ -56,34 +79,34 @@ public async Task TestNotNotify() fixture.Subscribe(sc => { - if (sc.StateID == fixture.IntStateAsync.ID) + if (sc.StateID == fixture.IntCommandAsync.ID) { stateChangeCount++; } - _output.WriteLine($"Done {fixture.IntStateAsync.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.IntStateAsync.Phase}, ID {sc.StateID}, VPID {fixture.IntStateAsync.ID}"); + _output.WriteLine($"Done {fixture.IntCommandAsync.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.IntCommandAsync.Phase}, ID {sc.StateID}, VPID {fixture.IntCommandAsync.ID}"); - if (fixture.IntStateAsync.Done()) + if (fixture.IntCommandAsync.Done()) { done = true; } }); - await fixture.IntStateAsync.ChangeAsync(s => { s.Value = 0; return Task.CompletedTask; }, false); - Assert.Equal(0, fixture.IntStateAsync.Value); + await fixture.IntCommandAsync.ChangeAsync(fixture.ResetAsync, false); + Assert.Equal(0, fixture.IntStateResult); - await fixture.IntStateAsync.ChangeAsync(_ => throw new ArgumentOutOfRangeException("Test"), false); + await fixture.IntCommandAsync.ChangeAsync(_ => throw new ArgumentOutOfRangeException("Test"), false); while (!done) ; - Assert.Equal(0, fixture.IntStateAsync.Value); + Assert.Equal(0, fixture.IntStateResult); Assert.Equal(2, stateChangeCount); - await fixture.IntStateAsync.ChangeAsync(async s => { await Task.Delay(10); s.NotifyChanging() ; s.Value = 1; }, false); - Assert.Equal(1, fixture.IntStateAsync.Value); + await fixture.IntCommandAsync.ChangeAsync(async () => { await Task.Delay(10); fixture.IntCommandAsync.NotifyChanging() ; fixture.IntStateResult = 1; }, false); + Assert.Equal(1, fixture.IntStateResult); - await fixture.IntStateAsync.ChangeAsync(async s => { await Task.Delay(10); throw new ArgumentOutOfRangeException("Test"); }, false); + await fixture.IntCommandAsync.ChangeAsync(async () => { await Task.Delay(10); throw new ArgumentOutOfRangeException("Test"); }, false); while (!done) ; - Assert.Equal(1, fixture.IntStateAsync.Value); + Assert.Equal(1, fixture.IntStateResult); Assert.Equal(5, stateChangeCount); } @@ -97,28 +120,28 @@ public void TestAdd() fixture.Subscribe(sc => { - if (sc.StateID == fixture.IntState.ID) + if (sc.StateID == fixture.IntCommand.ID) { stateChangeCount++; } - _output.WriteLine($"Done {fixture.IntState.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.IntState.Phase}, ID {sc.StateID}, VPID {fixture.IntState.ID}"); + _output.WriteLine($"Done {fixture.IntCommand.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.IntCommand.Phase}, ID {sc.StateID}, VPID {fixture.IntCommand.ID}"); - if (fixture.IntState.Done()) + if (fixture.IntCommand.Done()) { done = true; } }); - fixture.IntState.Change(s => s.Value = 0); + fixture.IntCommand.Change(fixture.Reset); while (!done) ; - Assert.Equal(0, fixture.IntState.Value); + Assert.Equal(0, fixture.IntStateResult); - fixture.IntState.Change(ServiceFixture.Add(10)); + fixture.IntCommand.Change(fixture.Add(10)); while (!done) ; - Assert.Equal(10, fixture.IntState.Value); - Assert.Equal(4, stateChangeCount); + Assert.Equal(10, fixture.IntStateResult); + Assert.Equal(2, stateChangeCount); } [Fact] @@ -130,27 +153,29 @@ public async Task TestIntStateAsyncX() fixture.Subscribe(sc => { - if (sc.StateID == fixture.IntStateAsync.ID) + if (sc.StateID == fixture.IntCommandAsync.ID) { stateChangeCount++; } - _output.WriteLine($"Done {fixture.IntStateAsync.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.IntStateAsync.Phase}, ID {sc.StateID}, VPID {fixture.IntStateAsync.ID}"); + _output.WriteLine($"Done {fixture.IntCommandAsync.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.IntCommandAsync.Phase}, ID {sc.StateID}, VPID {fixture.IntCommandAsync.ID}"); - if (fixture.IntStateAsync.Done()) + if (fixture.IntCommandAsync.Done()) { done = true; } }); - Assert.Equal(10, fixture.IntStateAsync.Value); - Assert.True(fixture.IntStateAsync.CanChange(ServiceFixture.CanChangeBelow(20))); + fixture.IntStateResult = 10; - await fixture.IntStateAsync.ChangeAsync(ServiceFixture.MultiplyAsync(5)); + Assert.Equal(10, fixture.IntStateResult); + Assert.True(fixture.CanChangeBelow(20)()); + + await fixture.IntCommandAsync.ChangeAsync(fixture.MultiplyAsync(5)); while (!done) ; - Assert.False(fixture.IntStateAsync.CanChange(ServiceFixture.CanChangeBelow(20))); - Assert.Equal(50, fixture.IntStateAsync.Value); + Assert.False(fixture.CanChangeBelow(20)()); + Assert.Equal(50, fixture.IntStateResult); Assert.Equal(2, stateChangeCount); } @@ -164,44 +189,44 @@ public async Task TestAddThrow() fixture.Subscribe(sc => { - if (!done && sc.StateID == fixture.IntStateAsync.ID) + if (!done && sc.StateID == fixture.IntCommandAsync.ID) { stateChangeCount++; } - if (fixture.IntStateAsync.Phase is StatePhase.EXCEPTION && + if (fixture.IntCommandAsync.Phase is StatePhase.EXCEPTION && fixture.Exceptions.First().Exception.Message == "AddAsync") { exception = true; } - _output.WriteLine($"Done {fixture.IntStateAsync.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.IntStateAsync.Phase}, ID {sc.StateID}, VPID {fixture.IntStateAsync.ID}"); + _output.WriteLine($"Done {fixture.IntCommandAsync.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.IntCommandAsync.Phase}, ID {sc.StateID}, VPID {fixture.IntCommandAsync.ID}"); - if (fixture.IntStateAsync.Done()) + if (fixture.IntCommandAsync.Done()) { done = true; } }); fixture.ResetExceptions(); - await fixture.IntStateAsync.ChangeAsync(s => { s.Value = 0; return Task.CompletedTask; }); + await fixture.IntCommandAsync.ChangeAsync(fixture.ResetAsync); while (!done) ; - Assert.Equal(0, fixture.IntStateAsync.Value); + Assert.Equal(0, fixture.IntStateResult); - await fixture.IntStateAsync.ChangeAsync(ServiceFixture.AddAsync(10)); + await fixture.IntCommandAsync.ChangeAsync(fixture.AddAsync(10)); while (!done) ; - Assert.Equal(10, fixture.IntStateAsync.Value); - Assert.Equal(1, stateChangeCount); + Assert.Equal(10, fixture.IntStateResult); + Assert.Equal(2, stateChangeCount); done = false; stateChangeCount = 0; - await fixture.IntStateAsync.ChangeAsync(ServiceFixture.AddAsync(1)); + await fixture.IntCommandAsync.ChangeAsync(fixture.AddAsync(1)); while (!done) ; Assert.True(exception); - Assert.Equal(1, stateChangeCount); + Assert.Equal(2, stateChangeCount); } [Fact] @@ -216,29 +241,29 @@ public void TestStateObservable() { phaseChangeCount++; - _output.WriteLine($"Value {fixture.IntState.Value}, PCC {phaseChangeCount} Phase {p}"); + _output.WriteLine($"Value {fixture.IntStateResult}, PCC {phaseChangeCount} Phase {p}"); - if (fixture.IntState.Phase is StatePhase.CHANGED) + if (fixture.IntCommand.Phase is StatePhase.CHANGED) { - initialized = fixture.IntState.Value == 0; - completed = !completed && fixture.IntState.Value == 2; + initialized = fixture.IntStateResult == 0; + completed = !completed && fixture.IntStateResult == 2; } }); - fixture.IntState.Change(s => s.Value = 0); + fixture.IntCommand.Change(fixture.Reset); while (!initialized) ; - Assert.Equal(0, fixture.IntState.Value); + Assert.Equal(0, fixture.IntStateResult); var changer = Observable.Range(0, 3); var disposable = changer .ObserveOn(ImmediateScheduler.Instance) .SubscribeOn(ImmediateScheduler.Instance) - .Subscribe(r => fixture.IntState.Change(s => s.Value = r)); + .Subscribe(r => fixture.IntCommand.Change(() => fixture.IntStateResult = r)); while (!completed) ; - Assert.Equal(8, phaseChangeCount); + Assert.Equal(4, phaseChangeCount); } [Fact] @@ -255,14 +280,14 @@ IDisposable subscribeTest() return fixture.Subscribe(sc => { - if (!done && sc.StateID == fixture.CRUDListState.ID) + if (!done && sc.StateID == fixture.CRUDListCommand.ID) { stateChangeCount++; } - _output.WriteLine($"Done {fixture.CRUDListState.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.CRUDListState.Phase}, ID {sc.StateID}, VPID {fixture.CRUDListState.ID}"); + _output.WriteLine($"Done {fixture.CRUDListCommand.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.CRUDListCommand.Phase}, ID {sc.StateID}, VPID {fixture.CRUDListCommand.ID}"); - if (fixture.CRUDListState.Done()) + if (fixture.CRUDListCommand.Done()) { done = true; } @@ -270,63 +295,63 @@ IDisposable subscribeTest() } var disposable = subscribeTest(); - await fixture.CRUDListState.ChangeAsync(ServiceFixture.ChangeCrudListAsync((ServiceFixture.CMD_CRUD.CLEAR, null))); + await fixture.CRUDListCommand.ChangeAsync(fixture.ChangeCrudListAsync((ServiceFixture.CMD_CRUD.CLEAR, null))); while (!done) ; disposable.Dispose(); Assert.Equal(2, stateChangeCount); disposable.Dispose(); - Assert.Empty(fixture.CRUDListState.Value); + Assert.Empty(fixture.CRUDList); disposable = subscribeTest(); - await fixture.CRUDListState.ChangeAsync(ServiceFixture.ChangeCrudListAsync((ServiceFixture.CMD_CRUD.ADD, new CRUDTest("Item1", Guid.NewGuid())))); + await fixture.CRUDListCommand.ChangeAsync(fixture.ChangeCrudListAsync((ServiceFixture.CMD_CRUD.ADD, new CRUDTest("Item1", Guid.NewGuid())))); while (!done) ; disposable.Dispose(); - Assert.Single(fixture.CRUDListState.Value); - Assert.Equal("Item1", fixture.CRUDListState.Value.Last().Item); + Assert.Single(fixture.CRUDList); + Assert.Equal("Item1", fixture.CRUDList.Last().Item); Assert.Equal(2, stateChangeCount); disposable = subscribeTest(); - await fixture.CRUDListState.ChangeAsync(ServiceFixture.ChangeCrudListAsync((ServiceFixture.CMD_CRUD.ADD, new CRUDTest("Item2", Guid.NewGuid())))); + await fixture.CRUDListCommand.ChangeAsync(fixture.ChangeCrudListAsync((ServiceFixture.CMD_CRUD.ADD, new CRUDTest("Item2", Guid.NewGuid())))); while (!done) ; disposable.Dispose(); - Assert.Equal(2, fixture.CRUDListState.Value.Count); - Assert.Equal("Item2", fixture.CRUDListState.Value.Last().Item); + Assert.Equal(2, fixture.CRUDList.Count); + Assert.Equal("Item2", fixture.CRUDList.Last().Item); Assert.Equal(2, stateChangeCount); - var lastItem = fixture.CRUDListState.Value.Last(); + var lastItem = fixture.CRUDList.Last(); var updateItem = lastItem with { Item = "Item3" }; disposable = subscribeTest(); - await fixture.CRUDListState.ChangeAsync(ServiceFixture.ChangeCrudListAsync((ServiceFixture.CMD_CRUD.UPDATE, updateItem))); + await fixture.CRUDListCommand.ChangeAsync(fixture.ChangeCrudListAsync((ServiceFixture.CMD_CRUD.UPDATE, updateItem))); while (!done) ; disposable.Dispose(); - Assert.Equal(2, fixture.CRUDListState.Value.Count); - Assert.Equal("Item3", fixture.CRUDListState.Value.Last().Item); + Assert.Equal(2, fixture.CRUDList.Count); + Assert.Equal("Item3", fixture.CRUDList.Last().Item); Assert.Equal(2, stateChangeCount); disposable = subscribeTest(); - await fixture.CRUDListState.ChangeAsync(ServiceFixture.ChangeCrudListAsync((ServiceFixture.CMD_CRUD.DELETE, updateItem))); + await fixture.CRUDListCommand.ChangeAsync(fixture.ChangeCrudListAsync((ServiceFixture.CMD_CRUD.DELETE, updateItem))); while (!done) ; disposable.Dispose(); - Assert.Single(fixture.CRUDListState.Value); + Assert.Single(fixture.CRUDList); Assert.Equal(2, stateChangeCount); disposable = subscribeTest(); - await fixture.CRUDListState.ChangeAsync(ServiceFixture.ChangeCrudListAsync((ServiceFixture.CMD_CRUD.CLEAR, null))); + await fixture.CRUDListCommand.ChangeAsync(fixture.ChangeCrudListAsync((ServiceFixture.CMD_CRUD.CLEAR, null))); while (!done) ; disposable.Dispose(); - Assert.Empty(fixture.CRUDListState.Value); + Assert.Empty(fixture.CRUDList); Assert.Equal(2, stateChangeCount); } [Fact] - public async Task TestCRUDDict() + public async Task TestCRUDDictCommand() { ServiceFixture fixture = new(); var stateChangeCount = 0; @@ -339,14 +364,14 @@ IDisposable subscribeTest() return fixture.Subscribe(sc => { - if (!done && sc.StateID == fixture.CRUDDictState.ID) + if (!done && sc.StateID == fixture.CRUDDictCommand.ID) { stateChangeCount++; } - _output.WriteLine($"Done {fixture.CRUDDictState.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.CRUDDictState.Phase}, ID {sc.StateID}, VPID {fixture.CRUDDictState.ID}"); + _output.WriteLine($"Done {fixture.CRUDDictCommand.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.CRUDDictCommand.Phase}, ID {sc.StateID}, VPID {fixture.CRUDDictCommand.ID}"); - if (fixture.CRUDDictState.Done()) + if (fixture.CRUDDictCommand.Done()) { done = true; } @@ -354,60 +379,60 @@ IDisposable subscribeTest() } var disposable = subscribeTest(); - await fixture.CRUDDictState.ChangeAsync(ServiceFixture.ChangeCrudDictAsync((ServiceFixture.CMD_CRUD.CLEAR, default, null))); + await fixture.CRUDDictCommand.ChangeAsync(fixture.ChangeCrudDictAsync((ServiceFixture.CMD_CRUD.CLEAR, default, null))); while (!done) ; disposable.Dispose(); Assert.Equal(2, stateChangeCount); disposable.Dispose(); - Assert.Empty(fixture.CRUDDictState.Value); + Assert.Empty(fixture.CRUDDict); disposable = subscribeTest(); var addGuid1 = Guid.NewGuid(); - await fixture.CRUDDictState.ChangeAsync(ServiceFixture.ChangeCrudDictAsync((ServiceFixture.CMD_CRUD.ADD, addGuid1, new CRUDTest("Item1", addGuid1)))); + await fixture.CRUDDictCommand.ChangeAsync(fixture.ChangeCrudDictAsync((ServiceFixture.CMD_CRUD.ADD, addGuid1, new CRUDTest("Item1", addGuid1)))); while (!done) ; disposable.Dispose(); - Assert.Single(fixture.CRUDDictState.Value); - Assert.Equal("Item1", fixture.CRUDDictState.Value.Last().Value.Item); + Assert.Single(fixture.CRUDDict); + Assert.Equal("Item1", fixture.CRUDDict.Last().Value.Item); Assert.Equal(2, stateChangeCount); disposable = subscribeTest(); var addGuid2 = Guid.NewGuid(); - await fixture.CRUDDictState.ChangeAsync(ServiceFixture.ChangeCrudDictAsync((ServiceFixture.CMD_CRUD.ADD, addGuid2, new CRUDTest("Item2", addGuid2)))); + await fixture.CRUDDictCommand.ChangeAsync(fixture.ChangeCrudDictAsync((ServiceFixture.CMD_CRUD.ADD, addGuid2, new CRUDTest("Item2", addGuid2)))); while (!done) ; disposable.Dispose(); - Assert.Equal(2, fixture.CRUDDictState.Value.Count()); - Assert.Equal("Item2", fixture.CRUDDictState.Value.Last().Value.Item); + Assert.Equal(2, fixture.CRUDDict.Count()); + Assert.Equal("Item2", fixture.CRUDDict.Last().Value.Item); Assert.Equal(2, stateChangeCount); - var lastItem = fixture.CRUDDictState.Value.Last().Value; + var lastItem = fixture.CRUDDict.Last().Value; var updateItem = lastItem with { Item = "Item3" }; disposable = subscribeTest(); - await fixture.CRUDDictState.ChangeAsync(ServiceFixture.ChangeCrudDictAsync((ServiceFixture.CMD_CRUD.UPDATE, updateItem.Id, updateItem))); + await fixture.CRUDDictCommand.ChangeAsync(fixture.ChangeCrudDictAsync((ServiceFixture.CMD_CRUD.UPDATE, updateItem.Id, updateItem))); while (!done) ; disposable.Dispose(); - Assert.Equal(2, fixture.CRUDDictState.Value.Count()); - Assert.Equal("Item3", fixture.CRUDDictState.Value.Last().Value.Item); + Assert.Equal(2, fixture.CRUDDict.Count()); + Assert.Equal("Item3", fixture.CRUDDict.Last().Value.Item); Assert.Equal(2, stateChangeCount); disposable = subscribeTest(); - await fixture.CRUDDictState.ChangeAsync(ServiceFixture.ChangeCrudDictAsync((ServiceFixture.CMD_CRUD.DELETE, updateItem.Id, null))); + await fixture.CRUDDictCommand.ChangeAsync(fixture.ChangeCrudDictAsync((ServiceFixture.CMD_CRUD.DELETE, updateItem.Id, null))); while (!done) ; disposable.Dispose(); - Assert.Single(fixture.CRUDDictState.Value); + Assert.Single(fixture.CRUDDict); Assert.Equal(2, stateChangeCount); disposable = subscribeTest(); - await fixture.CRUDDictState.ChangeAsync(ServiceFixture.ChangeCrudDictAsync((ServiceFixture.CMD_CRUD.CLEAR, default, null))); + await fixture.CRUDDictCommand.ChangeAsync(fixture.ChangeCrudDictAsync((ServiceFixture.CMD_CRUD.CLEAR, default, null))); while (!done) ; disposable.Dispose(); - Assert.Empty(fixture.CRUDDictState.Value); + Assert.Empty(fixture.CRUDDict); Assert.Equal(2, stateChangeCount); } @@ -433,13 +458,13 @@ public void TestStateGroup() } }); - fixture.EnumStateGroup.Change(s => s.Value = TestEnum.THREE); + fixture.EnumStateGroup.Change(() => fixture.EnumStateGroup.Value = TestEnum.THREE); while (!done) ; Assert.Equal(TestEnum.THREE, fixture.EnumStateGroup.Value); Assert.True(fixture.EnumStateGroup.ItemDisabled(1)); - Assert.Equal(2, stateChangeCount); + Assert.Equal(1, stateChangeCount); } [Fact] @@ -464,13 +489,13 @@ public void TestStateGroupSync() } }); - fixture.EnumStateGroup.Change(s => s.Value = TestEnum.THREE); + fixture.EnumStateGroup.Change(() => fixture.EnumStateGroup.Value = TestEnum.THREE); while (!done) ; Assert.Equal(TestEnum.THREE, fixture.EnumStateGroup.Value); Assert.True(fixture.EnumStateGroup.ItemDisabled(1)); - Assert.Equal(2, stateChangeCount); + Assert.Equal(1, stateChangeCount); } [Fact] @@ -482,24 +507,28 @@ public async Task TestStateGroupAsync() fixture.Subscribe(sc => { - if (sc.StateID == fixture.EnumStateGroupLR.ID) + if (sc.StateID == fixture.EnumStateGroupAsync.ID) { stateChangeCount++; } - _output.WriteLine($"Done {fixture.EnumStateGroupLR.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.EnumStateGroupLR.Phase}, ID {sc.StateID}, VPID {fixture.EnumStateGroupLR.ID}"); + _output.WriteLine($"Done {fixture.EnumStateGroupAsync.Done()}, CC {stateChangeCount} Reason {sc.Reason}, Phase {fixture.EnumStateGroupAsync.Phase}, ID {sc.StateID}, VPID {fixture.EnumStateGroupAsync.ID}"); - if (fixture.EnumStateGroupLR.Done()) + if (fixture.EnumStateGroupAsync.Done()) { done = true; } }); - await fixture.EnumStateGroupLR.ChangeAsync(async static (s, ct) => { await Task.Delay(1000, ct); s.Value = TestEnum.THREE; }); + await fixture.EnumStateGroupAsync.ChangeAsync(async ct => + { + await Task.Delay(1000, ct); fixture.EnumStateGroupAsync.Value = TestEnum.THREE; + }); + while (!done) ; - Assert.Equal(TestEnum.THREE, fixture.EnumStateGroupLR.Value); + Assert.Equal(TestEnum.THREE, fixture.EnumStateGroupAsync.Value); - Assert.True(fixture.EnumStateGroupLR.ItemDisabled(1)); + Assert.True(fixture.EnumStateGroupAsync.ItemDisabled(1)); Assert.Equal(2, stateChangeCount); } diff --git a/RxMudBlazorLight/ButtonBase/ButtonBaseRx.cs b/RxMudBlazorLight/ButtonBase/ButtonBaseRx.cs index 23f4cfd..73656b0 100644 --- a/RxMudBlazorLight/ButtonBase/ButtonBaseRx.cs +++ b/RxMudBlazorLight/ButtonBase/ButtonBaseRx.cs @@ -5,7 +5,7 @@ namespace RxMudBlazorLight.ButtonBase { - internal class ButtonBaseRx + internal class ButtonBaseRx { public bool Disabled { get; protected set; } = true; public Color Color { get; protected set; } @@ -107,7 +107,7 @@ public RenderFragment RenderProgress() => builder => } }; - public (string? StartIcon, string? EndIcon, string? Label) GetFabParameters(IStateBase stateBase, string? startIcon, string? endIcon, string? label, MBIconVariant? iconVariant, bool canCancel) + public (string? StartIcon, string? EndIcon, string? Label) GetFabParameters(IStateCommandBase stateCommandBase, string? startIcon, string? endIcon, string? label, MBIconVariant? iconVariant, bool canCancel) { if (_iconForState is IconForState.None) { @@ -128,7 +128,7 @@ public RenderFragment RenderProgress() => builder => _buttonLabel = label; } - if (!stateBase.Changing() || stateBase.ChangeCallerID != _id) + if (!stateCommandBase.Changing() || stateCommandBase.ChangeCallerID != _id) { if (_iconForState is IconForState.Start) { @@ -170,9 +170,9 @@ public RenderFragment RenderProgress() => builder => return (startIcon, endIcon, label); } - public string GetIconButtonParameters(IStateBase state, string icon, MBIconVariant? iconVariant) + public string GetIconButtonParameters(IStateCommandBase stateCommand, string icon, MBIconVariant? iconVariant) { - if (!state.Changing() || state.ChangeCallerID != _id) + if (!stateCommand.Changing() || stateCommand.ChangeCallerID != _id) { if (_iconForState is IconForState.None) { @@ -197,9 +197,9 @@ public string GetIconButtonParameters(IStateBase state, string icon, MBIconVa return icon; } - public string GetBadgeIcon(IStateBase state, MBIconVariant? iconVariant, bool canCancel) + public string GetBadgeIcon(IStateCommandBase stateCommand, MBIconVariant? iconVariant, bool canCancel) { - if (canCancel && state.Changing() && state.ChangeCallerID == _id) + if (canCancel && stateCommand.Changing() && stateCommand.ChangeCallerID == _id) { return iconVariant.GetCancelIcon(); } @@ -223,8 +223,8 @@ protected void VerifyButtonParameters() } } - protected void VerifyButtonParametersAsync(Func, Task>? changeStateAsync, - Func, CancellationToken, Task>? changeStateAsyncCancel) + protected void VerifyButtonParametersAsync(Func? changeStateAsync, + Func? changeStateAsyncCancel) { if (changeStateAsync is null && changeStateAsyncCancel is null) { diff --git a/RxMudBlazorLight/ButtonBase/ButtonRx.cs b/RxMudBlazorLight/ButtonBase/ButtonRx.cs index 389468f..466ee18 100644 --- a/RxMudBlazorLight/ButtonBase/ButtonRx.cs +++ b/RxMudBlazorLight/ButtonBase/ButtonRx.cs @@ -6,7 +6,7 @@ namespace RxMudBlazorLight.ButtonBase { - internal class ButtonRx : ButtonBaseRx + internal class ButtonRx : ButtonBaseRx { public EventCallback? OnClick { get; private set; } public EventCallback? OnTouch { get; private set; } @@ -20,42 +20,42 @@ private ButtonRx(MBButtonType type, Func>? confirmExecutionAsync, Col _confirmExecutionAsync = confirmExecutionAsync; } - public static ButtonRx Create(MBButtonType type, Func>? confirmExecutionAsync, Color buttonColor, + public static ButtonRx Create(MBButtonType type, Func>? confirmExecutionAsync, Color buttonColor, RenderFragment? buttonChildContent = null, bool hasProgress = false, string? cancelText = null, Color? cancelColor = null) { - return new ButtonRx(type, confirmExecutionAsync, buttonColor, buttonChildContent, hasProgress, cancelText, cancelColor); + return new ButtonRx(type, confirmExecutionAsync, buttonColor, buttonChildContent, hasProgress, cancelText, cancelColor); } [MemberNotNull(nameof(OnClick))] [MemberNotNull(nameof(OnTouch))] - public void SetParameter(IState state, Action> changeState, Func, bool>? canChange) + public void SetParameter(IStateCommand stateCommand, Action changeState, Func? canChange) { VerifyButtonParameters(); Color = _buttonColor; if (_buttonType is not MBButtonType.FAB) { - ChildContent = state.Changing() && _hasProgress ? RenderProgress() : _buttonChildContent; + ChildContent = stateCommand.Changing() && _hasProgress ? RenderProgress() : _buttonChildContent; } - OnClick = EventCallback.Factory.Create(this, () => ExecuteState(state, changeState)); - OnTouch = EventCallback.Factory.Create(this, () => ExecuteState(state, changeState)); + OnClick = EventCallback.Factory.Create(this, () => ExecuteState(stateCommand, changeState)); + OnTouch = EventCallback.Factory.Create(this, () => ExecuteState(stateCommand, changeState)); - Disabled = canChange is not null && !state.CanChange(canChange); + Disabled = canChange is not null && !canChange(); } [MemberNotNull(nameof(OnClick))] [MemberNotNull(nameof(OnTouch))] - public void SetParameter(IStateAsync state, - Func, Task>? changeStateAsync, - Func, CancellationToken, Task>? changeStateAsyncCancel, - Func, bool>? canChange, bool deferredNotification) + public void SetParameter(IStateCommandAsync stateCommand, + Func? changeStateAsync, + Func? changeStateAsyncCancel, + Func? canChange, bool deferredNotification) { VerifyButtonParametersAsync(changeStateAsync, changeStateAsyncCancel); - if (state.Changing()) + if (stateCommand.Changing()) { - if (state.ChangeCallerID == _id) + if (stateCommand.ChangeCallerID == _id) { if (changeStateAsyncCancel is not null) { @@ -66,8 +66,8 @@ public void SetParameter(IStateAsync state, ChildContent = RenderCancel(); } - OnClick = EventCallback.Factory.Create(this, state.Cancel); - OnTouch = EventCallback.Factory.Create(this, state.Cancel); + OnClick = EventCallback.Factory.Create(this, stateCommand.Cancel); + OnTouch = EventCallback.Factory.Create(this, stateCommand.Cancel); if (_buttonType is MBButtonType.ICON) { @@ -103,38 +103,38 @@ public void SetParameter(IStateAsync state, Color = _buttonColor; OnClick = EventCallback.Factory.Create(this, () => - ExecuteStateAsync(state, changeStateAsync, changeStateAsyncCancel, deferredNotification)); + ExecuteStateAsync(stateCommand, changeStateAsync, changeStateAsyncCancel, deferredNotification)); OnTouch = EventCallback.Factory.Create(this, () => - ExecuteStateAsync(state, changeStateAsync, changeStateAsyncCancel, deferredNotification)); + ExecuteStateAsync(stateCommand, changeStateAsync, changeStateAsyncCancel, deferredNotification)); - Disabled = canChange is not null && !state.CanChange(canChange); + Disabled = canChange is not null && !canChange(); } OnClick ??= EventCallback.Factory.Create(this, _ => { }); OnTouch ??= EventCallback.Factory.Create(this, _ => { }); } - private async Task ExecuteState(IState state, Action> changeState) + private async Task ExecuteState(IStateCommand stateCommand, Action changeState) { if (_confirmExecutionAsync is null || await _confirmExecutionAsync()) { - state.Change(changeState); + stateCommand.Change(changeState); } } - private async Task ExecuteStateAsync(IStateAsync state, Func, Task>? changeStateAsync, - Func, CancellationToken, Task>? changeStateAsyncCancel, bool deferredNotification) + private async Task ExecuteStateAsync(IStateCommandAsync stateCommand, Func? changeStateAsync, + Func? changeStateAsyncCancel, bool deferredNotification) { if (_confirmExecutionAsync is null || await _confirmExecutionAsync()) { if (changeStateAsync is not null) { - await state.ChangeAsync(changeStateAsync, _hasProgress, _id); + await stateCommand.ChangeAsync(changeStateAsync, _hasProgress, _id); } if (changeStateAsyncCancel is not null) { - await state.ChangeAsync(changeStateAsyncCancel, !deferredNotification, _id); + await stateCommand.ChangeAsync(changeStateAsyncCancel, !deferredNotification, _id); } } } diff --git a/RxMudBlazorLight/ButtonBase/MudButtonAsyncBaseRx.cs b/RxMudBlazorLight/ButtonBase/MudButtonAsyncBaseRx.cs index 4569e0f..8ed249d 100644 --- a/RxMudBlazorLight/ButtonBase/MudButtonAsyncBaseRx.cs +++ b/RxMudBlazorLight/ButtonBase/MudButtonAsyncBaseRx.cs @@ -5,19 +5,19 @@ namespace RxMudBlazorLight.ButtonBase { - public class MudButtonAsyncBaseRx : MudButton + public class MudButtonAsyncBaseRx : MudButton { [Parameter, EditorRequired] - public required IStateAsync State { get; init; } + public required IStateCommandAsync StateCommand { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public Func>? ConfirmExecutionAsync { get; init; } - protected Func, Task>? _changeStateAsync; - protected Func, CancellationToken, Task>? _changeStateAsyncCancel; + protected Func? _changeStateAsync; + protected Func? _changeStateAsyncCancel; protected string? _cancelText; protected Color? _cancelColor; protected bool _hasProgress = false; @@ -25,11 +25,11 @@ public class MudButtonAsyncBaseRx : MudButton protected RenderFragment RenderBase() => base.BuildRenderTree; - private ButtonRx? _buttonRx; + private ButtonRx? _buttonRx; protected override void OnInitialized() { - _buttonRx = ButtonRx.Create(MBButtonType.DEFAULT, ConfirmExecutionAsync, Color, ChildContent, _hasProgress, _cancelText, _cancelColor); + _buttonRx = ButtonRx.Create(MBButtonType.DEFAULT, ConfirmExecutionAsync, Color, ChildContent, _hasProgress, _cancelText, _cancelColor); base.OnInitialized(); } @@ -37,7 +37,7 @@ protected override void OnInitialized() protected override void OnParametersSet() { ArgumentNullException.ThrowIfNull(_buttonRx); - _buttonRx.SetParameter(State, _changeStateAsync, _changeStateAsyncCancel, CanChange, _deferredNotification); + _buttonRx.SetParameter(StateCommand, _changeStateAsync, _changeStateAsyncCancel, CanChange, _deferredNotification); ChildContent = _buttonRx.ChildContent; Color = _buttonRx.Color; diff --git a/RxMudBlazorLight/ButtonBase/MudFabAsyncBaseRx.cs b/RxMudBlazorLight/ButtonBase/MudFabAsyncBaseRx.cs index 2fe5118..44d646a 100644 --- a/RxMudBlazorLight/ButtonBase/MudFabAsyncBaseRx.cs +++ b/RxMudBlazorLight/ButtonBase/MudFabAsyncBaseRx.cs @@ -5,13 +5,13 @@ namespace RxMudBlazorLight.ButtonBase { - public class MudFabAsyncBaseRx : MudFab + public class MudFabAsyncBaseRx : MudFab { [Parameter, EditorRequired] - public required IStateAsync State { get; init; } + public required IStateCommandAsync StateCommand { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public Func>? ConfirmExecutionAsync { get; init; } @@ -19,19 +19,19 @@ public class MudFabAsyncBaseRx : MudFab [Parameter] public MBIconVariant? IconVariant { get; set; } - protected Func, Task>? _changeStateAsync; - protected Func, CancellationToken, Task>? _changeStateAsyncCancel; + protected Func? _changeStateAsync; + protected Func? _changeStateAsyncCancel; protected string? _cancelText; protected Color? _cancelColor; protected bool _hasProgress = false; protected bool _deferredNotification = false; protected RenderFragment RenderBase() => base.BuildRenderTree; - internal ButtonRx? _buttonRx; + internal ButtonRx? _buttonRx; protected override void OnInitialized() { - _buttonRx = ButtonRx.Create(MBButtonType.FAB, ConfirmExecutionAsync, Color, null, _hasProgress, _cancelText, _cancelColor); + _buttonRx = ButtonRx.Create(MBButtonType.FAB, ConfirmExecutionAsync, Color, null, _hasProgress, _cancelText, _cancelColor); base.OnInitialized(); } @@ -39,9 +39,9 @@ protected override void OnInitialized() protected override void OnParametersSet() { ArgumentNullException.ThrowIfNull(_buttonRx); - _buttonRx.SetParameter(State, _changeStateAsync, _changeStateAsyncCancel, CanChange, _deferredNotification); + _buttonRx.SetParameter(StateCommand, _changeStateAsync, _changeStateAsyncCancel, CanChange, _deferredNotification); - var parameters = _buttonRx.GetFabParameters(State, StartIcon, EndIcon, Label, IconVariant, _changeStateAsyncCancel is not null); + var parameters = _buttonRx.GetFabParameters(StateCommand, StartIcon, EndIcon, Label, IconVariant, _changeStateAsyncCancel is not null); StartIcon = parameters.StartIcon; EndIcon = parameters.EndIcon; diff --git a/RxMudBlazorLight/ButtonBase/MudIconButtonAsyncBaseRx.cs b/RxMudBlazorLight/ButtonBase/MudIconButtonAsyncBaseRx.cs index 8bb83e3..89463dc 100644 --- a/RxMudBlazorLight/ButtonBase/MudIconButtonAsyncBaseRx.cs +++ b/RxMudBlazorLight/ButtonBase/MudIconButtonAsyncBaseRx.cs @@ -5,13 +5,13 @@ namespace RxMudBlazorLight.ButtonBase { - public class MudIconButtonAsyncBaseRx : MudIconButton + public class MudIconButtonAsyncBaseRx : MudIconButton { [Parameter, EditorRequired] - public required IStateAsync State { get; init; } + public required IStateCommandAsync StateCommand { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public Func>? ConfirmExecutionAsync { get; init; } @@ -19,19 +19,19 @@ public class MudIconButtonAsyncBaseRx : MudIconButton [Parameter] public MBIconVariant? IconVariant { get; set; } - protected Func, Task>? _changeStateAsync; - protected Func, CancellationToken, Task>? _changeStateAsyncCancel; + protected Func? _changeStateAsync; + protected Func? _changeStateAsyncCancel; protected string? _cancelText; protected Color? _cancelColor; protected bool _hasProgress = false; protected bool _deferredNotification = false; protected RenderFragment RenderBase() => base.BuildRenderTree; - internal ButtonRx? _buttonRx; + internal ButtonRx? _buttonRx; protected override void OnInitialized() { - _buttonRx = ButtonRx.Create(MBButtonType.ICON, ConfirmExecutionAsync, Color, null, _hasProgress, null, _cancelColor); + _buttonRx = ButtonRx.Create(MBButtonType.ICON, ConfirmExecutionAsync, Color, null, _hasProgress, null, _cancelColor); base.OnInitialized(); } @@ -40,9 +40,9 @@ protected override void OnParametersSet() { ArgumentNullException.ThrowIfNull(_buttonRx); ArgumentNullException.ThrowIfNull(Icon); - _buttonRx.SetParameter(State, _changeStateAsync, _changeStateAsyncCancel, CanChange, _deferredNotification); + _buttonRx.SetParameter(StateCommand, _changeStateAsync, _changeStateAsyncCancel, CanChange, _deferredNotification); - Icon = _buttonRx.GetIconButtonParameters(State, Icon, IconVariant); + Icon = _buttonRx.GetIconButtonParameters(StateCommand, Icon, IconVariant); Color = _buttonRx.Color; OnClick = (EventCallback)_buttonRx.OnClick; Disabled = _buttonRx.Disabled; diff --git a/RxMudBlazorLight/Buttons/MudButtonAsyncCancelRx.razor b/RxMudBlazorLight/Buttons/MudButtonAsyncCancelRx.razor index 08bd75d..d197674 100644 --- a/RxMudBlazorLight/Buttons/MudButtonAsyncCancelRx.razor +++ b/RxMudBlazorLight/Buttons/MudButtonAsyncCancelRx.razor @@ -1,11 +1,10 @@ -@inherits MudButtonAsyncBaseRx -@typeparam T +@inherits MudButtonAsyncBaseRx @RenderBase() @code { [Parameter, EditorRequired] - public required Func, CancellationToken, Task> ChangeStateAsync { get; init; } + public required Func ChangeStateAsync { get; init; } [Parameter, EditorRequired] public required string CancelText { get; init; } diff --git a/RxMudBlazorLight/Buttons/MudButtonAsyncRx.razor b/RxMudBlazorLight/Buttons/MudButtonAsyncRx.razor index 601b145..6a96b96 100644 --- a/RxMudBlazorLight/Buttons/MudButtonAsyncRx.razor +++ b/RxMudBlazorLight/Buttons/MudButtonAsyncRx.razor @@ -1,11 +1,10 @@ -@inherits MudButtonAsyncBaseRx -@typeparam T +@inherits MudButtonAsyncBaseRx @RenderBase() @code { [Parameter, EditorRequired] - public required Func, Task> ChangeStateAsync { get; init; } + public required Func ChangeStateAsync { get; init; } [Parameter] public bool HasProgress { get; set; } = false; diff --git a/RxMudBlazorLight/Buttons/MudButtonRx.razor b/RxMudBlazorLight/Buttons/MudButtonRx.razor index 942f0c8..abea83a 100644 --- a/RxMudBlazorLight/Buttons/MudButtonRx.razor +++ b/RxMudBlazorLight/Buttons/MudButtonRx.razor @@ -1,28 +1,27 @@ @inherits MudButton -@typeparam T @RenderBase() @code { [Parameter, EditorRequired] - public required IState State { get; init; } + public required IStateCommand StateCommand { get; init; } [Parameter, EditorRequired] - public required Action> ChangeState { get; init; } + public required Action ChangeState { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public Func>? ConfirmExecutionAsync { get; init; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); - private ButtonRx? _buttonRx; + private ButtonRx? _buttonRx; private Color _buttonColor; protected override void OnInitialized() { - _buttonRx = ButtonRx.Create(MBButtonType.DEFAULT, ConfirmExecutionAsync, Color, ChildContent, false); + _buttonRx = ButtonRx.Create(MBButtonType.DEFAULT, ConfirmExecutionAsync, Color, ChildContent, false); _buttonColor = Color; base.OnInitialized(); @@ -31,7 +30,7 @@ protected override void OnParametersSet() { ArgumentNullException.ThrowIfNull(_buttonRx); - _buttonRx.SetParameter(State, ChangeState, CanChange); + _buttonRx.SetParameter(StateCommand, ChangeState, CanChange); ChildContent = _buttonRx.ChildContent; Color = _buttonRx.Color; diff --git a/RxMudBlazorLight/Dialogs/DialogAsyncRx.razor b/RxMudBlazorLight/Dialogs/DialogAsyncRx.razor index 3262092..4a47409 100644 --- a/RxMudBlazorLight/Dialogs/DialogAsyncRx.razor +++ b/RxMudBlazorLight/Dialogs/DialogAsyncRx.razor @@ -1,6 +1,5 @@ -@typeparam TService -@typeparam TParam -@inherits RxBLServiceSubscriber +@typeparam T where T : IRxBLService +@inherits RxBLServiceSubscriber @@ -12,14 +11,14 @@ @if (ChangeStateAsyncCancel is not null) { + StateCommand=@StateCommand ChangeStateAsync=@ChangeStateAsyncCancel CanChange=@CanChange CancelColor=@CancelColor CancelText=@CancelText HasProgress=@HasProgress> @ConfirmButton } else { + StateCommand=@StateCommand ChangeStateAsync=@(ChangeStateAsync!) CanChange=@CanChange HasProgress=@HasProgress> @ConfirmButton } diff --git a/RxMudBlazorLight/Dialogs/DialogAsyncRx.razor.cs b/RxMudBlazorLight/Dialogs/DialogAsyncRx.razor.cs index 7175e5c..151b685 100644 --- a/RxMudBlazorLight/Dialogs/DialogAsyncRx.razor.cs +++ b/RxMudBlazorLight/Dialogs/DialogAsyncRx.razor.cs @@ -5,7 +5,7 @@ namespace RxMudBlazorLight.Dialogs { - public partial class DialogAsyncRx : RxBLServiceSubscriber where TService : IRxBLService + public partial class DialogAsyncRx { [CascadingParameter] MudDialogInstance? MudDialog { get; set; } @@ -23,16 +23,16 @@ public partial class DialogAsyncRx : RxBLServiceSubscriber State { get; init; } + public required IStateCommandAsync StateCommand { get; init; } [Parameter] - public Func, Task>? ChangeStateAsync { get; init; } + public Func? ChangeStateAsync { get; init; } [Parameter] - public Func, CancellationToken, Task>? ChangeStateAsyncCancel { get; init; } + public Func? ChangeStateAsyncCancel { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public string? CancelText { get; set; } @@ -43,18 +43,18 @@ public partial class DialogAsyncRx : RxBLServiceSubscriber? _buttonRef; + private MudButtonAsyncBaseRx? _buttonRef; private IDisposable? _buttonDisposable; private bool _canceled = false; public static async Task Show(IDialogService dialogService, - IStateAsync state, Func, Task>? changeStateAsync, string title, + IStateCommandAsync stateCommand, Func? changeStateAsync, string title, string message, string confirmButton, string cancelButton, bool successOnConfirm, bool hasProgress = true, - Func, bool>? canChange = null) + Func? canChange = null) { var parameters = new DialogParameters { - ["State"] = state, + ["StateCommand"] = stateCommand, ["ChangeStateAsync"] = changeStateAsync, ["CanChange"] = canChange, ["HasProgress"] = hasProgress, @@ -64,7 +64,7 @@ public static async Task Show(IDialogService dialogService, ["SuccessOnConfirm"] = successOnConfirm }; - var dialog = dialogService.Show>(title, parameters); + var dialog = dialogService.Show>(title, parameters); var res = await dialog.Result; @@ -77,13 +77,13 @@ public static async Task Show(IDialogService dialogService, } public static async Task Show(IDialogService dialogService, - IStateAsync state, Func, CancellationToken, Task>? changeStateAsyncCancel, string title, + IStateCommandAsync stateCommand, Func? changeStateAsyncCancel, string title, string message, string confirmButton, string cancelButton, bool successOnConfirm, string cancelText, Color? cancelColor = null, bool hasProgress = true, - Func, bool>? canChange = null) + Func? canChange = null) { var parameters = new DialogParameters { - ["State"] = state, + ["StateCommand"] = stateCommand, ["ChangeStateAsyncCancel"] = changeStateAsyncCancel, ["CanChange"] = canChange, ["CancelColor"] = cancelColor, @@ -95,7 +95,7 @@ public static async Task Show(IDialogService dialogService, ["SuccessOnConfirm"] = successOnConfirm }; - var dialog = dialogService.Show>(title, parameters); + var dialog = dialogService.Show>(title, parameters); var res = await dialog.Result; @@ -109,19 +109,19 @@ public static async Task Show(IDialogService dialogService, private bool CanNotCancel() { - if (_buttonRef?.State is null) + if (_buttonRef?.StateCommand is null) { return false; } - return _buttonRef.State.Changing(); + return _buttonRef.StateCommand.Changing(); } private void Cancel() { - ArgumentNullException.ThrowIfNull(_buttonRef?.State); + ArgumentNullException.ThrowIfNull(_buttonRef?.StateCommand); - if (!_buttonRef.State.Changing()) + if (!_buttonRef.StateCommand.Changing()) { MudDialog?.Cancel(); } @@ -129,11 +129,11 @@ private void Cancel() protected override void OnServiceStateHasChanged(ServiceChangeReason cr) { - if (cr.Reason is ChangeReason.STATE && _buttonRef is not null && cr.StateID == _buttonRef.State.ID) + if (cr.Reason is ChangeReason.STATE && _buttonRef is not null && cr.StateID == _buttonRef.StateCommand.ID) { - if (_buttonRef.State.Done()) + if (_buttonRef.StateCommand.Done()) { - _canceled = State.Canceled(); + _canceled = StateCommand.Canceled(); if (!_canceled) { _buttonDisposable?.Dispose(); diff --git a/RxMudBlazorLight/Dialogs/DialogRx.razor b/RxMudBlazorLight/Dialogs/DialogRx.razor index b6674a9..e3efbac 100644 --- a/RxMudBlazorLight/Dialogs/DialogRx.razor +++ b/RxMudBlazorLight/Dialogs/DialogRx.razor @@ -1,6 +1,5 @@ -@typeparam TService -@typeparam TParam -@inherits RxBLServiceSubscriber +@typeparam T where T : IRxBLService +@inherits RxBLServiceSubscriber @@ -10,7 +9,7 @@ Cancel + StateCommand=@StateCommand ChangeState=@ChangeState CanChange=@CanChange> @ConfirmButton diff --git a/RxMudBlazorLight/Dialogs/DialogRx.razor.cs b/RxMudBlazorLight/Dialogs/DialogRx.razor.cs index c9cee4f..2c1e8da 100644 --- a/RxMudBlazorLight/Dialogs/DialogRx.razor.cs +++ b/RxMudBlazorLight/Dialogs/DialogRx.razor.cs @@ -5,7 +5,7 @@ namespace RxMudBlazorLight.Dialogs { - public partial class DialogRx : RxBLServiceSubscriber where TService : IRxBLService + public partial class DialogRx { [CascadingParameter] MudDialogInstance? MudDialog { get; set; } @@ -23,32 +23,29 @@ public partial class DialogRx : RxBLServiceSubscriber State { get; init; } + public required IStateCommand StateCommand { get; init; } [Parameter] - public Func, Task>? ChangeStateAsync { get; init; } + public Action? ChangeState { get; init; } [Parameter] - public Action>? ChangeState { get; init; } - - [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public bool HasProgress { get; set; } = false; - private MudButtonRx? _buttonRef; + private MudButtonRx? _buttonRef; private IDisposable? _buttonDisposable; private bool _canceled = false; public static async Task Show(IDialogService dialogService, - IState state, Action> changeState, string title, + IStateCommand stateCommand, Action changeState, string title, string message, string confirmButton, string cancelButton, bool successOnConfirm, - Func, bool>? canChange = null) + Func? canChange = null) { var parameters = new DialogParameters { - ["State"] = state, + ["StateCommand"] = stateCommand, ["ChangeState"] = changeState, ["CanChange"] = canChange, ["Message"] = message, @@ -57,7 +54,7 @@ public static async Task Show(IDialogService dialogService, ["SuccessOnConfirm"] = successOnConfirm }; - var dialog = dialogService.Show>(title, parameters); + var dialog = dialogService.Show>(title, parameters); var res = await dialog.Result; @@ -71,19 +68,19 @@ public static async Task Show(IDialogService dialogService, private bool CanNotCancel() { - if (_buttonRef?.State is null) + if (_buttonRef?.StateCommand is null) { return false; } - return _buttonRef.State.Changing(); + return _buttonRef.StateCommand.Changing(); } private void Cancel() { - ArgumentNullException.ThrowIfNull(_buttonRef?.State); + ArgumentNullException.ThrowIfNull(_buttonRef?.StateCommand); - if (!_buttonRef.State.Changing()) + if (!_buttonRef.StateCommand.Changing()) { MudDialog?.Cancel(); } @@ -91,11 +88,11 @@ private void Cancel() protected override void OnServiceStateHasChanged(ServiceChangeReason cr) { - if (cr.Reason is ChangeReason.STATE && _buttonRef is not null && cr.StateID == _buttonRef.State.ID) + if (cr.Reason is ChangeReason.STATE && _buttonRef is not null && cr.StateID == _buttonRef.StateCommand.ID) { - if (_buttonRef.State.Done()) + if (_buttonRef.StateCommand.Done()) { - _canceled = State.Canceled(); + _canceled = StateCommand.Canceled(); if (!_canceled) { _buttonDisposable?.Dispose(); diff --git a/RxMudBlazorLight/FabButtons/MudFabAsyncCancelRx.razor b/RxMudBlazorLight/FabButtons/MudFabAsyncCancelRx.razor index a247bbe..f0e2c1e 100644 --- a/RxMudBlazorLight/FabButtons/MudFabAsyncCancelRx.razor +++ b/RxMudBlazorLight/FabButtons/MudFabAsyncCancelRx.razor @@ -1,14 +1,13 @@ -@inherits MudFabAsyncBaseRx -@typeparam T +@inherits MudFabAsyncBaseRx - + @RenderBase() @code { [Parameter, EditorRequired] - public required Func, CancellationToken, Task> ChangeStateAsync { get; init; } + public required Func ChangeStateAsync { get; init; } [Parameter] public string? CancelText { get; init; } diff --git a/RxMudBlazorLight/FabButtons/MudFabAsyncRx.razor b/RxMudBlazorLight/FabButtons/MudFabAsyncRx.razor index 83f07d1..0cbf57a 100644 --- a/RxMudBlazorLight/FabButtons/MudFabAsyncRx.razor +++ b/RxMudBlazorLight/FabButtons/MudFabAsyncRx.razor @@ -1,14 +1,13 @@ -@inherits MudFabAsyncBaseRx -@typeparam T +@inherits MudFabAsyncBaseRx - + @RenderBase() @code { [Parameter, EditorRequired] - public required Func, Task> ChangeStateAsync { get; init; } + public required Func ChangeStateAsync { get; init; } [Parameter] public bool HasProgress { get; set; } = false; diff --git a/RxMudBlazorLight/FabButtons/MudFabRx.razor b/RxMudBlazorLight/FabButtons/MudFabRx.razor index 9bf6458..7381f22 100644 --- a/RxMudBlazorLight/FabButtons/MudFabRx.razor +++ b/RxMudBlazorLight/FabButtons/MudFabRx.razor @@ -1,17 +1,16 @@ @inherits MudFab -@typeparam T @RenderBase() @code { [Parameter, EditorRequired] - public required IState State { get; init; } + public required IStateCommand StateCommand { get; init; } [Parameter, EditorRequired] - public required Action> ChangeState { get; init; } + public required Action ChangeState { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public Func>? ConfirmExecutionAsync { get; init; } @@ -20,11 +19,11 @@ public MBIconVariant? IconVariant { get; set; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); - private ButtonRx? _buttonRx; + private ButtonRx? _buttonRx; protected override void OnInitialized() { - _buttonRx = ButtonRx.Create(MBButtonType.FAB, ConfirmExecutionAsync, Color); + _buttonRx = ButtonRx.Create(MBButtonType.FAB, ConfirmExecutionAsync, Color); base.OnInitialized(); } @@ -32,9 +31,9 @@ protected override void OnParametersSet() { ArgumentNullException.ThrowIfNull(_buttonRx); - _buttonRx.SetParameter(State, ChangeState, CanChange); + _buttonRx.SetParameter(StateCommand, ChangeState, CanChange); - var parameters = _buttonRx.GetFabParameters(State, StartIcon, EndIcon, Label, IconVariant, false); + var parameters = _buttonRx.GetFabParameters(StateCommand, StartIcon, EndIcon, Label, IconVariant, false); StartIcon = parameters.StartIcon; EndIcon = parameters.EndIcon; Label = parameters.Label; diff --git a/RxMudBlazorLight/IconButtons/MudIconButtonAsyncCancelRx.razor b/RxMudBlazorLight/IconButtons/MudIconButtonAsyncCancelRx.razor index b024439..c5c8bb1 100644 --- a/RxMudBlazorLight/IconButtons/MudIconButtonAsyncCancelRx.razor +++ b/RxMudBlazorLight/IconButtons/MudIconButtonAsyncCancelRx.razor @@ -1,14 +1,13 @@ -@inherits MudIconButtonAsyncBaseRx -@typeparam T +@inherits MudIconButtonAsyncBaseRx - + @RenderBase() @code { [Parameter, EditorRequired] - public required Func, CancellationToken, Task> ChangeStateAsync { get; init; } + public required Func ChangeStateAsync { get; init; } [Parameter] public Color? CancelColor { get; set; } diff --git a/RxMudBlazorLight/IconButtons/MudIconButtonAsyncRx.razor b/RxMudBlazorLight/IconButtons/MudIconButtonAsyncRx.razor index 5218080..7501196 100644 --- a/RxMudBlazorLight/IconButtons/MudIconButtonAsyncRx.razor +++ b/RxMudBlazorLight/IconButtons/MudIconButtonAsyncRx.razor @@ -1,14 +1,13 @@ -@inherits MudIconButtonAsyncBaseRx -@typeparam T +@inherits MudIconButtonAsyncBaseRx - + @RenderBase() @code { [Parameter, EditorRequired] - public required Func, Task> ChangeStateAsync { get; init; } + public required Func ChangeStateAsync { get; init; } [Parameter] public bool HasProgress { get; set; } = false; diff --git a/RxMudBlazorLight/IconButtons/MudIconButtonRx.razor b/RxMudBlazorLight/IconButtons/MudIconButtonRx.razor index 1231eed..ce477bb 100644 --- a/RxMudBlazorLight/IconButtons/MudIconButtonRx.razor +++ b/RxMudBlazorLight/IconButtons/MudIconButtonRx.razor @@ -1,17 +1,16 @@ @inherits MudIconButton -@typeparam T @RenderBase() @code { [Parameter, EditorRequired] - public required IState State { get; init; } + public required IStateCommand StateCommand { get; init; } [Parameter, EditorRequired] - public required Action> ChangeState { get; init; } + public required Action ChangeState { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public Func>? ConfirmExecutionAsync { get; init; } @@ -20,11 +19,11 @@ public MBIconVariant? IconVariant { get; set; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); - private ButtonRx? _buttonRx; + private ButtonRx? _buttonRx; protected override void OnInitialized() { - _buttonRx = ButtonRx.Create(MBButtonType.ICON, ConfirmExecutionAsync, Color); + _buttonRx = ButtonRx.Create(MBButtonType.ICON, ConfirmExecutionAsync, Color); base.OnInitialized(); } @@ -33,9 +32,9 @@ { ArgumentNullException.ThrowIfNull(_buttonRx); ArgumentNullException.ThrowIfNull(Icon); - _buttonRx.SetParameter(State, ChangeState, CanChange); + _buttonRx.SetParameter(StateCommand, ChangeState, CanChange); - Icon = _buttonRx.GetIconButtonParameters(State, Icon, IconVariant); + Icon = _buttonRx.GetIconButtonParameters(StateCommand, Icon, IconVariant); OnClick = (EventCallback)_buttonRx.OnClick; Disabled = _buttonRx.Disabled; diff --git a/RxMudBlazorLight/Inputs/MudAutocompleteRx.razor b/RxMudBlazorLight/Inputs/MudAutocompleteRx.razor index 87daccb..b5f1aed 100644 --- a/RxMudBlazorLight/Inputs/MudAutocompleteRx.razor +++ b/RxMudBlazorLight/Inputs/MudAutocompleteRx.razor @@ -8,7 +8,7 @@ public required IState State { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); @@ -16,7 +16,7 @@ { ValueChanged = EventCallback.Factory.Create(this, v => { - State.Change(s => s.Value = v); + State.Value = v; }); base.OnInitialized(); @@ -24,12 +24,12 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !State.CanChange(CanChange)); + Disabled = (CanChange is not null && !CanChange(State.Value)); Value = State.Value; Text = Value?.ToString(); - if (Validation is Func, StateValidation> validate) + if (Validation is Func validate) { - var validation = validate(State); + var validation = validate(Value); ErrorText = validation.Message; Error = validation.Error; } diff --git a/RxMudBlazorLight/Inputs/MudCheckBoxRx.razor b/RxMudBlazorLight/Inputs/MudCheckBoxRx.razor index 98d6250..5da94e4 100644 --- a/RxMudBlazorLight/Inputs/MudCheckBoxRx.razor +++ b/RxMudBlazorLight/Inputs/MudCheckBoxRx.razor @@ -7,7 +7,7 @@ public required IState State { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); @@ -15,7 +15,7 @@ { ValueChanged = EventCallback.Factory.Create(this, v => { - State.Change(s => s.Value = v); + State.Value = v; }); base.OnInitialized(); @@ -23,11 +23,11 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !State.CanChange(CanChange)); + Disabled = (CanChange is not null && !CanChange(State.Value)); Value = State.Value; - if (Validation is Func, StateValidation> validate) + if (Validation is Func validate) { - var validation = validate(State); + var validation = validate(Value); ErrorText = validation.Message; Error = validation.Error; } diff --git a/RxMudBlazorLight/Inputs/MudDatePickerRx.razor b/RxMudBlazorLight/Inputs/MudDatePickerRx.razor index 41672a8..186a51b 100644 --- a/RxMudBlazorLight/Inputs/MudDatePickerRx.razor +++ b/RxMudBlazorLight/Inputs/MudDatePickerRx.razor @@ -7,7 +7,7 @@ public required IState State { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); @@ -17,7 +17,7 @@ { if (v.HasValue) { - State.Change(s => s.Value = v.Value); + State.Value = v.Value; } }); @@ -26,11 +26,11 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !State.CanChange(CanChange)); + Disabled = (CanChange is not null && !CanChange(State.Value)); Date = State.Value; - if (Validation is Func, StateValidation> validate) + if (Validation is Func validate) { - var validation = validate(State); + var validation = validate(Date.Value); ErrorText = validation.Message; Error = validation.Error; } diff --git a/RxMudBlazorLight/Inputs/MudNumericFieldRx.razor b/RxMudBlazorLight/Inputs/MudNumericFieldRx.razor index 1e01dbc..17e1061 100644 --- a/RxMudBlazorLight/Inputs/MudNumericFieldRx.razor +++ b/RxMudBlazorLight/Inputs/MudNumericFieldRx.razor @@ -8,7 +8,7 @@ public required IState State { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); @@ -16,7 +16,7 @@ { ValueChanged = EventCallback.Factory.Create(this, v => { - State.Change(s => s.Value = v); + State.Value = v; }); base.OnInitialized(); @@ -24,12 +24,12 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !State.CanChange(CanChange)); + Disabled = (CanChange is not null && !CanChange(State.Value)); Value = State.Value; Text = Value?.ToString(); - if (Validation is Func, StateValidation> validate) + if (Validation is Func validate) { - var validation = validate(State); + var validation = validate(Value); ErrorText = validation.Message; Error = validation.Error; } diff --git a/RxMudBlazorLight/Inputs/MudRatingRx.razor b/RxMudBlazorLight/Inputs/MudRatingRx.razor index 5b45d90..13e06ee 100644 --- a/RxMudBlazorLight/Inputs/MudRatingRx.razor +++ b/RxMudBlazorLight/Inputs/MudRatingRx.razor @@ -7,7 +7,7 @@ public required IState State { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); @@ -15,7 +15,7 @@ { SelectedValueChanged = EventCallback.Factory.Create(this, v => { - State.Change(s => s.Value = v); + State.Value = v; }); base.OnInitialized(); @@ -23,7 +23,7 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !State.CanChange(CanChange)); + Disabled = (CanChange is not null && !CanChange(State.Value)); SelectedValue = State.Value; base.OnParametersSet(); diff --git a/RxMudBlazorLight/Inputs/MudSliderRx.razor b/RxMudBlazorLight/Inputs/MudSliderRx.razor index a53dedd..df05ea1 100644 --- a/RxMudBlazorLight/Inputs/MudSliderRx.razor +++ b/RxMudBlazorLight/Inputs/MudSliderRx.razor @@ -8,7 +8,7 @@ public required IState State { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); @@ -16,7 +16,7 @@ { ValueChanged = EventCallback.Factory.Create(this, v => { - State.Change(s => s.Value = v); + State.Value = v; }); base.OnInitialized(); @@ -24,7 +24,7 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !State.CanChange(CanChange)); + Disabled = (CanChange is not null && !CanChange(State.Value)); Value = State.Value; base.OnParametersSet(); diff --git a/RxMudBlazorLight/Inputs/MudSwitchRx.razor b/RxMudBlazorLight/Inputs/MudSwitchRx.razor index 9f55927..1c86766 100644 --- a/RxMudBlazorLight/Inputs/MudSwitchRx.razor +++ b/RxMudBlazorLight/Inputs/MudSwitchRx.razor @@ -7,7 +7,7 @@ public required IState State { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); @@ -15,7 +15,7 @@ { ValueChanged = EventCallback.Factory.Create(this, v => { - State.Change(s => s.Value = v); + State.Value = v; }); base.OnInitialized(); @@ -23,11 +23,11 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !State.CanChange(CanChange)); + Disabled = (CanChange is not null && !CanChange(State.Value)); Value = State.Value; - if (Validation is Func, StateValidation> validate) + if (Validation is Func validate) { - var validation = validate(State); + var validation = validate(Value); ErrorText = validation.Message; Error = validation.Error; } diff --git a/RxMudBlazorLight/Inputs/MudTextFieldRx.razor b/RxMudBlazorLight/Inputs/MudTextFieldRx.razor index ba8a89d..4116bf7 100644 --- a/RxMudBlazorLight/Inputs/MudTextFieldRx.razor +++ b/RxMudBlazorLight/Inputs/MudTextFieldRx.razor @@ -8,7 +8,7 @@ public required IState State { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); @@ -16,7 +16,7 @@ { ValueChanged = EventCallback.Factory.Create(this, v => { - State.Change(s => s.Value = v); + State.Value = v; }); base.OnInitialized(); @@ -24,12 +24,12 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !State.CanChange(CanChange)); + Disabled = (CanChange is not null && !CanChange(State.Value)); Value = State.Value; Text = Value?.ToString(); - if (Validation is Func, StateValidation> validate) + if (Validation is Func validate) { - var validation = validate(State); + var validation = validate(Value); ErrorText = validation.Message; Error = validation.Error; } diff --git a/RxMudBlazorLight/Inputs/MudTimePickerRx.razor b/RxMudBlazorLight/Inputs/MudTimePickerRx.razor index 2d1516a..4c6a1c8 100644 --- a/RxMudBlazorLight/Inputs/MudTimePickerRx.razor +++ b/RxMudBlazorLight/Inputs/MudTimePickerRx.razor @@ -7,7 +7,7 @@ public required IState State { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); @@ -17,7 +17,7 @@ { if (v.HasValue) { - State.Change(s => s.Value = v.Value); + State.Value = v.Value; } }); @@ -26,11 +26,11 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !State.CanChange(CanChange)); + Disabled = (CanChange is not null && !CanChange(State.Value)); Time = State.Value; - if (Validation is Func, StateValidation> validate) + if (Validation is Func validate) { - var validation = validate(State); + var validation = validate(Time.Value); ErrorText = validation.Message; Error = validation.Error; } diff --git a/RxMudBlazorLight/Inputs/Radio/MudRadioGroupAsyncRx.razor b/RxMudBlazorLight/Inputs/Radio/MudRadioGroupAsyncRx.razor index 2e99f05..465d769 100644 --- a/RxMudBlazorLight/Inputs/Radio/MudRadioGroupAsyncRx.razor +++ b/RxMudBlazorLight/Inputs/Radio/MudRadioGroupAsyncRx.razor @@ -8,10 +8,10 @@ public required IStateGroupAsync StateGroupAsync { get; init; } [Parameter] - public Func, T, Task>? SelectionChangedAsync { get; init; } + public Func? SelectionChangedAsync { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public Func DenseCallback { get; set; } = _ => false; @@ -31,13 +31,13 @@ { ValueChanged = EventCallback.Factory.Create(this, async v => { - await StateGroupAsync.ChangeAsync(async s => + await StateGroupAsync.ChangeAsync(async _ => { if (SelectionChangedAsync is not null) { - await SelectionChangedAsync(s, v); + await SelectionChangedAsync(v); } - s.Value = v; + StateGroupAsync.Value = v; }); }); @@ -68,14 +68,13 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !StateGroupAsync.CanChange(CanChange)) || StateGroupAsync.Changing(); + Disabled = (CanChange is not null && !CanChange(StateGroupAsync.Value)) || StateGroupAsync.Changing(); if (StateGroupAsync.Done()) { Value = StateGroupAsync.Value; - if (Validation is not null) + if (Validation is Func validate) { - var validate = (Func, StateValidation>)Validation; - var validation = validate(StateGroupAsync); + var validation = validate(StateGroupAsync.Value); ErrorText = validation.Message; Error = validation.Error; } diff --git a/RxMudBlazorLight/Inputs/Radio/MudRadioGroupRx.razor b/RxMudBlazorLight/Inputs/Radio/MudRadioGroupRx.razor index 33f2f85..c9cf92c 100644 --- a/RxMudBlazorLight/Inputs/Radio/MudRadioGroupRx.razor +++ b/RxMudBlazorLight/Inputs/Radio/MudRadioGroupRx.razor @@ -8,10 +8,10 @@ public required IStateGroup StateGroup { get; init; } [Parameter] - public Action, T>? SelectionChanged { get; init; } + public Action? SelectionChanged { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public Func DenseCallback { get; set; } = _ => false; @@ -31,13 +31,13 @@ { ValueChanged = EventCallback.Factory.Create(this, v => { - StateGroup.Change(s => + StateGroup.Change(() => { if (SelectionChanged is not null) { - SelectionChanged(s, v); + SelectionChanged(v); } - s.Value = v; + StateGroup.Value = v; }); }); @@ -68,12 +68,11 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !StateGroup.CanChange(CanChange)); + Disabled = (CanChange is not null && !CanChange(StateGroup.Value)); Value = StateGroup.Value; - if (Validation is not null) + if (Validation is Func validate) { - var validate = (Func, StateValidation>)Validation; - var validation = validate(StateGroup); + var validation = validate(StateGroup.Value); ErrorText = validation.Message; Error = validation.Error; } diff --git a/RxMudBlazorLight/Inputs/Select/MudSelectAsyncRx.razor b/RxMudBlazorLight/Inputs/Select/MudSelectAsyncRx.razor index a8b86d6..ab40b79 100644 --- a/RxMudBlazorLight/Inputs/Select/MudSelectAsyncRx.razor +++ b/RxMudBlazorLight/Inputs/Select/MudSelectAsyncRx.razor @@ -8,10 +8,10 @@ public required IStateGroupAsync StateGroupAsync { get; init; } [Parameter] - public Func, T, Task>? SelectionChangedAsync { get; init; } + public Func? SelectionChangedAsync { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public bool HideDisabled { get; set; } = false; @@ -21,16 +21,16 @@ protected override void OnInitialized() { ValueChanged = EventCallback.Factory.Create(this, async v => - { - await StateGroupAsync.ChangeAsync(async s => - { - if (SelectionChangedAsync is not null) - { - await SelectionChangedAsync(s, v); - } - s.Value = v; - }); - }); + { + await StateGroupAsync.ChangeAsync(async _ => + { + if (SelectionChangedAsync is not null) + { + await SelectionChangedAsync(v); + } + StateGroupAsync.Value = v; + }); + }); if (ChildContent is null) { @@ -59,15 +59,14 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !StateGroupAsync.CanChange(CanChange)) || StateGroupAsync.Changing(); + Disabled = (CanChange is not null && !CanChange(StateGroupAsync.Value)) || StateGroupAsync.Changing(); if (StateGroupAsync.Done()) { Value = StateGroupAsync.Value; Text = Value?.ToString(); - if (Validation is not null) + if (Validation is Func validate) { - var validate = (Func, StateValidation>)Validation; - var validation = validate(StateGroupAsync); + var validation = validate(StateGroupAsync.Value); ErrorText = validation.Message; Error = validation.Error; } diff --git a/RxMudBlazorLight/Inputs/Select/MudSelectRx.razor b/RxMudBlazorLight/Inputs/Select/MudSelectRx.razor index 5ee097c..972cf75 100644 --- a/RxMudBlazorLight/Inputs/Select/MudSelectRx.razor +++ b/RxMudBlazorLight/Inputs/Select/MudSelectRx.razor @@ -8,10 +8,10 @@ public required IStateGroup StateGroup { get; init; } [Parameter] - public Action, T>? SelectionChanged { get; init; } + public Action? SelectionChanged { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public bool HideDisabled { get; set; } = false; @@ -22,13 +22,13 @@ { ValueChanged = EventCallback.Factory.Create(this, v => { - StateGroup.Change(s => + StateGroup.Change(() => { if (SelectionChanged is not null) { - SelectionChanged(s, v); + SelectionChanged(v); } - s.Value = v; + StateGroup.Value = v; }); }); @@ -37,21 +37,21 @@ var values = StateGroup.Items; ChildContent = builder => - { - for (var i = 0; i < values.Length; i++) { - if (HideDisabled && StateGroup.ItemDisabled(i)) + for (var i = 0; i < values.Length; i++) { - continue; - } + if (HideDisabled && StateGroup.ItemDisabled(i)) + { + continue; + } - builder.OpenComponent(0, typeof(MudSelectItemRx)); - builder.AddAttribute(1, "Index", i); - builder.AddAttribute(2, "Values", values); - builder.AddAttribute(3, "Disabled", StateGroup.ItemDisabled(i)); - builder.CloseComponent(); - } - }; + builder.OpenComponent(0, typeof(MudSelectItemRx)); + builder.AddAttribute(1, "Index", i); + builder.AddAttribute(2, "Values", values); + builder.AddAttribute(3, "Disabled", StateGroup.ItemDisabled(i)); + builder.CloseComponent(); + } + }; } base.OnInitialized(); @@ -59,13 +59,12 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !StateGroup.CanChange(CanChange)); + Disabled = (CanChange is not null && !CanChange(StateGroup.Value)); Value = StateGroup.Value; Text = Value?.ToString(); - if (Validation is not null) + if (Validation is Func validate) { - var validate = (Func, StateValidation>)Validation; - var validation = validate(StateGroup); + var validation = validate(StateGroup.Value); ErrorText = validation.Message; Error = validation.Error; } diff --git a/RxMudBlazorLight/Menus/MudMenuItemAsyncRx.razor b/RxMudBlazorLight/Menus/MudMenuItemAsyncRx.razor index 05c47cf..db25d75 100644 --- a/RxMudBlazorLight/Menus/MudMenuItemAsyncRx.razor +++ b/RxMudBlazorLight/Menus/MudMenuItemAsyncRx.razor @@ -1,34 +1,33 @@ @inherits MudMenuItem -@typeparam T @RenderBase() @code { [Parameter, EditorRequired] - public required IStateAsync State { get; init; } + public required IStateCommandAsync StateCommand { get; init; } [Parameter, EditorRequired] - public required Func, Task> ChangeStateAsync { get; init; } + public required Func ChangeStateAsync { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public Func>? ConfirmExecutionAsync { get; init; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); - private ButtonRx? _buttonRx; + private ButtonRx? _buttonRx; protected override void OnInitialized() { - _buttonRx = ButtonRx.Create(MBButtonType.MENU, ConfirmExecutionAsync, Color.Default); + _buttonRx = ButtonRx.Create(MBButtonType.MENU, ConfirmExecutionAsync, Color.Default); base.OnInitialized(); } protected override void OnParametersSet() { ArgumentNullException.ThrowIfNull(_buttonRx); - _buttonRx.SetParameter(State, ChangeStateAsync, null, CanChange, false); + _buttonRx.SetParameter(StateCommand, ChangeStateAsync, null, CanChange, false); OnClick = (EventCallback)_buttonRx.OnClick; OnTouch = (EventCallback)_buttonRx.OnTouch; diff --git a/RxMudBlazorLight/Menus/MudMenuItemRx.razor b/RxMudBlazorLight/Menus/MudMenuItemRx.razor index 1130919..7099c9a 100644 --- a/RxMudBlazorLight/Menus/MudMenuItemRx.razor +++ b/RxMudBlazorLight/Menus/MudMenuItemRx.razor @@ -1,34 +1,33 @@ @inherits MudMenuItem -@typeparam T @RenderBase() @code { [Parameter, EditorRequired] - public required IState State { get; init; } + public required IStateCommand StateCommand { get; init; } [Parameter, EditorRequired] - public required Action> ChangeState { get; init; } + public required Action ChangeState { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public Func>? ConfirmExecutionAsync { get; init; } private RenderFragment RenderBase() => builder => base.BuildRenderTree(builder); - private ButtonRx? _buttonRx; + private ButtonRx? _buttonRx; protected override void OnInitialized() { - _buttonRx = ButtonRx.Create(MBButtonType.MENU, ConfirmExecutionAsync, Color.Default); + _buttonRx = ButtonRx.Create(MBButtonType.MENU, ConfirmExecutionAsync, Color.Default); base.OnInitialized(); } protected override void OnParametersSet() { ArgumentNullException.ThrowIfNull(_buttonRx); - _buttonRx.SetParameter(State, ChangeState, CanChange); + _buttonRx.SetParameter(StateCommand, ChangeState, CanChange); OnClick = (EventCallback)_buttonRx.OnClick; OnTouch = (EventCallback)_buttonRx.OnTouch; diff --git a/RxMudBlazorLight/ToggleIconButtons/MudToggleIconButtonRx.razor b/RxMudBlazorLight/ToggleIconButtons/MudToggleIconButtonRx.razor index 3a992f2..433d805 100644 --- a/RxMudBlazorLight/ToggleIconButtons/MudToggleIconButtonRx.razor +++ b/RxMudBlazorLight/ToggleIconButtons/MudToggleIconButtonRx.razor @@ -7,7 +7,7 @@ public required IState State { get; init; } [Parameter] - public Func, bool>? CanChange { get; init; } + public Func? CanChange { get; init; } [Parameter] public Func>? ConfirmExecution { get; set; } @@ -24,7 +24,7 @@ { ToggledChanged = EventCallback.Factory.Create(this, v => { - State.Change(s => s.Value = v); + State.Value = v; }); base.OnInitialized(); @@ -32,7 +32,7 @@ protected override void OnParametersSet() { - Disabled = (CanChange is not null && !State.CanChange(CanChange)); + Disabled = (CanChange is not null && !CanChange(State.Value)); Toggled = State.Value; base.OnParametersSet(); diff --git a/RxMudBlazorLightTestBase/CRUD/CRUDItemAddOrUpdate.razor b/RxMudBlazorLightTestBase/CRUD/CRUDItemAddOrUpdate.razor index bdd1aca..6d286c2 100644 --- a/RxMudBlazorLightTestBase/CRUD/CRUDItemAddOrUpdate.razor +++ b/RxMudBlazorLightTestBase/CRUD/CRUDItemAddOrUpdate.razor @@ -2,11 +2,11 @@ @if (AddMode) { - + } else { - + } @code { @@ -19,7 +19,7 @@ else [Inject] public required IDialogService DialogService { get; init; } - private async Task ShowAddUpdateDialog(IStateAsync _) + private async Task ShowAddUpdateDialog() { var parameters = new DialogParameters { ["Item"] = Item }; var dialog = DialogService.Show(AddMode ? "Add ToDo" : "Edit ToDo", parameters); diff --git a/RxMudBlazorLightTestBase/CRUD/CRUDTable.razor b/RxMudBlazorLightTestBase/CRUD/CRUDTable.razor index 8e45ec4..c27d516 100644 --- a/RxMudBlazorLightTestBase/CRUD/CRUDTable.razor +++ b/RxMudBlazorLightTestBase/CRUD/CRUDTable.razor @@ -16,12 +16,12 @@ - + ConfirmDelete(DeleteType.Completed)) - State=@Service.CRUDDBState CanChange=@Service.CanRemoveCompletedCRUDItems ChangeStateAsync=@(Service.RemoveCompletedCRUDItems()) /> + StateCommand=@Service.CRUDDBStateCMD CanChange=@Service.CanRemoveCompletedCRUDItems ChangeStateAsync=@(Service.RemoveCompletedCRUDItems()) /> ConfirmDelete(DeleteType.All)) - State=@Service.CRUDDBState CanChange=@Service.CanRemoveAllCRUDItems ChangeStateAsync=@(Service.RemoveAllCRUDItems()) /> + StateCommand=@Service.CRUDDBStateCMD CanChange=@Service.CanRemoveAllCRUDItems ChangeStateAsync=@(Service.RemoveAllCRUDItems()) /> @@ -55,14 +55,14 @@ @context.Text @DateTimeForItem(context) - - ConfirmDelete(DeleteType.One)) ChangeStateAsync=@(Service.RemoveCRUDItem(context)) /> diff --git a/RxMudBlazorLightTestBase/Components/AsyncButtons.razor b/RxMudBlazorLightTestBase/Components/AsyncButtons.razor index d28f80d..839505b 100644 --- a/RxMudBlazorLightTestBase/Components/AsyncButtons.razor +++ b/RxMudBlazorLightTestBase/Components/AsyncButtons.razor @@ -3,33 +3,33 @@ AsyncButtons - Current count Async: @Service.CountStateAsync.Value + Current count Async: @Service.Counter + StateCommand=@Service.CounterCMDAsync ChangeStateAsync=@Service.IncrementCounterAsync> Increment + StateCommand=@Service.CounterCMDAsync ChangeStateAsync=@Service.IncrementCounterAsync HasProgress=@true> Increment+Progress Cancel+Text+Color+Progress Cancel+Text+Color Cancel+Text+Progress @@ -37,75 +37,75 @@ + StateCommand=@Service.CounterCMDAsync ChangeStateAsync=@Service.IncrementCounterAsync /> + StateCommand=@Service.CounterCMDAsync ChangeStateAsync=@Service.IncrementCounterAsync HasProgress=@true /> + StateCommand=@Service.CounterCMDAsync ChangeStateAsync=@Service.IncrementCounterAsync /> + StateCommand=@Service.CounterCMDAsync ChangeStateAsync=@Service.IncrementCounterAsync HasProgress=@true /> + StateCommand=@Service.CounterCMDAsync ChangeStateAsync=@Service.AddToCounterAsync(10) CancelColor=Color.Error /> + StateCommand=@Service.CounterCMDAsync ChangeStateAsync=@Service.AddToCounterAsync(10) /> @@ -113,7 +113,7 @@ [Inject] public required IDialogService DialogService { get; init; } - private async Task DoPrepareAddAsync(IStateAsync st, CancellationToken ct) + private async Task DoPrepareAddAsync(CancellationToken ct) { ArgumentNullException.ThrowIfNull(DialogService); var value = 10; @@ -133,8 +133,8 @@ return; } - st.NotifyChanging(); + Service.CounterCMDAsync.NotifyChanging(); await Task.Delay(4000, ct); - st.Value += value; + Service.Counter += value; } } diff --git a/RxMudBlazorLightTestBase/Components/ButtonTest.razor b/RxMudBlazorLightTestBase/Components/ButtonTest.razor index a8c90b3..fc6d276 100644 --- a/RxMudBlazorLightTestBase/Components/ButtonTest.razor +++ b/RxMudBlazorLightTestBase/Components/ButtonTest.razor @@ -4,31 +4,30 @@ Counter - @Service.ServiceState.Value.State + @Service.ServiceState.State - Current count: @Service.CountState.Value - Current count Async: @Service.CountStateAsync.Value + Current count: @Service.Counter - Increment - Add 5 - IncrementAsync - Add Async 2 - + Increment + Add 5 + IncrementAsync + Add Async 2 + AddWithConfirmAsync 10 - Increment - Add 5 - IncrementAsync + Increment + Add 5 + IncrementAsync CmdDialogClick()) OnTouch=@(() => CmdDialogClick())>Dialog IncrementAsync CmdDialogClick(2)) OnTouch=@(() => CmdDialogClick(2))>Dialog AddAsync 2 @@ -36,11 +35,11 @@ - - - - - + + + + + @@ -49,15 +48,15 @@ - CmdDialogClick())>Increment AsyncDialog CmdDialogClick(10))>Add AsyncDialog 10 - Service.ChangeState("TEST1"))>Change State to Test1 - Service.ChangeState("TEST2"))>Change State to Test2 + Service.ChangeServiceState("Test1"))>Change State to Test1 + Service.ChangeServiceState("Test2"))>Change State to Test2 @@ -69,7 +68,7 @@ } - + @@ -106,7 +105,7 @@ return true; } - private async Task DoPrepareAddAsync(IStateAsync st, CancellationToken ct) + private async Task DoPrepareAddAsync(CancellationToken ct) { ArgumentNullException.ThrowIfNull(DialogService); var value = 2; @@ -123,9 +122,9 @@ if (!res.Canceled) { - st.NotifyChanging(); + Service.CounterCMDAsync.NotifyChanging(); await Task.Delay(4000, ct); - st.Value = value; + Service.Counter = value; } } @@ -133,15 +132,15 @@ { if (parameter is null) { - return await DialogRx.Show(DialogService, Service.CountState, TestService.IncrementCounter, + return await DialogRx.Show(DialogService, Service.CounterCMD, Service.IncrementCounter, "Increment", "Increment counter.", "Increment", "Cancel", false); } - return await DialogAsyncRx.Show(DialogService, Service.CountStateAsync, TestService.AddToCounterAsync((int)parameter), + return await DialogAsyncRx.Show(DialogService, Service.CounterCMDAsync, Service.AddToCounterAsync((int)parameter), $"Add {parameter}", $"Add {parameter} to counter?", $"Add {parameter}", "Cancel", true, "Cancel adding!", Color.Error); } - private async Task DoPrepareAddRemoveAsync(IStateAsync st, CancellationToken ct) + private async Task DoPrepareAddRemoveAsync(CancellationToken ct) { var config = (SnackbarOptions options) => { @@ -163,14 +162,14 @@ Snackbar.RemoveByKey(sbKey); } - var value = Math.Abs(Service.CountState.Value) * 5; + var value = Math.Abs(Service.Counter) * 5; if (!Service.AddMode.Value) { value = -value; } - st.Value += value; + Service.Counter += value; } private string GetExceptions() diff --git a/RxMudBlazorLightTestBase/Components/ColorsScopedGroup.razor b/RxMudBlazorLightTestBase/Components/ColorsScopedGroup.razor index 366f895..894bea6 100644 --- a/RxMudBlazorLightTestBase/Components/ColorsScopedGroup.razor +++ b/RxMudBlazorLightTestBase/Components/ColorsScopedGroup.razor @@ -1,5 +1,5 @@  - @code { diff --git a/RxMudBlazorLightTestBase/Components/DebugComponent.razor b/RxMudBlazorLightTestBase/Components/DebugComponent.razor index fc634fa..4250344 100644 --- a/RxMudBlazorLightTestBase/Components/DebugComponent.razor +++ b/RxMudBlazorLightTestBase/Components/DebugComponent.razor @@ -3,20 +3,20 @@ - @Service.CountState.Value + @Service.Counter - Increment + Increment - + - @Service.ServiceState.Value.State + @Service.ServiceState.State - s.Value = s.Value with { State = "Test" })>Change State to Test + Change State to Test diff --git a/RxMudBlazorLightTestBase/Components/IconButtons.razor b/RxMudBlazorLightTestBase/Components/IconButtons.razor index 726b8b8..09dcd1b 100644 --- a/RxMudBlazorLightTestBase/Components/IconButtons.razor +++ b/RxMudBlazorLightTestBase/Components/IconButtons.razor @@ -1,21 +1,20 @@ @inherits RxBLServiceSubscriber - - - + + + - Current count: @Scope.CountState.Value - Current count Async: @Scope.CountStateAsync.Value + Current count: @Scope.Counter - - - + Scope.Counter = v)) /> + + diff --git a/RxMudBlazorLightTestBase/Components/InputTest.razor b/RxMudBlazorLightTestBase/Components/InputTest.razor index 29eea5a..bdbb203 100644 --- a/RxMudBlazorLightTestBase/Components/InputTest.razor +++ b/RxMudBlazorLightTestBase/Components/InputTest.razor @@ -9,15 +9,14 @@ - Current count: @Service.CountState.Value - Current count Async: @Service.CountStateAsync.Value + Current count: @Service.Counter - Size.Large) PlacementCallback=@(_ => Placement.Start) /> - + Size.Large) PlacementCallback=@(_ => Placement.Start) /> + - + Red @@ -29,11 +28,11 @@ - - + + - - + + @@ -43,9 +42,9 @@ ToggledIcon="@Icons.Material.Outlined.CheckCircle" ToggledColor="@Color.Success" ToggledTitle="On" /> CanIncrementCheck is @(Service.CanIncrementCheck.Value ? "On" : "Off") - IncrementValue - Add 5 - Add Count + Service.NumericState.Value < 20)>IncrementValue + Add 5 + Add Count @@ -85,7 +84,7 @@ protected override void OnServiceStateHasChanged(ServiceChangeReason cr) { - if (cr.StateID == Service.CanIncrementCheck.ID && Service.CanIncrementCheck.Changed()) + if (cr.StateID == Service.CanIncrementCheck.ID) { SetImageSource(); } diff --git a/RxMudBlazorLightTestBase/Components/SimpleState.razor b/RxMudBlazorLightTestBase/Components/SimpleState.razor index 1fd56da..4821c59 100644 --- a/RxMudBlazorLightTestBase/Components/SimpleState.razor +++ b/RxMudBlazorLightTestBase/Components/SimpleState.razor @@ -4,11 +4,11 @@ Current count: @Service.Counter.Value - Current count Async: @Service.CounterAsync.Value + Current count Async: @Service.CounterAsync - Increment - Add + Service.Counter.Value++)>Increment + Add \ No newline at end of file diff --git a/RxMudBlazorLightTestBase/Components/TestPlayground.razor b/RxMudBlazorLightTestBase/Components/TestPlayground.razor index 663e0bc..330d2c2 100644 --- a/RxMudBlazorLightTestBase/Components/TestPlayground.razor +++ b/RxMudBlazorLightTestBase/Components/TestPlayground.razor @@ -8,10 +8,10 @@ - Current count: @Service.CountState.Value - - Increment - Add 5 + Current count: @Service.Counter + + Increment + Add 5 @($"Count State Changes {_countStateChanges}") @@ -33,7 +33,7 @@ protected override void OnServiceStateHasChanged(ServiceChangeReason cr) { - if (cr.StateID == Service.CountState.ID && Service.CountState.Changed()) + if (cr.StateID == Service.CounterCMD.ID && Service.CounterCMD.Changed()) { _countStateChanges++; } diff --git a/RxMudBlazorLightTestBase/Service/CrudService.cs b/RxMudBlazorLightTestBase/Service/CrudService.cs index e1e5d0d..a94c6ea 100644 --- a/RxMudBlazorLightTestBase/Service/CrudService.cs +++ b/RxMudBlazorLightTestBase/Service/CrudService.cs @@ -1,6 +1,5 @@  using RxBlazorLightCore; -using System.Reactive; namespace RxMudBlazorLightTestBase.Service { @@ -71,7 +70,7 @@ public sealed partial class CrudItemInput(CrudService service, CRUDToDoItem? ite public async Task SubmitAsync() { var newItem = new CRUDToDoItem(Text.Value, DueDateDate.Value + DueDateTime.Value, false, _item?.Id ?? Guid.NewGuid()); - await service.CRUDDBState.ChangeAsync(service.AddCRUDItem(newItem)); + await service.CRUDDBStateCMD.ChangeAsync(service.AddCRUDItem(newItem)); } public bool CanSubmit() { @@ -82,35 +81,35 @@ public bool CanSubmit() (Text.Value != _item?.Text || dateNew != dateItem); } - public Func, bool> CanUpdateText => _ => + public Func CanUpdateText => _ => { return service.CanUpdateText; }; - public Func, bool> CanUpdateDueDate => s => + public Func CanUpdateDueDate => _ => { return service.CanUpdateDueDate; }; - public static Func, StateValidation> ValidateText => s => + public static Func ValidateText => v => { - return new("Text can not be empty!", s.Value.Length == 0); + return new("Text can not be empty!", v.Length == 0); }; - public static Func, StateValidation> ValidateDueDate => s => + public static Func ValidateDueDate => v => { - return new("DueDate can not be in the past!", s.Value.Date < DateTime.Now.Date); + return new("DueDate can not be in the past!", v.Date < DateTime.Now.Date); }; - public Func, StateValidation> ValidateDueDateTime => s => + public Func ValidateDueDateTime => v => { var dateNowNS = NoSeconds(DateTime.Now); - var dateNew = _item is null ? NoSeconds(DueDateDate.Value.Date + s.Value) : dateNowNS; + var dateNew = _item is null ? NoSeconds(DueDateDate.Value.Date + v) : dateNowNS; return new("DueDate can not be in the past!", dateNew < dateNowNS); }; - public Func, bool> CanUpdateTime => s => + public Func CanUpdateTime => _ => { return service.CanUpdateDueDate; }; @@ -129,14 +128,14 @@ public bool CanSubmit() public IEnumerable CRUDItems => _db.Values; - public IStateAsync CRUDDBState { get; } + public IStateCommandAsync CRUDDBStateCMD { get; } public IStateGroup CRUDDBRoleGroup { get; } private readonly Dictionary _db; public CrudService() { _db = []; - CRUDDBState = this.CreateStateAsync(Unit.Default); + CRUDDBStateCMD = this.CreateStateCommandAsync(); CRUDDBRoleGroup = this.CreateStateGroup([DBRole.Admin, DBRole.User, DBRole.Guest], DBRole.Admin); } @@ -145,14 +144,14 @@ public CrudItemInput CreateItemInput(CRUDToDoItem? item = null) return new CrudItemInput(this, item); } - public static Func, bool> CanChangeRole => _ => true; + public static Func CanChangeRole => _ => true; - public Func, bool> CanAdd => _ => CRUDDBRoleGroup.CanAdd(); - public Func, bool> CanUpdate(CRUDToDoItem? item) => _ => (CanUpdateText || CanUpdateDueDate) && !(item is not null && item.Completed); + public Func CanAdd => () => CRUDDBRoleGroup.CanAdd(); + public Func CanUpdate(CRUDToDoItem? item) => () => (CanUpdateText || CanUpdateDueDate) && !(item is not null && item.Completed); - public Func, Task> AddCRUDItem(CRUDToDoItem item) + public Func AddCRUDItem(CRUDToDoItem item) { - return async _ => + return async () => { ArgumentNullException.ThrowIfNull(item.Id, nameof(item.Id)); _db[item.Id!.Value] = item; @@ -160,9 +159,9 @@ public Func, Task> AddCRUDItem(CRUDToDoItem item) }; } - public Func, Task> UpdateCRUDItemAsync(CRUDToDoItem item) + public Func UpdateCRUDItemAsync(CRUDToDoItem item) { - return async _ => + return async () => { ArgumentNullException.ThrowIfNull(item.Id, nameof(item.Id)); _db[item.Id!.Value] = item; @@ -170,12 +169,11 @@ public Func, Task> UpdateCRUDItemAsync(CRUDToDoItem item) }; } - public Func, bool> CanToggleCRUDItemCompleted => - _ => CRUDDBRoleGroup.CanUpdateCompleted(); + public Func CanToggleCRUDItemCompleted => () => CRUDDBRoleGroup.CanUpdateCompleted(); - public Func, CancellationToken, Task> ToggleCRUDItemCompletedAsync(CRUDToDoItem item) + public Func ToggleCRUDItemCompletedAsync(CRUDToDoItem item) { - return async (_, ct) => + return async ct => { ArgumentNullException.ThrowIfNull(item.Id, nameof(item.Id)); item = item with { Completed = !item.Completed }; @@ -184,14 +182,11 @@ public Func, CancellationToken, Task> ToggleCRUDItemCompletedA }; } - public Func, bool> CanRemoveCRUDItem => _ => - { - return CRUDDBRoleGroup.CanDeleteOne(); - }; + public Func CanRemoveCRUDItem => () => CRUDDBRoleGroup.CanDeleteOne(); - public Func, Task> RemoveCRUDItem(CRUDToDoItem item) + public Func RemoveCRUDItem(CRUDToDoItem item) { - return async s => + return async () => { ArgumentNullException.ThrowIfNull(item.Id, nameof(item.Id)); _db.Remove(item.Id!.Value); @@ -199,15 +194,15 @@ public Func, Task> RemoveCRUDItem(CRUDToDoItem item) }; } - public Func, bool> CanRemoveCompletedCRUDItems => s => + public Func CanRemoveCompletedCRUDItems => () => { return _db.Values.Where(x => x.Completed).Any() && CRUDDBRoleGroup.CanDeleteCompleted(); }; - public Func, Task> RemoveCompletedCRUDItems() + public Func RemoveCompletedCRUDItems() { - return async s => + return async () => { foreach (var item in _db.Values) { @@ -220,14 +215,14 @@ public Func, Task> RemoveCompletedCRUDItems() }; } - public Func, bool> CanRemoveAllCRUDItems => s => + public Func CanRemoveAllCRUDItems => () => { return _db.Values.Count != 0 && CRUDDBRoleGroup.CanDeleteAll(); }; - public Func, Task> RemoveAllCRUDItems() + public Func RemoveAllCRUDItems() { - return async s => + return async () => { _db.Clear(); await Task.Delay(200); diff --git a/RxMudBlazorLightTestBase/Service/StateService.cs b/RxMudBlazorLightTestBase/Service/StateService.cs index bd259e5..fd97e24 100644 --- a/RxMudBlazorLightTestBase/Service/StateService.cs +++ b/RxMudBlazorLightTestBase/Service/StateService.cs @@ -5,36 +5,38 @@ namespace RxMudBlazorLightTestBase.Service public class StateService : RxBLService { public IState Counter { get; } - public IStateAsync CounterAsync { get; } + public int CounterAsync { get; private set; } + + public IStateCommandAsync CounterAsyncCMD { get; } public StateService() { Counter = this.CreateState(0); - CounterAsync = this.CreateStateAsync(0); + CounterAsyncCMD = this.CreateStateCommandAsync(); } - public static Func, bool> CounterCanChange => s => s.Value < 20; - public static Func, bool> CounterAsyncCanChange => s => s.Value < 10; + public static Func CounterCanChange => v => v < 20; + public Func CounterAsyncCanChange => () => CounterAsync < 10; - public static Action> Increment => s => s.Value++; + public Action Increment => () => CounterAsync++; - public static Action> Add(int value) + public Action Add(int value) { - return s => s.Value += value; + return () => CounterAsync += value; } - public static Func, Task> IncrementAsync => async s => + public Func IncrementAsync => async () => { await Task.Delay(1000); - s.Value++; + CounterAsync++; }; - public static Func, CancellationToken, Task> AddAsync(int value) + public Func AddAsync(int value) { - return async (s, ct) => + return async ct => { await Task.Delay(1000, ct); - s.Value += value; + CounterAsync += value; }; } } diff --git a/RxMudBlazorLightTestBase/Service/TestService.State.cs b/RxMudBlazorLightTestBase/Service/TestService.State.cs index 09f694b..41cc700 100644 --- a/RxMudBlazorLightTestBase/Service/TestService.State.cs +++ b/RxMudBlazorLightTestBase/Service/TestService.State.cs @@ -4,27 +4,32 @@ namespace RxMudBlazorLightTestBase.Service { public sealed partial class TestService { - public static Action> CounterException => _ => throw new InvalidOperationException("Command test exception!"); + public static Action CounterException => () => throw new InvalidOperationException("Command test exception!"); - public static Action> IncrementCounter => s => s.Value++; + public Action IncrementCounter => () => Counter++; - public static Func, Task> IncrementCounterAsync => async s => + public static Action IncrementCounterIndirect(int value, Action setter) => () => + { + setter(++value); + }; + + public Func IncrementCounterAsync => async () => { await Task.Delay(1000); - s.Value++; + Counter++; }; - public static Action> AddToCounter(int value) + public Action AddToCounter(int value) => () => { - return s => s.Value += value; - } + Counter += value; + }; - public static Func, CancellationToken, Task> AddToCounterAsync(int value) + public Func AddToCounterAsync(int value) { - return async (s, ct) => + return async ct => { await Task.Delay(2000, ct); - s.Value += value; + Counter += value; }; } @@ -48,43 +53,43 @@ private bool ColorDisabled(int index) return index == 1 && CanIncrementCheck.Value; } - public bool AddModeCanChange(IState _) + public bool AddModeCanChange(bool _) { - return !CountState.Changing(); + return !CounterCMD.Changing() && !CounterCMDAsync.Changing(); } - public bool IncrementStateCanChangeCheck(IState _) + public bool IncrementStateCanChangeCheck() { return CanIncrementCheck.Value; } - public bool IncrementStateCanChange(IStateBase _) + public bool IncrementStateCanChange() { return _canIncrement; } - public static Func, bool> IntStateCanChangeLowerBound(int lowerBound) => s => s.Value > lowerBound; + public Func CounterCanChangeLowerBound(int lowerBound) => () => Counter > lowerBound; - public bool RatingValueCanChange(IState _) + public bool RatingValueCanChange(int _) { return GetRadio().Value.Color is ColorEnum.GREEN; } - public static Func, Pizza, Task> ChangePizzaAsync => async (s, p) => + public Func ChangePizzaAsync => async p => { await Task.Delay(1000); - s.Value = p; + GetPizzas2().Value = p; }; - public static Action, Pizza> ChangePizza(string value) + public Action ChangePizza(string value) { - return (s, p) => + return p => { var context = value; - s.Value = p; + GetPizzas1().Value = p; }; } - public static Action, TestColor> ChangeTestColor => (s, p) => s.Value = p; + public Action ChangeTestColor => c => GetRadio().Value = c; } } diff --git a/RxMudBlazorLightTestBase/Service/TestService.cs b/RxMudBlazorLightTestBase/Service/TestService.cs index 125014f..a1a0100 100644 --- a/RxMudBlazorLightTestBase/Service/TestService.cs +++ b/RxMudBlazorLightTestBase/Service/TestService.cs @@ -43,13 +43,15 @@ public record StateInfo(string State); public partial class TestServiceBase : RxBLService { - public IState ServiceState { get; } + public StateInfo ServiceState { get; protected set; } + public IStateCommand ServiceStateCMD { get; } public TestServiceBase(IServiceProvider _) { Console.WriteLine("TestService Create"); - ServiceState = this.CreateState(new StateInfo(string.Empty)); + ServiceState = new StateInfo(string.Empty); + ServiceStateCMD = this.CreateStateCommand(); } } @@ -57,9 +59,11 @@ public partial class TestService : TestServiceBase { public sealed class Scope(TestService service) : RxBLStateScope(service) { - public IState CountState = service.CreateState(0); + public int Counter { get; set; } - public IStateAsync CountStateAsync = service.CreateStateAsync(0); + public IStateCommand CounterCMD = service.CreateStateCommand(); + + public IStateCommandAsync CounterCMDAsync = service.CreateStateCommandAsync(); public override ValueTask OnContextReadyAsync() { @@ -74,6 +78,23 @@ protected override void Dispose(bool disposing) Console.WriteLine("Scope Disposed"); } } + + public Action IncrementCounter => () => Counter++; + + public Func IncrementCounterAsync => async () => + { + await Task.Delay(1000); + Counter++; + }; + + public Func AddToCounterAsync(int value) + { + return async ct => + { + await Task.Delay(2000, ct); + Counter += value; + }; + } } public class ColorsStateScope(TestService service) : RxBLStateScope(service) @@ -87,27 +108,26 @@ public class ColorsStateScope(TestService service) : RxBLStateScope public IStateGroupAsync TestColors = service.CreateStateGroupAsync(Colors, Colors[0]); - public static Func, TestColor, Task> ChangeTestColorAsync(int context) + public Func ChangeTestColorAsync(int context) { - return async (s, p) => + return async _ => { - var newContext = context; await Task.Delay(1000); - s.Value = p; + TestColors.Value = Colors[context]; }; } - public static Func, bool> CanChangeTestColor(int context) + public static Func CanChangeTestColor(int context) => c => { - return s => - { - return context != 1; - }; - } + return context != 1; + }; } - public IState CountState { get; } - public IStateAsync CountStateAsync { get; } + public int Counter { get; set; } + + public IStateCommand CounterCMD { get; } + + public IStateCommandAsync CounterCMDAsync { get; } public IState AddMode { get; } @@ -127,8 +147,8 @@ public TestService(IServiceProvider sp) : base(sp) { Console.WriteLine("TestService Create"); - CountState = this.CreateState(0); - CountStateAsync = this.CreateStateAsync(0); + CounterCMD = this.CreateStateCommand(); + CounterCMDAsync = this.CreateStateCommandAsync(); CanIncrementCheck = this.CreateState(false); AddMode = this.CreateState(false); @@ -157,13 +177,13 @@ protected override async ValueTask ContextReadyAsync() Console.WriteLine("TestService OnContextInitialized"); await Task.Delay(3000); _canIncrement = true; - ServiceState.Change(s => s.Value = new StateInfo("Initialized")); + ServiceState = new StateInfo("Initialized"); } - public void ChangeState(string state) + public Action ChangeServiceState(string state) => () => { - ServiceState.Change(s => s.Value = s.Value with { State = state }); - } + ServiceState = ServiceState with { State = state }; + }; public IStateGroup GetPizzas1() { diff --git a/RxMudBlazorLightTestBase/Service/TimerService.cs b/RxMudBlazorLightTestBase/Service/TimerService.cs index d69a141..fa79f0c 100644 --- a/RxMudBlazorLightTestBase/Service/TimerService.cs +++ b/RxMudBlazorLightTestBase/Service/TimerService.cs @@ -19,6 +19,7 @@ public IRxBLStateScope CreateScope() public sealed partial class TimerStateScope(TimerService service) : RxBLStateScope(service) { public IState ComponentTimer { get; } = service.CreateState(0L); + public IState Suspended { get; } = service.CreateState(false); public IState TimerState { get; } = service.CreateState(State.STARTED); @@ -33,11 +34,11 @@ public override ValueTask OnContextReadyAsync() { if (!Suspended.Value) { - ComponentTimer.Change(s => s.Value++); + ComponentTimer.Value++; if (ComponentTimer.Value > 20 && TimerState.Value is State.STARTED) { - TimerState.Change(s => s.Value = State.OVER20); + TimerState.Value = State.OVER20; } } });