Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit f49518d

Browse files
authored
Merge pull request #521 from dotnet-architecture/xamarin-location
Removed geolocation plugin from mobile apps
2 parents 7c7a609 + 6e7a0ac commit f49518d

38 files changed

+1285
-103
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using eShopOnContainers.Core.Models.Location;
2+
using eShopOnContainers.Core.Services.Dependency;
23
using eShopOnContainers.Core.Services.Location;
34
using eShopOnContainers.Core.Services.Settings;
45
using eShopOnContainers.Core.ViewModels.Base;
56
using eShopOnContainers.Services;
6-
using Plugin.Geolocator;
7+
using System;
8+
using System.Diagnostics;
79
using System.Globalization;
810
using System.Threading.Tasks;
911
using Xamarin.Forms;
@@ -48,12 +50,10 @@ protected override async void OnStart()
4850
{
4951
await InitNavigation();
5052
}
51-
5253
if (_settingsService.AllowGpsLocation && !_settingsService.UseFakeLocation)
5354
{
5455
await GetGpsLocation();
5556
}
56-
5757
if (!_settingsService.UseMocks && !string.IsNullOrEmpty(_settingsService.AuthAccessToken))
5858
{
5959
await SendCurrentLocation();
@@ -69,17 +69,23 @@ protected override void OnSleep()
6969

7070
private async Task GetGpsLocation()
7171
{
72-
var locator = CrossGeolocator.Current;
72+
var dependencyService = ViewModelLocator.Resolve<IDependencyService>();
73+
var locator = dependencyService.Get<ILocationServiceImplementation>();
7374

7475
if (locator.IsGeolocationEnabled && locator.IsGeolocationAvailable)
7576
{
76-
locator.AllowsBackgroundUpdates = true;
7777
locator.DesiredAccuracy = 50;
7878

79-
var position = await locator.GetPositionAsync();
80-
81-
_settingsService.Latitude = position.Latitude.ToString();
82-
_settingsService.Longitude = position.Longitude.ToString();
79+
try
80+
{
81+
var position = await locator.GetPositionAsync();
82+
_settingsService.Latitude = position.Latitude.ToString();
83+
_settingsService.Longitude = position.Longitude.ToString();
84+
}
85+
catch (Exception ex)
86+
{
87+
Debug.WriteLine(ex);
88+
}
8389
}
8490
else
8591
{
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace eShopOnContainers.Core.Models.Location
2+
{
3+
public enum GeolocationError
4+
{
5+
PositionUnavailable,
6+
Unauthorized
7+
}
8+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
3+
namespace eShopOnContainers.Core.Models.Location
4+
{
5+
public class GeolocationException : Exception
6+
{
7+
public GeolocationError Error { get; private set; }
8+
9+
public GeolocationException(GeolocationError error)
10+
: base("A geolocation error occured: " + error)
11+
{
12+
if (!Enum.IsDefined(typeof(GeolocationError), error))
13+
throw new ArgumentException("error is not a valid GelocationError member", "error");
14+
15+
Error = error;
16+
}
17+
18+
public GeolocationException(GeolocationError error, Exception innerException)
19+
: base("A geolocation error occured: " + error, innerException)
20+
{
21+
if (!Enum.IsDefined(typeof(GeolocationError), error))
22+
throw new ArgumentException("error is not a valid GelocationError member", "error");
23+
24+
Error = error;
25+
}
26+
}
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
3+
namespace eShopOnContainers.Core.Models.Location
4+
{
5+
public class Position
6+
{
7+
public DateTimeOffset Timestamp { get; set; }
8+
public double Latitude { get; set; }
9+
public double Longitude { get; set; }
10+
public double Altitude { get; set; }
11+
public double Accuracy { get; set; }
12+
public double AltitudeAccuracy { get; set; }
13+
public double Heading { get; set; }
14+
public double Speed { get; set; }
15+
16+
public Position()
17+
{
18+
}
19+
20+
public Position(double latitude, double longitude)
21+
{
22+
23+
Timestamp = DateTimeOffset.UtcNow;
24+
Latitude = latitude;
25+
Longitude = longitude;
26+
}
27+
28+
public Position(Position position)
29+
{
30+
if (position == null)
31+
throw new ArgumentNullException("position");
32+
33+
Timestamp = position.Timestamp;
34+
Latitude = position.Latitude;
35+
Longitude = position.Longitude;
36+
Altitude = position.Altitude;
37+
AltitudeAccuracy = position.AltitudeAccuracy;
38+
Accuracy = position.Accuracy;
39+
Heading = position.Heading;
40+
Speed = position.Speed;
41+
}
42+
}
43+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace eShopOnContainers.Core.Models.Permissions
2+
{
3+
public enum Permission
4+
{
5+
Unknown,
6+
Location,
7+
LocationAlways,
8+
LocationWhenInUse
9+
}
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace eShopOnContainers.Core.Models.Permissions
2+
{
3+
public enum PermissionStatus
4+
{
5+
Denied,
6+
Disabled,
7+
Granted,
8+
Restricted,
9+
Unknown
10+
}
11+
}

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Basket/BasketMockService.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ public class BasketMockService : IBasketService
1313
BuyerId = "9245fe4a-d402-451c-b9ed-9c1a04247482",
1414
Items = new List<BasketItem>
1515
{
16-
new BasketItem { Id = "1", PictureUrl = Device.RuntimePlatform != Device.UWP ? "fake_product_01.png" : "Assets/fake_product_01.png", ProductId = Common.Common.MockCatalogItemId01, ProductName = ".NET Bot Blue Sweatshirt (M)", Quantity = 1, UnitPrice = 19.50M },
17-
new BasketItem { Id = "2", PictureUrl = Device.RuntimePlatform != Device.UWP ? "fake_product_04.png" : "Assets/fake_product_04.png", ProductId = Common.Common.MockCatalogItemId04, ProductName = ".NET Black Cupt", Quantity = 1, UnitPrice = 17.00M }
16+
new BasketItem { Id = "1", PictureUrl = Device.RuntimePlatform != Device.UWP ? "fake_product_01.png" : "Assets/fake_product_01.png", ProductId = Common.Common.MockCatalogItemId01, ProductName = ".NET Bot Blue Sweatshirt (M)", Quantity = 1, UnitPrice = 19.50M },
17+
new BasketItem { Id = "2", PictureUrl = Device.RuntimePlatform != Device.UWP ? "fake_product_04.png" : "Assets/fake_product_04.png", ProductId = Common.Common.MockCatalogItemId04, ProductName = ".NET Black Cupt", Quantity = 1, UnitPrice = 17.00M }
1818
}
1919
};
2020

2121
public async Task<CustomerBasket> GetBasketAsync(string guidUser, string token)
2222
{
23-
await Task.Delay(500);
23+
await Task.Delay(10);
2424

25-
if(string.IsNullOrEmpty(guidUser) || string.IsNullOrEmpty(token))
25+
if (string.IsNullOrEmpty(guidUser) || string.IsNullOrEmpty(token))
2626
{
2727
return new CustomerBasket();
2828
}
@@ -32,7 +32,7 @@ public async Task<CustomerBasket> GetBasketAsync(string guidUser, string token)
3232

3333
public async Task<CustomerBasket> UpdateBasketAsync(CustomerBasket customerBasket, string token)
3434
{
35-
await Task.Delay(500);
35+
await Task.Delay(10);
3636

3737
if (string.IsNullOrEmpty(token))
3838
{
@@ -46,7 +46,7 @@ public async Task<CustomerBasket> UpdateBasketAsync(CustomerBasket customerBaske
4646

4747
public async Task ClearBasketAsync(string guidUser, string token)
4848
{
49-
await Task.Delay(500);
49+
await Task.Delay(10);
5050

5151
if (string.IsNullOrEmpty(token))
5252
{

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Catalog/CatalogMockService.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,40 @@ public class CatalogMockService : ICatalogService
2424

2525
private ObservableCollection<CatalogItem> MockCatalog = new ObservableCollection<CatalogItem>
2626
{
27-
new CatalogItem { Id = Common.Common.MockCatalogItemId01, PictureUri = Device.RuntimePlatform != Device.UWP ? "fake_product_01.png" : "Assets/fake_product_01.png", Name = ".NET Bot Blue Sweatshirt (M)", Price = 19.50M, CatalogBrandId = 2, CatalogBrand = "Visual Studio", CatalogTypeId = 2, CatalogType = "T-Shirt" },
28-
new CatalogItem { Id = Common.Common.MockCatalogItemId02, PictureUri = Device.RuntimePlatform != Device.UWP ? "fake_product_02.png" : "Assets/fake_product_02.png", Name = ".NET Bot Purple Sweatshirt (M)", Price = 19.50M, CatalogBrandId = 2, CatalogBrand = "Visual Studio", CatalogTypeId = 2, CatalogType = "T-Shirt" },
29-
new CatalogItem { Id = Common.Common.MockCatalogItemId03, PictureUri = Device.RuntimePlatform != Device.UWP ? "fake_product_03.png" : "Assets/fake_product_03.png", Name = ".NET Bot Black Sweatshirt (M)", Price = 19.95M, CatalogBrandId = 2, CatalogBrand = "Visual Studio", CatalogTypeId = 2, CatalogType = "T-Shirt" },
30-
new CatalogItem { Id = Common.Common.MockCatalogItemId04, PictureUri = Device.RuntimePlatform != Device.UWP ? "fake_product_04.png" : "Assets/fake_product_04.png", Name = ".NET Black Cupt", Price = 17.00M, CatalogBrandId = 2, CatalogBrand = "Visual Studio", CatalogTypeId = 1, CatalogType = "Mug" },
31-
new CatalogItem { Id = Common.Common.MockCatalogItemId05, PictureUri = Device.RuntimePlatform != Device.UWP ? "fake_product_05.png" : "Assets/fake_product_05.png", Name = "Azure Black Sweatshirt (M)", Price = 19.50M, CatalogBrandId = 1, CatalogBrand = "Azure", CatalogTypeId = 2, CatalogType = "T-Shirt" }
27+
new CatalogItem { Id = Common.Common.MockCatalogItemId01, PictureUri = Device.RuntimePlatform != Device.UWP ? "fake_product_01.png" : "Assets/fake_product_01.png", Name = ".NET Bot Blue Sweatshirt (M)", Price = 19.50M, CatalogBrandId = 2, CatalogBrand = "Visual Studio", CatalogTypeId = 2, CatalogType = "T-Shirt" },
28+
new CatalogItem { Id = Common.Common.MockCatalogItemId02, PictureUri = Device.RuntimePlatform != Device.UWP ? "fake_product_02.png" : "Assets/fake_product_02.png", Name = ".NET Bot Purple Sweatshirt (M)", Price = 19.50M, CatalogBrandId = 2, CatalogBrand = "Visual Studio", CatalogTypeId = 2, CatalogType = "T-Shirt" },
29+
new CatalogItem { Id = Common.Common.MockCatalogItemId03, PictureUri = Device.RuntimePlatform != Device.UWP ? "fake_product_03.png" : "Assets/fake_product_03.png", Name = ".NET Bot Black Sweatshirt (M)", Price = 19.95M, CatalogBrandId = 2, CatalogBrand = "Visual Studio", CatalogTypeId = 2, CatalogType = "T-Shirt" },
30+
new CatalogItem { Id = Common.Common.MockCatalogItemId04, PictureUri = Device.RuntimePlatform != Device.UWP ? "fake_product_04.png" : "Assets/fake_product_04.png", Name = ".NET Black Cupt", Price = 17.00M, CatalogBrandId = 2, CatalogBrand = "Visual Studio", CatalogTypeId = 1, CatalogType = "Mug" },
31+
new CatalogItem { Id = Common.Common.MockCatalogItemId05, PictureUri = Device.RuntimePlatform != Device.UWP ? "fake_product_05.png" : "Assets/fake_product_05.png", Name = "Azure Black Sweatshirt (M)", Price = 19.50M, CatalogBrandId = 1, CatalogBrand = "Azure", CatalogTypeId = 2, CatalogType = "T-Shirt" }
3232
};
3333

3434
public async Task<ObservableCollection<CatalogItem>> GetCatalogAsync()
3535
{
36-
await Task.Delay(500);
36+
await Task.Delay(10);
3737

3838
return MockCatalog;
3939
}
4040

4141
public async Task<ObservableCollection<CatalogItem>> FilterAsync(int catalogBrandId, int catalogTypeId)
4242
{
43-
await Task.Delay(500);
43+
await Task.Delay(10);
4444

4545
return MockCatalog
4646
.Where(c => c.CatalogBrandId == catalogBrandId &&
47-
c.CatalogTypeId == catalogTypeId)
47+
c.CatalogTypeId == catalogTypeId)
4848
.ToObservableCollection();
4949
}
5050

5151
public async Task<ObservableCollection<CatalogBrand>> GetCatalogBrandAsync()
5252
{
53-
await Task.Delay(500);
53+
await Task.Delay(10);
5454

5555
return MockCatalogBrand;
5656
}
5757

5858
public async Task<ObservableCollection<CatalogType>> GetCatalogTypeAsync()
5959
{
60-
await Task.Delay(500);
60+
await Task.Delay(10);
6161

6262
return MockCatalogType;
6363
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using eShopOnContainers.Core.Models.Location;
2+
using System;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
6+
namespace eShopOnContainers.Core.Services.Location
7+
{
8+
public interface ILocationServiceImplementation
9+
{
10+
double DesiredAccuracy { get; set; }
11+
bool IsGeolocationAvailable { get; }
12+
bool IsGeolocationEnabled { get; }
13+
14+
Task<Position> GetPositionAsync(TimeSpan? timeout = null, CancellationToken? token = null);
15+
}
16+
}

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Location/LocationService.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ public LocationService(IRequestProvider requestProvider)
1616
public async Task UpdateUserLocation(eShopOnContainers.Core.Models.Location.Location newLocReq, string token)
1717
{
1818
UriBuilder builder = new UriBuilder(GlobalSetting.Instance.LocationEndpoint);
19-
2019
builder.Path = "api/v1/locations";
21-
2220
string uri = builder.ToString();
23-
2421
await _requestProvider.PostAsync(uri, newLocReq, token);
2522
}
2623
}

0 commit comments

Comments
 (0)