From 3b30ff68ad2ed9044f656762848a92694ede79d3 Mon Sep 17 00:00:00 2001 From: Ed Giardina Date: Thu, 20 Jun 2024 16:32:00 -0400 Subject: [PATCH] Default calendarlocation if error (#147) * default calendar location if we get an error geolocating * Bump calendar improvement --- IfpaMaui.csproj | 8 ++++---- ViewModels/CalendarViewModel.cs | 9 ++++----- Views/CalendarPage.xaml.cs | 18 +++++++++++++++--- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/IfpaMaui.csproj b/IfpaMaui.csproj index 6c06015..a50005d 100644 --- a/IfpaMaui.csproj +++ b/IfpaMaui.csproj @@ -48,16 +48,16 @@ - - - + + + - + diff --git a/ViewModels/CalendarViewModel.cs b/ViewModels/CalendarViewModel.cs index 7bc643f..ff7d088 100644 --- a/ViewModels/CalendarViewModel.cs +++ b/ViewModels/CalendarViewModel.cs @@ -55,7 +55,7 @@ private async Task ViewCalendarDetails(long tournamentId) await Shell.Current.GoToAsync($"calendar-detail?tournamentId={tournamentId}"); } - public async Task ExecuteLoadItemsCommand(string address, int distance) + public async Task ExecuteLoadItemsCommand(Location geoLocation, int distance) { IsBusy = true; @@ -67,14 +67,13 @@ public async Task ExecuteLoadItemsCommand(string address, int distance) logger.LogDebug("Cleared collections in {0}", sw.ElapsedMilliseconds); - var geoLocation = await geocoding.GetLocationsAsync(address); - var longitude = geoLocation.FirstOrDefault()?.Longitude; - var latitude = geoLocation.FirstOrDefault()?.Latitude; + var longitude = geoLocation?.Longitude; + var latitude = geoLocation?.Latitude; if (longitude == null || latitude == null) { - logger.LogWarning("Unable to geocode address {0}", address); + logger.LogWarning("Unable to geocode address {0}", geoLocation); return; } diff --git a/Views/CalendarPage.xaml.cs b/Views/CalendarPage.xaml.cs index 3daf166..cf8c4a3 100644 --- a/Views/CalendarPage.xaml.cs +++ b/Views/CalendarPage.xaml.cs @@ -51,9 +51,21 @@ private async Task UpdateCalendarData() try { mapShim.Children.Clear(); - var geoLocation = await Geocoding.GetLocationsAsync(Settings.LastCalendarLocation); - var mapSpan = MapSpan.FromCenterAndRadius(new Location(geoLocation.First().Latitude, geoLocation.First().Longitude), + Location geoLocation; + + try + { + geoLocation = (await Geocoding.GetLocationsAsync(Settings.LastCalendarLocation)).First(); + } + catch (Exception e) + { + logger.LogWarning(e, "Error geolocating"); + + geoLocation = await Geolocation.GetLastKnownLocationAsync(); + } + + var mapSpan = MapSpan.FromCenterAndRadius(new Location(geoLocation.Latitude, geoLocation.Longitude), Distance.FromMiles(Settings.LastCalendarDistance)); var map = new Microsoft.Maui.Controls.Maps.Map(mapSpan); @@ -63,7 +75,7 @@ private async Task UpdateCalendarData() mapShim.Children.Add(map); - await ViewModel.ExecuteLoadItemsCommand(Settings.LastCalendarLocation, Settings.LastCalendarDistance); + await ViewModel.ExecuteLoadItemsCommand(geoLocation, Settings.LastCalendarDistance); // For whatever reason Android on re-load via modal doesn't re-center the map. map.MoveToRegion(mapSpan);