diff --git a/Helpers/EmailHelper.cs b/Helpers/EmailHelper.cs new file mode 100644 index 0000000..9801927 --- /dev/null +++ b/Helpers/EmailHelper.cs @@ -0,0 +1,33 @@ +using Microsoft.Maui.ApplicationModel.Communication; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace Justice.Helpers +{ + public static class EmailHelper + { + public static async Task SendEmailAsync(string subject, string body, string recipient) + { + try + { + var message = new EmailMessage + { + Subject = subject, + Body = body, + To = new List { recipient } + }; + + await Email.ComposeAsync(message); + } + catch (FeatureNotSupportedException) + { + throw new Exception("Email is not supported on this device or platform."); + } + catch (Exception ex) + { + throw new Exception($"Unable to send email: {ex.Message}"); + } + } + } +} diff --git a/Helpers/EmailService.cs b/Helpers/EmailService.cs new file mode 100644 index 0000000..0d82c30 --- /dev/null +++ b/Helpers/EmailService.cs @@ -0,0 +1,78 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Net.Mail; +using System.Threading.Tasks; + +namespace ChildGuard.Services +{ + public class EmailService + { + private readonly string _smtpServer = "smtp.gmail.com"; + private readonly int _smtpPort = 587; + private readonly string _senderEmail = "amritsyangtan1@gmail.com"; // Replace with your email + private readonly string _senderPassword = "bdcx ycpy cbrr szpm"; // Replace with your app password + private readonly Dictionary _incidentTypeToEmail; + + public EmailService() + { + // Map incident types to authority emails + _incidentTypeToEmail = new Dictionary + { + { "Accident", "amreitsyanf@gmail.com" }, + { "Crime", "crime-authority@example.com" }, + { "Fire", "fire-department@example.com" }, + { "Other", "general-authority@example.com" } + }; + } + + /// + /// Sends an email using SMTP. + /// + /// Recipient's email address. + /// Email subject. + /// Email body. + /// True if successful, otherwise false. + public async Task SendEmailAsync(string recipientEmail, string subject, string body) + { + try + { + using (var smtpClient = new SmtpClient(_smtpServer, _smtpPort)) + { + smtpClient.Credentials = new NetworkCredential(_senderEmail, _senderPassword); + smtpClient.EnableSsl = true; + + var mailMessage = new MailMessage + { + From = new MailAddress(_senderEmail), + Subject = subject, + Body = body, + IsBodyHtml = true + }; + + mailMessage.To.Add(recipientEmail); + + await smtpClient.SendMailAsync(mailMessage); + } + + Console.WriteLine("Email sent successfully."); + return true; + } + catch (Exception ex) + { + Console.WriteLine($"Error sending email: {ex.Message}"); + return false; + } + } + + /// + /// Gets the recipient email based on the incident type. + /// + /// Incident type. + /// Recipient email address. + public string GetRecipientEmail(string incidentType) + { + return _incidentTypeToEmail.TryGetValue(incidentType, out var email) ? email : "default-authority@example.com"; + } + } +} diff --git a/Helpers/GeolocationHelper.cs b/Helpers/GeolocationHelper.cs index bf7c3e1..ec8210a 100644 --- a/Helpers/GeolocationHelper.cs +++ b/Helpers/GeolocationHelper.cs @@ -1,22 +1,20 @@ -using System; -using System.Collections.Generic; +using Microsoft.Maui.ApplicationModel; using System.Linq; -using System.Text; using System.Threading.Tasks; -using Microsoft.Maui.ApplicationModel; namespace Justice.Helpers { - public class GeolocationHelper + public static class GeolocationHelper { /// - /// Retrieves the user's current latitude, longitude, and a readable address. + /// Fetches the user's current latitude, longitude, and readable address. /// /// A tuple containing Latitude, Longitude, and Address. - public static async Task<(double Latitude, double Longitude, string Address)> GetCurrentLocationAsync() + public static async Task<(double Latitude, double Longitude, string Address)> GetLocationAsync() { try { + // Request the user's current location var location = await Geolocation.Default.GetLocationAsync(new GeolocationRequest { DesiredAccuracy = GeolocationAccuracy.High, @@ -25,36 +23,30 @@ namespace Justice.Helpers if (location != null) { + // Reverse geocode the location to get the address var placemarks = await Geocoding.Default.GetPlacemarksAsync(location); var placemark = placemarks.FirstOrDefault(); - string address = "Address not found"; - if (placemark != null) - { - address = $"{placemark.SubLocality}, {placemark.Thoroughfare}, " + - $"{placemark.Locality}, {placemark.AdminArea}, {placemark.CountryName}"; - } + string address = placemark != null + ? $"{placemark.Thoroughfare}, {placemark.Locality}, {placemark.AdminArea}, {placemark.CountryName}" + : "Address not found"; return (location.Latitude, location.Longitude, address); } - // Return a default value if location is null - return (0, 0, "Unable to fetch location."); + throw new Exception("Location not available."); } catch (FeatureNotSupportedException) { - // Return a default value if location services are not supported - return (0, 0, "Location services are not supported on this device."); + throw new Exception("Location services are not supported on this device."); } catch (PermissionException) { - // Return a default value if permissions are not granted - return (0, 0, "Location permissions are not granted."); + throw new Exception("Location permissions are not granted."); } catch (Exception ex) { - // Return a default value for other errors - return (0, 0, $"An error occurred: {ex.Message}"); + throw new Exception($"Unable to fetch location: {ex.Message}"); } } } diff --git a/Helpers/SosService.cs b/Helpers/SosService.cs index b85d9c0..e870090 100644 --- a/Helpers/SosService.cs +++ b/Helpers/SosService.cs @@ -12,7 +12,7 @@ namespace Justice.Services try { // Fetch location and address - (double latitude, double longitude, string address) = await GeolocationHelper.GetCurrentLocationAsync(); + (double latitude, double longitude, string address) = await GeolocationHelper.GetLocationAsync(); // Prepare the SOS message diff --git a/Justice.csproj.user b/Justice.csproj.user index 3743e56..7fa239b 100644 --- a/Justice.csproj.user +++ b/Justice.csproj.user @@ -3,8 +3,8 @@ False net9.0-android - Samsung SM-M325F (Android 13.0 - API 33) - PhysicalDevice + Medium Phone API 35 (Android 15.0 - API 35) + Emulator Medium_Phone_API_35 diff --git a/Models/IncidentReport.cs b/Models/IncidentReport.cs index a5b2569..57116b2 100644 --- a/Models/IncidentReport.cs +++ b/Models/IncidentReport.cs @@ -11,7 +11,7 @@ namespace Justice.Models { [PrimaryKey, AutoIncrement] public int Id { get; set; } - public string ReportName { get; set; } + public string ReporterName { get; set; } public string IncidentType { get; set; } public string Description { get; set; } public double Latitude { get; set; } diff --git a/Views/DashboardPage.xaml.cs b/Views/DashboardPage.xaml.cs index 71e3fa8..75b9774 100644 --- a/Views/DashboardPage.xaml.cs +++ b/Views/DashboardPage.xaml.cs @@ -27,7 +27,7 @@ public partial class DashboardPage : ContentPage } private void OnIncidentReportClicked(object sender, EventArgs e) { - DisplayAlert("Report Incident button clicked", "Reprot", "OK"); + Navigation.PushAsync(new IncidentReportPage()); } private void OnSOSButtonClicked(object sender, EventArgs e) diff --git a/Views/IncidentReportPage.xaml b/Views/IncidentReportPage.xaml index 3bf726e..dea2739 100644 --- a/Views/IncidentReportPage.xaml +++ b/Views/IncidentReportPage.xaml @@ -1,12 +1,41 @@ - - - - \ No newline at end of file + Title="Report Incident"> + + + + +