1- using System . Runtime . CompilerServices ;
2- using Uno . Extensions . Reactive . Commands ;
3-
41namespace DevTKSS . Uno . Samples . MvuxGallery . Presentation . ViewModels ;
52public 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