diff --git a/sessions/Season-06/0604/README.md b/sessions/Season-06/0604/README.md new file mode 100644 index 0000000..4123a1b --- /dev/null +++ b/sessions/Season-06/0604/README.md @@ -0,0 +1,499 @@ +## Session 0604 : Platform Features and Themes + +Our application is running on a modern operating system on a phone or desktop and should be able to access the features made available on that system. In this first part, we will access the map feature to display a monkey's location + +### Check for internet + +We can easily check to see if our user is connected to the internet with the built in `IConnectivity` of .NET MAUI + +1. First, let's get access to the `IConnectivity` found inside of .NET MAUI. Let's inject `IConnectivity` into our `MonkeysViewModel` constructor: + + ```csharp + IConnectivity connectivity; + public MonkeysViewModel(MonkeyService monkeyService, IConnectivity connectivity) + { + Title = "Monkey Finder"; + this.monkeyService = monkeyService; + this.connectivity = connectivity; + } + ``` + +1. Register the `Connectivity.Current` in our `MauiProgram.cs`. + + +1. While we are here let's add both `IGeolocation` and `IMap`, add the code: + + ```csharp + builder.Services.AddSingleton(Connectivity.Current); + builder.Services.AddSingleton(Geolocation.Default); + builder.Services.AddSingleton(Map.Default); + ``` + +1. Now, let's check for internet inside of the `GetMonkeysAsync` method and display an alert if offline. + + ```csharp + if (connectivity.NetworkAccess != NetworkAccess.Internet) + { + await Shell.Current.DisplayAlert("No connectivity!", + $"Please check internet and try again.", "OK"); + return; + } + ``` + + Run the app on your emulator and toggle on and off airplane mode to check your implementation. + + +### Find Closest Monkey! + +We can add more functionality to this page using the GPS of the device since each monkey has a latitude and longitude associated with it. + +1. First, let's get access to the `IGeolocator` found inside of .NET MAUI. Let's inject `IGeolocator` into our `MonkeysViewModel` constructor: + + ```csharp + IConnectivity connectivity; + IGeolocation geolocation; + public MonkeysViewModel(MonkeyService monkeyService, IConnectivity connectivity, IGeolocation geolocation) + { + Title = "Monkey Finder"; + this.monkeyService = monkeyService; + this.connectivity = connectivity; + this.geolocation = geolocation; + } + ``` + + +1. In our `MonkeysViewModel.cs`, let's create another method called `GetClosestMonkey`: + + ```csharp + [RelayCommand] + async Task GetClosestMonkey() + { + + } + ``` + +1. We can then fill it in by using .NET MAUI to query for our location and helpers that find the closest monkey to us: + + ```csharp + [RelayCommand] + async Task GetClosestMonkey() + { + if (IsBusy || Monkeys.Count == 0) + return; + + try + { + // Get cached location, else get real location. + var location = await geolocation.GetLastKnownLocationAsync(); + if (location == null) + { + location = await geolocation.GetLocationAsync(new GeolocationRequest + { + DesiredAccuracy = GeolocationAccuracy.Medium, + Timeout = TimeSpan.FromSeconds(30) + }); + } + + // Find closest monkey to us + var first = Monkeys.OrderBy(m => location.CalculateDistance( + new Location(m.Latitude, m.Longitude), DistanceUnits.Miles)) + .FirstOrDefault(); + + await Shell.Current.DisplayAlert("", first.Name + " " + + first.Location, "OK"); + + } + catch (Exception ex) + { + Debug.WriteLine($"Unable to query location: {ex.Message}"); + await Shell.Current.DisplayAlert("Error!", ex.Message, "OK"); + } + } + ``` + + +1. Back in our `MainPage.xaml` we can add another `Button` that will call this new method: + + Add the following XAML under the Search button. + + ```xml +