DefaultGeolocation.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.ComponentModel;
  2. using System.Drawing;
  3. using System.Runtime.CompilerServices;
  4. using InABox.Core;
  5. namespace InABox.Avalonia.Platform;
  6. public class DefaultGeolocation : INotifyPropertyChanged, IGeolocation
  7. {
  8. private DateTime _nextCheck = DateTime.MaxValue;
  9. private TimeSpan _remaining = TimeSpan.Zero;
  10. public bool Scanning
  11. {
  12. get => _nextCheck == DateTime.MaxValue;
  13. set => SetField(ref _nextCheck, value ? DateTime.MinValue : DateTime.MaxValue);
  14. }
  15. private GeoPoint? _currentLocation;
  16. public GeoPoint? CurrentLocation
  17. {
  18. get => _currentLocation;
  19. set
  20. {
  21. SetField(ref _currentLocation, value);
  22. LocationChanged?.Invoke(this, new GeoLocationChangedArgs(value));
  23. }
  24. }
  25. private TimeSpan _frequency = TimeSpan.FromSeconds(10);
  26. public TimeSpan Frequency
  27. {
  28. get => _frequency;
  29. set => SetField(ref _frequency, value);
  30. }
  31. public event GeoLocationChangedHandler? LocationChanged;
  32. public event GeoLocationTimerHandler? TimerChanged;
  33. public Logger? Logger { get; set; }
  34. public virtual Task<GeoPoint?> GetLocationAsync(CancellationTokenSource cancel)
  35. {
  36. return Task.FromResult<GeoPoint?>(new GeoPoint());
  37. }
  38. private readonly CancellationTokenSource _cancelTokenSource;
  39. private bool _isCheckingLocation;
  40. public DefaultGeolocation()
  41. {
  42. _cancelTokenSource = new CancellationTokenSource();
  43. try
  44. {
  45. _ = Task.Run(async () =>
  46. {
  47. while (!_cancelTokenSource.Token.IsCancellationRequested)
  48. {
  49. if (!_isCheckingLocation)
  50. {
  51. if (_nextCheck < DateTime.Now)
  52. {
  53. try
  54. {
  55. _isCheckingLocation = true;
  56. CurrentLocation =
  57. await PlatformTools.Geolocation.GetLocationAsync(_cancelTokenSource);
  58. }
  59. catch (Exception ex)
  60. {
  61. Logger?.Send(LogType.Error, "", $"GPS Location Error: {ex.Message}");
  62. CurrentLocation = null;
  63. }
  64. _isCheckingLocation = false;
  65. _nextCheck = DateTime.Now.Add(_frequency);
  66. }
  67. else
  68. {
  69. _remaining = _nextCheck - DateTime.Now;
  70. TimerChanged?.Invoke(this, new GeoLocationTimerArgs(_frequency, _remaining));
  71. Thread.Sleep(100);
  72. }
  73. }
  74. }
  75. }
  76. );
  77. }
  78. catch (OperationCanceledException e)
  79. {
  80. Logger?.Send(LogType.Error, "", $"GPS Scanning Cancelled: {e.Message}");
  81. }
  82. catch (Exception e)
  83. {
  84. Logger?.Send(LogType.Error, "", $"GPS Error: {e.Message}");
  85. }
  86. }
  87. public void Cancel()
  88. {
  89. if (_isCheckingLocation && _cancelTokenSource.IsCancellationRequested == false)
  90. _cancelTokenSource.Cancel();
  91. }
  92. #region INotifyPropertyChanged
  93. public event PropertyChangedEventHandler? PropertyChanged;
  94. protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
  95. {
  96. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  97. }
  98. protected bool SetField<T>(ref T field, T value, [CallerMemberName] string? propertyName = null)
  99. {
  100. if (EqualityComparer<T>.Default.Equals(field, value)) return false;
  101. field = value;
  102. OnPropertyChanged(propertyName);
  103. return true;
  104. }
  105. #endregion
  106. }