-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathLocationService.cs
194 lines (162 loc) · 6.19 KB
/
LocationService.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// Copyright Fela Ameghino 2015-2025
//
// Distributed under the GNU General Public License v3.0. (See accompanying
// file LICENSE or copy at https://www.gnu.org/licenses/gpl-3.0.txt)
//
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Telegram.Navigation.Services;
using Telegram.Td.Api;
using Windows.ApplicationModel.ExtendedExecution;
using Windows.Devices.Enumeration;
using Windows.Devices.Geolocation;
using Windows.System;
using Windows.UI.Xaml.Controls;
namespace Telegram.Services
{
public readonly struct GetVenuesResult
{
public string NextOffset { get; }
public List<Venue> Venues { get; }
public GetVenuesResult(string offset, List<Venue> venues)
{
NextOffset = offset;
Venues = venues;
}
}
public interface ILocationService
{
Task<Geolocator> StartTrackingAsync();
void StopTracking();
Task<Location> GetPositionAsync(INavigationService navigation);
Task<GetVenuesResult> GetVenuesAsync(long chatId, double latitude, double longitude, string query = null, string offset = null);
}
public partial class LocationService : ILocationService
{
private readonly IClientService _clientService;
public LocationService(IClientService clientService)
{
_clientService = clientService;
}
private Geolocator _locator;
private ExtendedExecutionSession _session;
public async Task<Geolocator> StartTrackingAsync()
{
if (_session != null)
{
return _locator;
}
if (_locator == null)
{
try
{
var accessStatus = await Geolocator.RequestAccessAsync();
if (accessStatus == GeolocationAccessStatus.Allowed)
{
_locator = new Geolocator { DesiredAccuracy = PositionAccuracy.Default, ReportInterval = uint.MaxValue, MovementThreshold = 20 };
}
}
catch { }
}
_session = new ExtendedExecutionSession();
_session.Description = "Live Location";
_session.Reason = ExtendedExecutionReason.LocationTracking;
_session.Revoked += ExtendedExecutionSession_Revoked;
var result = await _session.RequestExtensionAsync();
if (result == ExtendedExecutionResult.Denied)
{
//TODO: handle denied
}
return _locator;
}
public void StopTracking()
{
if (_session != null)
{
_session.Dispose();
_session = null;
}
}
private void ExtendedExecutionSession_Revoked(object sender, ExtendedExecutionRevokedEventArgs args)
{
StopTracking();
}
public async Task<Location> GetPositionAsync(INavigationService navigation)
{
try
{
var accessStatus = await CheckDeviceAccessAsync(navigation);
if (accessStatus)
{
var geolocator = new Geolocator { DesiredAccuracy = PositionAccuracy.Default };
var location = await geolocator.GetGeopositionAsync();
return new Location(
location.Coordinate.Point.Position.Latitude,
location.Coordinate.Point.Position.Longitude,
location.Coordinate.Accuracy);
}
}
catch { }
return null;
}
public async Task<bool> CheckDeviceAccessAsync(INavigationService navigation)
{
var access = DeviceAccessInformation.CreateFromDeviceClass(DeviceClass.Location);
if (access.CurrentStatus == DeviceAccessStatus.Unspecified)
{
var accessStatus = await Geolocator.RequestAccessAsync();
if (accessStatus == GeolocationAccessStatus.Allowed)
{
return true;
}
return false;
}
else if (access.CurrentStatus != DeviceAccessStatus.Allowed)
{
var message = Strings.PermissionNoLocationPosition;
var confirm = await navigation.ShowPopupAsync(message, Strings.AppName, Strings.PermissionOpenSettings, Strings.OK);
if (confirm == ContentDialogResult.Primary)
{
await Launcher.LaunchUriAsync(new Uri("ms-settings:appsfeatures-app"));
}
return false;
}
return true;
}
public async Task<GetVenuesResult> GetVenuesAsync(long chatId, double latitude, double longitude, string query = null, string offset = null)
{
var results = new List<Venue>();
var option = _clientService.Options.VenueSearchBotUsername;
if (string.IsNullOrEmpty(option))
{
// TODO: use hardcoded bot?
return new GetVenuesResult(null, results);
}
var chat = await _clientService.SendAsync(new SearchPublicChat(option)) as Chat;
if (chat == null)
{
return new GetVenuesResult(null, results);
}
var user = _clientService.GetUser(chat);
if (user == null)
{
return new GetVenuesResult(null, results);
}
var response = await _clientService.SendAsync(new GetInlineQueryResults(user.Id, chatId, new Location(latitude, longitude, 0), query ?? string.Empty, offset ?? string.Empty));
if (response is InlineQueryResults inlineResults)
{
foreach (var item in inlineResults.Results)
{
if (item is InlineQueryResultVenue venue)
{
results.Add(venue.Venue);
}
}
new GetVenuesResult(inlineResults.NextOffset, results);
}
return new GetVenuesResult(null, results);
}
}
}