-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPageViewModel.cs
More file actions
153 lines (138 loc) · 3.32 KB
/
MainPageViewModel.cs
File metadata and controls
153 lines (138 loc) · 3.32 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Threading;
using System.Windows.Input;
using Windows.UI.Core;
namespace GeolocatorService.Samples
{
public class MainPageViewModel : INotifyPropertyChanged
{
private CoreDispatcher _dispatcher;
private IGeolocatorService _geolocatorService;
private IDisposable _locationOrNullSubscription = null;
private IDisposable _permissionSubscription = null;
public event PropertyChangedEventHandler PropertyChanged;
public MainPageViewModel(CoreDispatcher dispatcher)
{
_dispatcher = dispatcher;
_geolocatorService = new GeolocatorService();
}
private string _pageContent = "";
public string PageContent
{
get => _pageContent;
set
{
_pageContent = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PageContent)));
}
}
private void SetPageContent(string content)
{
if (_dispatcher.HasThreadAccess)
{
PageContent = content;
}
else
{
_ = _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
SetPageContent(content);
});
}
}
private string _permissionStatus = "";
public string PermissionStatus
{
get => _permissionStatus;
set
{
_permissionStatus = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(PermissionStatus)));
}
}
private void SetPermissionStatus(string content)
{
if (_dispatcher.HasThreadAccess)
{
PermissionStatus = content;
}
else
{
_ = _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
SetPermissionStatus(content);
});
}
}
public DelegateCommand RequestPermissionCommand => new DelegateCommand(() =>
{
_locationOrNullSubscription = _geolocatorService
.GetAndObserveLocationOrDefault()
.Subscribe(
location =>
{
if (location == null)
{
SetPageContent("Please give this app the location permission.");
}
else
{
SetPageContent($"You are currently located at ({location.Point.Position.Longitude}, {location.Point.Position.Latitude})");
}
},
error =>
{
SetPageContent($"An error has occurred: {error}");
}
);
_permissionSubscription = _geolocatorService
.GetAndObserveIsPermissionGranted()
.Subscribe(
isGranted =>
{
if (isGranted)
{
SetPermissionStatus("Permission: Granted");
}
else
{
SetPermissionStatus($"Permission: Denied");
}
},
error =>
{
SetPermissionStatus($"An error has occurred: {error}");
}
);
});
public DelegateCommand GetLocationCommand => new DelegateCommand(async () =>
{
try
{
var location = await _geolocatorService.GetLocation(CancellationToken.None);
SetPageContent($"GetLocation- ({location.Point.Position.Longitude}, {location.Point.Position.Latitude})");
}
catch(Exception ex)
{
SetPageContent($"GetLocation- An error has occurred : {ex}");
}
});
public class DelegateCommand : ICommand
{
private Action _command;
public DelegateCommand(Action command)
{
_command = command;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => true;
public void Execute(object parameter)
{
_command();
}
}
}
}