Exemple #1
0
        async void RefreshFromUserDataAsync()
        {
            try
            {
                IncrementPendingRequestCount(); // show loading

                // do async data read
                var todaysWeightEntry = await DataService.GetWeightEntryForDate(DateTime.Today.Date);

                var goal = await DataService.GetGoal();

                // update the UI color (NOTE:: both goal and todaysWeightEntry could be null)
                var infoForToday = WeightLogicHelpers.GetTodaysDisplayInfo(goal, todaysWeightEntry);
                WindowColorService.ChangeAppBaseColor(infoForToday.ColorToShow);
            }
            catch (Exception ex)
            {
                AnalyticsService.TrackFatalError($"{nameof(RefreshFromUserDataAsync)} - an exception occurred.", ex);
                // NOTE:: not showing an error here as this is not in response to user action. potentially should show a non-intrusive error banner
            }
            finally
            {
                DecrementPendingRequestCount(); // hide loading
            }
        }
        async void RefreshFromUserDataAsync()
        {
            try
            {
                IncrementPendingRequestCount(); // show loading

                IsWeightListingVisible = false;
                PlaceholderText        = Constants.Strings.GraphPage_PlaceholderText_Loading; // defaults to 'loading..' as it is seen briefly

                // do async data read
                var todaysWeightEntry = await DataService.GetWeightEntryForDate(DateTime.Today.Date);

                var goal = await DataService.GetGoal();

                // update the UI color (NOTE:: both goal and todaysWeightEntry could be null)
                var infoForToday = WeightLogicHelpers.GetTodaysDisplayInfo(goal, todaysWeightEntry);
                WindowColorService.ChangeAppBaseColor(infoForToday.ColorToShow);

                // update the listing of weight entries
                var latestWeightEntries = await DataService.GetLatestWeightEntries(Constants.App.WeightListingMaxCount)
                                          .ConfigureAwait(false) as List <WeightEntry>;

                IsWeightListingVisible = latestWeightEntries.Count > 0; // will show/hide list and placeholder label
                PlaceholderText        = IsWeightListingVisible ? string.Empty : Constants.Strings.GraphPage_PlaceholderText_NoEntries;

                // create a lookup of success/failure values for weight entries and set on converter (used for showing/hiding checkmark per row)
                Dictionary <WeightEntry, bool> successForDateLookup = new Dictionary <WeightEntry, bool>();
                foreach (var entry in latestWeightEntries)
                {
                    bool success = WeightLogicHelpers.WeightMetGoalOnDate(goal, entry.Date, entry.Weight);
                    successForDateLookup.Add(entry, success);
                }
                CheckmarkVisibilityConverter.SuccessForDateLookup = successForDateLookup;

                // NOTE:: Updates to observable collections must happen on UI thread
                Device.BeginInvokeOnMainThread(() =>
                {
                    LatestWeightEntries = (latestWeightEntries == null) ?
                                          LatestWeightEntries = new ObservableCollection <WeightEntry>() :
                                                                LatestWeightEntries = new ObservableCollection <WeightEntry>(latestWeightEntries);
                });

                // NOTE:: we limit how many points are graphed as it can be a performance concern if too high, and OxyPlot appears to stop drawing connecting lines beyond ~530 items
                int maxGraphPoints = (Device.RuntimePlatform == Device.Android) ? Constants.App.WeightGraphingMaxCount_Android :
                                     Constants.App.WeightGraphingMaxCount;
                var limitedEntries = latestWeightEntries.OrderByDescending(x => x.Date).Take(maxGraphPoints).ToList();
                RefreshGraphDataModel(limitedEntries, goal);
            }
            catch (Exception ex)
            {
                AnalyticsService.TrackFatalError($"{nameof(RefreshFromUserDataAsync)} - an exception occurred.", ex);
                // NOTE:: not showing an error here as this is not in response to user action. potentially should show a non-intrusive error banner
            }
            finally
            {
                DecrementPendingRequestCount(); // hide loading
            }
        }
        async void RefreshFromUserDataAsync()
        {
            if (_isFirstStartup)
            {
                _isFirstStartup = false;

                // if we need to show the welcome flow don't bother updating data (will be updated when this page appears again)
                if (await ShowWelcomeFlowIfNeeded())
                {
                    return;
                }
            }

            try
            {
                IncrementPendingRequestCount(); // show loading

                // do async data read
                var todaysWeightEntry = await DataService.GetWeightEntryForDate(DateTime.Today.Date);

                var goal = await DataService.GetGoal();

                // get all the info needed to update the UI (NOTE:: both goal and todaysWeightEntry could be null)
                var infoForToday = WeightLogicHelpers.GetTodaysDisplayInfo(goal, todaysWeightEntry);

                TodaysWeight  = infoForToday.TodaysDisplayWeight;
                TodaysMessage = infoForToday.TodaysMessage;
                HowToEatText  = infoForToday.HowToEatMessage;

                IsEnterWeightButtonVisible = infoForToday.IsEnterWeightButtonVisible;
                IsSetGoalButtonVisible     = infoForToday.IsSetGoalButtonVisible;

                MainLabelFontSize = SettingsService.WeightUnit == Enumerations.WeightUnitEnum.StonesAndPounds ?
                                    Constants.UI.DailyInfoFontSize_Stones : Constants.UI.DailyInfoFontSize_Normal;
                WindowColorService.ChangeAppBaseColor(infoForToday.ColorToShow);
            }
            catch (Exception ex)
            {
                AnalyticsService.TrackFatalError($"{nameof(RefreshFromUserDataAsync)} - an exception occurred.", ex);
                // NOTE:: not showing an error here as this is not in response to user action. potentially should show a non-intrusive error banner
            }
            finally
            {
                DecrementPendingRequestCount(); // hide loading
            }
        }