The Connectivity Plugin is working for me, but it is doing too good of a job. What I mean by that is the way in which I have implemented the plugin causes multiple alerts to fire when the app loses the WiFi connection.
Let's say that I have 15 pages in my app. I have implemented the following check in the main method of each page. As you can see, when a change is detected, if should fire off an alert and push the SignIn Page onto the stack.
CrossConnectivity.Current.ConnectivityChanged += async delegate (object sender, Plugin.Connectivity.Abstractions.ConnectivityChangedEventArgs e)
{
if (e.IsConnected == false)
{
await DisplayAlert("Connectivity Alert", ValidationResources.NotConnectedMessage3, "OK");
this.IsBusy = true;
await Navigation.PushAsync(new SignIn());
this.IsBusy = false;
}
};
If 8 of the pages are on the stack, then I get 8 alerts when a change is detected. This is a horrible user experience.
I see how this guy has implemented his solution in the App.cs file, but I don't see how he can then alert the user that something has happened.
https://forums.xamarin.com/discussion/51735/notification-to-pcl-when-wifi-state-changed
With my method, should I clear the stack before I push the a new SignIn Page? Is there a better approach to managing this?
By the way, I also implement this method in all my Command initiated ViewModel methods. This will check to make sure the user is connected before making a request to the remote API, for example.
if (CrossConnectivity.Current.IsConnected == false)
{
NetworkAlert = ValidationResources.NotConnectedMessage2;
var notificator = DependencyService.Get<IToastNotificator>();
await notificator.Notify(ToastNotificationType.Warning, "Connectivity Alert", NetworkAlert, TimeSpan.FromSeconds(3));
IsBusy = false;
return;
}
Any help is much appreciated. Thanks!