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.
60 lines
1.9 KiB
60 lines
1.9 KiB
using Justice.Helpers;
|
|
using Justice.Models;
|
|
using Justice.Services;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Justice.Views
|
|
{
|
|
public partial class SosPage : ContentPage
|
|
{
|
|
private readonly DatabaseHelper _databaseHelper;
|
|
private readonly SosService _sosService;
|
|
|
|
public SosPage()
|
|
{
|
|
InitializeComponent();
|
|
_databaseHelper = new DatabaseHelper();
|
|
_sosService = new SosService();
|
|
}
|
|
|
|
private async void OnSendSosClicked(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
// Get the selected group
|
|
var selectedGroup = GroupPicker.SelectedItem as string;
|
|
|
|
if (string.IsNullOrEmpty(selectedGroup))
|
|
{
|
|
await DisplayAlert("Error", "Please select a group.", "OK");
|
|
return;
|
|
}
|
|
|
|
// Fetch emergency contacts from the selected group
|
|
var contacts = await _databaseHelper.GetAllAsync<EmergencyContact>();
|
|
var groupContacts = contacts
|
|
.Where(contact => contact.Group.Equals(selectedGroup, StringComparison.OrdinalIgnoreCase))
|
|
.ToList();
|
|
|
|
if (!groupContacts.Any())
|
|
{
|
|
await DisplayAlert("Error", $"No contacts found in the group '{selectedGroup}'.", "OK");
|
|
return;
|
|
}
|
|
|
|
// Extract mobile numbers for the SOS alert
|
|
var emergencyNumbers = groupContacts.Select(contact => contact.MobileNumber).ToList();
|
|
|
|
// Send the SOS alert
|
|
await _sosService.SendSosAlertAsync(emergencyNumbers);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await DisplayAlert("Error", $"Failed to send SOS Alert: {ex.Message}", "OK");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|