public DrivePageViewModel(IEventAggregator eventAggregator, INavigationService navigationService, IUserRepository userRepository,
                                  IUserSettingsRepository userSettingsRepository, AppSettings settings) : base(navigationService)
        {
            this.eventAggregator        = eventAggregator;
            this.userRepository         = userRepository;
            this.userSettingsRepository = userSettingsRepository;
            this.appSettings            = settings;

            CurrentValue = this.userRepository.GetCurrentUser().CurrentMeasure;
            //The modified value adapted to string format (Hi & Lo)
            GlucoseValue = HiLowValueHelper.NewMeasureToString(CurrentValue);

            userSettings           = this.userSettingsRepository.GetCurrentUserSettings();
            MaximumGlucoseTreshold = userSettings.MaximumGlucoseTreshold;
            MinimumGlucoseTreshold = userSettings.MinimumGlucoseTreshold;

            eventAggregator.GetEvent <NotificationMeasureEvent>().Subscribe((notification) => {
                this.ChangeCurrentValue(notification);
            });

            //Get value battery percentage and last value checked time at startup
            UpdateBatteryAndLastValue();
            //Get trend value at startup
            UpdateTrendValue();

            //Update value battery percentage and last value checked time from event
            eventAggregator.GetEvent <DrivePageDataUpdateEvent>().Subscribe(() => {
                UpdateBatteryAndLastValue();
                UpdateTrendValue();
            });

            //Update value trend value
            eventAggregator.GetEvent <TrendEvent>().Subscribe((trend) => {
                UpdateTrendValue();
            });

            this.CallCommand = new DelegateCommand(async() =>
            {
                if (this.IsLowValue || this.IsHighValue || this.IsLow1Value || this.IsHigh2Value)
                {
                    await Launcher.OpenAsync(new Uri(string.Format("tel:{0}", this.appSettings.EMERGENCY_NUMBER)));
                }
            });
        }
Пример #2
0
        /// <summary>
        /// Spreads the last measure on the application (database, UI, alerts)
        /// </summary>
        /// <param name="lastMeasure"></param>
        private void SpreadLastMeasure(float lastMeasure)
        {
            var  currentUser          = this.userRepository.GetCurrentUser();
            var  userSettings         = this.userSettingsRepository.GetCurrentUserSettings();
            long countExistingMesures = this.glucoseMeasureRepository.CountByUser(currentUser.Id);
            bool isFirstReading       = countExistingMesures == 0;

            // 1 - Driving mode
            // NOTE : We only handle MGDL Measure for the H&D MVP
            this.userRepository.ChangeCurrentMeasure(currentUser, lastMeasure);

            // 2 The notification channel
            var notification = new NotificationMeasure();

            notification.NotificationMessage    = string.Format(Utils.GetTranslation("NotificationMessageCurrentMeasure"), HiLowValueHelper.NewMeasureToString(lastMeasure));
            notification.NewMeasure             = lastMeasure;
            notification.MinimumGlucoseTreshold = userSettings.MinimumGlucoseTreshold;
            notification.MaximumGlucoseTreshold = userSettings.MaximumGlucoseTreshold;
            notification.IsAlert = userRepository.GetCurrentUser().IsAlert;

            //Current Trend management //
            GlucoseMeasure pastFifteenMinMeasure      = glucoseMeasureRepository.GetPastFifteenMinutesMeasureByUser(currentUser.Id);
            float?         pastFifteenMinMeasureValue = null;

            if (pastFifteenMinMeasure != null)
            {
                pastFifteenMinMeasureValue = pastFifteenMinMeasure.GlucoseLevelMGDL;
            }

            //Trend management : Builder //
            switch (TrendHelper.ValuesToTypeTrend(lastMeasure, pastFifteenMinMeasureValue))
            {
            case MeasureTrend.IncreasingHeavy:
                notification.MeasureTrend = MeasureTrend.IncreasingHeavy;
                break;

            case MeasureTrend.Increasing:
                notification.MeasureTrend = MeasureTrend.Increasing;
                break;

            case MeasureTrend.Constant:
                notification.MeasureTrend = MeasureTrend.Constant;
                break;

            case MeasureTrend.Decreasing:
                notification.MeasureTrend = MeasureTrend.Decreasing;
                break;

            case MeasureTrend.DecreasingHeavy:
                notification.MeasureTrend = MeasureTrend.DecreasingHeavy;
                break;

            default:
                notification.MeasureTrend = MeasureTrend.None;
                break;
            }

            //Trend management : Notifications //
            //Save the measure trend in the user data
            this.userRepository.UpdateMeasureTrend(currentUser, notification.MeasureTrend);
            //Send the measure trend event to the drive page
            this.eventAggregator.GetEvent <TrendEvent>().Publish(notification.MeasureTrend);
            //Send the measure trend event to the notification
            this.eventAggregator.GetEvent <NotificationMeasureEvent>().Publish(notification);

            // 3 - Alerts raised to the user ?
            if (userRepository.GetCurrentUser().IsAlert)
            {
                if (lastMeasure > userSettings.MaximumGlucoseTreshold || lastMeasure < userSettings.MinimumGlucoseTreshold)
                {
                    notification.NotificationMessage = string.Format(Utils.GetTranslation("NotificationMessageAlertGlycemia"), HiLowValueHelper.NewMeasureToString(lastMeasure));
                    this.eventAggregator.GetEvent <PushNotificationAlertEvent>().Publish(notification);
                }
            }

            this.precedingMeasure = lastMeasure;
        }
 /// <summary>
 /// Update the current value
 /// </summary>
 /// <param name="notification"></param>
 private void ChangeCurrentValue(NotificationMeasure notification)
 {
     CurrentValue = notification.NewMeasure;
     GlucoseValue = HiLowValueHelper.NewMeasureToString(CurrentValue);
 }