-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainPage.xaml.cs
99 lines (89 loc) · 3.6 KB
/
MainPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using JournalApp.Models;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using Microsoft.Maui.Controls;
namespace JournalApp
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
BindingContext = new MainPageViewModel();
// Check if the view model is being instantiated correctly
System.Diagnostics.Debug.WriteLine("MainPageViewModel instantiated");
}
private async void AddEntryButton_Clicked(object sender, EventArgs e)
{
//Brainydaps editing
var newEntry = new JournalEntry { Date = DateTime.Now };
System.Diagnostics.Debug.WriteLine("AddEntryButtonclicked method was called and var new entry was made");
await Navigation.PushAsync(new JournalEntryPage
{
BindingContext = new JournalEntry { Date = DateTime.Now }
});
}
private async void EditEntryButton_Clicked(object sender, EventArgs e)
{
if (sender is Button button && button.CommandParameter is JournalEntry entry)
{
await Navigation.PushAsync(new JournalEntryPage
{
BindingContext = entry
});
}
}
private void DeleteEntryButton_Clicked(object sender, EventArgs e)
{
if (sender is Button button && button.CommandParameter is JournalEntry entry)
{
App.JournalEntries.Remove(entry);
(BindingContext as MainPageViewModel)?.RefreshEntries();
}
}
protected override void OnAppearing()
{
//Brainydaps editing
System.Diagnostics.Debug.WriteLine("OnAppearing method of MainPage was called");
base.OnAppearing();
(BindingContext as MainPageViewModel)?.RefreshEntries();
}
}
public class MainPageViewModel : INotifyPropertyChanged
{
private ObservableCollection<JournalEntry> journalEntries = new();
public ObservableCollection<JournalEntry> JournalEntries
{
get => journalEntries;
set
{
journalEntries = value;
OnPropertyChanged(nameof(JournalEntries));
// Check if the JournalEntries property is being updated correctly
System.Diagnostics.Debug.WriteLine("JournalEntries updated");
}
}
public MainPageViewModel()
{
RefreshEntries();
System.Diagnostics.Debug.WriteLine("RefreshEntries method called in MainPageViewModel");
JournalEntries = new ObservableCollection<JournalEntry>(App.JournalEntries.OrderByDescending(e => e.Date));
// Check if the JournalEntries collection is being populated correctly
System.Diagnostics.Debug.WriteLine("JournalEntries populated");
}
public void RefreshEntries()
{
JournalEntries = new ObservableCollection<JournalEntry>(App.JournalEntries.OrderByDescending(e => e.Date));
// Check if the RefreshEntries method is being called correctly
System.Diagnostics.Debug.WriteLine("RefreshEntries called");
//Brainydaps Editing
OnPropertyChanged(nameof(JournalEntries));
}
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}