using System; using System.ComponentModel; using System.Threading.Tasks; using ShareeBike.MultilingualResources; using Xamarin.Essentials; namespace SharedGui.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))); } } }