Fixed app crash when switching from mock to real services.
When the previous page is the MainView, switching from mock to real services will force re-authentication by navigating to the LoginView.
This commit is contained in:
parent
0457e26e4f
commit
9472c7061b
@ -5,6 +5,8 @@ namespace eShopOnContainers.Services
|
||||
{
|
||||
public interface INavigationService
|
||||
{
|
||||
ViewModelBase PreviousPageViewModel { get; }
|
||||
|
||||
Task InitializeAsync();
|
||||
|
||||
Task NavigateToAsync<TViewModel>() where TViewModel : ViewModelBase;
|
||||
|
@ -12,6 +12,16 @@ namespace eShopOnContainers.Services
|
||||
{
|
||||
public class NavigationService : INavigationService
|
||||
{
|
||||
public ViewModelBase PreviousPageViewModel
|
||||
{
|
||||
get
|
||||
{
|
||||
var mainPage = Application.Current.MainPage as CustomNavigationView;
|
||||
var viewModel = mainPage.Navigation.NavigationStack[mainPage.Navigation.NavigationStack.Count - 2].BindingContext;
|
||||
return viewModel as ViewModelBase;
|
||||
}
|
||||
}
|
||||
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
if(string.IsNullOrEmpty(Settings.AuthAccessToken))
|
||||
|
@ -3,6 +3,7 @@ using System.Windows.Input;
|
||||
using Xamarin.Forms;
|
||||
using System.Threading.Tasks;
|
||||
using eShopOnContainers.Core.Helpers;
|
||||
using eShopOnContainers.Core.Models.User;
|
||||
|
||||
namespace eShopOnContainers.Core.ViewModels
|
||||
{
|
||||
@ -67,13 +68,7 @@ namespace eShopOnContainers.Core.ViewModels
|
||||
}
|
||||
}
|
||||
|
||||
public ICommand MockServicesCommand => new Command(MockServices);
|
||||
|
||||
private void MockServices()
|
||||
{
|
||||
ViewModelLocator.RegisterDependencies(!UseAzureServices);
|
||||
UpdateInfo();
|
||||
}
|
||||
public ICommand ToggleMockServicesCommand => new Command(async () => await ToggleMockServicesAsync());
|
||||
|
||||
public override Task InitializeAsync(object navigationData)
|
||||
{
|
||||
@ -82,7 +77,30 @@ namespace eShopOnContainers.Core.ViewModels
|
||||
return base.InitializeAsync(navigationData);
|
||||
}
|
||||
|
||||
private void UpdateInfo()
|
||||
private async Task ToggleMockServicesAsync()
|
||||
{
|
||||
ViewModelLocator.RegisterDependencies(!UseAzureServices);
|
||||
UpdateInfo();
|
||||
|
||||
var previousPageViewModel = NavigationService.PreviousPageViewModel;
|
||||
if (previousPageViewModel != null)
|
||||
{
|
||||
if (previousPageViewModel is MainViewModel)
|
||||
{
|
||||
// Slight delay so that page navigation isn't instantaneous
|
||||
await Task.Delay(1000);
|
||||
if (UseAzureServices)
|
||||
{
|
||||
Settings.AuthAccessToken = string.Empty;
|
||||
Settings.AuthIdToken = string.Empty;
|
||||
await NavigationService.NavigateToAsync<LoginViewModel>(new LogoutParameter { Logout = true });
|
||||
await NavigationService.RemoveBackStackAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateInfo()
|
||||
{
|
||||
if (!UseAzureServices)
|
||||
{
|
||||
|
@ -121,7 +121,7 @@
|
||||
Grid.Row="1"
|
||||
Animate="True"
|
||||
Checked="{Binding UseAzureServices, Mode=TwoWay}"
|
||||
Command="{Binding MockServicesCommand}"
|
||||
Command="{Binding ToggleMockServicesCommand}"
|
||||
Style="{StaticResource SettingsToggleButtonStyle}">
|
||||
<controls:ToggleButton.CheckedImage>
|
||||
<OnPlatform x:TypeArguments="ImageSource"
|
||||
|
Loading…
x
Reference in New Issue
Block a user