Skip to content

Commit b930725

Browse files
committed
feat(IRouteNotifyer): Add IRouteNotifyer to MainModel for dynamic NavigationView Header
1 parent db72dcb commit b930725

File tree

2 files changed

+38
-52
lines changed

2 files changed

+38
-52
lines changed

src/DevTKSS.Uno.Samples.MvuxGallery/Assets/Samples/Model-with-IRouteNotifyer.txt

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,44 @@ using Uno.Extensions.Reactive.Commands;
33
namespace DevTKSS.Uno.Samples.MvuxGallery.Presentation.ViewModels;
44
public partial record MainModel
55
{
6-
private readonly INavigator _navigator;
7-
private readonly ILocalizationService _localizationService;
86
private readonly IStringLocalizer _stringLocalizer;
97
private readonly IRouteNotifier _routeNotifier;
108
private readonly ILogger _logger;
9+
1110
public MainModel(
12-
ILocalizationService localizationService,
1311
IStringLocalizer stringLocalizer,
14-
INavigator navigator,
1512
IRouteNotifier routeNotifier,
1613
ILogger<MainModel> logger)
1714
{
1815
_logger = logger;
1916
_routeNotifier = routeNotifier;
20-
_navigator = navigator;
21-
_localizationService = localizationService;
2217
_stringLocalizer = stringLocalizer;
2318
_routeNotifier.RouteChanged += routeNotifier_RouteChanged;
24-
2519
}
2620

2721
public IState<string> CurrentNotifierRoute => State<string>.Value(this, () => string.Empty)
2822
.ForEach(UpdateCurrentHeaderAsync);
2923
private async void routeNotifier_RouteChanged(object? sender, RouteChangedEventArgs e)
30-
{
24+
{
3125
var RouteName = e.Navigator?.Route?.ToString() ?? string.Empty;
3226
_logger.LogDebug("Route was: {RouteAsString}", RouteName);
3327
await this.CurrentNotifierRoute.SetAsync(RouteName);
3428
}
3529

3630
public IState<string> CurrentHeader => State<string>.Value(this, () => string.Empty);
3731

38-
public ValueTask UpdateCurrentHeaderAsync([FeedParameter(nameof(CurrentNotifierRoute))] string? currentNotifierRoute, CancellationToken ct = default)
32+
public async ValueTask UpdateCurrentHeaderAsync([FeedParameter(nameof(CurrentNotifierRoute))] string? currentNotifierRoute, CancellationToken ct = default)
3933
{
4034
_logger.LogDebug("CurrentNotifierRoute was: {CurrentNotifierRoute}", currentNotifierRoute);
41-
string newHeader = _stringLocalizer["WelcomeGreeting"];
35+
4236
if (!string.IsNullOrWhiteSpace(currentNotifierRoute))
4337
{
44-
newHeader = _stringLocalizer[currentNotifierRoute + "Title"];
38+
await CurrentHeader.SetAsync(_stringLocalizer[currentNotifierRoute + "Title"]);
39+
}
40+
else
41+
{
42+
_logger.LogTrace("{CurrentNotifierRoute} was empty, setting default header using 'WelcomeGreeting' Key",nameof(currentNotifierRoute));
43+
await CurrentHeader.SetAsync(_stringLocalizer["WelcomeGreeting"]);
4544
}
46-
return this.CurrentHeader.SetAsync(newHeader);
4745
}
4846
}
49-
Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
1-
using System.Runtime.CompilerServices;
2-
using Uno.Extensions.Reactive.Commands;
3-
41
namespace DevTKSS.Uno.Samples.MvuxGallery.Presentation.ViewModels;
52
public partial record MainModel
63
{
7-
private readonly INavigator _navigator;
8-
private readonly ILocalizationService _localizationService;
94
private readonly IStringLocalizer _stringLocalizer;
105
private readonly IRouteNotifier _routeNotifier;
116
private readonly ILogger _logger;
12-
private readonly ICodeSampleService<MainCodeSampleOptions> _sampleService;
7+
private readonly ICodeSampleService<MainSampleOptions> _sampleService;
138

149
public MainModel(
15-
ILocalizationService localizationService,
1610
IStringLocalizer stringLocalizer,
17-
INavigator navigator,
1811
IRouteNotifier routeNotifier,
1912
ILogger<MainModel> logger,
20-
ICodeSampleService<MainCodeSampleOptions> sampleService)
13+
ICodeSampleService<MainSampleOptions> sampleService)
2114
{
2215
_logger = logger;
2316
_routeNotifier = routeNotifier;
24-
_navigator = navigator;
25-
_localizationService = localizationService;
2617
_stringLocalizer = stringLocalizer;
2718
_routeNotifier.RouteChanged += routeNotifier_RouteChanged;
2819
_sampleService = sampleService;
@@ -39,42 +30,40 @@ private async void routeNotifier_RouteChanged(object? sender, RouteChangedEventA
3930

4031
public IState<string> CurrentHeader => State<string>.Value(this, () => string.Empty);
4132

42-
public ValueTask UpdateCurrentHeaderAsync([FeedParameter(nameof(CurrentNotifierRoute))] string? currentNotifierRoute, CancellationToken ct = default)
33+
public async ValueTask UpdateCurrentHeaderAsync([FeedParameter(nameof(CurrentNotifierRoute))] string? currentNotifierRoute, CancellationToken ct = default)
4334
{
4435
_logger.LogDebug("CurrentNotifierRoute was: {CurrentNotifierRoute}", currentNotifierRoute);
45-
string newHeader = _stringLocalizer["WelcomeGreeting"];
36+
4637
if (!string.IsNullOrWhiteSpace(currentNotifierRoute))
4738
{
48-
newHeader = _stringLocalizer[currentNotifierRoute + "Title"];
39+
await CurrentHeader.SetAsync(_stringLocalizer[currentNotifierRoute + "Title"]);
40+
}
41+
else
42+
{
43+
_logger.LogTrace("{CurrentNotifierRoute} was empty, setting default header using 'WelcomeGreeting' Key",nameof(currentNotifierRoute));
44+
await CurrentHeader.SetAsync(_stringLocalizer["WelcomeGreeting"]);
4945
}
50-
return this.CurrentHeader.SetAsync(newHeader);
5146
}
5247

53-
public IListFeed<string> CodeSampleOptions =>
54-
ListFeed<string>.Async(GetCodeSampleOptionsAsync);
55-
/// <summary>
56-
/// Get a static Collection of Values for <see cref="CodeSampleOptions"/>
57-
/// </summary>
58-
/// <param name="ct">
59-
/// A CancellationToken to make it compileable
60-
/// </param>
61-
/// <returns>The available Values to select from.</returns>
62-
/// <remarks>
63-
/// This uses the explicit `ImmutableList.Create` function (non generic!)<br/>
64-
/// overload:<br/>
65-
/// `params ReadOnlySpan<string> items` this takes in an (e.g.) array of generic typed values
66-
/// </remarks>
67-
public async ValueTask<IImmutableList<string>> GetCodeSampleOptionsAsync(CancellationToken ct = default)
48+
public IListFeed<string> CodeSampleOptions => ListFeed<string>.Async(_sampleService.GetCodeSampleOptionsAsync)
49+
.Selection(SelectedOption);
50+
public IState<string> SelectedOption => State<string>.Value(this, () => string.Empty)
51+
.ForEach(SwitchCodeSampleAsync);
52+
public IState<string> CurrentCodeSample => State<string>.Value(this, () => string.Empty);
53+
54+
public async ValueTask SwitchCodeSampleAsync([FeedParameter(nameof(SelectedOption))] string? selectedOption, CancellationToken ct = default)
6855
{
69-
await Task.Delay(1, ct);
70-
return ImmutableList.Create(
71-
items:
72-
[
73-
"IRouteNotifyer Event",
74-
"Xaml Binding"
75-
]
76-
);
77-
}
56+
_logger.LogTrace("{method} called with parameter: {selectedOption}",nameof(SwitchCodeSampleAsync), selectedOption);
7857

58+
if (string.IsNullOrWhiteSpace(selectedOption))
59+
{
60+
var options = await CodeSampleOptions;
61+
selectedOption = options.FirstOrDefault(string.Empty);
62+
}
63+
64+
var sample = await _sampleService.GetCodeSampleAsync(selectedOption, ct);
65+
66+
await CurrentCodeSample.SetAsync(sample, ct);
67+
}
7968
}
8069

0 commit comments

Comments
 (0)