Skip to content

Commit

Permalink
Select tab was unnecessarily async
Browse files Browse the repository at this point in the history
  • Loading branch information
BurkusCat committed Oct 7, 2023
1 parent 423b164 commit c31556c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
15 changes: 9 additions & 6 deletions samples/DemoApp/ViewModels/DemoTabsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace DemoApp.ViewModels;

/// <summary>
/// All three tabs (Alpha/Beta/Charlie), share this viewmodel.
/// </summary>
public partial class DemoTabsViewModel : BaseViewModel
{
#region Constructors
Expand Down Expand Up @@ -30,27 +33,27 @@ private async Task GoBack()
/// Navigate to Alpha tab page.
/// </summary>
[RelayCommand]
private async Task SwitchToAlphaTabPage()
private void SwitchToAlphaTabPage()
{
await navigationService.SelectTab<AlphaTabPage>();
navigationService.SelectTab<AlphaTabPage>();
}

/// <summary>
/// Navigate to Beta tab page.
/// </summary>
[RelayCommand]
private async Task SwitchToBetaTabPage()
private void SwitchToBetaTabPage()
{
await navigationService.SelectTab<BetaTabPage>();
navigationService.SelectTab<BetaTabPage>();
}

/// <summary>
/// Navigate to Charlie tab page.
/// </summary>
[RelayCommand]
private async Task SwitchToCharlieTabPage()
private void SwitchToCharlieTabPage()
{
await navigationService.SelectTab<CharlieTabPage>();
navigationService.SelectTab<CharlieTabPage>();
}

#endregion Commands
Expand Down
1 change: 1 addition & 0 deletions samples/DemoApp/ViewModels/LoginViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private async Task Login()
{ "username", Username },
};

// after we login, we replace the stack so the user can't go back to the Login page
await navigationService.ResetStackAndPush<HomePage>(navigationParameters);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Abstractions/INavigationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ Task ResetStackAndPush<T>(NavigationParameters navigationParameters)
/// When within a <see cref="TabbedPage"/>, use this method to select a tab.
/// </summary>
/// <typeparam name="T">Type of Page</typeparam>
/// <returns>A completed task</returns>
Task SelectTab<T>()
void SelectTab<T>()
where T : Page;
}
20 changes: 18 additions & 2 deletions src/Services/NavigationService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace Burkus.Mvvm.Maui;
namespace Burkus.Mvvm.Maui;

internal class NavigationService : INavigationService
{
#region Core navigation methods

public async Task Push<T>() where T : Page
{
var parameters = new NavigationParameters();
Expand Down Expand Up @@ -63,6 +65,10 @@ await HandleNavigation<Page>(async () =>
navigationParameters);
}

#endregion Core navigation methods

#region Advanced navigation methods

public async Task ReplaceTopPage<T>()
where T : Page
{
Expand Down Expand Up @@ -115,6 +121,10 @@ await HandleNavigation<Page>(async () =>
navigationParameters);
}

#endregion Advanced navigation methods

#region Internal implementation

private async Task HandleNavigation<T>(Func<Task> navigationAction, NavigationParameters navigationParameters)
where T : Page
{
Expand Down Expand Up @@ -143,7 +153,11 @@ private async Task HandleNavigation<T>(Func<Task> navigationAction, NavigationPa
}
}

public async Task SelectTab<T>() where T : Page
#endregion Internal implementation

#region Tab navigation methods

public void SelectTab<T>() where T : Page
{
var tabbedPage = MauiPageUtility.GetTopPage() as TabbedPage;

Expand Down Expand Up @@ -171,4 +185,6 @@ public async Task SelectTab<T>() where T : Page
}
}
}

#endregion Tab navigation methods
}

0 comments on commit c31556c

Please sign in to comment.