parent
47f3cf95c7
commit
6f28748796
@ -1,39 +1,113 @@ |
||||
using Justice.Models; |
||||
using Justice.Views; |
||||
using Justice.Helpers; |
||||
using Justice.Models; |
||||
using System.Collections.ObjectModel; |
||||
using System.Threading.Tasks; |
||||
|
||||
namespace Justice.Views |
||||
{ |
||||
public partial class AddContactPage : ContentPage |
||||
{ |
||||
private readonly DatabaseHelper _databaseHelper; |
||||
public ObservableCollection<EmergencyContact> Contacts { get; set; } |
||||
|
||||
namespace Justice.Views; |
||||
public AddContactPage() |
||||
{ |
||||
InitializeComponent(); |
||||
_databaseHelper = new DatabaseHelper(); |
||||
Contacts = new ObservableCollection<EmergencyContact>(); |
||||
ContactsListView.ItemsSource = Contacts; |
||||
} |
||||
|
||||
public partial class AddContactPage : ContentPage |
||||
{ |
||||
private readonly DatabaseHelper _databaseHelper; |
||||
public AddContactPage() |
||||
{ |
||||
InitializeComponent(); |
||||
_databaseHelper = new DatabaseHelper(); |
||||
} |
||||
private async void OnSaveContactClicked(object sender, EventArgs e) |
||||
{ |
||||
try |
||||
{ |
||||
if (string.IsNullOrWhiteSpace(NameEntry.Text) || string.IsNullOrWhiteSpace(MobileNumberEntry.Text) || GroupPicker.SelectedItem == null) |
||||
{ |
||||
await DisplayAlert("Error", "All Fields Required", "OK"); |
||||
} |
||||
var contact = new EmergencyContact |
||||
{ |
||||
Name = NameEntry.Text, |
||||
MobileNumber = MobileNumberEntry.Text, |
||||
Group = GroupPicker.SelectedItem.ToString() |
||||
}; |
||||
await _databaseHelper.InsertAsync(contact); |
||||
await DisplayAlert("Success", "Contact Saved Successfully", "OK"); |
||||
await Navigation.PushAsync(new EmergencyContactsPage()); |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
await DisplayAlert("Success", $"{ex}", "OK"); |
||||
protected override async void OnAppearing() |
||||
{ |
||||
base.OnAppearing(); |
||||
await LoadContactsAsync(); |
||||
} |
||||
|
||||
private async Task LoadContactsAsync() |
||||
{ |
||||
try |
||||
{ |
||||
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 are required.", "OK"); |
||||
return; |
||||
} |
||||
|
||||
var contact = new EmergencyContact |
||||
{ |
||||
Name = NameEntry.Text.Trim(), |
||||
MobileNumber = MobileNumberEntry.Text.Trim(), |
||||
Group = GroupPicker.SelectedItem.ToString() |
||||
}; |
||||
|
||||
try |
||||
{ |
||||
await _databaseHelper.InsertAsync(contact); |
||||
await LoadContactsAsync(); |
||||
|
||||
NameEntry.Text = string.Empty; |
||||
MobileNumberEntry.Text = string.Empty; |
||||
GroupPicker.SelectedItem = null; |
||||
|
||||
await DisplayAlert("Success", "Contact saved successfully!", "OK"); |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
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