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

145 lines
5.3 KiB

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