// ----------------------------------------------------------------------- // // This code can be used in commercial, free and open source projects. // // ----------------------------------------------------------------------- using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Threading; using System.Threading.Tasks; using VDsoft.OnlineMeter.Uwp.Dal; using Windows.UI.Xaml.Media; using GalaSoft.MvvmLight.Messaging; using VDsoft.OnlineMeter.Uwp.Model; namespace VDsoft.OnlineMeter.Uwp.ViewModel { /// /// View model for the view. /// public class MeterViewModel : ViewModelBase { /// /// Message when system is online. /// private readonly string onlineMessage = "You're online and ready to go."; /// /// Message when system is offline. /// private readonly string offlineMessage = "Woow, you're offline. Better check your connection."; /// /// Green color. /// private readonly Windows.UI.Color green = Windows.UI.Colors.Green; /// /// Red color. /// private readonly Windows.UI.Color red = Windows.UI.Colors.Red; /// /// Brush for the red led. /// private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red); /// /// Brush for the green led. /// private SolidColorBrush greenBrush = new SolidColorBrush(Windows.UI.Colors.Green); /// /// Brush for the gray default. /// private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.Gray); /// /// Status message. /// private string statusMessage = null; /// /// Initializes a new instance of the class. /// public MeterViewModel() { if (this.IsInDesignMode) { this.RedBrush = this.grayBrush; this.StatusMessage = this.onlineMessage; } else { this.GreenBrush = this.grayBrush; this.RedBrush = this.grayBrush; } // register to receive the status updates. Messenger.Default.Register(this, ViewModelLocator.StatusUpdateToken, this.UpdateStatus); Task.Run(() => { InternetMonitor monitor = new InternetMonitor(); monitor.StartConnectionCheck(); }); } /// /// Gets or sets the brush for the red led. /// public SolidColorBrush RedBrush { get { return this.redBrush; } set { if (value == this.redBrush) { return; } this.redBrush = value; this.RaisePropertyChanged(); } } /// /// Gets or sets the brush for the green led. /// public SolidColorBrush GreenBrush { get { return this.greenBrush; } set { if (value == this.greenBrush) { return; } this.greenBrush = value; this.RaisePropertyChanged(); } } /// /// Gets or sets the status message. /// public string StatusMessage { get { return this.statusMessage; } set { if (value == this.statusMessage) { return; } this.statusMessage = value; this.RaisePropertyChanged(); } } /// /// Updates the current status depending on the provided . /// /// of the check. private void UpdateStatus(ConnectionResult result) { DispatcherHelper.CheckBeginInvokeOnUI(() => { if (result.Online) { this.StatusMessage = this.onlineMessage; this.GreenBrush = new SolidColorBrush(this.green); this.RedBrush = this.grayBrush; } else { this.StatusMessage = this.offlineMessage; this.RedBrush = new SolidColorBrush(this.red); this.GreenBrush = this.grayBrush; } }); } } }