You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Seekers/Views/IncidentReportPage.xaml.cs

162 lines
5.6 KiB

6 months ago
using Justice.Services;
using Justice.Helpers;
using Justice.Models;
6 months ago
using Microsoft.Maui.Storage;
using System;
6 months ago
using System.IO;
using System.Threading.Tasks;
using MailKit;
namespace Justice.Views
{
public partial class IncidentReportPage : ContentPage
{
private readonly DatabaseHelper _databaseHelper;
private readonly EmailService _emailService;
6 months ago
private string _selectedAttachmentPath; // Store the file path of the selected attachment
public IncidentReportPage()
{
InitializeComponent();
_databaseHelper = new DatabaseHelper();
_emailService = new EmailService();
}
6 months ago
public async Task SetLoadingStateAsync(bool isLoading)
{
LoadingIndicator.IsRunning = isLoading;
LoadingIndicator.IsVisible = isLoading;
}
protected override async void OnAppearing()
{
base.OnAppearing();
await FetchLocationAsync();
}
private async Task FetchLocationAsync()
{
try
{
var (latitude, longitude, address) = await GeolocationHelper.GetLocationAsync();
LocationEntry.Text = address;
6 months ago
}
catch (Exception ex)
{
LocationEntry.Text = "Unable to fetch location.";
Console.WriteLine($"Error fetching location: {ex.Message}");
}
6 months ago
finally
{
await SetLoadingStateAsync(false);
}
}
6 months ago
private async void OnChooseFileClicked(object sender, EventArgs e)
{
try
{
var customFileType = new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.Android, new[] { "*/*" } }, // Allow all files on Android
{ DevicePlatform.iOS, new[] { "public.data" } }, // iOS equivalent for all files
{ DevicePlatform.WinUI, new[] { "*" } } // Allow all files on Windows
});
var pickOptions = new PickOptions
{
PickerTitle = "Select a file",
FileTypes = customFileType
};
var result = await FilePicker.PickAsync(pickOptions);
if (result != null)
{
// Save the file path
_selectedAttachmentPath = result.FullPath;
AttachmentLabel.Text = $"Selected File: {Path.GetFileName(_selectedAttachmentPath)}";
}
else
{
AttachmentLabel.Text = "No file selected";
}
}
catch (Exception ex)
{
Console.WriteLine($"File selection error: {ex.Message}");
await DisplayAlert("Error", "Failed to select file.", "OK");
}
}
private async void OnSubmitReportClicked(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(ReporterNameEntry.Text) ||
IncidentTypePicker.SelectedItem == null ||
string.IsNullOrWhiteSpace(DescriptionEditor.Text))
{
await DisplayAlert("Error", "Please fill in all required fields.", "OK");
return;
}
var (latitude, longitude, address) = await GeolocationHelper.GetLocationAsync();
var report = new IncidentReport
{
ReporterName = ReporterNameEntry.Text.Trim(),
IncidentType = IncidentTypePicker.SelectedItem.ToString(),
Description = DescriptionEditor.Text.Trim(),
Latitude = latitude,
Longitude = longitude,
Address = address,
DateTime = DateTime.Now,
6 months ago
AttachmentPath = _selectedAttachmentPath // Save the attachment path
};
try
{
6 months ago
// Save the report to the database
await SetLoadingStateAsync(true);
await _databaseHelper.InsertAsync(report);
6 months ago
// Get the recipient email based on the incident type
string recipientEmail = _emailService.GetRecipientEmail(report.IncidentType);
6 months ago
// Prepare the email content
string emailBody = $"Incident Report:\n\n" +
$"Name: {report.ReporterName}\n" +
$"Type: {report.IncidentType}\n" +
$"Description: {report.Description}\n" +
$"Latitude: {report.Latitude}\n" +
$"Longitude: {report.Longitude}\n" +
$"Location: {report.Address}\n" +
$"Date/Time: {report.DateTime}";
// Send email
6 months ago
bool emailSent = await _emailService.SendEmailWithAttachmentAsync(recipientEmail, "Incident Report", emailBody, _selectedAttachmentPath);
if (emailSent)
{
await DisplayAlert("Success", "Incident report submitted and email alert sent.", "OK");
}
else
{
await DisplayAlert("Error", "Failed to send email alert.", "OK");
}
}
catch (Exception ex)
{
await DisplayAlert("Error", $"Failed to submit report: {ex.Message}", "OK");
}
6 months ago
finally
{
6 months ago
await SetLoadingStateAsync(false);
}
}
6 months ago
}
}