Exemple #1
0
        public async Task ExecuteSearchFoodsCommand()
        {
            if (cancellationTokenSource != null)
            {
                cancellationTokenSource.Cancel();
            }

            cancellationTokenSource = new CancellationTokenSource();
            SetResultVisibility(true, false);

            try
            {
                Foods.Clear();
                var includUnknownFructose = AppPreferences.UnknownFructose;
                foreach (var food in await FoodDataStore.SearchFoodsAsync(SearchText, includUnknownFructose, cancellationTokenSource.Token))
                {
                    Foods.Add(new FoodViewModel(food));
                }
                SetResultVisibility(false, true);

                Analytics.TrackEvent("Search Completed", new Dictionary <string, string> {
                    { "Search Text", SearchText }
                });
            }
            catch (TaskCanceledException)
            {
                // Task was canceled so just handle the exception and move on
            }
            catch (Exception exception)
            {
                Analytics.TrackEvent("Error Searching Food", new Dictionary <string, string> {
                    { "Exception Message", exception.Message }
                });
            }
        }
        async Task ExecuteLoadItemsCommand()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            try
            {
                FoodItems.Clear();
                var foodItems = await FoodDataStore.GetItemsAsync(true);

                foreach (var item in foodItems)
                {
                    FoodItems.Add(item);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }
Exemple #3
0
        public ItemsViewModel()
        {
            Title            = "Home";
            GlucItems        = new ObservableCollection <GlucoseEntry>();
            RecentGlucItems  = new ObservableCollection <GlucoseEntry>();
            ExItems          = new ObservableCollection <ExerciseEntry>();
            RecentExItems    = new ObservableCollection <ExerciseEntry>();
            FoodItems        = new ObservableCollection <FoodEntry>();
            RecentFoodItems  = new ObservableCollection <FoodEntry>();
            LoadItemsCommand = new Command(async() => await ExecuteLoadItemsCommand());

            MessagingCenter.Subscribe <GlucoseEntryPage, GlucoseEntry>(this, "AddItem", async(obj, item) =>
            {
                var newGlucItem = item as GlucoseEntry;
                GlucItems.Add(newGlucItem);
                await GlucDataStore.AddItemAsync(newGlucItem);
            });

            MessagingCenter.Subscribe <ExerciseEntryPage, ExerciseEntry>(this, "AddItem", async(obj, item) =>
            {
                var newExItem = item as ExerciseEntry;
                ExItems.Add(newExItem);
                await ExDataStore.AddItemAsync(newExItem);
            });

            MessagingCenter.Subscribe <FoodEntryPage, FoodEntry>(this, "AddItem", async(obj, item) =>
            {
                var newFoodItem = item as FoodEntry;
                FoodItems.Add(newFoodItem);
                await FoodDataStore.AddItemAsync(newFoodItem);
            });
        }
        public GroupsViewModel()
        {
            var groupViewModels = from foodGroup in FoodDataStore.GetFoodGroups().OrderBy(g => g.Name)
                                  select new GroupViewModel(foodGroup);

            Groups = new ObservableCollection <GroupViewModel>(groupViewModels);
        }
Exemple #5
0
        async Task ExecuteLoadItemsCommand()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            try
            {
                GlucItems.Clear();
                RecentGlucItems.Clear();
                var glucItems = await GlucDataStore.GetItemsAsync(true);

                foreach (var item in glucItems)
                {
                    GlucItems.Add(item);
                }

                if (GlucItems.Count > 10)
                {
                    for (int i = GlucItems.Count - 10; i < GlucItems.Count; i++)
                    {
                        RecentGlucItems.Add(GlucItems[i]);
                    }
                }
                else
                {
                    foreach (GlucoseEntry entry in GlucItems)
                    {
                        RecentGlucItems.Add(entry);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            try
            {
                ExItems.Clear();
                RecentExItems.Clear();
                var exItems = await ExDataStore.GetItemsAsync(true);

                foreach (var item in exItems)
                {
                    ExItems.Add(item);
                }

                if (ExItems.Count > 10)
                {
                    for (int i = ExItems.Count - 10; i < ExItems.Count; i++)
                    {
                        RecentExItems.Add(ExItems[i]);
                    }
                }
                else
                {
                    foreach (ExerciseEntry entry in ExItems)
                    {
                        RecentExItems.Add(entry);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }

            try
            {
                FoodItems.Clear();
                RecentFoodItems.Clear();
                var foodItems = await FoodDataStore.GetItemsAsync(true);

                foreach (var item in foodItems)
                {
                    FoodItems.Add(item);
                }

                if (FoodItems.Count > 10)
                {
                    for (int i = FoodItems.Count - 10; i < FoodItems.Count; i++)
                    {
                        RecentFoodItems.Add(FoodItems[i]);
                    }
                }
                else
                {
                    foreach (FoodEntry entry in FoodItems)
                    {
                        RecentFoodItems.Add(entry);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }