Skip to content

Commit 1564873

Browse files
authored
Merge pull request dotnet-architecture#193 from dotnet-architecture/xamarin
Fixed app crash when switching from mock to real services.
2 parents f85b131 + 34d5d68 commit 1564873

4 files changed

Lines changed: 39 additions & 9 deletions

File tree

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Navigation/INavigationService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace eShopOnContainers.Services
55
{
66
public interface INavigationService
77
{
8+
ViewModelBase PreviousPageViewModel { get; }
9+
810
Task InitializeAsync();
911

1012
Task NavigateToAsync<TViewModel>() where TViewModel : ViewModelBase;

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Navigation/NavigationService.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@ namespace eShopOnContainers.Services
1212
{
1313
public class NavigationService : INavigationService
1414
{
15+
public ViewModelBase PreviousPageViewModel
16+
{
17+
get
18+
{
19+
var mainPage = Application.Current.MainPage as CustomNavigationView;
20+
var viewModel = mainPage.Navigation.NavigationStack[mainPage.Navigation.NavigationStack.Count - 2].BindingContext;
21+
return viewModel as ViewModelBase;
22+
}
23+
}
24+
1525
public Task InitializeAsync()
1626
{
1727
if(string.IsNullOrEmpty(Settings.AuthAccessToken))

src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Xamarin.Forms;
44
using System.Threading.Tasks;
55
using eShopOnContainers.Core.Helpers;
6+
using eShopOnContainers.Core.Models.User;
67

78
namespace eShopOnContainers.Core.ViewModels
89
{
@@ -67,13 +68,7 @@ public string Endpoint
6768
}
6869
}
6970

70-
public ICommand MockServicesCommand => new Command(MockServices);
71-
72-
private void MockServices()
73-
{
74-
ViewModelLocator.RegisterDependencies(!UseAzureServices);
75-
UpdateInfo();
76-
}
71+
public ICommand ToggleMockServicesCommand => new Command(async () => await ToggleMockServicesAsync());
7772

7873
public override Task InitializeAsync(object navigationData)
7974
{
@@ -82,7 +77,30 @@ public override Task InitializeAsync(object navigationData)
8277
return base.InitializeAsync(navigationData);
8378
}
8479

85-
private void UpdateInfo()
80+
private async Task ToggleMockServicesAsync()
81+
{
82+
ViewModelLocator.RegisterDependencies(!UseAzureServices);
83+
UpdateInfo();
84+
85+
var previousPageViewModel = NavigationService.PreviousPageViewModel;
86+
if (previousPageViewModel != null)
87+
{
88+
if (previousPageViewModel is MainViewModel)
89+
{
90+
// Slight delay so that page navigation isn't instantaneous
91+
await Task.Delay(1000);
92+
if (UseAzureServices)
93+
{
94+
Settings.AuthAccessToken = string.Empty;
95+
Settings.AuthIdToken = string.Empty;
96+
await NavigationService.NavigateToAsync<LoginViewModel>(new LogoutParameter { Logout = true });
97+
await NavigationService.RemoveBackStackAsync();
98+
}
99+
}
100+
}
101+
}
102+
103+
private void UpdateInfo()
86104
{
87105
if (!UseAzureServices)
88106
{

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Views/SettingsView.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
Grid.Row="1"
122122
Animate="True"
123123
Checked="{Binding UseAzureServices, Mode=TwoWay}"
124-
Command="{Binding MockServicesCommand}"
124+
Command="{Binding ToggleMockServicesCommand}"
125125
Style="{StaticResource SettingsToggleButtonStyle}">
126126
<controls:ToggleButton.CheckedImage>
127127
<OnPlatform x:TypeArguments="ImageSource"

0 commit comments

Comments
 (0)