Repository navigation
Expand file tree
/
Copy pathMigrationExamples.cs
More file actions
162 lines (127 loc) · 6.66 KB
/
Copy pathMigrationExamples.cs
File metadata and controls
162 lines (127 loc) · 6.66 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
// Copyright (c) 2019-2026 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 Microsoft.Maui.Controls;
using ReactiveUI.Binding.Documentation.GitHub;
using ReactiveUI.Binding.Documentation.Todo;
namespace ReactiveUI.Binding.Documentation.Converters;
/// <summary>Moves the converters an older app kept in a Splat resolver into the binding converter service.</summary>
public static class MigrationExamples
{
/// <summary>Reads the converters out of the older app's Splat resolver.</summary>
public static void ExtractConvertersFromExistingResolver()
{
using Splat.ModernDependencyResolver existingResolver = ExistingAppDependencyResolver.Create();
ExtractedConverters extracted = ConverterMigrationHelper.ExtractConverters(existingResolver);
Console.WriteLine(extracted.TypedConverters.Count);
Console.WriteLine(extracted.FallbackConverters.Count);
Console.WriteLine(extracted.SetMethodConverters.Count);
Console.WriteLine(extracted.TypedConverters[0].GetType().Name);
Console.WriteLine(extracted.FallbackConverters[0].GetType().Name);
Console.WriteLine(extracted.SetMethodConverters[0].GetType().Name);
// Output:
// 1
// 1
// 1
// ExistingPriorityToColorConverter
// ExistingObjectToStringFallbackConverter
// ExistingTagListSetMethodConverter
}
/// <summary>Copies the older app's converters into a converter service and uses each one.</summary>
public static void ImportExistingConvertersIntoService()
{
using Splat.ModernDependencyResolver existingResolver = ExistingAppDependencyResolver.Create();
ConverterService service = new();
service.ImportFrom(existingResolver);
IBindingTypeConverter? typed = service.TypedConverters.TryGetConverter(typeof(TodoPriority), typeof(string));
Console.WriteLine(typed!.TryConvertTyped(TodoPriority.High, null, out var highColour));
Console.WriteLine(highColour);
IBindingFallbackConverter? fallback = service.FallbackConverters.TryGetConverter(typeof(IssueState), typeof(string));
Console.WriteLine(fallback!.TryConvert(typeof(IssueState), IssueState.Closed, typeof(string), null, out var stateText));
Console.WriteLine(stateText);
ISetMethodBindingConverter? setMethod = service.SetMethodConverters.TryGetConverter(typeof(IReadOnlyList<string>), typeof(List<string>));
List<string> tagList = ["draft"];
List<string> newTags = ["travel", "admin"];
_ = setMethod!.PerformSet(tagList, newTags, null);
Console.WriteLine(string.Join(", ", tagList));
// Output:
// True
// #D32F2F
// True
// Closed
// travel, admin
}
/// <summary>Binds a priority badge with the converter the older app registered, passed to the binding as the converter override.</summary>
public static void BindToWithMigratedConverterOverride()
{
using Splat.ModernDependencyResolver existingResolver = ExistingAppDependencyResolver.Create();
BindingConverters.Current.ImportFrom(existingResolver);
IBindingTypeConverter? migrated = BindingConverters.Current.TypedConverters.TryGetConverter(typeof(TodoPriority), typeof(string));
TodoItem item = new() { Title = "Renew passport", Priority = TodoPriority.High };
Label priorityBadge = new();
using (item.WhenChanged(static x => x.Priority).BindTo(priorityBadge, static x => x.Text, migrated!))
{
Console.WriteLine(priorityBadge.Text);
item.Priority = TodoPriority.Low;
Console.WriteLine(priorityBadge.Text);
}
// Output:
// #D32F2F
// #9E9E9E
}
/// <summary>Binds a priority badge and lets the binding find the migrated converter in the service.</summary>
public static void BindToResolvesMigratedConverter()
{
using Splat.ModernDependencyResolver existingResolver = ExistingAppDependencyResolver.Create();
BindingConverters.Current.ImportFrom(existingResolver);
TodoItem item = new() { Title = "Book dentist", Priority = TodoPriority.Normal };
Label priorityBadge = new();
using (item.WhenChanged(static x => x.Priority).BindTo(priorityBadge, static x => x.Text))
{
Console.WriteLine(priorityBadge.Text);
item.Priority = TodoPriority.High;
Console.WriteLine(priorityBadge.Text);
}
// Output:
// #FB8C00
// #D32F2F
}
/// <summary>Groups converters into an <c>ExtractedConverters</c> yourself and takes the three groups apart again.</summary>
public static void GroupConvertersAndDeconstruct()
{
List<IBindingTypeConverter> typed = [new ExistingPriorityToColorConverter()];
List<IBindingFallbackConverter> fallback = [new ExistingObjectToStringFallbackConverter()];
List<ISetMethodBindingConverter> setMethod = [new ExistingTagListSetMethodConverter()];
ExtractedConverters extracted = new(typed, fallback, setMethod);
var (typedConverters, fallbackConverters, setMethodConverters) = extracted;
Console.WriteLine(typedConverters[0].GetType().Name);
Console.WriteLine(fallbackConverters[0].GetType().Name);
Console.WriteLine(setMethodConverters[0].GetType().Name);
// Output:
// ExistingPriorityToColorConverter
// ExistingObjectToStringFallbackConverter
// ExistingTagListSetMethodConverter
}
/// <summary>Compares extracted converters; two results are equal when they hold the same three groups.</summary>
public static void CompareExtractedConverters()
{
using Splat.ModernDependencyResolver existingResolver = ExistingAppDependencyResolver.Create();
ExtractedConverters extracted = ConverterMigrationHelper.ExtractConverters(existingResolver);
ExtractedConverters same = extracted with { };
ExtractedConverters withoutFallback = extracted with { FallbackConverters = [] };
object boxedSame = same;
Console.WriteLine(extracted == same);
Console.WriteLine(extracted != withoutFallback);
Console.WriteLine(extracted.Equals(same));
Console.WriteLine(extracted.Equals(boxedSame));
Console.WriteLine(extracted.GetHashCode() == same.GetHashCode());
Console.WriteLine(extracted.ToString().StartsWith(nameof(ExtractedConverters), StringComparison.Ordinal));
// Output:
// True
// True
// True
// True
// True
// True
}
}