Skip to content

Commit

Permalink
Merge pull request #3121 from PrismLibrary/dev/ds/select-tab
Browse files Browse the repository at this point in the history
Update Select Tab
  • Loading branch information
dansiegel committed Apr 7, 2024
2 parents e8d6d1a + 64a41f6 commit d8b81f8
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 169 deletions.
8 changes: 4 additions & 4 deletions e2e/Maui/MauiModule/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.ObjectModel;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;

namespace MauiModule.ViewModels;
Expand Down Expand Up @@ -29,7 +29,7 @@ protected ViewModelBase(BaseServices baseServices)
SelectedDialog = AvailableDialogs.FirstOrDefault();
ShowDialog = new DelegateCommand(OnShowDialogCommand, () => !string.IsNullOrEmpty(SelectedDialog))
.ObservesProperty(() => SelectedDialog);
GoBack = new DelegateCommand<string>(OnGoBack);
GoBack = new DelegateCommand<string>(OnGoToBack);
}

public IEnumerable<string> AvailableDialogs { get; }
Expand Down Expand Up @@ -77,10 +77,10 @@ private void OnShowDialogCommand()
private void DialogCallback(IDialogResult result) =>
Messages.Add("Dialog Closed");

private void OnGoBack(string viewName)
private void OnGoToBack(string viewName)
{
Messages.Add($"On Go Back {viewName}");
_navigationService.GoBackAsync(viewName);
_navigationService.GoBackToAsync(viewName);
}

public void Initialize(INavigationParameters parameters)
Expand Down
4 changes: 3 additions & 1 deletion src/Maui/Prism.Maui/Mvvm/ViewModelLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ private static void OnViewModelLocatorBehaviorChanged(BindableObject bindable, o
propertyChanged: OnViewModelPropertyChanged);

public static readonly BindableProperty NavigationNameProperty =
BindableProperty.CreateAttached("NavigationName", typeof(string), typeof(ViewModelLocator), null);
BindableProperty.CreateAttached("NavigationName", typeof(string), typeof(ViewModelLocator), null, defaultValueCreator: CreateDefaultNavigationName);

private static object CreateDefaultNavigationName(BindableObject bindable) => bindable.GetType().Name;

public static string GetNavigationName(BindableObject bindable) =>
(string)bindable.GetValue(NavigationNameProperty);
Expand Down
6 changes: 3 additions & 3 deletions src/Maui/Prism.Maui/Navigation/Builder/INavigationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public interface INavigationBuilder
INavigationBuilder UseRelativeNavigation();

/// <summary>
/// Navigates back to the previous view model asynchronously.
/// Navigates back to the specified view asynchronously.
/// </summary>
/// <typeparam name="TViewModel">The type of the view model to navigate back to.</typeparam>
/// <param name="name">The name of the View to navigate back to.</param>
/// <returns>A task representing the asynchronous operation.</returns>
Task<INavigationResult> GoBackAsync<TViewModel>();
Task<INavigationResult> GoBackToAsync(string name);

/// <summary>
/// Navigates to the specified view model asynchronously.
Expand Down
10 changes: 5 additions & 5 deletions src/Maui/Prism.Maui/Navigation/Builder/NavigationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public INavigationBuilder AddParameter(string key, object value)
return this;
}

public async Task<INavigationResult> GoBackAsync<TViewModel>()
{
var name = NavigationBuilderExtensions.GetNavigationKey<TViewModel>(this);
return await _navigationService.GoBackAsync(name, _navigationParameters);
}
public Task<INavigationResult> GoBackToAsync<TViewModel>() =>
GoBackToAsync(NavigationBuilderExtensions.GetNavigationKey<TViewModel>(this));

public Task<INavigationResult> GoBackToAsync(string name) =>
_navigationService.GoBackToAsync(name, _navigationParameters);

public Task<INavigationResult> NavigateAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ public static ICreateTabBuilder AddNavigationPage(this ICreateTabBuilder builder
public static INavigationBuilder AddNavigationPage(this INavigationBuilder builder, bool useModalNavigation) =>
builder.AddNavigationPage(o => o.UseModalNavigation(useModalNavigation));

/// <summary>
/// Navigates back to the specified view model asynchronously.
/// </summary>
/// <typeparam name="TViewModel">The ViewModel to navigate to.</typeparam>
/// <param name="builder">The <see cref="INavigationBuilder"/>.</param>
/// <returns>A task representing the asynchronous operation.</returns>
public static Task<INavigationResult> GoBackToAsync<TViewModel>(this INavigationBuilder builder) =>
builder.GoBackToAsync(GetNavigationKey<TViewModel>(builder));

//public static INavigationBuilder AddSegment(this INavigationBuilder builder, string segmentName, params string[] createTabs)
//{
// return builder;
Expand Down
7 changes: 4 additions & 3 deletions src/Maui/Prism.Maui/Navigation/INavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface INavigationService
/// <param name="viewName">The name of the View to navigate back to</param>
/// <param name="parameters">The navigation parameters</param>
/// <returns>If <c>true</c> a go back operation was successful. If <c>false</c> the go back operation failed.</returns>
Task<INavigationResult> GoBackAsync(string viewName, INavigationParameters parameters);
Task<INavigationResult> GoBackToAsync(string viewName, INavigationParameters parameters);

/// <summary>
/// When navigating inside a NavigationPage: Pops all but the root Page off the navigation stack
Expand All @@ -41,10 +41,11 @@ public interface INavigationService
Task<INavigationResult> NavigateAsync(Uri uri, INavigationParameters parameters);

/// <summary>
/// Selects a Tab of the TabbedPage parent.
/// Selects a Tab of the TabbedPage parent and Navigates to a specified Uri
/// </summary>
/// <param name="name">The name of the tab to select</param>
/// <param name="uri">The Uri to navigate to</param>
/// <param name="parameters">The navigation parameters</param>
/// <returns><see cref="INavigationResult"/> indicating whether the request was successful or if there was an encountered <see cref="Exception"/>.</returns>
Task<INavigationResult> SelectTabAsync(string name, INavigationParameters parameters);
Task<INavigationResult> SelectTabAsync(string name, Uri uri, INavigationParameters parameters);
}
45 changes: 43 additions & 2 deletions src/Maui/Prism.Maui/Navigation/INavigationServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static Task<INavigationResult> GoBackAsync(this INavigationService naviga
/// <param name="navigationService">Service for handling navigation between views</param>
/// <param name="viewName">The name of the View to navigate back to</param>
/// <returns>If <c>true</c> a go back operation was successful. If <c>false</c> the go back operation failed.</returns>
public static Task<INavigationResult> GoBackAsync(this INavigationService navigationService, string viewName) => navigationService.GoBackAsync(viewName, new NavigationParameters());
public static Task<INavigationResult> GoBackToAsync(this INavigationService navigationService, string viewName) => navigationService.GoBackToAsync(viewName, new NavigationParameters());

/// <summary>
/// When navigating inside a NavigationPage: Pops all but the root Page off the navigation stack
Expand Down Expand Up @@ -120,10 +120,51 @@ public static void OnNavigationError(this Task<INavigationResult> navigationTask
});
}

/// <summary>
/// Selects a Tab of the TabbedPage parent.
/// </summary>
/// <param name="navigationService">Service for handling navigation between views</param>
/// <param name="name">The name of the tab to select</param>
/// <param name="parameters">The navigation parameters</param>
/// <returns><see cref="INavigationResult"/> indicating whether the request was successful or if there was an encountered <see cref="Exception"/>.</returns>
public static Task<INavigationResult> SelectTabAsync(this INavigationService navigationService, string name, INavigationParameters parameters) =>
navigationService.SelectTabAsync(name, null, parameters);

/// <summary>
/// Selects a Tab of the TabbedPage parent.
/// </summary>
/// <param name="navigationService">Service for handling navigation between views</param>
/// <param name="name">The name of the tab to select</param>
/// <param name="uri">The Uri to navigate to</param>
/// <returns><see cref="INavigationResult"/> indicating whether the request was successful or if there was an encountered <see cref="Exception"/>.</returns>
public static Task<INavigationResult> SelectTabAsync(this INavigationService navigationService, string name, Uri uri) =>
navigationService.SelectTabAsync(name, uri, new NavigationParameters());

/// <summary>
/// Selects a Tab of the TabbedPage parent.
/// </summary>
/// <param name="navigationService">Service for handling navigation between views</param>
/// <param name="name">The name of the tab to select</param>
/// <param name="uri">The Uri to navigate to</param>
/// <returns><see cref="INavigationResult"/> indicating whether the request was successful or if there was an encountered <see cref="Exception"/>.</returns>
public static Task<INavigationResult> SelectTabAsync(this INavigationService navigationService, string name, string uri) =>
navigationService.SelectTabAsync(name, UriParsingHelper.Parse(uri), new NavigationParameters());

/// <summary>
/// Selects a Tab of the TabbedPage parent.
/// </summary>
/// <param name="navigationService">Service for handling navigation between views</param>
/// <param name="name">The name of the tab to select</param>
/// <param name="uri">The Uri to navigate to</param>
/// <param name="parameters">The navigation parameters</param>
/// <returns><see cref="INavigationResult"/> indicating whether the request was successful or if there was an encountered <see cref="Exception"/>.</returns>
public static Task<INavigationResult> SelectTabAsync(this INavigationService navigationService, string name, string uri, INavigationParameters parameters) =>
navigationService.SelectTabAsync(name, UriParsingHelper.Parse(uri), parameters);

/// <summary>
/// Selects a tab programatically
/// </summary>
/// <param name="navigationService"></param>
/// <param name="navigationService">Service for handling navigation between views</param>
/// <param name="tabName">The name of the tab to select</param>
/// <returns>The <see cref="INavigationResult"/>.</returns>
public static Task<INavigationResult> SelectTabAsync(this INavigationService navigationService, string tabName) =>
Expand Down

0 comments on commit d8b81f8

Please sign in to comment.