Exemplo n.º 1
0
        public static decimal ConvertWeightUnits(decimal weightValue, WeightUnitEnum origUnits, WeightUnitEnum newUnits)
        {
            if (origUnits == newUnits)
            {
                return(weightValue);
            }

            // we treat pounds and stones/pounds the same (just changes in how shown on graph, text, etc. - stored the same)
            if (origUnits == WeightUnitEnum.ImperialPounds && newUnits == WeightUnitEnum.StonesAndPounds ||
                origUnits == WeightUnitEnum.StonesAndPounds && newUnits == WeightUnitEnum.ImperialPounds)
            {
                return(weightValue);
            }

            if ((origUnits == WeightUnitEnum.ImperialPounds || origUnits == WeightUnitEnum.StonesAndPounds) && newUnits == WeightUnitEnum.Kilograms)
            {
                return(weightValue * Constants.App.PoundsToKilograms);
            }
            else if (origUnits == WeightUnitEnum.Kilograms && (newUnits == WeightUnitEnum.ImperialPounds || newUnits == WeightUnitEnum.StonesAndPounds))
            {
                return(weightValue * Constants.App.KilogramsToPounds);
            }
            else // unknown conversion, do nothing
            {
#if DEBUG
                Debugger.Break();
#endif
                return(weightValue);
            }
        }
Exemplo n.º 2
0
        public WeightEntry()
        {
            Weight = decimal.MinValue;

            // NOTE:: The original database did not have this field, and SQLite-Net does not support Default attributes, so we make sure
            // we explicitly set this here to the default we want for pre-existing records before this field was added
            WeightUnit = WeightUnitEnum.ImperialPounds;
        }
Exemplo n.º 3
0
 public WeightLossGoal(DateTime startDate, decimal startWeight, DateTime goalDate, decimal goalWeight, WeightUnitEnum units)
 {
     StartDate   = startDate;
     StartWeight = startWeight;
     GoalDate    = goalDate;
     GoalWeight  = goalWeight;
     WeightUnit  = units;
 }
Exemplo n.º 4
0
        public static string ToSettingsName(this WeightUnitEnum weightUnit)
        {
            switch (weightUnit)
            {
            case WeightUnitEnum.ImperialPounds:
                return(Constants.Strings.Settings_ImperialPound);

            case WeightUnitEnum.Kilograms:
                return(Constants.Strings.Settings_Kilograms);

            case WeightUnitEnum.StonesAndPounds:
                return(Constants.Strings.Settings_StonesAndPounds);

            default:
#if DEBUG
                Debugger.Break();
#endif
                return("??");
            }
        }
Exemplo n.º 5
0
        public static string ToSentenceUsageName(this WeightUnitEnum weightUnit)
        {
            switch (weightUnit)
            {
            case WeightUnitEnum.ImperialPounds:
                return(Constants.Strings.Common_WeightUnit_ImperialPounds);

            case WeightUnitEnum.Kilograms:
                return(Constants.Strings.Common_WeightUnit_Kilograms);

            case WeightUnitEnum.StonesAndPounds:
                // NOTE:: this method is not used for the summary info as stones are displayed as "X stones, X.X pounds", unlike other unit types
                return(Constants.Strings.Common_WeightUnit_Stones);

            default:
#if DEBUG
                Debugger.Break();
#endif
                return("??");
            }
        }
        async void UpdateWeightEntriesAndGoalUnits(WeightUnitEnum newUnits, bool convertValues)
        {
            ResultWithErrorText result;

            try
            {
                IncrementPendingRequestCount();

                result = await DataService.ChangeWeightAndGoalUnits(newUnits, convertValues);

                if (result.Success)
                {
                    // update setting
                    SettingsService.WeightUnit = newUnits;
                    SetupMenu(); // refresh the menu to show the new setting
                }
            }
            catch (Exception ex)
            {
                AnalyticsService.TrackFatalError($"{nameof(UpdateWeightEntriesAndGoalUnits)} - an exception occurred calling {nameof(IDataService.ChangeWeightAndGoalUnits)}, cannot continue.", ex);
                result = new ResultWithErrorText(false, $"Exception occurred in call to {nameof(IDataService.ChangeWeightAndGoalUnits)}");
            }
            finally
            {
                DecrementPendingRequestCount();
            }

            if (result != null && !result.Success)
            {
                var msgText = convertValues ? Constants.Strings.Settings_ChangeWeightUnits_ConvertWeightValues_FatalError :
                              Constants.Strings.Settings_ChangeWeightUnits_ChangeUnits_FatalError;

                await DialogService.DisplayAlertAsync(Constants.Strings.Common_FatalError, msgText + result.ErrorText, Constants.Strings.Generic_OK);

                return;
            }
        }
Exemplo n.º 7
0
 public MockSettingsService()
 {
     WeightUnit = WeightUnitEnum.ImperialPounds;
 }
        async void WeightUnitTypeSelected(WeightUnitEnum newUnits)
        {
            // already using this same setting, just return
            if (newUnits == SettingsService.WeightUnit)
            {
                return;
            }

            // see if there are existing weight entries not of this type
            IList <WeightEntry> allEntries = null;

            try
            {
                IncrementPendingRequestCount();
                allEntries = await DataService.GetAllWeightEntries();

                var weightsWithDifferentUnits = allEntries.Where(w => w.WeightUnit != newUnits);
                if (!weightsWithDifferentUnits.Any())
                {
                    var currentGoal = await DataService.GetGoal();

                    if (currentGoal == null)
                    {
                        SettingsService.WeightUnit = newUnits; // nothing to change, so just change the setting and return
                        SetupMenu();                           // refresh the menu to show the new setting
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                AnalyticsService.TrackFatalError($"{nameof(WeightUnitTypeSelected)} - an exception occurred getting all weights", ex);
                await DialogService.DisplayAlertAsync(Constants.Strings.Settings_ChangeWeightUnits_GetWeightsError_Title,
                                                      Constants.Strings.Settings_ChangeWeightUnits_GetWeightsError_Message,
                                                      Constants.Strings.Generic_OK);
            }
            finally
            {
                DecrementPendingRequestCount();
            }

            // if they are switching between pounds and stones/pounds we don't need to ask them how to convert as the data is stored
            // the same either way (just changes how presented), so just go straight to the conversion in this case
            if ((newUnits == WeightUnitEnum.ImperialPounds && SettingsService.WeightUnit == WeightUnitEnum.StonesAndPounds) ||
                (newUnits == WeightUnitEnum.StonesAndPounds && SettingsService.WeightUnit == WeightUnitEnum.ImperialPounds))
            {
                UpdateWeightEntriesAndGoalUnits(newUnits, false);
                return;
            }

            // show warning of needing to convert these values
            var convertAction = ActionSheetButton.CreateButton(Constants.Strings.Settings_ChangeWeightUnits_ConvertWarning_ConvertWeightValues,
                                                               new DelegateCommand(() => { UpdateWeightEntriesAndGoalUnits(newUnits, true); }));
            var changeUnitAction = ActionSheetButton.CreateButton(Constants.Strings.Settings_ChangeWeightUnits_ConvertWarning_ChangeUnits,
                                                                  new DelegateCommand(() => { UpdateWeightEntriesAndGoalUnits(newUnits, false); }));
            var cancelAction = ActionSheetButton.CreateCancelButton(Constants.Strings.Generic_Cancel,
                                                                    new DelegateCommand(() => { }));

            await DialogService.DisplayActionSheetAsync(Constants.Strings.Settings_ChangeWeightUnits_ConvertWarning,
                                                        convertAction, changeUnitAction, cancelAction);
        }
Exemplo n.º 9
0
        public async Task <ResultWithErrorText> ChangeWeightAndGoalUnits(WeightUnitEnum newUnits, bool convertValues)
        {
            // convert all values
            bool   success   = true;
            string errorText = string.Empty;

            var allEntries = await GetAllWeightEntries();

            if (allEntries == null)
            {
                AnalyticsService.TrackFatalError($"{nameof(ChangeWeightAndGoalUnits)} - an error occurred trying to get weights!");
                success   = false;
                errorText = Constants.Strings.DataService_ChangeWeightAndGoalUnits_UnableToGetWeights;
            }
            else
            {
                var weightsWithDifferentUnits = allEntries.Where(w => w.WeightUnit != newUnits);
                if (weightsWithDifferentUnits.Any())
                {
                    foreach (var weight in weightsWithDifferentUnits)
                    {
                        if (!await RemoveWeightEntryForDate(weight.Date.Date))
                        {
                            AnalyticsService.TrackFatalError($"{nameof(ChangeWeightAndGoalUnits)} - an error occurred removing weight entry for date {weight.Date.Date}!");
                            success   = false;
                            errorText = string.Format(Constants.Strings.DataService_ChangeWeightAndGoalUnits_FailedRemovingWeight, weight.Date.Date);
                            break;
                        }

                        if (convertValues)
                        {
                            weight.Weight = WeightLogicHelpers.ConvertWeightUnits(weight.Weight, weight.WeightUnit, newUnits);
                        }

                        weight.WeightUnit = newUnits;
                        if (!await AddWeightEntry(weight))
                        {
                            AnalyticsService.TrackFatalError($"{nameof(ChangeWeightAndGoalUnits)} - an error occurred adding weight entry for date {weight.Date.Date}!");
                            success   = false;
                            errorText = string.Format(Constants.Strings.DataService_ChangeWeightAndGoalUnits_FailedAddingWeight, weight.Date.Date);
                            break;
                        }
                    }
                }

                // if we have converted all weights, now update the goal (if exists)
                if (success)
                {
                    var goal = await GetGoal();

                    if (goal != null)
                    {
                        if (convertValues)
                        {
                            goal.StartWeight = WeightLogicHelpers.ConvertWeightUnits(goal.StartWeight, goal.WeightUnit, newUnits);
                            goal.GoalWeight  = WeightLogicHelpers.ConvertWeightUnits(goal.GoalWeight, goal.WeightUnit, newUnits);
                        }
                        goal.WeightUnit = newUnits;

                        if (!await RemoveGoal())
                        {
                            AnalyticsService.TrackFatalError($"{nameof(ChangeWeightAndGoalUnits)} - an error occurred removing previous goal!");
                            errorText = Constants.Strings.DataService_ChangeWeightAndGoalUnits_FailedRemovingGoal;
                            success   = false;
                        }
                        else
                        {
                            if (!await SetGoal(goal))
                            {
                                AnalyticsService.TrackFatalError($"{nameof(ChangeWeightAndGoalUnits)} - an error occurred adding new goal!");
                                errorText = Constants.Strings.DataService_ChangeWeightAndGoalUnits_FailedAddingGoal;
                                success   = false;
                            }
                        }
                    }
                }
            }

            return(new ResultWithErrorText(success, errorText));
        }
Exemplo n.º 10
0
 public Task <ResultWithErrorText> ChangeWeightAndGoalUnits(WeightUnitEnum newUnits, bool convertValues)
 {
     return(Task.FromResult(new ResultWithErrorText(true, string.Empty)));
 }
Exemplo n.º 11
0
 public WeightEntry(DateTime dt, decimal wt, WeightUnitEnum weightUnit)
 {
     Date       = dt;
     Weight     = wt;
     WeightUnit = weightUnit;
 }
Exemplo n.º 12
0
        public static Tuple <decimal, decimal> GetMinMaxWeightRange(WeightUnitEnum weightUnit, WeightLossGoal goal, List <WeightEntry> entries, DateTime dateStart, DateTime dateEnd)
        {
            if (entries.Count == 0)
            {
                if (goal != null)
                {
                    var goalPadding = (weightUnit == WeightUnitEnum.ImperialPounds || weightUnit == WeightUnitEnum.StonesAndPounds ?
                                       Constants.App.Graph_GoalOnly_Pounds_Padding : Constants.App.Graph_GoalOnly_Kilograms_Padding);
                    // there should normally be at least one weight if a goal is set, there is nothing really to graph but the goal line itself
                    return(new Tuple <decimal, decimal>(goal.GoalWeight - goalPadding, goal.GoalWeight + goalPadding));
                }

                // no goal set and no saved weights, so just graph a default range
                var rangeDefaultMin = (weightUnit == WeightUnitEnum.ImperialPounds || weightUnit == WeightUnitEnum.StonesAndPounds ?
                                       Constants.App.Graph_WeightRange_Pounds_DefaultMin : Constants.App.Graph_WeightRange_Kilograms_DefaultMin);
                var rangeDefaultMax = (weightUnit == WeightUnitEnum.ImperialPounds || weightUnit == WeightUnitEnum.StonesAndPounds ?
                                       Constants.App.Graph_WeightRange_Pounds_DefaultMax : Constants.App.Graph_WeightRange_Kilograms_DefaultMax);
                return(new Tuple <decimal, decimal>(rangeDefaultMin, rangeDefaultMax));
            }

            decimal minValue = decimal.MaxValue;
            decimal maxValue = decimal.MinValue;

            // if there is a goal set then we want to include that line for the date range we're looking at
            if (goal != null)
            {
                // now see if our goal is greater/less for start end dates, if so include those in the range
                decimal goalStart = GetGoalWeightForDate(goal, dateStart);
                if (goalStart != decimal.MinValue)
                {
                    if (goalStart < minValue)
                    {
                        minValue = goalStart;
                    }
                    if (goalStart > maxValue)
                    {
                        maxValue = goalStart;
                    }
                }

                decimal goalEnd = GetGoalWeightForDate(goal, dateEnd);
                if (goalEnd != decimal.MinValue)
                {
                    if (goalEnd < minValue)
                    {
                        minValue = goalEnd;
                    }
                    if (goalEnd > maxValue)
                    {
                        maxValue = goalEnd;
                    }
                }
            }

            foreach (var entry in entries)
            {
                if (entry.Weight < minValue)
                {
                    minValue = (decimal)entry.Weight;
                }
                if (entry.Weight > maxValue)
                {
                    maxValue = (decimal)entry.Weight;
                }
            }

            minValue = minValue * Constants.App.Graph_WeightRange_MinPadding;
            maxValue = maxValue * Constants.App.Graph_WeightRange_MaxPadding;

            minValue = (decimal)Math.Floor(minValue); // use whole numbers
            maxValue = (decimal)Math.Ceiling(maxValue);

            return(new Tuple <decimal, decimal>(minValue, maxValue));
        }