Skip to content

Commit 191e201

Browse files
committed
chore: Update Samples
chore: unify logging for Models SwitchCodeSampleAsync
1 parent a6aacac commit 191e201

22 files changed

+361
-408
lines changed

src/DevTKSS.Uno.Samples.MvuxGallery/App.xaml.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected async override void OnLaunched(LaunchActivatedEventArgs args)
6666
.Section<AppConfig>()
6767

6868
.EmbeddedSource<App>("sampledata")
69-
.Section<DashboardSampleOptions>()
69+
//.Section<DashboardSampleOptions>()
7070
.Section<MainSampleOptions>()
7171
.Section<ListboardSampleOptions>()
7272
.Section<SimpleCardsSampleOptions>()
@@ -84,7 +84,7 @@ protected async override void OnLaunched(LaunchActivatedEventArgs args)
8484
.AddSingleton<ICodeSampleService<ListboardSampleOptions>,CodeSampleService<ListboardSampleOptions>>()
8585
.AddSingleton<ICodeSampleService<SimpleCardsSampleOptions>, CodeSampleService<SimpleCardsSampleOptions>>()
8686
.AddSingleton<ICodeSampleService<CounterSampleOptions>, CodeSampleService<CounterSampleOptions>>()
87-
.AddSingleton<ICodeSampleService<DashboardSampleOptions>, CodeSampleService<DashboardSampleOptions>>()
87+
//.AddSingleton<ICodeSampleService<DashboardSampleOptions>, CodeSampleService<DashboardSampleOptions>>()
8888
)
8989
.UseNavigation(ReactiveViewModelMappings.ViewModelMappings, RegisterRoutes)
9090
.UseSerialization((context, services) =>
@@ -100,7 +100,7 @@ protected async override void OnLaunched(LaunchActivatedEventArgs args)
100100
.AddJsonTypeInfo(CodeSampleOptionContext.Default.ListboardSampleOptions)
101101
.AddJsonTypeInfo(CodeSampleOptionContext.Default.SimpleCardsSampleOptions)
102102
.AddJsonTypeInfo(CodeSampleOptionContext.Default.CounterSampleOptions)
103-
.AddJsonTypeInfo(CodeSampleOptionContext.Default.DashboardSampleOptions)
103+
//.AddJsonTypeInfo(CodeSampleOptionContext.Default.DashboardSampleOptions)
104104

105105
.AddSingleton(new JsonSerializerOptions { PropertyNameCaseInsensitive = true })
106106
)
@@ -122,8 +122,8 @@ private static void RegisterRoutes(IViewRegistry views, IRouteRegistry routes)
122122
new ViewMap<MainPage, MainModel>(),
123123
new ViewMap<CounterPage, CounterModel>(),
124124
new ViewMap<DashboardPage, DashboardModel>(),
125-
new ViewMap<ListboardPage, ListboardModel>(),
126-
new ViewMap<SimpleCardsPage, SimpleCardsModel>()
125+
new ViewMap<ListboardPage, ListboardModel>(),
126+
new ViewMap<SimpleCardsPage, SimpleCardsModel>()
127127
);
128128

129129
routes.Register(

src/DevTKSS.Uno.Samples.MvuxGallery/Assets/Samples/Card-GalleryImage.DataTemplate.xaml.txt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
xmlns:utu="using:Uno.UI.Toolkit"
2-
xmlns:gm="using:DevTKSS.Uno.Samples.MvuxGallery.GeneralModels"
2+
xmlns:gm="using:DevTKSS.Uno.Samples.MvuxGallery.Models.GalleryImages"
33

44
...
55

@@ -16,7 +16,7 @@ xmlns:gm="using:DevTKSS.Uno.Samples.MvuxGallery.GeneralModels"
1616
SubHeaderContentTemplate="{StaticResource CardSubHeaderContentTemplate}"
1717
MediaContentTemplate="{StaticResource CardMediaContentTemplate}"/>
1818
</DataTemplate>
19-
<!--#region Card HeaderContent Template-->
19+
2020
<DataTemplate x:DataType="gm:GalleryImageModel" x:Key="CardHeaderContentTemplate">
2121
<Border Background="{ThemeResource SurfaceInverseBrush}" Opacity=".7">
2222
<TextBlock Text="{Binding }"
@@ -26,9 +26,7 @@ xmlns:gm="using:DevTKSS.Uno.Samples.MvuxGallery.GeneralModels"
2626
TextWrapping="WrapWholeWords"/>
2727
</Border>
2828
</DataTemplate>
29-
<!--#endregion-->
30-
31-
<!--#region Card SubHeaderContent Template-->
29+
3230
<DataTemplate x:DataType="gm:GalleryImageModel" x:Key="CardSubHeaderContentTemplate">
3331
<Border Background="{ThemeResource SurfaceInverseBrush}" Opacity=".7">
3432
<TextBlock Text="{Binding }"
@@ -38,13 +36,9 @@ xmlns:gm="using:DevTKSS.Uno.Samples.MvuxGallery.GeneralModels"
3836
TextWrapping="WrapWholeWords"/>
3937
</Border>
4038
</DataTemplate>
41-
<!--#endregion-->
42-
43-
<!--#region Card MediaContentTemplate-->
39+
4440
<DataTemplate x:Key="CardMediaContentTemplate" x:DataType="gm:GalleryImageModel">
4541
<Image Source="{Binding }"
4642
Stretch="Uniform"
4743
Width="{utu:AncestorBinding AncestorType=utu:Card, Path=Width}" />
4844
</DataTemplate>
49-
50-
<!--#endregion-->
Lines changed: 81 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,100 @@
1-
namespace DevTKSS.Uno.Samples.MvuxGallery.Models.CodeSamples;
21

32
public record CodeSampleOptionsConfiguration
43
{
5-
public Dictionary<string, CodeSampleOption> Samples { get; init; } = new();
4+
public CodeSampleOption[] Samples { get; init; } = Array.Empty<CodeSampleOption>();
65
}
76

87
public record CodeSampleOption
98
{
9+
public string SampleID { get; init; } = string.Empty;
1010
public string Description { get; init; } = string.Empty;
1111
public string FilePath { get; init; } = string.Empty;
12-
public List<(int Start, int End)> LineRanges { get; init; } = [];
12+
public Lines[] LineRanges { get; init; } = [];
1313
}
14-
15-
namespace DevTKSS.Uno.Samples.MvuxGallery.Models.CodeSamples;
16-
17-
[JsonSerializable(typeof(CodeSampleOptionsConfiguration))]
18-
public partial class CodeSampleOptionsConfigurationContext : JsonSerializerContext
14+
public record Lines
1915
{
16+
public int Start { get; init; }
17+
public int End { get; init; }
2018
}
2119

20+
[JsonSerializable(typeof(CodeSampleOptionsConfiguration))]
2221
[JsonSerializable(typeof(CodeSampleOption))]
22+
[JsonSerializable(typeof(ListboardSampleOptions))]
23+
[JsonSerializable(typeof(MainSampleOptions))]
24+
[JsonSerializable(typeof(CounterSampleOptions))]
25+
[JsonSerializable(typeof(SimpleCardsSampleOptions))]
2326
public partial class CodeSampleOptionContext : JsonSerializerContext
2427
{
2528
}
2629

30+
public record ListboardSampleOptions : CodeSampleOptionsConfiguration
2731
{
28-
"ListboardSampleConfiguration": {
29-
"FeedView + ListView XAML": {
30-
"Description": "",
31-
"FilePath": "Assets/Samples/FeedView-ListView-Sample.xaml.txt",
32-
"LineRanges": []
33-
},
34-
"Use Service to load Data from Storage": {
35-
"Description": "This sample shows how to use a service to load data from storage. The service is registered in the DI container and can be injected into the view model.",
36-
"FilePath": "Assets/Samples/Listboard-SampleViaService.cs.txt",
37-
"LineRanges": []
38-
},
39-
"DI Storage handling Service": {
40-
"FilePath": "Assets/Samples/Listboard-StorageService.cs.txt",
41-
"LineRanges": []
42-
},
43-
"Settings DataModel Definition": {
44-
"Description": "While DataModels used to load Data via IOptions can not be fully immutable, using init instead of set allows us to set the properties only once, when the object is created.",
45-
"FilePath": "Assets/Samples/CodeSample-Definition.cs.txt",
46-
"LineRanges": [ 1, 13 ]
47-
},
48-
"Settings DataModel JsonContext": {
49-
"Description": "Define the JsonSerializerContext to load the code sample options configuration from the appsettings.json file.",
50-
"FilePath": "Assets/Samples/CodeSample-Definition.cs.txt",
51-
"LineRanges": [ 15, 25 ]
52-
},
53-
"Settings Data in appsettings": {
54-
"Description": "Settings to find storage Data, can easily be stored in appsettings.json, since we can update them via IWritableOptions.",
55-
"FilePath": "Assets/Samples/CodeSample-Definition.cs.txt",
56-
"LineRanges": []
57-
},
58-
"XAML DataTemplate": {
59-
"Description": "The DataTemplate is used to define how the data is displayed in the ListView. The DataTemplate is showing a Card Control from Uno.Toolkit.UI and is definied in a ResourcesDictionary loaded via MergedResourcesDictionaries.",
60-
"FilePath": "Assets/Samples/Card-GalleryImage.DataTemplate.xaml.txt",
61-
"LineRanges": []
62-
},
63-
},
32+
}
33+
34+
"ListboardSampleOptions": {
35+
"Samples": [
36+
{
37+
"SampleID": "FeedView + ListView XAML",
38+
"Description": "This sample shows how to use the FeedView control from Uno.Extensions.Reactive.UI. The FeedView usually is beeing used to let you bind to a Feed, ListFeed, State or ListState, which allows to Request a Refresh of the underlieing data. In this sample, we are using a ListView which then actually shows the Data, Binding to the depending Properties of the Records Definition. By default, if you dont explicitly tell the FeedView otherwhise, e.g. by <LoadingTemplate>, the DataTemplate defining the ListView, will be the <ValueTemplate> after fetching the Data.",
39+
"FilePath": "Assets/Samples/FeedView-ListView-Sample.xaml.txt",
40+
"LineRanges": []
41+
},
42+
{
43+
"SampleID": "Load Data from Storage \n (by Service)",
44+
"Description": "This sample shows how to use a service to load data from storage. The service is registered in the DI container and can be injected into the view model.",
45+
"FilePath": "Assets/Samples/Listboard-SampleViaService.cs.txt",
46+
"LineRanges": []
47+
},
48+
{
49+
"SampleID": "DI Storage handling Service",
50+
"Description": "",
51+
"FilePath": "Assets/Samples/CodeSampleService.cs.txt",
52+
"LineRanges": []
53+
},
54+
{
55+
"SampleID": "Settings DataModel Definition",
56+
"Description": "While DataModels used to load Data via IOptions can not be fully immutable, using init instead of set allows us to set the properties only once, when the object is created.",
57+
"FilePath": "Assets/Samples/CodeSample-Definition.cs.txt",
58+
"LineRanges": [
59+
{
60+
"Start": 1,
61+
"End": 13
62+
}
63+
]
64+
},
65+
{
66+
"SampleID": "Settings DataModel JsonContext",
67+
"Description": "Define the JsonSerializerContext to load the code sample options configuration from the appsettings.json file.",
68+
"FilePath": "Assets/Samples/CodeSample-Definition.cs.txt",
69+
"LineRanges": [
70+
{
71+
"Start": 15,
72+
"End": 32
73+
}
74+
]
75+
},
76+
{
77+
"SampleID": "Settings Data in appsettings",
78+
"Description": "Settings to find storage Data, can easily be stored in appsettings.json, since we can update them via IWritableOptions.",
79+
"FilePath": "Assets/Samples/CodeSample-Definition.cs.txt",
80+
"LineRanges": [
81+
{
82+
"Start": 34,
83+
"End": 70
84+
}
85+
]
86+
},
87+
{
88+
"SampleID": "XAML DataTemplate",
89+
"Description": "The DataTemplate is used to define how the data is displayed in the ListView. The DataTemplate is showing a Card Control from Uno.Toolkit.UI and is definied in a ResourcesDictionary loaded via MergedResourcesDictionaries.",
90+
"FilePath": "Assets/Samples/Card-GalleryImage.DataTemplate.xaml.txt",
91+
"LineRanges": []
92+
},
93+
{
94+
"SampleID": "Get GalleryImages via \n FeedList in Model",
95+
"Description": "This sample shows how to use a model with properties that are bound to the view. The model is a simple class with properties that are used in the view.",
96+
"FilePath": "Assets/Samples/ModelBinding-Sample.cs.txt",
97+
"LineRanges": []
98+
}
99+
]
100+
}
Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,63 @@
1-
namespace DevTKSS.Uno.Samples.MvuxGallery.Models.CodeSamples;
1+
using Uno.Extensions.Storage
22

3-
public record CodeSampleService : ICodeSampleService
3+
public partial record CodeSampleService<SampleOptions> : ICodeSampleService<SampleOptions>
4+
where SampleOptions : CodeSampleOptionsConfiguration
45
{
6+
private readonly IStorage _storage;
7+
private readonly ILogger<CodeSampleService<SampleOptions>> _logger;
8+
private readonly SampleOptions _options;
9+
510
public CodeSampleService(
6-
IOptions<CodeSampleOptionsConfiguration> options,
7-
ILogger<CodeSampleService> logger,
8-
IStorage storage)
11+
IOptions<SampleOptions> options,
12+
ILogger<CodeSampleService<SampleOptions>> logger,
13+
IStorage storage)
914
{
1015
_options = options.Value;
1116
_logger = logger;
1217
_storage = storage;
18+
if (_logger.IsEnabled(LogLevel.Trace))
19+
{
20+
// Log LineRanges for each sample option
21+
foreach (var sample in _options.Samples)
22+
{
23+
_logger.LogTrace("SampleID: {sampleID},\nDescription: {description},\nFilePath: {filePath},\nLineRanges: {lineRanges}",
24+
sample.SampleID,
25+
sample.Description,
26+
sample.FilePath,
27+
sample.LineRanges);
28+
29+
}
30+
}
1331
}
1432

15-
private readonly IStorage _storage;
16-
private readonly ILogger<CodeSampleService> _logger;
17-
private readonly CodeSampleOptionsConfiguration _options;
18-
19-
/// <summary>
20-
/// Get a static Collection of Values for <see cref="CodeSampleOptions"/>
21-
/// </summary>
22-
/// <param name="ct">
23-
/// A CancellationToken to make it compileable
24-
/// <remarks>
25-
/// since `ListFeed.Async` requires a CancellationToken even if Uno Documentation remarks this parameter to be optional.<br/>
26-
/// <see href="https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs0411?f1url=%3FappId%3Droslyn%26k%3Dk(CS0411)">CS0411</see><br/>
27-
/// <br/>
28-
/// adding then the type string or IImmutableList<string> to the ListFeed like `ListFeed<string>.Async(...)`,
29-
/// or to the Async Extension itself like `ListFeed.Async<IImutableList<string>` results in a type mismatch.<br/>
30-
/// <see href="https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs1503?f1url=%3FappId%3Droslyn%26k%3Dk(CS1503)">CS1503</see>
31-
/// </remarks>
32-
/// <returns>An awaitable <see cref="ValueTask{TResult}"/> providing a <see cref="ImmutableList{T}"/> of <see langword="string"/> with the Sample Names to select from</returns>
33+
34+
3335
public async ValueTask<IImmutableList<string>> GetCodeSampleOptionsAsync(CancellationToken ct = default)
3436
{
3537
await Task.Delay(1);
36-
return _options.Samples.Keys.ToImmutableList();
38+
var sampleOptions = _options.Samples.Select(sample => sample.SampleID).ToImmutableList();
39+
_logger.LogInformation("Options:\n{options}", sampleOptions.JoinBy("," + Environment.NewLine));
40+
return sampleOptions;
3741
}
3842

39-
public async ValueTask<string> GetCodeSampleAsync(string sampleID, CancellationToken ct = default)
43+
public async ValueTask<string> GetCodeSampleAsync(string? sampleID, CancellationToken ct = default)
4044
{
41-
if (_options.Samples.TryGetValue(sampleID, out CodeSampleOption? sampleOption))
45+
if (_options.Samples.FirstOrDefault(sample => sample.SampleID == sampleID) is CodeSampleOption sampleOption)
4246
{
43-
return await _storage.ReadLinesFromPackageFile(sampleOption.FilePath, sampleOption.LineRanges);
47+
if(_logger.IsEnabled(LogLevel.Trace))
48+
{
49+
_logger.LogTrace("SampleID: {sampleID},\nDescription: {description},\nFilePath: {filePath},\nLineRanges: {lineRanges}",
50+
sampleOption.SampleID,
51+
sampleOption.Description,
52+
sampleOption.FilePath,
53+
sampleOption.LineRanges);
54+
}
55+
56+
return await _storage.ReadLinesFromPackageFile(sampleOption.FilePath,sampleOption.LineRanges.Select(lr => (lr.Start, lr.End)));
4457
}
58+
4559

4660
_logger.LogWarning("Code sample with ID {sampleID} not found", sampleID);
4761
return string.Empty;
4862
}
49-
5063
}

src/DevTKSS.Uno.Samples.MvuxGallery/Assets/Samples/CounterModel.cs.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,4 @@ public partial record CounterModel
1414
public ValueTask IncrementCounter()
1515
=> Countable.UpdateAsync(c => c?.Increment());
1616

17-
public async Task ReloadCounterTitle()
18-
{
19-
await CounterTitle.SetAsync(_stringLocalizer["CounterTitle"]);
20-
}
2117
}

0 commit comments

Comments
 (0)