Add project files.
This commit is contained in:
21
MonkeyFinder/ViewModel/BaseViewModel.cs
Normal file
21
MonkeyFinder/ViewModel/BaseViewModel.cs
Normal file
@ -0,0 +1,21 @@
|
||||
|
||||
|
||||
namespace MonkeyFinder.ViewModel;
|
||||
|
||||
public partial class BaseViewModel : ObservableObject
|
||||
{
|
||||
public BaseViewModel()
|
||||
{
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
[NotifyPropertyChangedFor(nameof(IsNotBusy))]
|
||||
bool isBusy;
|
||||
|
||||
[ObservableProperty]
|
||||
string title;
|
||||
|
||||
|
||||
public bool IsNotBusy => !IsBusy;
|
||||
|
||||
}
|
||||
43
MonkeyFinder/ViewModel/MonkeyDetailsViewModel.cs
Normal file
43
MonkeyFinder/ViewModel/MonkeyDetailsViewModel.cs
Normal file
@ -0,0 +1,43 @@
|
||||
namespace MonkeyFinder.ViewModel;
|
||||
|
||||
[QueryProperty("Monkey", "Monkey")]
|
||||
|
||||
public partial class MonkeyDetailsViewModel : BaseViewModel
|
||||
{
|
||||
IMap map;
|
||||
public MonkeyDetailsViewModel(IMap map)
|
||||
{
|
||||
this.map = map;
|
||||
}
|
||||
|
||||
[ObservableProperty]
|
||||
Monkey monkey;
|
||||
|
||||
//[RelayCommand]
|
||||
//async Task GoBackAsync()
|
||||
//{
|
||||
// await Shell.Current.GoToAsync("..");
|
||||
//}
|
||||
|
||||
[RelayCommand]
|
||||
|
||||
async Task OpenMapAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
await map.OpenAsync(Monkey.Latitude, Monkey.Longitude,
|
||||
new MapLaunchOptions
|
||||
{
|
||||
Name = Monkey.Name,
|
||||
NavigationMode = NavigationMode.None
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex);
|
||||
await Shell.Current.DisplayAlert("Error!",
|
||||
$"Unable to open map: {ex.Message}", "OK");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
108
MonkeyFinder/ViewModel/MonkeysViewModel.cs
Normal file
108
MonkeyFinder/ViewModel/MonkeysViewModel.cs
Normal file
@ -0,0 +1,108 @@
|
||||
using MonkeyFinder.Services;
|
||||
|
||||
namespace MonkeyFinder.ViewModel;
|
||||
|
||||
public partial class MonkeysViewModel : BaseViewModel
|
||||
{
|
||||
MonkeyService monkeyService;
|
||||
public ObservableCollection<Monkey> Monkeys { get; } = new();
|
||||
|
||||
IConnectivity connectivity;
|
||||
IGeolocation geolocation;
|
||||
public MonkeysViewModel(MonkeyService monkeyService, IConnectivity connectivity, IGeolocation geolocation)
|
||||
{
|
||||
Title = "Monkey Finder";
|
||||
this.monkeyService = monkeyService;
|
||||
this.connectivity = connectivity;
|
||||
this.geolocation = geolocation;
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
async Task GetClosestMonkeyAsync()
|
||||
{
|
||||
if(IsBusy || Monkeys.Count == 0)
|
||||
return;
|
||||
try
|
||||
{
|
||||
var location = await geolocation.GetLastKnownLocationAsync();
|
||||
|
||||
if (location is null)
|
||||
{
|
||||
location = await geolocation.GetLocationAsync(
|
||||
new GeolocationRequest
|
||||
{
|
||||
DesiredAccuracy = GeolocationAccuracy.Medium,
|
||||
Timeout = TimeSpan.FromSeconds(30),
|
||||
});
|
||||
}
|
||||
|
||||
if (location is null)
|
||||
return;
|
||||
|
||||
var first = Monkeys.OrderBy(m =>
|
||||
location.CalculateDistance(m.Latitude, m.Longitude, DistanceUnits.Kilometers)
|
||||
).FirstOrDefault();
|
||||
|
||||
await Shell.Current.DisplayAlert("Closest Monkey",
|
||||
$"{first.Name} in {first.Location}", "OK");
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex);
|
||||
await Shell.Current.DisplayAlert("Error!",
|
||||
$"Unable to get closest monkey: {ex.Message}", "OK");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
[RelayCommand]
|
||||
async Task GoToDetailsAsync(Monkey monkey)
|
||||
{
|
||||
if (monkey is null)
|
||||
return;
|
||||
await Shell.Current.GoToAsync($"{nameof(DetailsPage)}", true,
|
||||
new Dictionary<string, object>
|
||||
{
|
||||
{"Monkey", monkey }
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
[RelayCommand]
|
||||
async Task GetMonkeysAsync()
|
||||
{
|
||||
if (IsBusy) return;
|
||||
|
||||
try
|
||||
{
|
||||
if (connectivity.NetworkAccess != NetworkAccess.Internet)
|
||||
{
|
||||
await Shell.Current.DisplayAlert("Internet issue",
|
||||
$"Check your internet and try again!", "OK");
|
||||
return;
|
||||
}
|
||||
|
||||
IsBusy = true;
|
||||
var monkeys = await monkeyService.GetMonkeys();
|
||||
if (Monkeys.Count != 0)
|
||||
Monkeys.Clear();
|
||||
foreach (var monkey in monkeys)
|
||||
Monkeys.Add(monkey);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine(ex);
|
||||
await Shell.Current.DisplayAlert("Error!",
|
||||
$"Unable to get monkeys: {ex.Message}", "OK");
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsBusy = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user