예제 #1
0
        /// <summary>
        /// Initializes the RecipeListViewModel object.
        /// </summary>
        /// <param name="loadTask">The task to load the list of recipes to display.</param>
        /// <param name="localDataManager">The LocalDataManger object.</param>
        /// <param name="navigation">The INavigation object for managing pages.</param>
        /// <param name="titleString">The string to be displayed in the navigation bar.</param>
        /// <param name="noResultsString">The string to display if no recipes are returned.</param>
        /// <param name="containsSavedItems">Flag for whether the recipe list will contain saved recipe items.</param>
        public RecipeListViewModel(
            Task <List <Recipe> > loadTask,
            LocalDataManager localDataManager,
            INavigation navigation,
            string titleString,
            string noResultsString,
            bool containsSavedItems)
        {
            _localDataManager   = localDataManager;
            _navigation         = navigation;
            _containsSavedItems = containsSavedItems;

            TitleString       = titleString;
            NoResultsString   = noResultsString;
            OpenRecipeCommand = new Command(OpenRecipe);

            // Task to wait for recipe list to load, then show the result to the user.
            Task.Run(async() =>
            {
                try
                {
                    UserIngredients = await _localDataManager.GetIngredients();
                    var result      = await loadTask;

                    if (_containsSavedItems)
                    {
                        CalculateMissingIngredients(result);
                        ////result.Sort(ObjectComparisons.SortByRecipeName);
                        result.Sort(ObjectComparisons.SortByMissingIngredients);
                    }

                    RecipeResults    = new ObservableCollection <Recipe>(result);
                    SearchHasResults = RecipeResults.Count != 0;
                }
                catch (Exception e)
                {
                    LoadError        = true;
                    SearchHasResults = true; // Set to true in order to hide NoResults Label.
                    RecipeResults    = new ObservableCollection <Recipe>();
                }

                SearchComplete = true;
                OnPropertyChanged(nameof(RecipeResults));
            });
        }