|
|
|
using Justice.Helpers;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.Maui.ApplicationModel.Communication;
|
|
|
|
|
|
|
|
namespace Justice.Views
|
|
|
|
{
|
|
|
|
public partial class MapTimer : ContentPage
|
|
|
|
{
|
|
|
|
private CancellationTokenSource _cancellationTokenSource;
|
|
|
|
private readonly double _tolerance = 0.0001;
|
|
|
|
|
|
|
|
public MapTimer()
|
|
|
|
{
|
|
|
|
InitializeComponent();
|
|
|
|
FetchLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
private async void FetchLocation()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var (latitude, longitude, address) = await GeolocationHelper.GetLocationAsync();
|
|
|
|
SourceLatitudeEntry.Text = latitude.ToString();
|
|
|
|
SourceLongitudeEntry.Text = longitude.ToString();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
await DisplayAlert("Error", $"Failed to fetch location: {ex.Message}", "OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async void StartTrackingClicked(object sender, EventArgs e)
|
|
|
|
{
|
|
|
|
if (!double.TryParse(DestinationLatitudeEntry.Text, out double destinationLatitude) ||
|
|
|
|
!double.TryParse(DestinationLongitudeEntry.Text, out double destinationLongitude))
|
|
|
|
{
|
|
|
|
await DisplayAlert("Error", "Please enter valid destination coordinates.", "OK");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!int.TryParse(TimerEntry.Text, out int timerSeconds) || timerSeconds <= 0)
|
|
|
|
{
|
|
|
|
await DisplayAlert("Error", "Please enter a valid timer value in seconds.", "OK");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await StartTimerAsync(destinationLatitude, destinationLongitude, timerSeconds);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task StartTimerAsync(double destinationLatitude, double destinationLongitude, int timerSeconds)
|
|
|
|
{
|
|
|
|
var startTime = DateTime.Now;
|
|
|
|
var endTime = startTime.AddSeconds(timerSeconds);
|
|
|
|
_cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
|
|
while (DateTime.Now <= endTime)
|
|
|
|
{
|
|
|
|
if (_cancellationTokenSource.Token.IsCancellationRequested)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Fetch the current location
|
|
|
|
var (currentLatitude, currentLongitude, _) = await GeolocationHelper.GetLocationAsync();
|
|
|
|
|
|
|
|
// Update TimerLabel
|
|
|
|
var remainingTime = endTime - DateTime.Now;
|
|
|
|
TimerLabel.Text = $"Remaining Time: {remainingTime.Seconds}s";
|
|
|
|
|
|
|
|
// Check if the user has reached the destination
|
|
|
|
if (Math.Abs(destinationLatitude - currentLatitude) < _tolerance &&
|
|
|
|
Math.Abs(destinationLongitude - currentLongitude) < _tolerance)
|
|
|
|
{
|
|
|
|
var successMessage = "Safely reached the destination!";
|
|
|
|
await SendSmsNotification("9767581384", successMessage);
|
|
|
|
//await SendEmailNotification("amreitsyanf@gmail.com", "Journey Update", successMessage);
|
|
|
|
TimerLabel.Text = "Journey Complete.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Wait for 1 second before re-checking
|
|
|
|
await Task.Delay(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timer expired, user didn't reach the destination
|
|
|
|
var failureMessage = "Did not reach the destination on time.";
|
|
|
|
await SendSmsNotification("9767581384", failureMessage);
|
|
|
|
//await SendEmailNotification("amreitsyanf@gmail.com", "Journey Update", failureMessage);
|
|
|
|
//await DisplayAlert("Alert", failureMessage, "OK");
|
|
|
|
TimerLabel.Text = "Time Expired.";
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task SendSmsNotification(string phoneNumber, string message)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (Sms.Default.IsComposeSupported)
|
|
|
|
{
|
|
|
|
var smsMessage = new SmsMessage
|
|
|
|
{
|
|
|
|
Body = message,
|
|
|
|
Recipients = new List<string> { phoneNumber }
|
|
|
|
};
|
|
|
|
|
|
|
|
await Sms.Default.ComposeAsync(smsMessage);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
await DisplayAlert("Error", "SMS is not supported on this device.", "OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
await DisplayAlert("Error", $"Failed to send SMS: {ex.Message}", "OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task SendEmailNotification(string emailAddress, string subject, string body)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (Email.Default.IsComposeSupported)
|
|
|
|
{
|
|
|
|
var emailMessage = new EmailMessage
|
|
|
|
{
|
|
|
|
Subject = subject,
|
|
|
|
Body = body,
|
|
|
|
To = new List<string> { emailAddress }
|
|
|
|
};
|
|
|
|
|
|
|
|
await Email.Default.ComposeAsync(emailMessage);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
await DisplayAlert("Error", "Email is not supported on this device.", "OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
await DisplayAlert("Error", $"Failed to send email: {ex.Message}", "OK");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|