public MainPageViewModel()
        {
            MessagingCenter.Subscribe <NoteDetailsViewModel>(this, "Unselect", (sender) =>
            {
                SelectedNote = null;
            });

            AddCommand = new Command(() =>
            {
                if (NewNote != "" && NewNote != null)
                {
                    NoteItem item = new NoteItem();

                    if (AllNotes.Count == 0)
                    {
                        item.Id = 1;
                    }
                    else
                    {
                        item.Id = AllNotes.LastOrDefault().Id + 1;
                    }

                    item.NoteText = NewNote;

                    AllNotes.Add(item);

                    AddToDB(item);

                    NewNote = string.Empty;
                }
            });

            SelectedNoteCommand = new Command(async() =>
            {
                if (SelectedNote != null)
                {
                    var noteDetailsVM              = new NoteDetailsViewModel(SelectedNote, AllNotes, ApiURL);
                    var noteDetailsPage            = new NoteDetails();
                    noteDetailsPage.BindingContext = noteDetailsVM;
                    await Application.Current.MainPage.Navigation.PushAsync(noteDetailsPage);
                }
            });
        }
Exemple #2
0
        public NoteDetailsViewModel(NoteItem note, ObservableCollection <NoteItem> collection, string apiurl)
        {
            selectedNote = note;
            notes        = collection;
            apiURL       = apiurl;


            MessagingCenter.Subscribe <EditNoteViewModel, NoteItem>(this, "Updated Note", (sender, arg) =>
            {
                MessagingCenter.Unsubscribe <EditNoteViewModel>(this, "Updated Note");

                UpdateDB(arg);
                SelectedNote = arg;
            });

            EditCommand = new Command(async() =>
            {
                var editNoteVM              = new EditNoteViewModel(SelectedNote, Notes);
                var editNotePage            = new EditNote();
                editNotePage.BindingContext = editNoteVM;
                await Application.Current.MainPage.Navigation.PushAsync(editNotePage);
            });

            DeleteCommand = new Command(async() =>
            {
                DeleteFromDB(Notes.ElementAt(Notes.IndexOf(SelectedNote)));

                Notes.Remove(Notes.ElementAt(Notes.IndexOf(SelectedNote)));



                //this.Notes.CollectionChanged += this.Notes.CollectionChanged;
                //NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)

                await Application.Current.MainPage.Navigation.PopAsync();
            });

            DismissCommand = new Command(async() =>
            {
                MessagingCenter.Send(this, "Unselect");
                await Application.Current.MainPage.Navigation.PopAsync();
            });
        }
        public EditNoteViewModel(NoteItem oldText, ObservableCollection <NoteItem> collection)
        {
            editText     = oldText;
            notes        = collection;
            previousText = oldText.NoteText;
            previousId   = oldText.Id;

            ConfirmCommand = new Command(async() =>
            {
                foreach (NoteItem item in Notes)
                {
                    if (item.Id == previousId)
                    {
                        updatedItem = item;
                    }
                }

                Notes[Notes.IndexOf(updatedItem)] = EditText;

                MessagingCenter.Send(this, "Updated Note", EditText);


                /* var noteDetailsVM = new NoteDetailsViewModel(EditText, Notes);
                 * var noteDetailsPage = new NoteDetails();
                 * noteDetailsPage.BindingContext = noteDetailsVM;
                 * Application.Current.MainPage.Navigation
                 *   .InsertPageBefore(noteDetailsPage, Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault());
                 *
                 * Application.Current.MainPage.Navigation
                 *   .RemovePage(Application.Current.MainPage.Navigation.NavigationStack.LastOrDefault());*/

                await Application.Current.MainPage.Navigation.PopAsync();
            });

            CancelCommand = new Command(async() =>
            {
                EditText.NoteText = previousText;
                await Application.Current.MainPage.Navigation.PopAsync();
            });
        }