Repository navigation
Expand file tree
/
Copy pathReactiveBindingExamples.cs
More file actions
366 lines (293 loc) · 14.6 KB
/
Copy pathReactiveBindingExamples.cs
File metadata and controls
366 lines (293 loc) · 14.6 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
// Copyright (c) ReactiveUI and Contributors. All rights reserved.
// 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.Globalization;
using ReactiveUI.Binding.Documentation.Banking;
using ReactiveUI.Binding.Documentation.CloudStorage;
using ReactiveUI.Binding.Documentation.Education;
using ReactiveUI.Binding.Documentation.Todo;
using ReactiveUI.Primitives;
using ReactiveUI.Primitives.Disposables;
using ReactiveUI.Primitives.Signals;
namespace ReactiveUI.Binding.Documentation.Bindings;
/// <summary>Shows the binding that <c>OneWayBind</c> and <c>Bind</c> return, and the update streams that decide when a two-way binding writes.</summary>
public static class ReactiveBindingExamples
{
/// <summary>The size of the file the upload example sends: four 4 MiB parts.</summary>
private const long UploadSizeBytes = 16_777_216;
/// <summary>The number that turns an upload percentage into the fraction a progress bar shows.</summary>
private const double PercentPerBar = 100D;
/// <summary>The text of the filter the customer types.</summary>
private const string CarFilter = "car";
/// <summary>The text of a filter the view model sets.</summary>
private const string DentistFilter = "dentist";
/// <summary>The reference the draft starts with.</summary>
private const string MarchReference = "Rent March";
/// <summary>The reference the customer types.</summary>
private const string AprilReference = "Rent April";
/// <summary>The reference the view model sets.</summary>
private const string MayReference = "Rent May";
/// <summary>The amount the draft starts with.</summary>
private const decimal RentAmount = 1200M;
/// <summary>The amount the customer types, as the box shows it.</summary>
private const string TypedAmountText = "85.50";
/// <summary>The score the view model starts with.</summary>
private const decimal FirstScore = 64M;
/// <summary>The score after a re-mark.</summary>
private const decimal RemarkedScore = 91M;
/// <summary>The score the teacher types.</summary>
private const string TypedScoreText = "82.5";
/// <summary>The format the amount converters use to show two decimal places.</summary>
private const string TwoDecimalPlaces = "F2";
/// <summary>The format the score converters use to show one decimal place.</summary>
private const string OneDecimalPlace = "F1";
/// <summary>Reads what a one-way binding reports: its view, its direction and every value it writes, starting with the current one.</summary>
/// <returns>A task that completes when the example finishes.</returns>
public static async Task ReadOneWayBinding()
{
StorageBrowserViewModel browser = new(InMemoryObjectStorage.CreateSeeded());
await browser.LoadBucketsAsync();
browser.SelectedBucket = browser.Buckets[0];
StorageBrowserView view = new() { ViewModel = browser };
List<double> written = [];
using (var binding = view.OneWayBind(browser, static x => x.UploadPercent, static v => v.UploadProgressBar.Progress, static percent => percent / PercentPerBar))
{
Console.WriteLine(binding.Direction);
Console.WriteLine(ReferenceEquals(view, binding.View));
using (binding.Changed.Subscribe(written.Add))
{
await browser.UploadAsync(new("launch-video.mp4", UploadSizeBytes, "video/mp4"));
}
}
// Changed replays the bar's starting value, so the empty bar is the first value a subscriber sees.
Console.WriteLine(string.Join(", ", written));
// Output:
// OneWay
// True
// 0, 0.25, 0.5, 0.75, 1
}
/// <summary>Reads what a two-way binding reports: each <see cref="BindingChange"/> says which side produced the value.</summary>
/// <returns>A task that completes when the example finishes.</returns>
public static async Task ReadTwoWayBinding()
{
TodoListViewModel list = await OpenTodoListAsync();
TodoView view = new() { ViewModel = list };
List<BindingChange> changes = [];
using (var binding = view.Bind(list, static x => x.FilterText, static v => v.FilterTextBox.Text))
using (binding.Changed.Subscribe(changes.Add))
{
Console.WriteLine(binding.Direction);
Console.WriteLine(ReferenceEquals(view, binding.View));
list.FilterText = CarFilter;
view.FilterTextBox.Text = DentistFilter;
}
foreach (BindingChange change in changes)
{
Console.WriteLine($"{change.Value}, from the view model: {change.FromViewModel}");
}
// A change is a value with two parts, so it compares, deconstructs and copies like any record struct.
var (value, fromViewModel) = changes[0];
Console.WriteLine(value);
Console.WriteLine(fromViewModel);
BindingChange copy = changes[0] with { FromViewModel = false };
Console.WriteLine($"{copy.Value}, from the view model: {copy.FromViewModel}");
// Output:
// TwoWay
// True
// car, from the view model: True
// dentist, from the view model: False
// car
// True
// car, from the view model: False
}
/// <summary>Reads the two expressions of a binding. A generated binding reads its paths at compile time, so it carries no expression trees.</summary>
/// <returns>A task that completes when the example finishes.</returns>
public static async Task ReadBindingExpressions()
{
TodoListViewModel list = await OpenTodoListAsync();
TodoView view = new() { ViewModel = list };
using IReactiveBinding<TodoView, BindingChange> binding = view.Bind(list, static x => x.FilterText, static v => v.FilterTextBox.Text);
Console.WriteLine(binding.ViewModelExpression is null);
Console.WriteLine(binding.ViewExpression is null);
// Output:
// True
// True
}
/// <summary>Wraps a subscription in a <see cref="ReactiveBinding{TView, TValue}"/>, which the binding disposes with itself.</summary>
/// <returns>A task that completes when the example finishes.</returns>
public static async Task CreateReactiveBinding()
{
TodoListViewModel list = await OpenTodoListAsync();
TodoView view = new() { ViewModel = list };
Signal<int> remainingChanges = new();
IDisposable subscription = Scope.Create(static () => Console.WriteLine("subscription disposed"));
ReactiveBinding<TodoView, int> binding = new(view, remainingChanges, BindingDirection.AsyncOneWay, subscription);
List<int> seen = [];
using IDisposable reader = binding.Changed.Subscribe(seen.Add);
remainingChanges.OnNext(list.RemainingCount);
Console.WriteLine(binding.Direction);
Console.WriteLine(ReferenceEquals(view, binding.View));
Console.WriteLine(binding.ViewExpression is null);
Console.WriteLine(binding.ViewModelExpression is null);
Console.WriteLine(string.Join(", ", seen));
binding.Dispose();
// Output:
// AsyncOneWay
// True
// True
// True
// 3
// subscription disposed
}
/// <summary>
/// Drives the view-to-view-model direction from a stream: the customer's typing stays in the box until the box is
/// left, and view model changes still reach the box at once. <see cref="TriggerUpdate.ViewToViewModel"/> is the default.
/// </summary>
public static void CommitReferenceWhenBoxIsLeft()
{
TransferViewModel viewModel = new(new InMemoryBankingBackend());
TransferView view = new() { ViewModel = viewModel };
Signal<RxVoid> boxLeft = new();
viewModel.Draft.Reference = MarchReference;
using (view.BindUnsafe(viewModel, x => x.Draft.Reference, v => v.ReferenceTextBox.Text, boxLeft))
{
Console.WriteLine(view.ReferenceTextBox.Text);
view.ReferenceTextBox.Text = AprilReference;
Console.WriteLine(viewModel.Draft.Reference);
boxLeft.OnNext(RxVoid.Default);
Console.WriteLine(viewModel.Draft.Reference);
viewModel.Draft.Reference = MayReference;
Console.WriteLine(view.ReferenceTextBox.Text);
}
// Output:
// Rent March
// Rent March
// Rent April
// Rent May
}
/// <summary>Converts the amount in each direction; the typed amount is read when the box is left, as <see cref="TriggerUpdate.ViewToViewModel"/> does by default.</summary>
public static void CommitAmountWhenBoxIsLeft()
{
TransferViewModel viewModel = new(new InMemoryBankingBackend());
TransferView view = new() { ViewModel = viewModel };
Signal<RxVoid> boxLeft = new();
viewModel.Draft.Amount = RentAmount;
using (view.BindUnsafe(viewModel, x => x.Draft.Amount, v => v.AmountTextBox.Text, FormatAmount, ParseAmount, boxLeft))
{
Console.WriteLine(view.AmountTextBox.Text);
view.AmountTextBox.Text = TypedAmountText;
Console.WriteLine(viewModel.Draft.Amount);
boxLeft.OnNext(RxVoid.Default);
Console.WriteLine(viewModel.Draft.Amount);
}
// Output:
// 1200.00
// 1200
// 85.50
}
/// <summary>
/// Drives the view-model-to-view direction from a stream: the view model's score reaches the box when the stream
/// signals a refresh, while the teacher's typing reaches the view model at once.
/// </summary>
public static void RefreshScoreOnSignal()
{
GradebookViewModel viewModel = new(InMemoryStudentRecords.CreateSeeded()) { ScoreToRecord = FirstScore };
GradebookView view = new() { ViewModel = viewModel };
Signal<RxVoid> refresh = new();
using (view.BindUnsafe(viewModel, x => x.ScoreToRecord, v => v.ScoreEntry.Text, FormatScore, ParseScore, refresh, TriggerUpdate.ViewModelToView))
{
Console.WriteLine(view.ScoreEntry.Text);
viewModel.ScoreToRecord = RemarkedScore;
Console.WriteLine(view.ScoreEntry.Text);
refresh.OnNext(RxVoid.Default);
Console.WriteLine(view.ScoreEntry.Text);
view.ScoreEntry.Text = TypedScoreText;
Console.WriteLine(viewModel.ScoreToRecord);
}
// Output:
// 64.0
// 64.0
// 91.0
// 82.5
}
/// <summary>Uses <see cref="TriggerUpdate.ViewModelToView"/> without converters; the registered converter for the pair runs.</summary>
/// <returns>A task that completes when the example finishes.</returns>
public static async Task RefreshFilterOnSignal()
{
TodoListViewModel list = await OpenTodoListAsync();
TodoView view = new() { ViewModel = list };
Signal<RxVoid> refresh = new();
using (view.BindUnsafe(list, x => x.FilterText, v => v.FilterTextBox.Text, refresh, TriggerUpdate.ViewModelToView))
{
list.FilterText = CarFilter;
Console.WriteLine($"'{view.FilterTextBox.Text}'");
refresh.OnNext(RxVoid.Default);
Console.WriteLine(view.FilterTextBox.Text);
view.FilterTextBox.Text = DentistFilter;
Console.WriteLine(list.FilterText);
}
// Output:
// ''
// car
// dentist
}
/// <summary>Passes no stream: both properties are observed, so the binding writes in both directions as they change.</summary>
/// <returns>A task that completes when the example finishes.</returns>
public static async Task ObserveBothWithoutStream()
{
TodoListViewModel list = await OpenTodoListAsync();
TodoView view = new() { ViewModel = list };
using (view.BindUnsafe(list, x => x.FilterText, v => v.FilterTextBox.Text, (IObservable<RxVoid>?)null))
{
list.FilterText = CarFilter;
Console.WriteLine(view.FilterTextBox.Text);
view.FilterTextBox.Text = DentistFilter;
Console.WriteLine(list.FilterText);
}
// Output:
// car
// dentist
}
/// <summary>Passes no stream to the converting overload, so both properties are observed.</summary>
public static void ObserveBothWithConvertersAndWithoutStream()
{
TransferViewModel viewModel = new(new InMemoryBankingBackend());
TransferView view = new() { ViewModel = viewModel };
viewModel.Draft.Amount = RentAmount;
using (view.BindUnsafe(viewModel, x => x.Draft.Amount, v => v.AmountTextBox.Text, FormatAmount, ParseAmount, (IObservable<RxVoid>?)null))
{
Console.WriteLine(view.AmountTextBox.Text);
view.AmountTextBox.Text = TypedAmountText;
Console.WriteLine(viewModel.Draft.Amount);
}
// Output:
// 1200.00
// 85.50
}
/// <summary>Shows the amount of money with two decimal places.</summary>
/// <param name="amount">The amount of money.</param>
/// <returns>The amount as text.</returns>
private static string FormatAmount(decimal amount) => amount.ToString(TwoDecimalPlaces, CultureInfo.InvariantCulture);
/// <summary>Reads an amount of money the customer typed.</summary>
/// <param name="text">The text in the box.</param>
/// <returns>The amount, or zero when the text is not a number.</returns>
private static decimal ParseAmount(string text) =>
decimal.TryParse(text, NumberStyles.Number, CultureInfo.InvariantCulture, out var amount) ? amount : 0M;
/// <summary>Shows a score with one decimal place.</summary>
/// <param name="score">The score.</param>
/// <returns>The score as text.</returns>
private static string FormatScore(decimal score) => score.ToString(OneDecimalPlace, CultureInfo.InvariantCulture);
/// <summary>Reads a score the teacher typed.</summary>
/// <param name="text">The text in the box.</param>
/// <returns>The score, or zero when the text is not a number.</returns>
private static decimal ParseScore(string text) =>
decimal.TryParse(text, NumberStyles.Number, CultureInfo.InvariantCulture, out var score) ? score : 0M;
/// <summary>Loads the seeded to-do list.</summary>
/// <returns>A task that completes with the list, which holds three unfinished items and one finished item.</returns>
private static async Task<TodoListViewModel> OpenTodoListAsync()
{
TodoListViewModel list = new(InMemoryTodoStore.CreateSeeded());
await list.LoadAsync();
return list;
}
}