sharee.bike-App/ShareeSharedGuiLib/ViewModel/NotConnectedToNetViewModel.cs
2023-02-28 16:03:56 +01:00

54 lines
1.5 KiB
C#

using System;
using System.ComponentModel;
using System.Threading.Tasks;
using TINK.MultilingualResources;
using Xamarin.Essentials;
namespace ShareeSharedGuiLib.ViewModel
{
public class NotConnectedToNetViewModel : INotifyPropertyChanged
{
public bool IsNotConnectedToNet { get; set; }
private bool _isBikesDataOutdatedLabelVisible = false;
public event PropertyChangedEventHandler PropertyChanged;
public bool IsBikesDataOutdatedLabelVisible
{
get { return _isBikesDataOutdatedLabelVisible; }
set
{
_isBikesDataOutdatedLabelVisible = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsBikesDataOutdatedLabelVisible)));
}
}
public NotConnectedToNetViewModel()
{
IsNotConnectedToNet = Connectivity.NetworkAccess != NetworkAccess.Internet;
// Register for connectivity changes, be sure to unsubscribe when finished
Connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
}
public async void Connectivity_ConnectivityChanged(object sender, ConnectivityChangedEventArgs e)
{
IsNotConnectedToNet = e.NetworkAccess != NetworkAccess.Internet;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsNotConnectedToNet)));
if (IsNotConnectedToNet)
{
IsBikesDataOutdatedLabelVisible = false;
}
else
{
IsBikesDataOutdatedLabelVisible = true;
await Task.Delay(TimeSpan.FromSeconds(5));
IsBikesDataOutdatedLabelVisible = false;
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsBikesDataOutdatedLabelVisible)));
}
}
}