async Task GetShroomsAsync() { if (IsBusy) { return; } try { IsBusy = true; var mushies = await DataService.GetMushroomsAsync(); Shrooms.Clear(); ListingShrooms.Clear(); foreach (var mushroom in mushies) { Shrooms.Add(mushroom); ListingShrooms.Add(mushroom); } } catch (Exception e) { Debug.WriteLine($"Error retieving shrooms: {e.Message}"); await Application.Current.MainPage.DisplayAlert("Error", e.Message, "Ok"); } finally { IsBusy = false; } }
async Task ShowTypeAsync(string type) { if (IsBusy) { return; } try { IsBusy = true; ListingShrooms.Clear(); foreach (var m in Shrooms) { if (m.Usage.Equals(type)) { ListingShrooms.Add(m); } } }catch (Exception e) { Debug.WriteLine($"Error retieving shrooms: {e.Message}"); await Application.Current.MainPage.DisplayAlert("Error", e.Message, "Ok"); } finally { IsBusy = false; } }
async Task GetInSeasonAsync() { if (IsBusy) { return; } try { IsBusy = true; getNowAsInt(); for (var i = 0; i < ListingShrooms.Count; i++) { Mushrooms m = ListingShrooms.ElementAt(i); if (!(Today >= m.SeasonStart && Today <= m.SeasonEnd)) { ListingShrooms.Remove(m); i--; } } if (ListingShrooms.Count == 0) { await Application.Current.MainPage.DisplayAlert("Sorry", "None known to be is season", "Ok"); } } catch (Exception e) { Debug.WriteLine($"Error retieving data: {e.Message}"); await Application.Current.MainPage.DisplayAlert("Error", e.Message, "Ok"); } finally { IsBusy = false; } }
async Task GetClosestAsync() { if (IsBusy || ListingShrooms.Count == 0) { return; } try { var location = await Geolocation.GetLastKnownLocationAsync(); if (location == null) { location = await Geolocation.GetLocationAsync(new GeolocationRequest { DesiredAccuracy = GeolocationAccuracy.Low, Timeout = TimeSpan.FromSeconds(30) }); } var first = ListingShrooms.OrderBy(m => location.CalculateDistance( new Location(m.Latitude, m.Longitude), DistanceUnits.Miles)).FirstOrDefault(); await Application.Current.MainPage.DisplayAlert("", first.Name, "Ok"); } catch (Exception e) { Debug.WriteLine($"Unable to query location: {e.Message}"); await Application.Current.MainPage.DisplayAlert("Error!", e.Message, "OK"); } }
async Task GetAllAsync() { ListingShrooms.Clear(); try { foreach (var m in Shrooms) { ListingShrooms.Add(m); } }catch (Exception e) { Debug.WriteLine($"Error retieving shrooms: {e.Message}"); await Application.Current.MainPage.DisplayAlert("Error", e.Message, "Ok"); } }