Skip to content

Commit 76e2c9d

Browse files
authored
Fix For CanExecute with IObservable<bool> Fields (#48)
Fix For #44
1 parent e746a92 commit 76e2c9d

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

src/ReactiveUI.SourceGenerators.Execute/TestViewModel.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace SGReactiveUI.SourceGenerators.Test;
1818
[DataContract]
1919
public partial class TestViewModel : ReactiveObject
2020
{
21+
private readonly IObservable<bool> _observable = Observable.Return(true);
22+
2123
[JsonInclude]
2224
[DataMember]
2325
[ObservableAsProperty]
@@ -67,6 +69,7 @@ public TestViewModel()
6769
cancel?.Dispose();
6870

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

7174
Console.ReadLine();
7275
}
@@ -171,4 +174,7 @@ public TestViewModel()
171174

172175
[ReactiveCommand]
173176
private async Task<Point> Test10Async(int size, CancellationToken ct) => await Task.FromResult(new Point(size, size));
177+
178+
[ReactiveCommand(CanExecute = nameof(_observable))]
179+
private void TestPrivateCanExecute() => Console.Out.WriteLine("TestPrivateCanExecute");
174180
}

src/ReactiveUI.SourceGenerators/ReactiveCommand/Models/CanExecuteTypeInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ namespace ReactiveUI.SourceGenerators.Input.Models;
88
internal enum CanExecuteTypeInfo
99
{
1010
PropertyObservable,
11-
MethodObservable
11+
MethodObservable,
12+
FieldObservable,
1213
}

src/ReactiveUI.SourceGenerators/ReactiveCommand/ReactiveCommandGenerator.Execute.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,18 @@ internal static bool TryGetCanExecuteExpressionFromSymbol(
316316

317317
return true;
318318
}
319+
else if (canExecuteSymbol is IFieldSymbol canExecuteFieldSymbol)
320+
{
321+
// The property type must always be a bool
322+
if (!IsObservableBoolType(canExecuteFieldSymbol.Type))
323+
{
324+
goto Failure;
325+
}
326+
327+
canExecuteTypeInfo = CanExecuteTypeInfo.FieldObservable;
328+
329+
return true;
330+
}
319331

320332
Failure:
321333
canExecuteTypeInfo = null;

0 commit comments

Comments
 (0)