Forum General

MVVM Qeustion does this belong in the viewmodel or model?

LJongLJong NLMember ✭✭✭

Hey all,

I am writing a app myself and I want to respect the MVVM way when making this app. There is only one thing bothering me a little about what goes into the model and what doesn't. I do know the properties of the ObservableCollection goes in a seperate model, but I want to know if I placed these things at the right location.

Are the public bools filterstates supposed to be in the model or in the viewmodel shown in the example below:

FilterPage viewmodel

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using BNApp.BL;
using BNApp.Controls;
using Xamarin.Forms;

namespace BNApp.ViewModels {
    class RelationFilterPageViewModel : INotifyPropertyChanged {
        /// <summary>
        /// Instance of the BNPage to allow acces to it's methods and properties. The values are parsed and set in the constructor.
        /// </summary>
        private BNPage _bnPage;

        public RelationFilterPageViewModel(BNPage currentbnPage) {
            _bnPage = currentbnPage;

        }

        /// <summary>
        /// ICommand attached to the button that pops the current page from the stack.
        /// </summary>
        public ICommand CancelFiltersCommand {
            get {
                return new Command(() => {
                    _bnPage.Navigation.PopModalAsync();
                });
            }
        }

        /// <summary>
        /// ICommand attached to the save button that pops the current page from the stack. This method will update the itemsSource of GetRelaties() depending on the filters chosen. 
        /// </summary>
        public ICommand SaveFiltersCommand {
            get {
                return new Command(() => {
                    //todo ReloadRelations based on filter
                    _bnPage.Navigation.PopModalAsync();

                });
            }
        }
        /// <summary>
        /// ClientFilterState that returns a value saved inside the config file. And when changed sets the file with the value; This boolean is bound to the Switch Toggled property.
        /// </summary>
        public bool ClientFilterState {
            get { return Globals.ClientState; }
            set {
                Globals.ClientState = value;
            }
        }

        /// <summary>
        /// ExClientFilterState that returns a value saved inside the config file. And when changed sets the file with the value; This boolean is bound to the Switch Toggled property.
        /// </summary>
        public bool ExClientFilterstate {
            get { return Globals.ExClientState; }
            set { Globals.ExClientState = value; }
        }
        /// <summary>
        /// ProspectFilterState that returns a value saved inside the config file. And when changed sets the file with the value; This boolean is bound to the Switch Toggled property.
        /// </summary>
        public bool ProspectFilterState {
            get { return Globals.ProspectState; }
            set { Globals.ProspectState = value; }
        }
        /// <summary>
        /// RemainingFilterState that returns a value saved inside the config file. And when changed sets the file with the value; This boolean is bound to the Switch Toggled property.
        /// </summary>
        public bool RemainingFilterState {
            get { return Globals.RemainingState; }
            set { Globals.RemainingState = value; }
        }
        /// <summary>
        /// NotInterestedFilterState that returns a value saved inside the config file. And when changed sets the file with the value; This boolean is bound to the Switch Toggled property.
        /// </summary>
        public bool NotInterestingFilterState {
            get { return Globals.NotInterestingState; }
            set {
                Globals.NotInterestingState = value;

            }
        }
        /// <summary>
        /// DisplaySpecificRelationFilterState that returns a value saved inside the config file. And when changed sets the file with the value; This boolean is bound to the Switch Toggled property.
        /// </summary>
        public bool DisplaySpecificRelationsFilterState {
            get { return Globals.DisplaySpecificRelationsFilterState; }
            set {
                Globals.DisplaySpecificRelationsFilterState = value;
                //OnPropertyChanged(nameof(DisplaySpecificRelationsFilterState));
            }
        }


        /// <summary>
        /// When the property is changed in this case "The Filterstates" it will notify the UI and update this change.
        /// </summary>
        /// <param name="propertyName"></param>
        protected virtual void OnPropertyChanged(string propertyName) {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

Cheers,

Luuk

Best Answer

Answers

  • AlexDunnAlexDunn USMember ✭✭✭

    If those properties impact anything in your View, then you are right to have them in your ViewModel.

  • LJongLJong NLMember ✭✭✭

    @AlexDunn said:
    If those properties impact anything in your View, then you are right to have them in your ViewModel.

    Yes they do they are bound to to the IsToggled property of the switches :)

Sign In or Register to comment.