Database view model using WHERE to select events

TomJefferisTomJefferis USMember
edited January 2017 in Xamarin.Forms

Hi, I have a database and i have connected it to a view page in a list, i want to have a where statement that narrows down the events so only some are displayed. would you recommend doing it in the view model or the actual view

Here is the view model

    [ImplementPropertyChanged]
    public class Database_ViewModel
    {
        bool _isLabelEmptyVisible { get; set; }
        TodoItem _selectedItem { get; set; }
        bool _isTapped { get; set; }
        int _count { get; set; }

        int Count
        {
            get { return _count; }
            set
            {
                _count = value;
                IsListViewVisible = (_count != 0);
                IsLabelEmptyVisible = (_count == 0);
            }
        }


        public ObservableCollection<TodoItem> List { get; set; } = new ObservableCollection<TodoItem>();
        public bool IsLabelEmptyVisible { get; set; }
        public bool IsListViewVisible { get; set; }


        public Database_ViewModel()
        {

            List.Add(new TodoItem { Time = "1:2:3", Event = "509m" });
            List.Add(new TodoItem { Time = "1:2:3", Event = "5091m" });
            List.Add(new TodoItem { Time = "1:2:3", Event = "5029m" });
            List.Add(new TodoItem { Time = "1:2:3", Event = "50m Freestyle" });
            Count = List.Count;

        }

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @TomJefferis

    would you recommend doing it in the view model or the actual view

    I would make the method on the view take a parameter to be passed to the where clause - maybe with a default of the filter you currently have in mind.

    Then you can (now or later) put a field on the view to use as the CommandParameter binded back to a property for the where clause.

    This way you have your default filter in place - and could allow the user to do their own custom search.

  • TomJefferisTomJefferis USMember
    edited January 2017

    @ClintStLaurent said:
    @TomJefferis

    would you recommend doing it in the view model or the actual view

    I would make the method on the view take a parameter to be passed to the where clause - maybe with a default of the filter you currently have in mind.

    Then you can (now or later) put a field on the view to use as the CommandParameter binded back to a property for the where clause.

    This way you have your default filter in place - and could allow the user to do their own custom search.

    So have the listview item source in the table, like this? sorry im new to xamarin

    ` _sqLiteConnection = DependencyService.Get().GetConnection();

                _sqLiteConnection.CreateTable<TodoItem>();
    
                lv.ItemsSource = _sqLiteConnection.Table<TodoItem>().Where(t => t.Event == "50m Freestyle");
    

    `

    At the moment my view is

    `this.BindingContext = new Database_ViewModel();
    ListView lv = new ListView { HasUnevenRows = true };
    lv.ItemTemplate = new DataTemplate(() =>
    {

                    var grid = new Grid();
                    grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
                    grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
                    grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
    
    
                    Label lTime = new Label();
                    lTime.SetBinding(Label.TextProperty, "Time", stringFormat: "Time: {0}");
    
                // LABEL Event
                Label lEvent = new Label();
                    lEvent.SetBinding(Label.TextProperty, "Event", stringFormat: "Event: {0}");
    
    
                //Label Date
                Label lSwam = new Label();
                    lSwam.SetBinding(Label.TextProperty, "Swam", stringFormat: "Date: {0}");
    
    
    
                    grid.Children.Add(lTime, 0, 0);
                    grid.Children.Add(lSwam, 1, 0);
                    return new ViewCell { View = grid };
                });
    
                lv.SetBinding(ListView.ItemsSourceProperty, "List");
                lv.SetBinding(ListView.IsVisibleProperty, "IsListViewVisible");
                lv.SetBinding(ListView.SelectedItemProperty, "SelectedItem");`
    
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @TomJefferis

    No. Your ViewModel really shouldn't know how its getting data. Usually you have a data access layer that does this. Your ViewModel asks the Data Access Layer for new records - the DAL gets the records and returns a collection. This way if you change data base engines (as an example) you don't have 1,000 references to change throughout your app: Instead you make one change in your DAL.

    You said you are new to Xamarin. Are you new to XAML/C#/MVVM design patterns in general? Have you ever done MVVM apps in the past; such as with WPF? If so this might give you a better 30,000ft view of the design architecture to employ. Its not done: I have more to write... But its more than nothing for now.
    https://redpillxamarin.com/2016/12/28/204-app-architecture/

Sign In or Register to comment.