Skip to content

Commit 4f3bf64

Browse files
committed
chore: Enhance code sample management and UI components
- Added logging in `CodeSampleService` and introduced `SwitchCodeSampleAsync` for switching between code samples. - Cleaned up `DashboardModel.cs` by removing unnecessary using directives. - Enhanced `ListboardModel.cs` with a logger and streamlined property syntax for `CodeSampleOptions` and `CurrentCodeSample`. - Removed redundant `SwitchCodeSampleAsync` method from `ListboardModel` as its functionality is now in `ICodeSampleService`. - Updated `ListboardPage.xaml` layout with a new `Expander` and `TabBar` for improved UI representation of code sample options.
2 parents 428fc25 + a982adc commit 4f3bf64

File tree

6 files changed

+64
-62
lines changed

6 files changed

+64
-62
lines changed

src/DevTKSS.Uno.Samples.MvuxGallery/Models/CodeSamples/CodeSampleOptionsConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace DevTKSS.Uno.Samples.MvuxGallery.Models.CodeSamples;
2-
public record CodeSampleOptionsConfiguration
2+
3+
public record CodeSampleOptionsConfiguration()
34
{
45
public Dictionary<string, CodeSampleOption> Samples { get; init; } = new();
56
}

src/DevTKSS.Uno.Samples.MvuxGallery/Models/CodeSamples/CodeSampleService.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,24 @@ public CodeSampleService(
3434
public async ValueTask<IImmutableList<string>> GetCodeSampleOptionsAsync(CancellationToken ct = default)
3535
{
3636
await Task.Delay(1);
37+
_logger.LogInformation("Options: {options}", _options.Samples.Keys.ToString());
3738
return _options.Samples.Keys.ToImmutableList();
3839
}
3940

40-
public async ValueTask<string> GetCodeSampleAsync(string sampleID, CancellationToken ct = default)
41+
public async ValueTask<string> GetCodeSampleAsync(string? sampleID, CancellationToken ct = default)
4142
{
42-
if (_options.Samples.TryGetValue(sampleID, out CodeSampleOption? sampleOption))
43+
if (sampleID is not null && _options.Samples.TryGetValue(sampleID, out CodeSampleOption? sampleOption))
4344
{
4445
return await _storage.ReadLinesFromPackageFile(sampleOption.FilePath, sampleOption.LineRanges);
4546
}
4647

4748
_logger.LogWarning("Code sample with ID {sampleID} not found", sampleID);
4849
return string.Empty;
4950
}
50-
51+
public async ValueTask<string> SwitchCodeSampleAsync(string? selectedSampleOption, CancellationToken ct = default)
52+
{
53+
string sample = await GetCodeSampleAsync(selectedSampleOption,ct);
54+
_logger.LogInformation("Switched to code sample {sample}", selectedSampleOption);
55+
return sample;
56+
}
5157
}

src/DevTKSS.Uno.Samples.MvuxGallery/Models/CodeSamples/ICodeSampleService.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,26 @@ public interface ICodeSampleService
66
/// <summary>
77
/// Get the content of a specific code sample asynchronously.
88
/// </summary>
9-
/// <param name="callerID">The identifier of the caller.</param>
109
/// <param name="sampleID">The identifier of the sample.</param>
10+
/// <param name="ct">A cancellation token for the operation.</param>
1111
/// <returns>The content of the code sample.</returns>
1212
ValueTask<string> GetCodeSampleAsync(string sampleID,CancellationToken ct = default);
1313

1414
/// <summary>
1515
/// Get a static collection of values for code sample options asynchronously.
1616
/// </summary>
17-
/// <param name="owner">The owner model to filter options.</param>
1817
/// <param name="ct">A cancellation token for the operation.</param>
1918
/// <returns>A list of available code sample options.</returns>
2019
ValueTask<IImmutableList<string>> GetCodeSampleOptionsAsync(CancellationToken ct = default);
2120

21+
22+
/// <summary>
23+
/// Switches the current code sample to the specified option asynchronously.
24+
/// </summary>
25+
/// <param name="selectedSampleOption">The selected code sample option to switch to.</param>
26+
/// <param name="ct">A cancellation token for the operation.</param>
27+
/// <returns>
28+
/// A <see cref="ValueTask{TResult}"/> representing the asynchronous operation, with the content of the switched code sample as a string.
29+
/// </returns>
30+
ValueTask<string> SwitchCodeSampleAsync(string? selectedSampleOption, CancellationToken ct = default);
2231
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using DevTKSS.Uno.Samples.MvuxGallery.Models.GalleryImages;
21
using Uno.Extensions.Reactive.Commands;
3-
using Uno.Extensions.Reactive.Config;
42

53
namespace DevTKSS.Uno.Samples.MvuxGallery.Presentation.ViewModels;
64

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

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ namespace DevTKSS.Uno.Samples.MvuxGallery.Presentation.ViewModels;
77
/// </summary>
88
public partial record ListboardModel
99
{
10+
1011
#region Services
12+
private readonly ILogger _logger;
1113
/// <summary>
1214
/// Service for retrieving localized strings.
1315
/// </summary>
@@ -33,8 +35,10 @@ public partial record ListboardModel
3335
public ListboardModel(
3436
IStringLocalizer stringLocalizer,
3537
IGalleryImageService galleryImageService,
36-
IServiceProvider serviceProvider)
38+
IServiceProvider serviceProvider,
39+
ILogger<ListboardModel> logger)
3740
{
41+
_logger = logger;
3842
this._stringLocalizer = stringLocalizer;
3943
this._galleryImageService = galleryImageService;
4044
this._codeSampleService = serviceProvider.GetRequiredNamedService<ICodeSampleService>("ListboardSampleService");
@@ -57,8 +61,7 @@ public ListboardModel(
5761
/// <remarks>
5862
/// The ListFeed is generic (`ListFeed<string>.Async`) and the service function returns a collection of strings.
5963
/// </remarks>
60-
public IListFeed<string> CodeSampleOptions => ListFeed<string>
61-
.Async(_codeSampleService.GetCodeSampleOptionsAsync)
64+
public IListFeed<string> CodeSampleOptions => ListFeed.Async(_codeSampleService.GetCodeSampleOptionsAsync)
6265
.Selection(SelectedOption);
6366

6467
/// <summary>
@@ -67,36 +70,13 @@ public ListboardModel(
6770
/// <remarks>
6871
/// Uses <see cref="string.Empty"/> as the default value to avoid null checks in the XAML.
6972
/// </remarks>
70-
public IState<string> SelectedOption => State<string>
71-
.Value(this, () => string.Empty)
72-
.ForEach(SwitchCodeSampleAsync);
73+
public IState<string> SelectedOption => State<string>.Value(this, () => string.Empty);
7374

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

80-
/// <summary>
81-
/// Switches the current code sample based on the selected option.
82-
/// </summary>
83-
/// <param name="choice">The selected code sample option.</param>
84-
/// <param name="ct">A cancellation token for the operation.</param>
85-
/// <returns>A task representing the asynchronous operation.</returns>
86-
/// <example>
87-
/// <code>
88-
/// public async ValueTask SwitchCodeSampleAsync(string? selectedSampleOption, CancellationToken ct = default)
89-
/// {
90-
/// string sample = await _codeSampleService.GetCodeSampleAsync(selectedSampleOption ?? string.Empty);
91-
/// await CurrentCodeSample.SetAsync(sample, ct);
92-
/// }
93-
/// </code>
94-
/// </example>
95-
public async ValueTask SwitchCodeSampleAsync([FeedParameter(nameof(SelectedOption))] string? choice, CancellationToken ct = default)
96-
{
97-
string sample = await _codeSampleService.GetCodeSampleAsync(choice ?? string.Empty);
98-
await CurrentCodeSample.SetAsync(sample, ct);
99-
}
10080
#endregion
10181

10282
#region ViewHeaderContent

src/DevTKSS.Uno.Samples.MvuxGallery/Presentation/Views/ListboardPage.xaml

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<RowDefinition Height="Auto"/>
2020
<RowDefinition Height="*"/>
2121
<RowDefinition Height="Auto" />
22+
<RowDefinition Height="Auto" />
2223
</Grid.RowDefinitions>
2324

2425
<TextBlock Grid.Row="0"
@@ -49,33 +50,40 @@
4950
</DataTemplate>
5051
</mvux:FeedView.ValueTemplate>
5152
</mvux:FeedView>
52-
<Expander Grid.Row="2"
53+
<!--<utu:TabBar Grid.Row="2"
54+
VerticalAlignment="Bottom"
55+
ItemsSource="{Binding CodeSampleOptions}"
56+
SelectedItem="{Binding SelectedOption, Mode=TwoWay}">
57+
<utu:TabBar.ItemTemplate>
58+
<DataTemplate>
59+
<utu:TabBarItem Content="{Binding}"
60+
Command="{Binding SwitchCodeSampleAsync}"
61+
CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content}"
62+
Style="{StaticResource MaterialTopTabBarItemStyle}"
63+
Icon="Home" />
64+
</DataTemplate>
65+
</utu:TabBar.ItemTemplate>
66+
</utu:TabBar>-->
67+
<Expander Grid.Row="3"
5368
ExpandDirection="Down"
54-
MaxHeight="300">
55-
56-
<Expander.Header>
57-
<utu:TabBar VerticalAlignment="Bottom"
58-
ItemsSource="{Binding CodeSampleOptions}"
59-
SelectedItem="{Binding SelectedSampleOption, Mode=TwoWay}">
60-
<utu:TabBar.ItemTemplate>
61-
<DataTemplate>
62-
<utu:TabBarItem Content="{Binding}"
63-
Command="{Binding SwitchCodeSampleAsync}"
64-
CommandParameter="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content}"
65-
Style="{StaticResource MaterialTopTabBarItemStyle}"
66-
Icon="Home" />
67-
</DataTemplate>
68-
</utu:TabBar.ItemTemplate>
69-
</utu:TabBar>
70-
</Expander.Header>
71-
<Expander.Content>
72-
<ScrollViewer>
73-
<TextBlock x:Name="CodeSampleBlock"
74-
Text="{Binding CurrentCodeSample}"
75-
TextWrapping="WrapWholeWords" />
76-
</ScrollViewer>
77-
</Expander.Content>
78-
69+
MaxHeight="400"
70+
Header="{Binding CodeSampleOptions,Mode=TwoWay}"
71+
Content="{Binding CurrentCodeSample, Mode=TwoWay}">
72+
<Expander.ContentTemplate>
73+
<DataTemplate>
74+
<ScrollViewer>
75+
<TextBlock Text="{Binding }"
76+
TextWrapping="WrapWholeWords" />
77+
</ScrollViewer>
78+
</DataTemplate>
79+
</Expander.ContentTemplate>
80+
<Expander.HeaderTemplate>
81+
<DataTemplate>
82+
<utu:TabBar SelectedIndex="1" ItemsSource="{Binding }"
83+
Style="{StaticResource MaterialTopTabBarItemStyle}" />
84+
</DataTemplate>
85+
</Expander.HeaderTemplate>
7986
</Expander>
87+
8088
</Grid>
8189
</Page>

0 commit comments

Comments
 (0)