Repository navigation
Expand file tree
/
Copy pathSettingsViewModel.cs
More file actions
344 lines (288 loc) · 10.9 KB
/
Copy pathSettingsViewModel.cs
File metadata and controls
344 lines (288 loc) · 10.9 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System.ComponentModel;
using System.Globalization;
using System.Windows.Input;
using eShop.ClientApp.Services;
using eShop.ClientApp.Services.AppEnvironment;
using eShop.ClientApp.Services.Location;
using eShop.ClientApp.Services.Settings;
using eShop.ClientApp.ViewModels.Base;
using Location = eShop.ClientApp.Models.Location.Location;
namespace eShop.ClientApp.ViewModels;
public class SettingsViewModel : ViewModelBase
{
//Needed if using Android Emulator Locally. See https://learn.microsoft.com/en-us/dotnet/maui/data-cloud/local-web-services?view=net-maui-8.0#android
private static string _baseAddress = DeviceInfo.Platform == DevicePlatform.Android ? "10.0.2.2" : "localhost";
private readonly IAppEnvironmentService _appEnvironmentService;
private readonly ILocationService _locationService;
private readonly ISettingsService _settingsService;
private bool _allowGpsLocation;
private string _gatewayBasketEndpoint;
private string _gatewayCatalogEndpoint;
private string _gatewayOrdersEndpoint;
private string _gpsWarningMessage;
private string _identityEndpoint;
private double _latitude;
private double _longitude;
private bool _useAzureServices;
private bool _useFakeLocation;
public SettingsViewModel(
ILocationService locationService, IAppEnvironmentService appEnvironmentService,
INavigationService navigationService, ISettingsService settingsService)
: base(navigationService)
{
_settingsService = settingsService;
_locationService = locationService;
_appEnvironmentService = appEnvironmentService;
_useAzureServices = !_settingsService.UseMocks;
_identityEndpoint = _settingsService.IdentityEndpointBase;
_latitude = double.Parse(_settingsService.Latitude, CultureInfo.CurrentCulture);
_longitude = double.Parse(_settingsService.Longitude, CultureInfo.CurrentCulture);
_useFakeLocation = _settingsService.UseFakeLocation;
_allowGpsLocation = _settingsService.AllowGpsLocation;
_gpsWarningMessage = string.Empty;
IdentityEndpoint =
!string.IsNullOrEmpty(_settingsService.IdentityEndpointBase)
? _settingsService.IdentityEndpointBase
: $"https://{_baseAddress}:5243";
GatewayCatalogEndpoint =
!string.IsNullOrEmpty(_settingsService.GatewayCatalogEndpointBase)
? _settingsService.GatewayCatalogEndpointBase
: $"http://{_baseAddress}:11632";
GatewayBasketEndpoint =
!string.IsNullOrEmpty(_settingsService.GatewayBasketEndpointBase)
? _settingsService.GatewayBasketEndpointBase
: $"http://{_baseAddress}:5221";
GatewayOrdersEndpoint =
!string.IsNullOrEmpty(_settingsService.GatewayOrdersEndpointBase)
? _settingsService.GatewayOrdersEndpointBase
: $"http://{_baseAddress}:11632";
ToggleMockServicesCommand = new RelayCommand(ToggleMockServices);
ToggleFakeLocationCommand = new RelayCommand(ToggleFakeLocation);
ToggleSendLocationCommand = new AsyncRelayCommand(ToggleSendLocationAsync);
ToggleAllowGpsLocationCommand = new RelayCommand(ToggleAllowGpsLocation);
UseAzureServices = !_settingsService.UseMocks;
}
public string TitleUseAzureServices => "Use Microservices/Containers from eShop";
public string DescriptionUseAzureServices => !UseAzureServices
? "Currently using mock services that are simulated objects that mimic the behavior of real services using a controlled approach. Toggle on to configure the use of microserivces/containers."
: "When enabling the use of microservices/containers, the app will attempt to use real services deployed as Docker/Kubernetes containers at the specified base endpoint, which will must be reachable through the network.";
public bool UseAzureServices
{
get => _useAzureServices;
set
{
SetProperty(ref _useAzureServices, value);
UpdateUseAzureServices();
}
}
public string TitleUseFakeLocation => !UseFakeLocation
? "Use Real Location"
: "Use Fake Location";
public string DescriptionUseFakeLocation => !UseFakeLocation
? "When enabling location, the app will attempt to use the location from the device."
: "Fake Location data is added for marketing campaign testing.";
public bool UseFakeLocation
{
get => _useFakeLocation;
set
{
SetProperty(ref _useFakeLocation, value);
UpdateFakeLocation();
}
}
public string TitleAllowGpsLocation => !AllowGpsLocation
? "GPS Location Disabled"
: "GPS Location Enabled";
public string DescriptionAllowGpsLocation => !AllowGpsLocation
? "When disabling location, you won't receive location campaigns based upon your location."
: "When enabling location, you'll receive location campaigns based upon your location.";
public string GpsWarningMessage
{
get => _gpsWarningMessage;
set => SetProperty(ref _gpsWarningMessage, value);
}
public string IdentityEndpoint
{
get => _identityEndpoint;
set
{
SetProperty(ref _identityEndpoint, value);
if (!string.IsNullOrEmpty(value))
{
UpdateIdentityEndpoint();
}
}
}
public string GatewayCatalogEndpoint
{
get => _gatewayCatalogEndpoint;
set
{
SetProperty(ref _gatewayCatalogEndpoint, value);
if (!string.IsNullOrEmpty(value))
{
UpdateGatewayShoppingEndpoint();
}
}
}
public string GatewayOrdersEndpoint
{
get => _gatewayOrdersEndpoint;
set
{
SetProperty(ref _gatewayOrdersEndpoint, value);
if (!string.IsNullOrEmpty(value))
{
UpdateGatewayOrdersEndpoint();
}
}
}
public string GatewayBasketEndpoint
{
get => _gatewayBasketEndpoint;
set
{
SetProperty(ref _gatewayBasketEndpoint, value);
if (!string.IsNullOrEmpty(value))
{
UpdateGatewayBasketEndpoint();
}
}
}
public double Latitude
{
get => _latitude;
set
{
SetProperty(ref _latitude, value);
UpdateLatitude();
}
}
public double Longitude
{
get => _longitude;
set
{
SetProperty(ref _longitude, value);
UpdateLongitude();
}
}
public bool AllowGpsLocation
{
get => _allowGpsLocation;
set => SetProperty(ref _allowGpsLocation, value);
}
public ICommand ToggleMockServicesCommand { get; }
public ICommand ToggleFakeLocationCommand { get; }
public ICommand ToggleSendLocationCommand { get; }
public ICommand ToggleAllowGpsLocationCommand { get; }
protected override async void OnPropertyChanged(PropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.PropertyName == nameof(AllowGpsLocation))
{
await UpdateAllowGpsLocation();
}
}
private void ToggleMockServices()
{
_appEnvironmentService.UpdateDependencies(!UseAzureServices);
OnPropertyChanged(nameof(TitleUseAzureServices));
OnPropertyChanged(nameof(DescriptionUseAzureServices));
}
private void ToggleFakeLocation()
{
_appEnvironmentService.UpdateDependencies(!UseAzureServices);
OnPropertyChanged(nameof(TitleUseFakeLocation));
OnPropertyChanged(nameof(DescriptionUseFakeLocation));
}
private async Task ToggleSendLocationAsync()
{
if (!_settingsService.UseMocks)
{
var locationRequest = new Location {Latitude = _latitude, Longitude = _longitude};
await _locationService.UpdateUserLocation(locationRequest);
}
}
private void ToggleAllowGpsLocation()
{
OnPropertyChanged(nameof(TitleAllowGpsLocation));
OnPropertyChanged(nameof(DescriptionAllowGpsLocation));
}
private void UpdateUseAzureServices()
{
// Save use mocks services to local storage
_settingsService.UseMocks = !UseAzureServices;
}
private void UpdateIdentityEndpoint()
{
// Update remote endpoint (save to local storage)
_settingsService.IdentityEndpointBase = _identityEndpoint;
}
private void UpdateGatewayShoppingEndpoint()
{
_settingsService.GatewayCatalogEndpointBase = _gatewayCatalogEndpoint;
}
private void UpdateGatewayOrdersEndpoint()
{
_settingsService.GatewayOrdersEndpointBase = _gatewayOrdersEndpoint;
}
private void UpdateGatewayBasketEndpoint()
{
_settingsService.GatewayBasketEndpointBase = _gatewayBasketEndpoint;
}
private void UpdateFakeLocation()
{
_settingsService.UseFakeLocation = _useFakeLocation;
}
private void UpdateLatitude()
{
// Update fake latitude (save to local storage)
_settingsService.Latitude = _latitude.ToString();
}
private void UpdateLongitude()
{
// Update fake longitude (save to local storage)
_settingsService.Longitude = _longitude.ToString();
}
private async Task UpdateAllowGpsLocation()
{
if (_allowGpsLocation)
{
bool hasWhenInUseLocationPermissions;
bool hasBackgroundLocationPermissions;
if (await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>() != PermissionStatus.Granted)
{
hasWhenInUseLocationPermissions = await Permissions.RequestAsync<Permissions.LocationWhenInUse>() ==
PermissionStatus.Granted;
}
else
{
hasWhenInUseLocationPermissions = true;
}
if (await Permissions.CheckStatusAsync<Permissions.LocationAlways>() != PermissionStatus.Granted)
{
hasBackgroundLocationPermissions = await Permissions.RequestAsync<Permissions.LocationAlways>() ==
PermissionStatus.Granted;
}
else
{
hasBackgroundLocationPermissions = true;
}
if (!hasWhenInUseLocationPermissions || !hasBackgroundLocationPermissions)
{
_allowGpsLocation = false;
GpsWarningMessage = "Enable the GPS sensor on your device";
}
else
{
_settingsService.AllowGpsLocation = _allowGpsLocation;
GpsWarningMessage = string.Empty;
}
}
else
{
_settingsService.AllowGpsLocation = _allowGpsLocation;
}
}
}