Exemple #1
0
        public MainPageViewModel()
        {
            List <Note> notesList = App.Database.GetNotesAsync().Result;

            foreach (var note in notesList)
            {
                notes.Add(note);
            }
            EraseCommand = new Command(() => {
                Note = string.Empty;
            });

            SaveCommand = new Command(async() => {
                Note note = new Note()
                {
                    Text = Note, Date = DateTime.Now
                };
                Notes.Add(note);
                await App.Database.SaveNoteAsync(note);
                Note = string.Empty;
            });

            SelectionChangedCommand = new Command(async() => {
                var detailVM = new DetailPageViewModel(SelectedNote);

                var detailView            = new DetailPage();
                detailView.BindingContext = detailVM;
                await Application.Current.MainPage.Navigation.PushAsync(detailView);
            });
        }
Exemple #2
0
        public NoteListViewModel(IPageService pageService)
        {
            _pageService = pageService;
            GetData();
            EraseCommand = new Command(() => {
                Note = string.Empty;
            });

            AddTaskCommand = new Command((parameter) =>
            {
                _pageService.PushAsync(new NoteEntryPage()
                {
                    BindingContext = this
                });
            });

            SaveCommand = new Command(async(parameter) => {
                if (SaveText.Equals("Save"))
                {
                    Note note = new Note()
                    {
                        Text = Note, Date = DateTime.Now
                    };
                    Notes.Add(note);
                    await App.Database.SaveNoteAsync(note);
                    Note = string.Empty;
                }
                else if (SelectedNote != null)
                {
                    int currentIndex = Notes.IndexOf(SelectedNote);
                    Notes.Remove(SelectedNote);
                    Note note = new Note()
                    {
                        Text = Note, Date = DateTime.Now, ID = SelectedNote.ID
                    };
                    await App.Database.SaveNoteAsync(note);
                    Notes.Insert(currentIndex, note);
                    SelectedNote = null;
                    Note         = string.Empty;
                    SaveText     = "Save";
                }
                _pageService.PopAsync();
            });

            SelectionChangedCommand = new Command(async() => {
                var detailVM = new DetailPageViewModel(SelectedNote);

                var detailView            = new DetailPage();
                detailView.BindingContext = detailVM;
                await Application.Current.MainPage.Navigation.PushAsync(detailView);
            });

            DeleteCommand = new Command(async(parameter) => {
                if (parameter != null)
                {
                    Note currentNote = (parameter as Note);
                    SelectedNote     = currentNote;
                    Notes.Remove(currentNote);
                    await App.Database.DeleteNoteAsync(currentNote);
                }
            });

            EditCommand = new Command((parameter) => {
                if (parameter != null)
                {
                    Note currentNote = (parameter as Note);
                    SelectedNote     = currentNote;
                    Note             = currentNote.Text;
                    SaveText         = "Update";
                    _pageService.PushAsync(new NoteEntryPage()
                    {
                        BindingContext = this
                    });
                }
            });

            SearchCommand = new Command((parameter) => {
                var textchanged = parameter as TextChangedEventArgs;
                if (textchanged != null)
                {
                    Notes.Clear();
                    foreach (var note in _notesList.Where(i => i.Text.StartsWith(textchanged.NewTextValue)))
                    {
                        Notes.Add(note);
                    }
                }
            });
        }