Skip to content

Commit

Permalink
Default calendarlocation if error (#147)
Browse files Browse the repository at this point in the history
* default calendar location if we get an error geolocating

* Bump calendar improvement
  • Loading branch information
edgiardina authored Jun 20, 2024
1 parent 78bc5df commit 3b30ff6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
8 changes: 4 additions & 4 deletions IfpaMaui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@

<!-- Package References -->
<ItemGroup>
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.40" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.40" />
<PackageReference Include="Microsoft.Maui.Controls.Maps" Version="8.0.40" />
<PackageReference Include="Microsoft.Maui.Controls" Version="8.0.60" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="8.0.60" />
<PackageReference Include="Microsoft.Maui.Controls.Maps" Version="8.0.60" />
<PackageReference Include="AathifMahir.Maui.MauiIcons.Fluent" Version="2.1.2" />
<PackageReference Include="CommunityToolkit.Maui" Version="8.0.1" />
<PackageReference Include="LiveChartsCore.SkiaSharpView.Maui" Version="2.0.0-beta.911" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="PinballApi" Version="3.0.1" />
<PackageReference Include="Plugin.Maui.Calendar" Version="1.1.6" />
<PackageReference Include="Plugin.Maui.Calendar" Version="1.1.9" />
<PackageReference Include="Scrutor" Version="4.2.2" />
<PackageReference Include="Serilog.Sinks.Xamarin" Version="1.0.0" />
<PackageReference Include="Shiny.Core" Version="3.2.2" />
Expand Down
9 changes: 4 additions & 5 deletions ViewModels/CalendarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down
18 changes: 15 additions & 3 deletions Views/CalendarPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 3b30ff6

Please sign in to comment.