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:
David Britch 2017-05-08 13:20:06 +01:00
parent 59b4204c7a
commit 34d5d689cd
4 changed files with 39 additions and 9 deletions

View File

@ -5,6 +5,8 @@ namespace eShopOnContainers.Services
{ {
public interface INavigationService public interface INavigationService
{ {
ViewModelBase PreviousPageViewModel { get; }
Task InitializeAsync(); Task InitializeAsync();
Task NavigateToAsync<TViewModel>() where TViewModel : ViewModelBase; Task NavigateToAsync<TViewModel>() where TViewModel : ViewModelBase;

View File

@ -12,6 +12,16 @@ namespace eShopOnContainers.Services
{ {
public class NavigationService : INavigationService 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() public Task InitializeAsync()
{ {
if(string.IsNullOrEmpty(Settings.AuthAccessToken)) if(string.IsNullOrEmpty(Settings.AuthAccessToken))

View File

@ -3,6 +3,7 @@ using System.Windows.Input;
using Xamarin.Forms; using Xamarin.Forms;
using System.Threading.Tasks; using System.Threading.Tasks;
using eShopOnContainers.Core.Helpers; using eShopOnContainers.Core.Helpers;
using eShopOnContainers.Core.Models.User;
namespace eShopOnContainers.Core.ViewModels namespace eShopOnContainers.Core.ViewModels
{ {
@ -67,13 +68,7 @@ namespace eShopOnContainers.Core.ViewModels
} }
} }
public ICommand MockServicesCommand => new Command(MockServices); public ICommand ToggleMockServicesCommand => new Command(async () => await ToggleMockServicesAsync());
private void MockServices()
{
ViewModelLocator.RegisterDependencies(!UseAzureServices);
UpdateInfo();
}
public override Task InitializeAsync(object navigationData) public override Task InitializeAsync(object navigationData)
{ {
@ -82,7 +77,30 @@ namespace eShopOnContainers.Core.ViewModels
return base.InitializeAsync(navigationData); 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) if (!UseAzureServices)
{ {

View File

@ -121,7 +121,7 @@
Grid.Row="1" Grid.Row="1"
Animate="True" Animate="True"
Checked="{Binding UseAzureServices, Mode=TwoWay}" Checked="{Binding UseAzureServices, Mode=TwoWay}"
Command="{Binding MockServicesCommand}" Command="{Binding ToggleMockServicesCommand}"
Style="{StaticResource SettingsToggleButtonStyle}"> Style="{StaticResource SettingsToggleButtonStyle}">
<controls:ToggleButton.CheckedImage> <controls:ToggleButton.CheckedImage>
<OnPlatform x:TypeArguments="ImageSource" <OnPlatform x:TypeArguments="ImageSource"