Parcourir la source

Improved Geolocation scanning for Home Screen

Frank van den Bos il y a 3 jours
Parent
commit
e7e1add901

+ 33 - 11
InABox.Avalonia.Platform/Geolocation/DefaultGeolocation.cs

@@ -9,6 +9,7 @@ public class DefaultGeolocation : INotifyPropertyChanged, IGeolocation
 {
     
     private DateTime _nextCheck = DateTime.MaxValue;
+    private TimeSpan _remaining = TimeSpan.Zero;
     
     public bool Scanning
     {
@@ -23,11 +24,21 @@ public class DefaultGeolocation : INotifyPropertyChanged, IGeolocation
         set
         {
             SetField(ref _currentLocation, value);
-            LocationChanged?.Invoke(this, EventArgs.Empty);
+            LocationChanged?.Invoke(this, new GeoLocationChangedArgs(value));
         }
     }
 
-    public event EventHandler? LocationChanged;
+    private TimeSpan _frequency = TimeSpan.FromSeconds(10);
+    
+    public TimeSpan Frequency
+    {
+        get => _frequency;
+        set => SetField(ref _frequency, value);
+    }
+
+    public event GeoLocationChangedHandler? LocationChanged;
+
+    public event GeoLocationTimerHandler? TimerChanged;
 
     public Logger? Logger { get; set; }
     
@@ -48,20 +59,31 @@ public class DefaultGeolocation : INotifyPropertyChanged, IGeolocation
                 {
                     while (!_cancelTokenSource.Token.IsCancellationRequested)
                     {
-                        if (_nextCheck < DateTime.Now && !_isCheckingLocation)
+                        if (!_isCheckingLocation)
                         {
-                            try
+                            if (_nextCheck < DateTime.Now)
                             {
-                                _isCheckingLocation = true;
-                                CurrentLocation = await PlatformTools.Geolocation.GetLocationAsync(_cancelTokenSource);
+                                try
+                                {
+                                    _isCheckingLocation = true;
+                                    CurrentLocation =
+                                        await PlatformTools.Geolocation.GetLocationAsync(_cancelTokenSource);
+                                }
+                                catch (Exception ex)
+                                {
+                                    Logger?.Send(LogType.Error, "", $"GPS Location Error: {ex.Message}");
+                                    CurrentLocation = null;
+                                }
+
+                                _isCheckingLocation = false;
+                                _nextCheck = DateTime.Now.Add(_frequency);
                             }
-                            catch (Exception ex)
+                            else
                             {
-                                Logger?.Send(LogType.Error, "", $"GPS Location Error: {ex.Message}");
-                                CurrentLocation = null;
+                                _remaining = _nextCheck - DateTime.Now;
+                                TimerChanged?.Invoke(this, new GeoLocationTimerArgs(_frequency, _remaining));
+                                Thread.Sleep(100);
                             }
-                            _isCheckingLocation = false;
-                            _nextCheck = DateTime.Now.AddSeconds(30);
                         }
                     }
                 }

+ 18 - 1
InABox.Avalonia.Platform/Geolocation/IGeolocation.cs

@@ -3,6 +3,21 @@ using InABox.Core;
 
 namespace InABox.Avalonia.Platform;
 
+public class GeoLocationTimerArgs(TimeSpan frequency, TimeSpan remaining) : EventArgs
+{
+    public TimeSpan Frequency { get; } = frequency;
+    public TimeSpan Remaining { get; } = remaining;
+}
+
+public delegate void GeoLocationTimerHandler(object sender, GeoLocationTimerArgs e);
+
+public class GeoLocationChangedArgs(GeoPoint? location) : EventArgs
+{
+    public GeoPoint? Location { get; } = location;
+}
+
+public delegate void GeoLocationChangedHandler(object sender, GeoLocationChangedArgs e);
+
 public interface IGeolocation : ILoggable
 {
     
@@ -10,7 +25,9 @@ public interface IGeolocation : ILoggable
     
     GeoPoint? CurrentLocation { get; set; }
     
-    public event EventHandler? LocationChanged; 
+    public event GeoLocationChangedHandler? LocationChanged; 
+    
+    public event GeoLocationTimerHandler? TimerChanged;
     
     Task<GeoPoint?> GetLocationAsync(CancellationTokenSource cancel);
 }

+ 11 - 3
InABox.Avalonia/DataModels/ShellColumns.cs

@@ -17,9 +17,17 @@ namespace InABox.Avalonia
 
         public ShellColumns<TParent,TEntity> Map(string property, Expression<Func<TEntity, object?>> expression)
         {
-            int iCol = _columns.TryGetValue(property, out var column)
-                ? column.Item1
-                : _columns.Keys.Count;
+            int iCol = _columns.Keys.Count;
+            bool bFound = _columns.TryGetValue(property, out var column);
+            try
+            {
+                if (bFound)
+                    iCol = column.Item1;
+            }
+            catch (Exception e)
+            {
+                
+            }
             
             _columns[property] = new Tuple<int, Expression<Func<TEntity, object?>>>(iCol, expression);
             //_columns[property] = new Tuple<int, Expression<Func<TEntity, object>>>(_columns.Keys.Count, expression);

+ 1 - 1
InABox.Avalonia/InABox.Avalonia.csproj

@@ -1,13 +1,13 @@
 <Project Sdk="Microsoft.NET.Sdk">
 
     <PropertyGroup>
-        <TargetFrameworks>net8.0;net9.0</TargetFrameworks>
         <ImplicitUsings>enable</ImplicitUsings>
         <Nullable>enable</Nullable>
         <RootNamespace>InABox.Avalonia</RootNamespace>
         <AvaloniaUseCompiledBindingsByDefault>true</AvaloniaUseCompiledBindingsByDefault>
         <Configurations>Debug;Release;Publish</Configurations>
         <Platforms>AnyCPU</Platforms>
+        <TargetFrameworks>net8.0;net9.0</TargetFrameworks>
     </PropertyGroup>
 
     <ItemGroup>

+ 1 - 1
InABox.Core/GeoPoint.cs

@@ -55,7 +55,7 @@ namespace InABox.Core
     public class GeoFenceDefinition
     {
         
-        public List<GeoPoint> Coordinates { get;} = new List<GeoPoint>();
+        public List<GeoPoint> Coordinates { get; set; } = new List<GeoPoint>();
         
         public bool Contains(GeoPoint p)
         {