-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
MainPageViewModel.cs
64 lines (56 loc) · 1.62 KB
/
MainPageViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
namespace MauiMaps;
using System.Collections.ObjectModel;
using AutoFixture;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Models;
public partial class MainPageViewModel(IFixture fixture) : ObservableObject
{
private LocationPin? currentLocationPin;
[RelayCommand]
private void Add()
{
var newLocation = fixture.Build<LocationPin>()
.With(x => x.ImageSource, ImageSource.FromUri(new Uri($"https://picsum.photos/{Random.Shared.Next(40, 60)}")))
.With(x => x.Location, new Location(Random.Shared.Next(44, 51), Random.Shared.Next(23, 40)))
.Create();
LocationPins.Add(newLocation);
}
[RelayCommand]
private void Remove()
{
if (LocationPins.Count > 0)
{
LocationPins.RemoveAt(LocationPins.Count - 1);
}
}
[RelayCommand]
private void RemoveAll()
{
LocationPins.Clear();
}
[RelayCommand(IncludeCancelCommand = true, AllowConcurrentExecutions = false)]
private async Task RealTimeLocationTracker(CancellationToken cancellationToken)
{
var progress = new Progress<Location>(location =>
{
if (currentLocationPin is null)
{
currentLocationPin = new LocationPin
{
ImageSource = ImageSource.FromUri(new Uri($"https://picsum.photos/{Random.Shared.Next(40, 60)}")),
Location = location,
Description = "I am here!"
};
}
else
{
LocationPins.Remove(currentLocationPin);
currentLocationPin.Location = location;
}
LocationPins.Add(currentLocationPin);
});
await Geolocator.Default.StartListening(progress, cancellationToken);
}
public ObservableCollection<LocationPin> LocationPins { get; } = new();
}