Skip to content

Commit 3146bff

Browse files
committed
chore(SampleService): Update and unify States in Models
1 parent eaa7214 commit 3146bff

File tree

4 files changed

+60
-18
lines changed

4 files changed

+60
-18
lines changed

src/DevTKSS.Uno.Samples.MvuxGallery/Presentation/ViewModels/CounterModel.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
namespace DevTKSS.Uno.Samples.MvuxGallery.Presentation.ViewModels;
23

34
public partial record CounterModel
@@ -21,8 +22,25 @@ public CounterModel(
2122
public ValueTask IncrementCounter()
2223
=> Countable.UpdateAsync(c => c?.Increment());
2324

24-
public async Task ReloadCounterTitle()
25+
26+
public IListFeed<string> CodeSampleOptions => ListFeed<string>.Async(_sampleService.GetCodeSampleOptionsAsync)
27+
.Selection(SelectedOption);
28+
public IState<string> SelectedOption => State<string>.Value(this, () => string.Empty)
29+
.ForEach(SwitchCodeSampleAsync);
30+
public IState<string> CurrentCodeSample => State<string>.Value(this, () => string.Empty);
31+
public async ValueTask SwitchCodeSampleAsync([FeedParameter(nameof(SelectedOption))] string? selectedOption, CancellationToken ct)
2532
{
26-
await CounterTitle.SetAsync(_stringLocalizer["CounterTitle"]);
33+
_logger.LogTrace("{methodName} called with selectedOption: '{SelectedOption}'", nameof(SwitchCodeSampleAsync), selectedOption);
34+
35+
if (string.IsNullOrWhiteSpace(selectedOption))
36+
{
37+
_logger.LogTrace("selectedOption is null or whitespace. Attempting to get default from CodeSampleOptions.");
38+
var options = await CodeSampleOptions;
39+
selectedOption = options.FirstOrDefault(string.Empty);
40+
_logger.LogTrace("selectedOption updated to: '{SelectedOption}' after fetching options.", selectedOption);
41+
}
42+
string codeSample = await _sampleService.GetCodeSampleAsync(selectedOption, ct);
43+
_logger.LogTrace("Loaded code sample for option '{SelectedOption}': {CodeSample}", selectedOption, codeSample);
44+
await CurrentCodeSample.SetAsync(codeSample, ct);
2745
}
2846
}

src/DevTKSS.Uno.Samples.MvuxGallery/Presentation/ViewModels/DashboardModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public DashboardModel(
3737
/// </remarks>
3838
public IState<HeaderContent> ViewHeaderContent => State<HeaderContent>.Value(this,
3939
() => new HeaderContent(ImageLocation: "Assets/Images/styled_logo.png",
40-
Caption: _stringLocalizer["GridViewTitle"]));
40+
Caption: _stringLocalizer["GridViewTitle"]));
4141
#endregion
4242

4343
#region CodeSample import directly in the Model
@@ -104,16 +104,16 @@ public static async ValueTask<IImmutableList<string>> GetCodeSampleOptionsAsync(
104104
/// <summary>
105105
/// Switches the <see cref="CurrentCodeSample"/> to the selected item in <see cref="CodeSampleOptions"/>
106106
/// </summary>
107-
/// <param name="choice">The selected item, provide-able via CommandParameter, prefer to let it get it via the <see cref="FeedParameterAttribute"/></param>
107+
/// <param name="selectedOption">The selected item, provide-able via CommandParameter, prefer to let it get it via the <see cref="FeedParameterAttribute"/></param>
108108
/// <param name="ct">A cancellation token for the operation to update the <see cref="CurrentCodeSample"/></param>
109109
/// <returns>A ValueTask to be awaited</returns>
110110
/// <remarks>
111111
/// Uses switch expression to select the correct code sample which provides better performance and less boilerplate code.
112112
/// </remarks>
113-
public async ValueTask SwitchCodeSampleAsync([FeedParameter(nameof(SelectedOption))] string? choice, CancellationToken ct = default)
113+
public async ValueTask SwitchCodeSampleAsync([FeedParameter(nameof(SelectedOption))] string? selectedOption, CancellationToken ct = default)
114114
{
115-
_logger.LogTrace("SwitchCodeSampleAsync called with parameter: {choice}", choice);
116-
await CurrentCodeSample.SetAsync(choice switch
115+
_logger.LogTrace("SwitchCodeSampleAsync called with parameter: {selectedOption}", selectedOption);
116+
await CurrentCodeSample.SetAsync(selectedOption switch
117117
{
118118
"C# in Model" => await _storage.ReadPackageFileAsync("Assets/Samples/ModelBinding-Sample.cs.txt"),
119119
"DI Service Resw" => await _storage.ReadPackageFileAsync("Assets/Samples/GalleryImageService-resw.cs.txt"),

src/DevTKSS.Uno.Samples.MvuxGallery/Presentation/ViewModels/ListboardModel.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public ListboardModel(
4646
/// <summary>
4747
/// Gets a feed of gallery images to be displayed.
4848
/// </summary>
49-
public IListFeed<GalleryImageModel> GalleryImages => ListFeed.Async(this._galleryImageService.GetGalleryImagesWithoutReswAsync);
49+
public IListFeed<GalleryImageModel> GalleryImages => ListFeed.Async(_galleryImageService.GetGalleryImagesWithoutReswAsync);
5050

5151
/// <summary>
5252
/// Gets the title of the Listboard.
@@ -61,21 +61,37 @@ public ListboardModel(
6161
/// The ListFeed is generic (`ListFeed<string>.Async`) and the service function returns a collection of strings.
6262
/// </remarks>
6363
public IListFeed<string> CodeSampleOptions => ListFeed.Async(_codeSampleService.GetCodeSampleOptionsAsync)
64-
.Selection(SelectedOption);
64+
.Selection(SelectedOption);
6565

6666
/// <summary>
6767
/// Represents the selected code sample option.
6868
/// </summary>
6969
/// <remarks>
7070
/// Uses <see cref="string.Empty"/> as the default value to avoid null checks in the XAML.
7171
/// </remarks>
72-
public IState<string> SelectedOption => State<string>.Value(this, () => string.Empty);
72+
public IState<string> SelectedOption => State<string>.Value(this, () => string.Empty)
73+
.ForEach(SwitchCodeSampleAsync);
7374

7475
/// <summary>
7576
/// Represents the content of the currently selected code sample.
7677
/// </summary>
77-
public IFeed<string> CurrentCodeSample => SelectedOption.SelectAsync((sample, ct)=> _codeSampleService.GetCodeSampleAsync(sample, ct));
78+
public IState<string> CurrentCodeSample => State<string>.Value(this, () => string.Empty);
7879

80+
public async ValueTask SwitchCodeSampleAsync([FeedParameter(nameof(SelectedOption))] string? selectedOption, CancellationToken ct = default)
81+
{
82+
_logger.LogTrace("{methodName} called with selectedOption: '{SelectedOption}'",nameof(SwitchCodeSampleAsync), selectedOption);
83+
84+
if (string.IsNullOrWhiteSpace(selectedOption))
85+
{
86+
_logger.LogTrace("selectedOption is null or whitespace. Attempting to get default from CodeSampleOptions.");
87+
var options = await CodeSampleOptions;
88+
selectedOption = options.FirstOrDefault(string.Empty);
89+
_logger.LogTrace("selectedOption updated to: '{SelectedOption}' after fetching options.", selectedOption);
90+
}
91+
string codeSample = await _codeSampleService.GetCodeSampleAsync(selectedOption, ct);
92+
_logger.LogTrace("Loaded code sample for option '{SelectedOption}': {CodeSample}", selectedOption, codeSample);
93+
await CurrentCodeSample.SetAsync(codeSample, ct);
94+
}
7995
#endregion
8096

8197
#region ViewHeaderContent

src/DevTKSS.Uno.Samples.MvuxGallery/Presentation/ViewModels/SimpleCardsModel.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,25 @@ ILogger<SimpleCardsModel> logger
1414

1515

1616
#region CodeSample import
17+
public IListFeed<string> CodeSampleOptions => ListFeed<string>.Async(_sampleService.GetCodeSampleOptionsAsync)
18+
.Selection(SelectedOption);
19+
public IState<string> SelectedOption => State<string>.Value(this, () => string.Empty)
20+
.ForEach(SwitchCodeSampleAsync);
1721
public IState<string> CurrentCodeSample => State<string>.Value(this, () => string.Empty);
18-
public async Task GetCodeSampleAsync(object? content)
22+
23+
public async ValueTask SwitchCodeSampleAsync(string? selectedOption,CancellationToken ct = default)
1924
{
20-
string? CodeSample = content?.ToString() switch
25+
_logger.LogTrace("{method} called with parameter: {selectedOption}", nameof(SwitchCodeSampleAsync), selectedOption);
26+
27+
if (string.IsNullOrWhiteSpace(selectedOption))
2128
{
22-
"XAML" => await _storage.ReadPackageFileAsync("Assets/Samples/SimpleCardSample.xaml.txt"),
23-
"C#" => await _storage.ReadPackageFileAsync("Assets/Samples/SimpleCardSample.cs.txt"),
24-
_ => string.Empty
25-
};
29+
var options = await CodeSampleOptions;
30+
selectedOption = options.FirstOrDefault(string.Empty);
31+
}
32+
33+
var sample = await _sampleService.GetCodeSampleAsync(selectedOption, ct);
2634

27-
await CurrentCodeSample.SetAsync(CodeSample);
35+
await CurrentCodeSample.SetAsync(sample, ct);
2836
}
2937
#endregion
3038
}

0 commit comments

Comments
 (0)