Skip to content

Commit

Permalink
Fix For CanExecute with IObservable<bool> Fields (#48)
Browse files Browse the repository at this point in the history
Fix For #44
  • Loading branch information
ChrisPulman authored Sep 19, 2024
1 parent e746a92 commit 76e2c9d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/ReactiveUI.SourceGenerators.Execute/TestViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace SGReactiveUI.SourceGenerators.Test;
[DataContract]
public partial class TestViewModel : ReactiveObject
{
private readonly IObservable<bool> _observable = Observable.Return(true);

[JsonInclude]
[DataMember]
[ObservableAsProperty]
Expand Down Expand Up @@ -67,6 +69,7 @@ public TestViewModel()
cancel?.Dispose();

Test10AsyncCommand?.Execute(200).Subscribe(r => Console.Out.WriteLine(r));
TestPrivateCanExecuteCommand?.Execute().Subscribe();

Console.ReadLine();
}
Expand Down Expand Up @@ -171,4 +174,7 @@ public TestViewModel()

[ReactiveCommand]
private async Task<Point> Test10Async(int size, CancellationToken ct) => await Task.FromResult(new Point(size, size));

[ReactiveCommand(CanExecute = nameof(_observable))]
private void TestPrivateCanExecute() => Console.Out.WriteLine("TestPrivateCanExecute");
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ namespace ReactiveUI.SourceGenerators.Input.Models;
internal enum CanExecuteTypeInfo
{
PropertyObservable,
MethodObservable
MethodObservable,
FieldObservable,
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,18 @@ internal static bool TryGetCanExecuteExpressionFromSymbol(

return true;
}
else if (canExecuteSymbol is IFieldSymbol canExecuteFieldSymbol)
{
// The property type must always be a bool
if (!IsObservableBoolType(canExecuteFieldSymbol.Type))
{
goto Failure;
}

canExecuteTypeInfo = CanExecuteTypeInfo.FieldObservable;

return true;
}

Failure:
canExecuteTypeInfo = null;
Expand Down

0 comments on commit 76e2c9d

Please sign in to comment.