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.
142 lines
5.1 KiB
142 lines
5.1 KiB
using Justice.Helpers;
|
|
using Microsoft.Maui.ApplicationModel.Communication;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Justice.Views
|
|
{
|
|
public partial class MapTimer : ContentPage
|
|
{
|
|
private bool isTrackingActive;
|
|
|
|
public MapTimer()
|
|
{
|
|
InitializeComponent();
|
|
FetchLocation();
|
|
}
|
|
|
|
private async void StartTrackingClicked(object sender, EventArgs e)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(DestinationLatitudeEntry.Text) ||
|
|
string.IsNullOrWhiteSpace(DestinationLongitudeEntry.Text))
|
|
{
|
|
await DisplayAlert("Error", "Please enter destination coordinates.", "OK");
|
|
return;
|
|
}
|
|
|
|
if (!double.TryParse(DestinationLatitudeEntry.Text, out double destinationLatitude) ||
|
|
!double.TryParse(DestinationLongitudeEntry.Text, out double destinationLongitude))
|
|
{
|
|
await DisplayAlert("Error", "Invalid destination coordinates.", "OK");
|
|
return;
|
|
}
|
|
|
|
if (!int.TryParse(TimerEntry.Text, out int timerMinutes))
|
|
{
|
|
await DisplayAlert("Error", "Invalid timer value. Please enter a numeric value.", "OK");
|
|
return;
|
|
}
|
|
|
|
isTrackingActive = true;
|
|
|
|
// Set timer duration
|
|
var timerDuration = TimeSpan.FromMinutes(timerMinutes);
|
|
var timerEnd = DateTime.Now.Add(timerDuration);
|
|
string phoneNumber = "9860104625";
|
|
|
|
// Start countdown display
|
|
_ = UpdateCountdownDisplay(timerEnd);
|
|
|
|
while (isTrackingActive && DateTime.Now < timerEnd)
|
|
{
|
|
await Task.Delay(5000); // Check location every 5 seconds
|
|
|
|
// Fetch current location
|
|
var (currentLatitude, currentLongitude, _) = await GeolocationHelper.GetLocationAsync();
|
|
|
|
// Define tolerance for proximity
|
|
const double Tolerance = 0.0001;
|
|
|
|
// Check if user has reached the destination
|
|
if (Math.Abs(destinationLatitude - currentLatitude) < Tolerance &&
|
|
Math.Abs(destinationLongitude - currentLongitude) < Tolerance)
|
|
{
|
|
isTrackingActive = false;
|
|
|
|
// Notify user and send SMS
|
|
string reachedMessage = "User has safely reached the destination.";
|
|
await DisplayAlert("Success", "Safely reached the destination!", "OK");
|
|
await SendSmsNotification(phoneNumber, reachedMessage);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// If timer expires and destination is not reached
|
|
if (isTrackingActive)
|
|
{
|
|
string notReachedMessage = "User did not reach the destination within the set time.";
|
|
await DisplayAlert("Time's Up", "You did not reach the destination within the set timer.", "OK");
|
|
await SendSmsNotification(phoneNumber, notReachedMessage);
|
|
}
|
|
}
|
|
|
|
private async Task UpdateCountdownDisplay(DateTime timerEnd)
|
|
{
|
|
while (isTrackingActive && DateTime.Now < timerEnd)
|
|
{
|
|
TimeSpan remainingTime = timerEnd - DateTime.Now;
|
|
|
|
// Update the UI with remaining time
|
|
TimerLabel.Text = $"Time Remaining: {remainingTime.Minutes:D2}:{remainingTime.Seconds:D2}";
|
|
|
|
// Wait for 1 second before updating again
|
|
await Task.Delay(1000);
|
|
}
|
|
|
|
// Clear the timer label after countdown ends
|
|
TimerLabel.Text = isTrackingActive ? "Time's Up!" : "You reached the destination!";
|
|
}
|
|
|
|
private async void FetchLocation()
|
|
{
|
|
try
|
|
{
|
|
var (latitude, longitude, address) = await GeolocationHelper.GetLocationAsync();
|
|
SourceLatitudeEntry.Text = latitude.ToString();
|
|
SourceLongitudeEntry.Text = longitude.ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error fetching location: {ex.Message}");
|
|
await DisplayAlert("Error", $"Failed to get location: {ex.Message}", "OK");
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
Console.WriteLine($"Error sending SMS: {ex.Message}");
|
|
await DisplayAlert("Error", "Failed to send SMS.", "OK");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|