Skip to content

Commit 6a02eb3

Browse files
committed
Add gps usage options
1 parent d75f480 commit 6a02eb3

4 files changed

Lines changed: 187 additions & 53 deletions

File tree

src/Mobile/eShopOnContainers/eShopOnContainers.Core/App.xaml.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected override async void OnStart()
5050
await InitNavigation();
5151
}
5252

53-
if (!Settings.UseFakeLocation)
53+
if (Settings.GpsUsage && !Settings.UseFakeLocation)
5454
{
5555
await GetRealLocation();
5656
}
@@ -71,13 +71,17 @@ protected override void OnSleep()
7171
private async Task GetRealLocation()
7272
{
7373
var locator = CrossGeolocator.Current;
74-
locator.AllowsBackgroundUpdates = true;
75-
locator.DesiredAccuracy = 50;
7674

77-
var position = await locator.GetPositionAsync(20000);
75+
if (locator.IsGeolocationEnabled && locator.IsGeolocationAvailable)
76+
{
77+
locator.AllowsBackgroundUpdates = true;
78+
locator.DesiredAccuracy = 50;
7879

79-
Settings.Latitude = position.Latitude;
80-
Settings.Longitude = position.Longitude;
80+
var position = await locator.GetPositionAsync(20000);
81+
82+
Settings.Latitude = position.Latitude;
83+
Settings.Longitude = position.Longitude;
84+
}
8185
}
8286

8387
private async Task SendCurrentLocation()

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Helpers/Settings.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ private static ISettings AppSettings
2828
private const string IdUseFakeLocation = "use_fake_location";
2929
private const string IdLatitude = "latitude";
3030
private const string IdLongitude = "flongitude";
31+
private const string IdGpsUsage = "gps_usage";
3132
private static readonly string AccessTokenDefault = string.Empty;
3233
private static readonly string IdTokenDefault = string.Empty;
3334
private static readonly bool UseMocksDefault = true;
3435
private static readonly bool UseFakeLocationDefault = false;
36+
private static readonly bool GpsUsageValue = false;
3537
private static readonly double FakeLatitudeValue = 47.604610d;
3638
private static readonly double FakeLongitudeValue = -122.315752d;
3739
private static readonly string UrlBaseDefault = GlobalSetting.Instance.BaseEndpoint;
@@ -84,5 +86,11 @@ public static double Longitude
8486
get => AppSettings.GetValueOrDefault<double>(IdLongitude, FakeLongitudeValue);
8587
set => AppSettings.AddOrUpdateValue<double>(IdLongitude, value);
8688
}
89+
90+
public static bool GpsUsage
91+
{
92+
get => AppSettings.GetValueOrDefault<bool>(IdGpsUsage, GpsUsageValue);
93+
set => AppSettings.AddOrUpdateValue<bool>(IdGpsUsage, value);
94+
}
8795
}
8896
}

src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs

Lines changed: 120 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,46 @@
1-
using eShopOnContainers.Core.ViewModels.Base;
2-
using System.Windows.Input;
3-
using Xamarin.Forms;
4-
using System.Threading.Tasks;
5-
using eShopOnContainers.Core.Helpers;
6-
using eShopOnContainers.Core.Models.User;
7-
using System;
8-
using eShopOnContainers.Core.Models.Location;
9-
using eShopOnContainers.Core.Services.Location;
10-
11-
namespace eShopOnContainers.Core.ViewModels
1+
namespace eShopOnContainers.Core.ViewModels
122
{
3+
using System.Windows.Input;
4+
using Xamarin.Forms;
5+
using System.Threading.Tasks;
6+
using Helpers;
7+
using Models.User;
8+
using Base;
9+
using Models.Location;
10+
using Services.Location;
11+
1312
public class SettingsViewModel : ViewModelBase
1413
{
1514
private string _titleUseAzureServices;
1615
private string _descriptionUseAzureServices;
1716
private bool _useAzureServices;
1817
private string _titleUseFakeLocation;
1918
private string _descriptionUseFakeLocation;
19+
private bool _gpsUsage;
20+
private string _titleGpsUsage;
21+
private string _descriptionGpsUsage;
2022
private bool _useFakeLocation;
2123
private string _endpoint;
2224
private double _latitude;
2325
private double _longitude;
24-
private ILocationService _locationService;
26+
27+
private readonly ILocationService _locationService;
2528

2629
public SettingsViewModel(ILocationService locationService)
2730
{
28-
UseAzureServices = !Settings.UseMocks;
29-
_useFakeLocation = Settings.UseFakeLocation;
31+
_locationService = locationService;
32+
33+
_useAzureServices = !Settings.UseMocks;
34+
_endpoint = Settings.UrlBase;
3035
_latitude = Settings.Latitude;
3136
_longitude = Settings.Longitude;
32-
_locationService = locationService;
37+
_useFakeLocation = Settings.UseFakeLocation;
38+
_gpsUsage = Settings.GpsUsage;
3339
}
3440

3541
public string TitleUseAzureServices
3642
{
37-
get { return _titleUseAzureServices; }
43+
get => _titleUseAzureServices;
3844
set
3945
{
4046
_titleUseAzureServices = value;
@@ -44,7 +50,7 @@ public string TitleUseAzureServices
4450

4551
public string DescriptionUseAzureServices
4652
{
47-
get { return _descriptionUseAzureServices; }
53+
get => _descriptionUseAzureServices;
4854
set
4955
{
5056
_descriptionUseAzureServices = value;
@@ -54,20 +60,20 @@ public string DescriptionUseAzureServices
5460

5561
public bool UseAzureServices
5662
{
57-
get { return _useAzureServices; }
63+
get => _useAzureServices;
5864
set
5965
{
6066
_useAzureServices = value;
6167

62-
// Save use mocks services to local storage
63-
Settings.UseMocks = !_useAzureServices;
68+
UpdateUseAzureServices();
69+
6470
RaisePropertyChanged(() => UseAzureServices);
6571
}
6672
}
6773

6874
public string TitleUseFakeLocation
6975
{
70-
get { return _titleUseFakeLocation; }
76+
get => _titleUseFakeLocation;
7177
set
7278
{
7379
_titleUseFakeLocation = value;
@@ -77,7 +83,7 @@ public string TitleUseFakeLocation
7783

7884
public string DescriptionUseFakeLocation
7985
{
80-
get { return _descriptionUseFakeLocation; }
86+
get => _descriptionUseFakeLocation;
8187
set
8288
{
8389
_descriptionUseFakeLocation = value;
@@ -87,27 +93,47 @@ public string DescriptionUseFakeLocation
8793

8894
public bool UseFakeLocation
8995
{
90-
get { return _useFakeLocation; }
96+
get => _useFakeLocation;
9197
set
9298
{
9399
_useFakeLocation = value;
94100

95-
// Save use fake location services to local storage
96-
Settings.UseFakeLocation = _useFakeLocation;
101+
UpdateFakeLocation();
102+
97103
RaisePropertyChanged(() => UseFakeLocation);
98104
}
99105
}
100106

107+
public string TitleGpsUsage
108+
{
109+
get => _titleGpsUsage;
110+
set
111+
{
112+
_titleGpsUsage = value;
113+
RaisePropertyChanged(() => TitleGpsUsage);
114+
}
115+
}
116+
117+
public string DescriptionGpsUsage
118+
{
119+
get => _descriptionGpsUsage;
120+
set
121+
{
122+
_descriptionGpsUsage = value;
123+
RaisePropertyChanged(() => DescriptionGpsUsage);
124+
}
125+
}
126+
101127
public string Endpoint
102128
{
103-
get { return _endpoint; }
129+
get => _endpoint;
104130
set
105131
{
106132
_endpoint = value;
107133

108134
if(!string.IsNullOrEmpty(_endpoint))
109135
{
110-
UpdateEndpoint(_endpoint);
136+
UpdateEndpoint();
111137
}
112138

113139
RaisePropertyChanged(() => Endpoint);
@@ -116,54 +142,66 @@ public string Endpoint
116142

117143
public double Latitude
118144
{
119-
get { return _latitude; }
145+
get => _latitude;
120146
set
121147
{
122148
_latitude = value;
123149

124-
UpdateLatitude(_latitude);
150+
UpdateLatitude();
125151

126152
RaisePropertyChanged(() => Latitude);
127153
}
128154
}
129155

130156
public double Longitude
131157
{
132-
get { return _longitude; }
158+
get => _longitude;
133159
set
134160
{
135161
_longitude = value;
136162

137-
UpdateLongitude(_longitude);
163+
UpdateLongitude();
138164

139165
RaisePropertyChanged(() => Longitude);
140166
}
141167
}
142168

169+
public bool GpsUsage
170+
{
171+
get => _gpsUsage;
172+
set
173+
{
174+
_gpsUsage = value;
175+
176+
UpdateGpsUsage();
177+
178+
RaisePropertyChanged(() => GpsUsage);
179+
}
180+
}
181+
143182
public bool UserIsLogged => !string.IsNullOrEmpty(Settings.AuthAccessToken);
144183

145184
public ICommand ToggleMockServicesCommand => new Command(async () => await ToggleMockServicesAsync());
146185

147-
public ICommand ToggleFakeLocationCommand => new Command(() => ToggleFakeLocationAsync());
186+
public ICommand ToggleFakeLocationCommand => new Command(ToggleFakeLocationAsync);
148187

149188
public ICommand ToggleSendLocationCommand => new Command(async () => await ToggleSendLocationAsync());
150189

190+
public ICommand ToggleGpsUsageCommand => new Command(ToggleGpsUsage);
191+
151192
public override Task InitializeAsync(object navigationData)
152193
{
153-
UpdateInfo();
194+
UpdateInfoUseAzureServices();
154195
UpdateInfoFakeLocation();
155-
156-
Endpoint = Settings.UrlBase;
157-
_latitude = Settings.Latitude;
158-
_longitude = Settings.Longitude;
159-
_useFakeLocation = Settings.UseFakeLocation;
196+
UpdateInfoGpsUsage();
197+
160198
return base.InitializeAsync(navigationData);
161199
}
162200

163201
private async Task ToggleMockServicesAsync()
164202
{
165203
ViewModelLocator.RegisterDependencies(!UseAzureServices);
166-
UpdateInfo();
204+
UpdateInfoUseAzureServices();
167205

168206
var previousPageViewModel = NavigationService.PreviousPageViewModel;
169207
if (previousPageViewModel != null)
@@ -204,7 +242,12 @@ private async Task ToggleSendLocationAsync()
204242
}
205243
}
206244

207-
private void UpdateInfo()
245+
private void ToggleGpsUsage()
246+
{
247+
UpdateInfoGpsUsage();
248+
}
249+
250+
private void UpdateInfoUseAzureServices()
208251
{
209252
if (!UseAzureServices)
210253
{
@@ -232,22 +275,53 @@ private void UpdateInfoFakeLocation()
232275
}
233276
}
234277

235-
private void UpdateEndpoint(string endpoint)
278+
private void UpdateInfoGpsUsage()
279+
{
280+
if (!GpsUsage)
281+
{
282+
TitleGpsUsage = "Enable GPS";
283+
DescriptionGpsUsage = "When enabling the use of device gps you will get the location campaigns through your real location.";
284+
}
285+
else
286+
{
287+
TitleGpsUsage = "Disable GPS";
288+
DescriptionGpsUsage = "When disabling the use of device gps you won't get the location campaigns through your real location.";
289+
}
290+
}
291+
292+
293+
private void UpdateUseAzureServices()
294+
{
295+
// Save use mocks services to local storage
296+
Settings.UseMocks = !_useAzureServices;
297+
}
298+
299+
private void UpdateEndpoint()
236300
{
237301
// Update remote endpoint (save to local storage)
238-
Settings.UrlBase = endpoint;
302+
GlobalSetting.Instance.BaseEndpoint = Settings.UrlBase = _endpoint;
239303
}
240304

241-
private void UpdateLatitude(double latitude)
305+
private void UpdateFakeLocation()
306+
{
307+
Settings.UseFakeLocation = _useFakeLocation;
308+
}
309+
310+
private void UpdateLatitude()
242311
{
243312
// Update fake latitude (save to local storage)
244-
Settings.Latitude = latitude;
313+
Settings.Latitude = _latitude;
245314
}
246315

247-
private void UpdateLongitude(double longitude)
316+
private void UpdateLongitude()
248317
{
249318
// Update fake longitude (save to local storage)
250-
Settings.Longitude = longitude;
319+
Settings.Longitude = _longitude;
320+
}
321+
322+
private void UpdateGpsUsage()
323+
{
324+
Settings.GpsUsage = _gpsUsage;
251325
}
252326
}
253327
}

0 commit comments

Comments
 (0)