Skip to content

Commit

Permalink
RX State all new State handing
Browse files Browse the repository at this point in the history
Fix a bunch of issues
  • Loading branch information
b-straub committed Apr 19, 2024
1 parent 16c61b5 commit 904e4f5
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 33 deletions.
29 changes: 14 additions & 15 deletions RxMudBlazorLight/ButtonBase/ButtonBaseRx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using MudBlazor;
using RxBlazorLightCore;
using RxMudBlazorLight.Extensions;
using System.Reflection.Emit;

namespace RxMudBlazorLight.ButtonBase
{
Expand Down Expand Up @@ -107,7 +108,7 @@ public RenderFragment RenderProgress() => builder =>
}
};

public (string? StartIcon, string? EndIcon, string? Label) GetFabParameters(IStateCommandAsync stateCommand, string? startIcon, string? endIcon, string? label, MBIconVariant? iconVariant, bool canCancel)
public (string? StartIcon, string? EndIcon, string? Label) GetFabParameters(IStateCommandAsync stateCommand, string? startIcon, string? endIcon, string? label, MBIconVariant? iconVariant)
{
if (_iconForState is IconForState.None)
{
Expand Down Expand Up @@ -161,7 +162,7 @@ public RenderFragment RenderProgress() => builder =>
}
}

if (_cancelText is not null && canCancel)
if (_cancelText is not null && stateCommand.CanCancel)
{
label = _cancelText;
}
Expand All @@ -172,19 +173,17 @@ public RenderFragment RenderProgress() => builder =>

public string GetIconButtonParameters(IStateCommandAsync stateCommand, string icon, MBIconVariant? iconVariant)
{
if (_iconForState is IconForState.None)
{
_iconForState = IconForState.Start;
_buttonIcon = icon;
}

if (!stateCommand.Changing() || stateCommand.ChangeCallerID != _id)
{
if (_iconForState is IconForState.None)
{
_buttonIcon = icon;
_iconForState = IconForState.Start;
}
else
{
ArgumentNullException.ThrowIfNull(_buttonIcon);
icon = _buttonIcon;
_iconForState = IconForState.None;
}
ArgumentNullException.ThrowIfNull(_buttonIcon);
icon = _buttonIcon;
_iconForState = IconForState.None;
}
else
{
Expand All @@ -197,9 +196,9 @@ public string GetIconButtonParameters(IStateCommandAsync stateCommand, string ic
return icon;
}

public string GetBadgeIcon(IStateCommandAsync stateCommand, MBIconVariant? iconVariant, bool canCancel)
public string GetBadgeIcon(IStateCommandAsync stateCommand, MBIconVariant? iconVariant)
{
if (canCancel && stateCommand.Changing() && stateCommand.ChangeCallerID == _id)
if (stateCommand.CanCancel && stateCommand.Changing() && stateCommand.ChangeCallerID == _id)
{
return iconVariant.GetCancelIcon();
}
Expand Down
12 changes: 10 additions & 2 deletions RxMudBlazorLight/ButtonBase/ButtonRx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ public void SetParameter(IStateCommand stateCommand, Action executeCallback, Fun
ChildContent = stateCommand.Changing() && _hasProgress ? RenderProgress() : _buttonChildContent;
}

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

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

private async Task ExecuteStateCommand(IStateCommand stateCommand, Action executeCallback)
{
if (_confirmExecutionAsync is null || await _confirmExecutionAsync())
{
stateCommand.Execute(executeCallback);
}
}

[MemberNotNull(nameof(OnClick))]
[MemberNotNull(nameof(OnTouch))]
public void SetParameter(IStateCommandAsync stateCommand, Func<IStateCommandAsync, Task> executeAsyncCallback, Func<bool>? canChangeCallback, bool deferredNotification)
Expand Down
6 changes: 3 additions & 3 deletions RxMudBlazorLight/FabButtons/MudFabAsyncRx.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@inherits MudFab

<MudBadge Visible=@(_buttonRx is not null && _buttonRx.GetBadgeIcon(StateCommand, IconVariant, CancelText is null) != string.Empty)
Icon=@_buttonRx!.GetBadgeIcon(StateCommand, IconVariant, CancelText is null) Color=@Color Overlap="true" OnClick=@OnClick>
<MudBadge Visible=@(_buttonRx is not null && _buttonRx.GetBadgeIcon(StateCommand, IconVariant) != string.Empty)
Icon=@_buttonRx!.GetBadgeIcon(StateCommand, IconVariant) Color=@Color Overlap="true" OnClick=@OnClick>
@RenderBase()
</MudBadge>

Expand Down Expand Up @@ -47,7 +47,7 @@
ArgumentNullException.ThrowIfNull(_buttonRx);
_buttonRx.SetParameter(StateCommand, ExecuteAsyncCallback, CanChangeCallback, DeferredNotification);

var parameters = _buttonRx.GetFabParameters(StateCommand, StartIcon, EndIcon, Label, IconVariant, StateCommand.CanCancel);
var parameters = _buttonRx.GetFabParameters(StateCommand, StartIcon, EndIcon, Label, IconVariant);

StartIcon = parameters.StartIcon;
EndIcon = parameters.EndIcon;
Expand Down
4 changes: 2 additions & 2 deletions RxMudBlazorLight/IconButtons/MudIconButtonAsyncRx.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@inherits MudIconButton

<MudBadge Visible=@(_buttonRx is not null && _buttonRx.GetBadgeIcon(StateCommand, IconVariant, true) != string.Empty)
Icon=@_buttonRx!.GetBadgeIcon(StateCommand, IconVariant, true) Color=@Color Overlap="true" OnClick=@OnClick>
<MudBadge Visible=@(_buttonRx is not null && _buttonRx.GetBadgeIcon(StateCommand, IconVariant) != string.Empty)
Icon=@_buttonRx!.GetBadgeIcon(StateCommand, IconVariant) Color=@Color Overlap="true" OnClick=@OnClick>
@RenderBase()
</MudBadge>

Expand Down
4 changes: 2 additions & 2 deletions RxMudBlazorLight/Inputs/Radio/MudRadioGroupAsyncRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public Func<T, T, Task>? ChangingAsyncCallback { get; init; }

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

[Parameter]
public Func<int, bool> ItemDisabledCallback { get; set; } = _ => false;
Expand Down Expand Up @@ -74,7 +74,7 @@
{
if (StateGroupAsync.HasValue())
{
Disabled = (CanChangeCallback is not null && !CanChangeCallback(StateGroupAsync.Value)) || StateGroupAsync.Changing();
Disabled = (CanChangeCallback is not null && !CanChangeCallback()) || StateGroupAsync.Changing();

if (StateGroupAsync.Done())
{
Expand Down
4 changes: 2 additions & 2 deletions RxMudBlazorLight/Inputs/Radio/MudRadioGroupRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public Action<T, T>? ChangingCallback { get; init; }

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

[Parameter]
public Func<int, bool> ItemDisabledCallback { get; set; } = _ => false;
Expand Down Expand Up @@ -74,7 +74,7 @@
{
if (StateGroup.HasValue())
{
Disabled = (CanChangeCallback is not null && !CanChangeCallback(StateGroup.Value));
Disabled = (CanChangeCallback is not null && !CanChangeCallback());
Value = StateGroup.Value;

if (Validation is Func<T, StateValidation> validate)
Expand Down
4 changes: 2 additions & 2 deletions RxMudBlazorLight/Inputs/Select/MudSelectAsyncRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public Func<T, T, Task>? ChangingAsyncCallback { get; init; }

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

[Parameter]
public Func<int, bool> ItemDisabledCallback { get; set; } = _ => false;
Expand Down Expand Up @@ -65,7 +65,7 @@
{
if (StateGroupAsync.HasValue())
{
Disabled = (CanChangeCallback is not null && !CanChangeCallback(StateGroupAsync.Value)) || StateGroupAsync.Changing();
Disabled = (CanChangeCallback is not null && !CanChangeCallback()) || StateGroupAsync.Changing();

if (StateGroupAsync.Done())
{
Expand Down
4 changes: 2 additions & 2 deletions RxMudBlazorLight/Inputs/Select/MudSelectRx.razor
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public Action<T, T>? ChangingCallback { get; init; }

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

[Parameter]
public Func<int, bool> ItemDisabledCallback { get; set; } = _ => false;
Expand Down Expand Up @@ -65,7 +65,7 @@
{
if (StateGroup.HasValue())
{
Disabled = (CanChangeCallback is not null && !CanChangeCallback(StateGroup.Value));
Disabled = (CanChangeCallback is not null && !CanChangeCallback());
Value = StateGroup.Value;
Text = Value?.ToString();

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

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions RxMudBlazorLightTestBase/Components/ButtonTest.razor
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
</MudStack>

<RxBLStateScope TScope=@TestService.Scope TService=@TestService ScopeFactory=@Service.CreateScope>
<MudIconButtonAsyncRx Color="Color.Secondary" StateCommand=@Service.CancellableCommandAsync ExecuteAsyncCallback=@DoPrepareAddRemoveAsync
Icon=@(Service.AddMode.Value ? @Icons.Material.Filled.Add : Icons.Material.Filled.Remove) CancelColor=@Color.Error />
<IconButtons />
</RxBLStateScope>

Expand Down
2 changes: 1 addition & 1 deletion RxMudBlazorLightTestBase/Service/CrudService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public CrudItemInput CreateItemInput(CRUDToDoItem? item = null)
return new CrudItemInput(this, item);
}

public static Func<DBRole, bool> CanChangeRole => _ => true;
public static Func<bool> CanChangeRole => () => true;

public Func<bool> CanAdd => () => CRUDDBRoleGroup.CanAdd();
public Func<bool> CanUpdate(CRUDToDoItem? item) => () => (CanUpdateText || CanUpdateDueDate) && !(item is not null && item.Completed);
Expand Down
2 changes: 1 addition & 1 deletion RxMudBlazorLightTestBase/Service/TestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public Func<TestColor, Task> ChangeTestColorAsync(int context)
};
}

public static Func<TestColor, bool> CanChangeTestColor(int context) => c =>
public static Func<bool> CanChangeTestColor(int context) => () =>
{
return context != 1;
};
Expand Down

0 comments on commit 904e4f5

Please sign in to comment.