-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTestViewModel.cs
More file actions
453 lines (379 loc) · 16.7 KB
/
TestViewModel.cs
File metadata and controls
453 lines (379 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright (c) 2025 ReactiveUI and contributors. All rights reserved.
// Licensed to the ReactiveUI and contributors under one or more agreements.
// The ReactiveUI and contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using DynamicData;
using ReactiveUI;
using ReactiveUI.SourceGenerators;
namespace SGReactiveUI.SourceGenerators.Test;
/// <summary>
/// TestClass.
/// </summary>
/// <seealso cref="ReactiveUI.ReactiveObject" />
/// <seealso cref="ReactiveUI.IActivatableViewModel" />
/// <seealso cref="System.IDisposable" />
/// <seealso cref="ReactiveObject" />
/// <seealso cref="IActivatableViewModel" />
/// <seealso cref="IDisposable" />
[DataContract]
public partial class TestViewModel : ReactiveObject, IActivatableViewModel, IDisposable
{
private readonly IObservable<bool> _observable = Observable.Return(true);
private readonly Subject<double?> _testSubject = new();
private readonly Subject<double> _testNonNullSubject = new();
private readonly Subject<int> _fromPartialTestSubject = new();
private readonly IScheduler _scheduler = RxApp.MainThreadScheduler;
[property: JsonInclude]
[DataMember]
[ObservableAsProperty]
private double? _test2Property = 1.1d;
[ObservableAsProperty(ReadOnly = false)]
private double? _test11Property = 11.1d;
[ObservableAsProperty(ReadOnly = false)]
private double _test13Property = 11.1d;
[ObservableAsProperty(UseProtected = true)]
private double _observableAsPropertyTest3Property;
[property: Test(AParameter = "Test Input")]
[Reactive]
private double? _test12Property = 12.1d;
[Reactive(SetModifier = AccessModifier.Protected)]
[property: JsonInclude]
[DataMember]
private int _test1Property;
private bool _disposedValue;
[Reactive]
private string _myStringProperty = "test";
[property: JsonInclude]
[DataMember]
[Reactive(Inheritance = InheritanceModifier.Virtual, SetModifier = AccessModifier.Protected)]
private string? _name;
[Reactive(SetModifier = AccessModifier.Init, UseRequired = true)]
private string _mustBeSet;
[Reactive]
private IEnumerable<Person> _people = [new Person()];
[Reactive]
private double? _myDoubleProperty;
[Reactive]
private double _myDoubleNonNullProperty;
[BindableDerivedList]
private ReadOnlyObservableCollection<Person>? _visiblePeople;
/// <summary>
/// Initializes a new instance of the <see cref="TestViewModel"/> class.
/// </summary>
[SetsRequiredMembers]
public TestViewModel()
{
MustBeSet = "Test";
this.WhenActivated(disposables =>
{
Console.Out.WriteLine("Activated");
_test11PropertyHelper = this.WhenAnyValue(x => x.Test12Property).ToProperty(this, x => x.Test11Property, out _).DisposeWith(disposables);
GetDataCommand.Do(_ => Console.Out.WriteLine("GetDataCommand Executed")).Subscribe().DisposeWith(disposables);
GetDataCommand.Execute().Subscribe().DisposeWith(disposables);
});
Console.Out.WriteLine("MyReadOnlyProperty before init");
// only settable prior to init, after init it will be ignored.
_myReadOnlyProperty = -1.0;
Console.Out.WriteLine(MyReadOnlyProperty);
Console.Out.WriteLine(_myReadOnlyProperty);
Console.Out.WriteLine("MyReadOnlyNonNullProperty before init");
// only settable prior to init, after init it will be ignored.
_myReadOnlyNonNullProperty = -5.0;
Console.Out.WriteLine(MyReadOnlyNonNullProperty);
Console.Out.WriteLine(_myReadOnlyNonNullProperty);
_observableAsPropertyTest2Property = 11223344;
Console.Out.WriteLine(ObservableAsPropertyTest2Property);
Console.Out.WriteLine(_observableAsPropertyTest2Property);
_referenceTypeObservableProperty = default!;
ReferenceTypeObservable = Observable.Return(new object());
NullableReferenceTypeObservable = Observable.Return(new object());
InitializeOAPH();
Console.Out.WriteLine(Test1Command);
Console.Out.WriteLine(Test2Command);
Console.Out.WriteLine(Test3Command);
Console.Out.WriteLine(Test4Command);
Console.Out.WriteLine(Test5StringToIntCommand);
Console.Out.WriteLine(Test6ArgOnlyCommand);
Console.Out.WriteLine(Test7ObservableCommand);
Console.Out.WriteLine(Test8ObservableCommand);
Console.Out.WriteLine(Test9Command);
Console.Out.WriteLine(Test10Command);
Test1Command?.Execute().Subscribe();
Test2Command?.Execute().Subscribe(r => Console.Out.WriteLine(r));
Test3Command?.Execute().Subscribe();
Test4Command?.Execute().Subscribe(r => Console.Out.WriteLine(r));
Test5StringToIntCommand?.Execute("100").Subscribe(Console.Out.WriteLine);
Test6ArgOnlyCommand?.Execute("Hello World").Subscribe();
Test7ObservableCommand?.Execute().Subscribe();
Console.Out.WriteLine($"Test2Property default Value: {Test2Property}");
_test2PropertyHelper = Test8ObservableCommand!.ToProperty(this, x => x.Test2Property);
_observableAsPropertyTest3PropertyHelper = this.WhenAnyValue(x => x.Test13Property)!.ToProperty(this, x => x.ObservableAsPropertyTest3Property);
Test8ObservableCommand?.Execute(100).Subscribe(d => Console.Out.WriteLine(d));
Console.Out.WriteLine($"Test2Property Value: {Test2Property}");
Console.Out.WriteLine($"Test2Property underlying Value: {_test2Property}");
Console.Out.WriteLine(ObservableAsPropertyTest2Property);
Console.Out.WriteLine("MyReadOnlyProperty After Init");
// setting this value should not update the _myReadOnlyPropertyHelper as the _testSubject has not been updated yet but the _myReadOnlyPropertyHelper should be updated with null upon init.
_myReadOnlyProperty = -2.0;
// null value expected as the _testSubject has not been updated yet, ignoring the private variable.
Console.Out.WriteLine(MyReadOnlyProperty);
Console.Out.WriteLine(_myReadOnlyProperty);
_testSubject.OnNext(10.0);
// expected value 10 as the _testSubject has been updated.
Console.Out.WriteLine(MyReadOnlyProperty);
Console.Out.WriteLine(_myReadOnlyProperty);
_testSubject.OnNext(null);
// expected value null as the _testSubject has been updated.
Console.Out.WriteLine(MyReadOnlyProperty);
Console.Out.WriteLine(_myReadOnlyProperty);
Console.Out.WriteLine("MyReadOnlyNonNullProperty After Init");
// setting this value should not update the _myReadOnlyNonNullProperty as the _testNonNullSubject has not been updated yet but the _myReadOnlyNonNullPropertyHelper should be updated with null upon init.
_myReadOnlyNonNullProperty = -2.0;
// 0 value expected as the _testNonNullSubject has not been updated yet, ignoring the private variable.
Console.Out.WriteLine(MyReadOnlyNonNullProperty);
Console.Out.WriteLine(_myReadOnlyNonNullProperty);
_testNonNullSubject.OnNext(11.0);
// expected value 11 as the _testNonNullSubject has been updated.
Console.Out.WriteLine(MyReadOnlyNonNullProperty);
Console.Out.WriteLine(_myReadOnlyNonNullProperty);
_testNonNullSubject.OnNext(default);
Console.Out.WriteLine(_test13Property);
Console.Out.WriteLine(Test13Property);
Console.Out.WriteLine(_test13PropertyHelper);
// expected value 0 as the _testNonNullSubject has been updated.
Console.Out.WriteLine(MyReadOnlyNonNullProperty);
Console.Out.WriteLine(_myReadOnlyNonNullProperty);
Test9Command?.ThrownExceptions.Subscribe(Console.Out.WriteLine);
var cancel = Test9Command?.Execute().Subscribe();
Task.Delay(1000).Wait();
cancel?.Dispose();
Test10Command?.Execute(200).Subscribe(r => Console.Out.WriteLine(r));
TestPrivateCanExecuteCommand?.Execute().Subscribe();
Console.Out.WriteLine($"Observable unset, value should be 10, value is : {ObservableAsPropertyFromProperty}");
_observableAsPropertyFromPropertyHelper = _fromPartialTestSubject.ToProperty(this, x => x.ObservableAsPropertyFromProperty);
_fromPartialTestSubject.OnNext(11);
Console.Out.WriteLine($"Observable updated, value should be 11, value is : {ObservableAsPropertyFromProperty}");
this.WhenAnyValue(vm => vm.People)
.Subscribe(people => people
.AsObservableChangeSet()
.AutoRefresh(x => x.Deleted)
.Filter(x => !x.Deleted)
.Bind(out _visiblePeople)
.Subscribe());
Console.ReadLine();
}
/// <summary>
/// Gets the instance.
/// </summary>
/// <value>
/// The instance.
/// </value>
public static TestViewModel Instance { get; } = new();
/// <summary>
/// Gets the test class oaph vm.
/// </summary>
/// <value>
/// The test class oaph vm.
/// </value>
public TestClassOAPH_VM TestClassOAPH_VM { get; } = new();
/// <summary>
/// Gets or sets the partial property test.
/// </summary>
/// <value>
/// The partial property test.
/// </value>
[Reactive]
[field: JsonInclude]
[JsonPropertyName("test")]
public partial string? PartialPropertyTest { get; set; }
/// <summary>
/// Gets or sets the partial property test.
/// </summary>
/// <value>
/// The partial property test.
/// </value>
[Reactive(UseRequired = true)]
public required partial string? PartialRequiredPropertyTest { get; set; }
/// <summary>
/// Gets the internal test property. Should not prompt to replace with INPC Reactive Property.
/// </summary>
/// <value>
/// The test property.
/// </value>
[JsonInclude]
public string? TestInternalSetProperty { get; internal set; } = "Test";
/// <summary>
/// Gets the test private set property. Should not prompt to replace with INPC Reactive Property.
/// </summary>
/// <value>
/// The test private set property.
/// </value>
[JsonInclude]
public string? TestPrivateSetProperty { get; private set; } = "Test";
/// <summary>
/// Gets or sets the test automatic property.
/// </summary>
/// <value>
/// The test automatic property.
/// </value>
[JsonInclude]
public string? TestAutoProperty { get; set; } = "Test, should prompt to replace with INPC Reactive Property";
/// <summary>
/// Gets the test read only property.
/// </summary>
/// <value>
/// The test read only property.
/// </value>
public string? TestReadOnlyProperty { get; } = "Test, should not prompt to replace with INPC Reactive Property";
/// <summary>
/// Gets or sets the reactive command test property. Should not prompt to replace with INPC Reactive Property.
/// </summary>
/// <value>
/// The reactive command test property.
/// </value>
public ReactiveCommand<Unit, Unit>? ReactiveCommandTestProperty { get; set; }
/// <summary>
/// Gets or sets the reactive property test property. Should not prompt to replace with INPC Reactive Property.
/// </summary>
/// <value>
/// The reactive property test property.
/// </value>
public ReactiveProperty<int>? ReactivePropertyTestProperty { get; set; }
/// <summary>
/// Gets the can execute test1.
/// </summary>
/// <value>
/// The can execute test1.
/// </value>
public IObservable<bool> CanExecuteTest1 => ObservableAsPropertyTest2.Select(x => x > 0);
/// <summary>
/// Gets the observable as property test2.
/// </summary>
/// <value>
/// The observable as property test2.
/// </value>
[ObservableAsProperty]
[property: Test(AParameter = "Test Input")]
public IObservable<int> ObservableAsPropertyTest2 => Observable.Return(9);
/// <summary>
/// Gets the Activator which will be used by the View when Activation/Deactivation occurs.
/// </summary>
public ViewModelActivator Activator { get; } = new();
/// <summary>
/// Gets the observable as property from property.
/// </summary>
/// <value>
/// The observable as property from property.
/// </value>
[ObservableAsProperty(InitialValue = "10")]
public partial int ObservableAsPropertyFromProperty { get; }
[Reactive]
internal partial int InternalPartialPropertyTest { get; set; }
[ObservableAsProperty]
private IObservable<object> ReferenceTypeObservable { get; }
[ObservableAsProperty]
private IObservable<object?> NullableReferenceTypeObservable { get; }
/// <summary>
/// Gets observables as property test.
/// </summary>
/// <returns>
/// Observable of double.
/// </returns>
[ObservableAsProperty(PropertyName = "MyReadOnlyProperty")]
public IObservable<double?> ObservableAsPropertyTest() => _testSubject;
/// <summary>
/// Observables as property test non null.
/// </summary>
/// <returns>Observable of double.</returns>
[ObservableAsProperty(PropertyName = "MyReadOnlyNonNullProperty")]
public IObservable<double> ObservableAsPropertyTestNonNull() => _testNonNullSubject;
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_testSubject.Dispose();
_testNonNullSubject.Dispose();
_fromPartialTestSubject.Dispose();
}
_disposedValue = true;
}
}
/// <summary>
/// Test1s this instance.
/// </summary>
[ReactiveCommand(CanExecute = nameof(CanExecuteTest1))]
[property: JsonInclude]
[property: Test(AParameter = "Test Input")]
private void Test1() => Console.Out.WriteLine("Test1 Command Executed");
/// <summary>
/// Test3s the asynchronous.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[ReactiveCommand]
private async Task Test3Async() => await Task.Delay(0);
/// <summary>
/// Test4s the asynchronous.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[ReactiveCommand]
private async Task<Point> Test4Async() => await Task.FromResult(new Point(100, 100));
/// <summary>
/// Test5s the string to int.
/// </summary>
/// <param name="str">The string.</param>
/// <returns>int.</returns>
[ReactiveCommand]
private int Test5StringToInt(string str) => int.Parse(str);
/// <summary>
/// Test6s the argument only.
/// </summary>
/// <param name="str">The string.</param>
[ReactiveCommand]
private void Test6ArgOnly(string str) => Console.Out.WriteLine($">>> {str}");
/// <summary>
/// Test7s the observable.
/// </summary>
/// <returns>An Observable of Unit.</returns>
[ReactiveCommand]
private IObservable<Unit> Test7Observable() => Observable.Return(Unit.Default);
/// <summary>
/// Test8s the observable.
/// </summary>
/// <param name="i">The i.</param>
/// <returns>An Observable of int.</returns>
[ReactiveCommand]
private IObservable<double?> Test8Observable(int i) => Observable.Return<double?>(i + 10.0);
[ReactiveCommand]
private async Task Test9Async(CancellationToken ct) => await Task.Delay(2000, ct);
[ReactiveCommand]
private async Task<Point> Test10Async(int size, CancellationToken ct) => await Task.FromResult(new Point(size, size));
[ReactiveCommand(CanExecute = nameof(_observable), OutputScheduler = nameof(_scheduler))]
private void TestPrivateCanExecute() => Console.Out.WriteLine("TestPrivateCanExecute");
[ReactiveCommand]
private Task<System.Collections.IEnumerable> GetData(CancellationToken ct) =>
Task.FromResult<System.Collections.IEnumerable>(Array.Empty<System.Collections.IEnumerable>());
}