parent
47f3cf95c7
commit
6f28748796
@ -1,39 +1,113 @@ |
|||||||
using Justice.Models; |
|
||||||
using Justice.Views; |
|
||||||
using Justice.Helpers; |
using Justice.Helpers; |
||||||
|
using Justice.Models; |
||||||
|
using System.Collections.ObjectModel; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
namespace Justice.Views |
||||||
namespace Justice.Views; |
{ |
||||||
|
|
||||||
public partial class AddContactPage : ContentPage |
public partial class AddContactPage : ContentPage |
||||||
{ |
{ |
||||||
private readonly DatabaseHelper _databaseHelper; |
private readonly DatabaseHelper _databaseHelper; |
||||||
|
public ObservableCollection<EmergencyContact> Contacts { get; set; } |
||||||
|
|
||||||
public AddContactPage() |
public AddContactPage() |
||||||
{ |
{ |
||||||
InitializeComponent(); |
InitializeComponent(); |
||||||
_databaseHelper = new DatabaseHelper(); |
_databaseHelper = new DatabaseHelper(); |
||||||
|
Contacts = new ObservableCollection<EmergencyContact>(); |
||||||
|
ContactsListView.ItemsSource = Contacts; |
||||||
} |
} |
||||||
private async void OnSaveContactClicked(object sender, EventArgs e) |
|
||||||
|
protected override async void OnAppearing() |
||||||
|
{ |
||||||
|
base.OnAppearing(); |
||||||
|
await LoadContactsAsync(); |
||||||
|
} |
||||||
|
|
||||||
|
private async Task LoadContactsAsync() |
||||||
{ |
{ |
||||||
try |
try |
||||||
{ |
{ |
||||||
if (string.IsNullOrWhiteSpace(NameEntry.Text) || string.IsNullOrWhiteSpace(MobileNumberEntry.Text) || GroupPicker.SelectedItem == null) |
Contacts.Clear(); |
||||||
|
var contactsFromDb = await _databaseHelper.GetAllAsync<EmergencyContact>(); |
||||||
|
foreach (var contact in contactsFromDb) |
||||||
|
{ |
||||||
|
Contacts.Add(contact); |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
await DisplayAlert("Error", $"Failed to load contacts: {ex.Message}", "OK"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private async void OnSaveContactClicked(object sender, EventArgs e) |
||||||
|
{ |
||||||
|
if (string.IsNullOrWhiteSpace(NameEntry.Text) || |
||||||
|
string.IsNullOrWhiteSpace(MobileNumberEntry.Text) || |
||||||
|
GroupPicker.SelectedItem == null) |
||||||
{ |
{ |
||||||
await DisplayAlert("Error", "All Fields Required", "OK"); |
await DisplayAlert("Error", "All fields are required.", "OK"); |
||||||
|
return; |
||||||
} |
} |
||||||
|
|
||||||
var contact = new EmergencyContact |
var contact = new EmergencyContact |
||||||
{ |
{ |
||||||
Name = NameEntry.Text, |
Name = NameEntry.Text.Trim(), |
||||||
MobileNumber = MobileNumberEntry.Text, |
MobileNumber = MobileNumberEntry.Text.Trim(), |
||||||
Group = GroupPicker.SelectedItem.ToString() |
Group = GroupPicker.SelectedItem.ToString() |
||||||
}; |
}; |
||||||
|
|
||||||
|
try |
||||||
|
{ |
||||||
await _databaseHelper.InsertAsync(contact); |
await _databaseHelper.InsertAsync(contact); |
||||||
await DisplayAlert("Success", "Contact Saved Successfully", "OK"); |
await LoadContactsAsync(); |
||||||
await Navigation.PushAsync(new EmergencyContactsPage()); |
|
||||||
|
NameEntry.Text = string.Empty; |
||||||
|
MobileNumberEntry.Text = string.Empty; |
||||||
|
GroupPicker.SelectedItem = null; |
||||||
|
|
||||||
|
await DisplayAlert("Success", "Contact saved successfully!", "OK"); |
||||||
} |
} |
||||||
catch (Exception ex) |
catch (Exception ex) |
||||||
{ |
{ |
||||||
await DisplayAlert("Success", $"{ex}", "OK"); |
await DisplayAlert("Error", $"Failed to save contact: {ex.Message}", "OK"); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
|
private async void OnDeleteContactClicked(object sender, EventArgs e) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
Console.WriteLine("Delete button clicked."); // Debug log |
||||||
|
|
||||||
|
var button = sender as Button; |
||||||
|
if (button?.CommandParameter is EmergencyContact contactToDelete) |
||||||
|
{ |
||||||
|
Console.WriteLine($"Attempting to delete contact: {contactToDelete.Name}"); // Debug log |
||||||
|
|
||||||
|
bool confirm = await DisplayAlert("Delete Contact", |
||||||
|
$"Are you sure you want to delete {contactToDelete.Name}?", "Yes", "No"); |
||||||
|
|
||||||
|
if (!confirm) |
||||||
|
return; |
||||||
|
|
||||||
|
await _databaseHelper.DeleteAsync(contactToDelete); |
||||||
|
Contacts.Remove(contactToDelete); |
||||||
|
|
||||||
|
await DisplayAlert("Success", "Contact deleted successfully!", "OK"); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
Console.WriteLine("CommandParameter is null or invalid."); // Debug log |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception ex) |
||||||
|
{ |
||||||
|
Console.WriteLine($"Error in OnDeleteContactClicked: {ex.Message}"); // Debug log |
||||||
|
await DisplayAlert("Error", $"Failed to delete contact: {ex.Message}", "OK"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
} |
} |
@ -0,0 +1,25 @@ |
|||||||
|
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" |
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" |
||||||
|
x:Class="Justice.Views.SosPage" |
||||||
|
Title="SOS Alert"> |
||||||
|
|
||||||
|
<StackLayout Padding="20" VerticalOptions="CenterAndExpand"> |
||||||
|
<Label Text="Select Group for SOS Alert" FontSize="Medium" /> |
||||||
|
<Picker x:Name="GroupPicker"> |
||||||
|
<Picker.ItemsSource> |
||||||
|
<x:Array Type="{x:Type x:String}"> |
||||||
|
<x:String>Parents</x:String> |
||||||
|
<x:String>Friends</x:String> |
||||||
|
<x:String>Authority</x:String> |
||||||
|
</x:Array> |
||||||
|
</Picker.ItemsSource> |
||||||
|
</Picker> |
||||||
|
|
||||||
|
<Button Text="Send SOS Alert" |
||||||
|
Clicked="OnSendSosClicked" |
||||||
|
BackgroundColor="Red" |
||||||
|
TextColor="White" |
||||||
|
FontSize="Large" |
||||||
|
HorizontalOptions="Center" /> |
||||||
|
</StackLayout> |
||||||
|
</ContentPage> |
@ -0,0 +1,60 @@ |
|||||||
|
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"); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue