예제 #1
0
    /// <summary>
    /// Initializes a new instance of the <see cref="RecipeListViewModel"/> class.
    /// </summary>
    /// <param name="dialogService">Dialog service dependency.</param>
    /// <param name="recipeFiltrator">RecipeFiltrator dependency.</param>
    /// <param name="container">IoC container.</param>
    /// <param name="regionManager">Region manager for Prism navigation.</param>
    /// <param name="recipeService">Recipe service dependency.</param>
    /// <param name="eventAggregator">Dependency on Prism event aggregator.</param>
    /// <param name="mapper">Mapper dependency.</param>
    /// <param name="localization">Localization provider dependency.</param>
    public RecipeListViewModel(DialogService dialogService,
                               RecipeFiltrator recipeFiltrator,
                               IContainerExtension container,
                               IRegionManager regionManager,
                               RecipeService recipeService,
                               IEventAggregator eventAggregator,
                               IMapper mapper,
                               ILocalization localization)
    {
        this.dialogService   = dialogService;
        this.recipeFiltrator = recipeFiltrator;
        this.container       = container;
        this.regionManager   = regionManager;
        this.recipeService   = recipeService;
        this.mapper          = mapper;
        this.localization    = localization;

        // Subscribe to events
        eventAggregator.GetEvent <RecipeCreatedEvent>().Subscribe(OnRecipeCreated, ThreadOption.UIThread);
        eventAggregator.GetEvent <RecipeUpdatedEvent>().Subscribe(OnRecipeUpdated, ThreadOption.UIThread);
        eventAggregator.GetEvent <RecipeDeletedEvent>().Subscribe(OnRecipeDeleted, ThreadOption.UIThread);

        LoadedCommand = new AsyncDelegateCommand(OnLoaded, executeOnce: true);

        ViewRecipeCommand = new DelegateCommand <Guid>(ViewRecipe);
        AddRecipeCommand  = new DelegateCommand(AddRecipe);

        RecipiesSource = new CollectionViewSource();
        RecipiesSource.SortDescriptions.Add(new SortDescription()
        {
            PropertyName = nameof(RecipeListViewDto.Name)
        });
    }
예제 #2
0
    /// <summary>
    /// Initializes a new instance of the <see cref="RecipeSelectViewModel"/> class.
    /// </summary>
    /// <param name="dialogService">Dialog service dependency.</param>
    /// <param name="recipeService">Recipe service dependency.</param>
    /// <param name="recipeFiltrator">RecipeFiltrator service dependency.</param>
    /// <param name="day">Day, which settings will be user for filtering.</param>
    /// <param name="garnishSelect">Select garnish.</param>
    public RecipeSelectViewModel(DialogService dialogService,
                                 RecipeService recipeService,
                                 RecipeFiltrator recipeFiltrator,
                                 DayPlan?day        = null,
                                 bool garnishSelect = false)
        : base(dialogService)
    {
        this.recipeService   = recipeService;
        this.recipeFiltrator = recipeFiltrator;
        if (garnishSelect && day != null)
        {
            List <Guid> possibleGarnishes = day.Recipe !.Garnishes.ConvertAll(x => x.ID);
            recipies = recipeService.GetProjected <RecipeListViewDto>(x => possibleGarnishes.Contains(x.ID));
        }
        else
        {
            recipies = recipeService.GetProjected <RecipeListViewDto>();
        }

        // CollectionViewSource must be created on UI thread
        Application.Current.Dispatcher.Invoke(() => RecipiesSource = new CollectionViewSource()
        {
            Source = recipies
        });

        if (!garnishSelect && day != null)
        {
            // TODO: Prepare string separately
            var sb = new StringBuilder();

            if (day.NeededDishTypes?.Any(x => x.IsChecked && x.CanBeRemoved) == true)
            {
                foreach (TagEdit dishType in day.NeededDishTypes)
                {
                    sb.Append(Consts.TagSymbol)
                    .Append('"')
                    .Append(dishType.Name)
                    .Append('"');

                    if (dishType != day.NeededDishTypes.Last())
                    {
                        sb.Append(" or ");
                    }
                }
            }

            if (day.NeededMainIngredients?.Any(x => x.IsChecked && x.CanBeRemoved) == true)
            {
                bool needEnd = false;
                if (sb.Length > 0)
                {
                    if (day.NeededDishTypes?.Count > 1)
                    {
                        sb.Insert(0, '(')
                        .Append(')');
                    }

                    sb.Append(" and ");

                    if (day.NeededMainIngredients.Count > 1)
                    {
                        sb.Append('(');
                        needEnd = true;
                    }
                }

                foreach (TagEdit mainIngredient in day.NeededMainIngredients)
                {
                    sb.Append(Consts.TagSymbol)
                    .Append('\"')
                    .Append(mainIngredient.Name)
                    .Append('\"');

                    if (mainIngredient != day.NeededMainIngredients.Last())
                    {
                        sb.Append(" or ");
                    }
                }

                if (needEnd)
                {
                    sb.Append(')');
                }
            }

            FilterText = sb.ToString();
        }
    }