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

170 lines
6.5 KiB

using Justice.Services;
using Justice.Helpers;
using Justice.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace Justice.Views
{
public partial class IncidentReportPage : ContentPage
{
private readonly DatabaseHelper _databaseHelper;
private readonly EmailService _emailService;
private string _selectedAttachmentPath; // Store the file path of the selected attachment
// Mapping Incident Types to Authority Emails
private readonly Dictionary<string, string> _incidentTypeToEmail = new()
{
{ "Accident", "amreitsyanf@gmail.com" },
{ "Fire", "policed282@gmail.com" },
{ "Crime", "policed282@gmail.com" },
{ "Medical", "amreitsyanf@gmail.com" },
{ "Other", "general_authority@example.com" }
};
public IncidentReportPage()
{
InitializeComponent();
_databaseHelper = new DatabaseHelper();
_emailService = new EmailService();
}
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;
}
catch (Exception ex)
{
LocationEntry.Text = "Unable to fetch location.";
Console.WriteLine($"Error fetching location: {ex.Message}");
}
finally
{
await SetLoadingStateAsync(false);
}
}
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;
}
await SetLoadingStateAsync(true); // Show loader
try
{
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,
AttachmentPath = _selectedAttachmentPath
};
// Save report to database
await _databaseHelper.InsertAsync(report);
// Get the email address for the selected incident type
string incidentType = report.IncidentType;
if (_incidentTypeToEmail.TryGetValue(incidentType, out var recipientEmail))
{
// 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
bool emailSent = await _emailService.SendEmailWithAttachmentAsync(recipientEmail, "Incident Report", emailBody, _selectedAttachmentPath);
if (emailSent)
{
Console.WriteLine($"Email sent to: {recipientEmail}");
await DisplayAlert("Success", "Incident report submitted and email sent.", "OK");
}
else
{
await DisplayAlert("Error", "Failed to send email.", "OK");
}
}
else
{
await DisplayAlert("Error", "No authority found for the selected incident type.", "OK");
}
}
catch (Exception ex)
{
await DisplayAlert("Error", $"Failed to submit report: {ex.Message}", "OK");
}
finally
{
await SetLoadingStateAsync(false); // Hide loader
}
}
}
}