-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPage.xaml.cs
More file actions
291 lines (247 loc) · 9.1 KB
/
MainPage.xaml.cs
File metadata and controls
291 lines (247 loc) · 9.1 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Views;
using FieldNotesApp.Models;
using FieldNotesApp.Services;
using Microsoft.Maui.Media;
namespace FieldNotesApp
{
public partial class MainPage : ContentPage
{
private readonly DatabaseService _database;
private bool _isMenuOpen = false;
private bool _menuInitialized = false;
public MainPage(DatabaseService database)
{
InitializeComponent();
_database = database;
}
protected override void OnAppearing()
{
base.OnAppearing();
LoadEntries(); // Refresh list when returning to page
// Initialize menu animation on first load
if (!_menuInitialized)
{
_ = InitializeMenu();
}
}
private async void LoadEntries()
{
try
{
var entries = await _database.GetAllEntriesAsync();
// Create display models with additional properties
var displayEntries = entries.Select(e => new EntryDisplayModel
{
Id = e.Id,
EntryName = e.EntryName,
CreatedAt = e.CreatedAt,
NumOfMedia = e.FilePaths.Count
}).OrderByDescending(e => e.CreatedAt).ToList();
EntriesCollectionView.ItemsSource = displayEntries;
}
catch (Exception ex)
{
await DisplayAlertAsync("Error", $"Failed to load entries: {ex.Message}", "OK");
}
}
private async void OnEntrySelected(object sender, SelectionChangedEventArgs e)
{
if (e.CurrentSelection.FirstOrDefault() is EntryDisplayModel selectedEntry)
{
// Load full entry from database and navigate to detail page
var photoEntry = await _database.GetEntryAsync(selectedEntry.Id);
if (photoEntry != null)
{
await Navigation.PushAsync(new PhotoDetailPage(_database, photoEntry.FilePaths, photoEntry.Id));
}
// Deselect the item
EntriesCollectionView.SelectedItem = null;
}
}
private async void OnAddExistingClicked(object sender, EventArgs e)
{
await CloseMenu();
try
{
var mediaFiles = await MediaPicker.PickPhotosAsync(new MediaPickerOptions
{
SelectionLimit = 50,
CompressionQuality = 100,
RotateImage = true,
PreserveMetaData = true,
});
if (mediaFiles != null && mediaFiles.Any())
{
var filePaths = new List<string>();
foreach (var media in mediaFiles)
{
filePaths.Add(media.FullPath);
}
// Navigate once with all file paths
await Navigation.PushAsync(new PhotoDetailPage(_database, filePaths));
}
}
catch (Exception ex)
{
await DisplayAlertAsync("Error", $"Failed to select media: {ex.Message}", "OK");
}
}
private async void OnTakePhotoClicked(object sender, EventArgs e)
{
await CloseMenu();
try
{
var cameraStatus = await Permissions.CheckStatusAsync<Permissions.Camera>();
if (cameraStatus != PermissionStatus.Granted)
{
cameraStatus = await Permissions.RequestAsync<Permissions.Camera>();
}
if (cameraStatus != PermissionStatus.Granted)
{
await DisplayAlertAsync("Permission Denied", "Camera permission is required", "OK");
return;
}
if (!MediaPicker.Default.IsCaptureSupported)
{
await DisplayAlertAsync("Not Supported", "Camera is not available", "OK");
return;
}
var photo = await MediaPicker.Default.CapturePhotoAsync();
if (photo != null)
{
#if ANDROID
await AndroidMediaSaver.SavePhotoToDcimAsync(photo);
Toast.Make("A copy has been saved to your camera folder").Show();
#endif
var filePaths = new List<string> { photo.FullPath };
await Navigation.PushAsync(new PhotoDetailPage(_database, filePaths));
}
}
catch (Exception ex)
{
await DisplayAlertAsync("Error", $"Failed: {ex.Message}", "OK");
}
}
private async void OnRecordVideoClicked(object sender, EventArgs e)
{
await CloseMenu();
try
{
var video = await MediaPicker.CaptureVideoAsync();
if (video != null)
{
#if ANDROID
Toast.Make("A copy has been saved to your camera folder").Show();
#endif
var filePaths = new List<string> { video.FullPath };
await Navigation.PushAsync(new PhotoDetailPage(_database, filePaths));
}
}
catch (Exception ex)
{
await DisplayAlertAsync("Error", $"Failed: {ex.Message}", "OK");
}
}
private async void OnFabClicked(object sender, EventArgs e)
{
if (_isMenuOpen)
{
await CloseMenu();
}
else
{
await OpenMenu();
}
}
private async Task OpenMenu()
{
_isMenuOpen = true;
// Now show with animation
MenuItems.IsVisible = true;
// Fade overlay smoothly
MenuOverlay.IsVisible = true;
await MenuOverlay.FadeToAsync(0.5, 200);
// Rotate FAB icon
FabIcon.RotateToAsync(45, 250, Easing.SpringOut);
// Animate menu items
await Task.WhenAll(
MenuItems.TranslateToAsync(0, 0, 300, Easing.CubicOut),
MenuItems.FadeToAsync(1, 250),
MenuItems.ScaleToAsync(1, 300, Easing.SpringOut)
);
}
private async Task CloseMenu()
{
_isMenuOpen = false;
// Rotate FAB back
FabIcon.RotateToAsync(0, 200, Easing.CubicIn);
// Animate everything away
await Task.WhenAll(
MenuItems.TranslateToAsync(0, 50, 200, Easing.CubicIn),
MenuItems.FadeToAsync(0, 200),
MenuItems.ScaleToAsync(0.8, 200, Easing.CubicIn)
);
MenuOverlay.IsVisible = false;
MenuItems.IsVisible = false;
MenuOverlay.Opacity = 0.5; // Reset for next time
}
private async Task InitializeMenu()
{
MenuOverlay.Opacity = 0;
MenuOverlay.IsVisible = true;
MenuItems.IsVisible = true;
MenuItems.Opacity = 0;
await Task.Delay(50);
MenuOverlay.Opacity = 0.5;
await Task.WhenAll(
MenuItems.TranslateToAsync(0, 0, 1, Easing.Linear),
MenuItems.FadeToAsync(1, 1),
MenuItems.ScaleToAsync(1, 1)
);
// Instantly hide again
MenuOverlay.IsVisible = false;
MenuOverlay.Opacity = 0;
MenuItems.IsVisible = false;
MenuItems.TranslationY = 100;
MenuItems.Opacity = 0;
MenuItems.Scale = 0.8;
_menuInitialized = true;
}
private async void OnOverlayTapped(object sender, EventArgs e)
{
await CloseMenu();
}
private async void OnDeleteTapped(object sender, EventArgs e)
{
var border = (Border)sender;
var displayEntry = (EntryDisplayModel)border.BindingContext;
bool confirm = await DisplayAlertAsync(
"Delete Entry",
"Are you sure you want to delete this entry? The Photo will remain in your gallery.",
"Yes",
"No");
if (confirm)
{
var fullEntry = await this._database.GetEntryAsync(displayEntry.Id);
if (fullEntry != null)
{
await this._database.DeleteEntryAsync(fullEntry);
LoadEntries();
}
else
{
await DisplayAlertAsync("Error", "Entry not found", "OK");
}
}
}
// Helper class for display
public class EntryDisplayModel
{
public int Id { get; set; }
public string EntryName { get; set; }
public DateTime CreatedAt { get; set; }
public int NumOfMedia {get; set; }
}
}
}