-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhotoDetailPage.xaml.cs
More file actions
164 lines (143 loc) · 5.46 KB
/
PhotoDetailPage.xaml.cs
File metadata and controls
164 lines (143 loc) · 5.46 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
using Microsoft.Maui.Controls;
using CommunityToolkit.Maui.Views;
using FieldNotesApp.Services;
using FieldNotesApp.Models;
using System.Collections.ObjectModel;
namespace FieldNotesApp
{
public partial class PhotoDetailPage : ContentPage
{
private readonly DatabaseService _database;
private double? _latitude;
private double? _longitude;
private byte[] _voiceRecordingBytes;
private int _entryId;
private List<string> _filePaths;
public ObservableCollection<MediaItem> MediaItems { get; set; }
public PhotoDetailPage(DatabaseService database, List<string> filepaths, int entryId = 0)
{
InitializeComponent();
_database = database;
_entryId = entryId;
_filePaths = filepaths;
// Initialize media items
MediaItems = new ObservableCollection<MediaItem>(
_filePaths.Select(MediaItem.FromPath)
);
// Set binding context
BindingContext = this;
// Update media counter
UpdateMediaCounter();
// Listen to carousel position changes
MediaCarousel.CurrentItemChanged += OnCarouselItemChanged;
// If editing existing entry, load its data
if (_entryId > 0)
{
LoadExistingEntry(_entryId);
}
else
{
EntryNameField.Text = "Note " + DateTime.Now.Date.ToString();
}
}
private void OnCarouselItemChanged(object sender, CurrentItemChangedEventArgs e)
{
UpdateMediaCounter();
}
private void UpdateMediaCounter()
{
var currentIndex = MediaCarousel.Position;
var totalCount = MediaItems.Count;
MediaCountLabel.Text = $"{currentIndex + 1} / {totalCount}";
}
private async void LoadExistingEntry(int entryId)
{
try
{
var entry = await _database.GetEntryAsync(entryId);
if (entry != null)
{
EntryNameField.Text = entry.EntryName;
_latitude = entry.Latitude;
_longitude = entry.Longitude;
NotesEditor.Text = entry.Notes;
if (entry.Latitude.HasValue && entry.Longitude.HasValue)
{
LocationLabel.Text = $"Lat: {entry.Latitude:F6}, Lon: {entry.Longitude:F6}";
}
}
}
catch (Exception ex)
{
await DisplayAlertAsync("Error", $"Failed to load entry: {ex.Message}", "OK");
}
}
private async void OnGeotagClicked(object sender, EventArgs e)
{
try
{
var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
if (status != PermissionStatus.Granted)
{
status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
}
if (status == PermissionStatus.Granted)
{
var location = await Geolocation.GetLocationAsync(new GeolocationRequest
{
DesiredAccuracy = GeolocationAccuracy.Medium,
Timeout = TimeSpan.FromSeconds(10)
});
if (location != null)
{
_latitude = location.Latitude;
_longitude = location.Longitude;
LocationLabel.Text = $"Lat: {_latitude:F6}, Lon: {_longitude:F6}";
}
}
else
{
await DisplayAlertAsync("Permission Denied", "Location permission is required", "OK");
}
}
catch (Exception ex)
{
await DisplayAlertAsync("Error", $"Failed to get location: {ex.Message}", "OK");
}
}
private async void OnRecordClicked(object sender, EventArgs e)
{
await DisplayAlertAsync("Coming Soon", "Voice recording will be implemented next", "OK");
}
private async void OnSaveClicked(object sender, EventArgs e)
{
try
{
// Save voice recording if exists
int? voiceRecordingId = null;
if (_voiceRecordingBytes != null)
{
voiceRecordingId = await _database.SaveVoiceRecordingAsync(_voiceRecordingBytes, 0);
}
// Create and save the entry
var entry = new NoteEntry
{
Id = _entryId, // Important for updates
EntryName = EntryNameField.Text,
VoiceRecordingId = voiceRecordingId,
Latitude = _latitude,
Longitude = _longitude,
Notes = NotesEditor.Text,
FilePaths = _filePaths
};
await _database.SaveNoteEntryAsync(entry);
await DisplayAlertAsync("Success", "Entry saved to database!", "OK");
await Navigation.PopAsync();
}
catch (Exception ex)
{
await DisplayAlertAsync("Error", $"Failed to save: {ex.Message}", "OK");
}
}
}
}