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/AuthorityReportsPage.xaml.cs

98 lines
3.2 KiB

6 months ago
using Justice.Helpers;
using Justice.Models;
using System.Collections.ObjectModel;
using Justice.Services;
namespace Justice.Views
{
public partial class AuthorityReportsPage : ContentPage
{
private readonly DatabaseHelper _dbHelper;
public ObservableCollection<IncidentReport> Reports { get; set; }
public AuthorityReportsPage()
{
InitializeComponent();
_dbHelper = new DatabaseHelper();
Reports = new ObservableCollection<IncidentReport>();
ReportsListView.ItemsSource = Reports;
}
protected override async void OnAppearing()
{
base.OnAppearing();
await LoadReportsAsync();
}
private async Task LoadReportsAsync()
{
try
{
Reports.Clear();
6 months ago
// Fetch reports from the database and sort them by DateTime in descending order
6 months ago
var reportsFromDb = await _dbHelper.GetAllAsync<IncidentReport>();
6 months ago
var sortedReports = reportsFromDb.OrderByDescending(r => r.DateTime).ToList();
foreach (var report in sortedReports)
6 months ago
{
Reports.Add(report);
}
}
catch (Exception ex)
{
await DisplayAlert("Error", $"Failed to load reports: {ex.Message}", "OK");
}
}
6 months ago
6 months ago
private async void OnUpdateStatusClicked(object sender, EventArgs e)
{
var button = sender as Button;
if (button?.CommandParameter is IncidentReport selectedReport)
{
selectedReport.Status = "Resolved";
try
{
await _dbHelper.UpdateAsync(selectedReport);
await DisplayAlert("Success", "Status updated to Resolved.", "OK");
// Push notification to end user (dummy logic)
await DisplayAlert("Notification", $"Notification sent to {selectedReport.ReporterName}.", "OK");
await LoadReportsAsync();
}
catch (Exception ex)
{
await DisplayAlert("Error", $"Failed to update status: {ex.Message}", "OK");
}
}
}
private async void OnViewAttachmentClicked(object sender, EventArgs e)
{
var button = sender as Button;
if (button?.CommandParameter is IncidentReport selectedReport)
{
if (!string.IsNullOrEmpty(selectedReport.AttachmentPath))
{
try
{
await Launcher.OpenAsync(new OpenFileRequest
{
File = new ReadOnlyFile(selectedReport.AttachmentPath)
});
}
catch (Exception ex)
{
await DisplayAlert("Error", $"Failed to open attachment: {ex.Message}", "OK");
}
}
else
{
await DisplayAlert("Error", "No attachment found for this report.", "OK");
}
}
}
}
}