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

Commit b9835a3

Browse files
David BritchDavid Britch
authored andcommitted
iOS LocationService complete.
1 parent 1fa48de commit b9835a3

File tree

10 files changed

+31
-130
lines changed

10 files changed

+31
-130
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,10 @@ protected override async void OnStart()
4848
{
4949
await InitNavigation();
5050
}
51-
5251
if (_settingsService.AllowGpsLocation && !_settingsService.UseFakeLocation)
5352
{
5453
await GetGpsLocation();
5554
}
56-
5755
if (!_settingsService.UseMocks && !string.IsNullOrEmpty(_settingsService.AuthAccessToken))
5856
{
5957
await SendCurrentLocation();
@@ -74,9 +72,10 @@ private async Task GetGpsLocation()
7472

7573
if (locator.IsGeolocationEnabled && locator.IsGeolocationAvailable)
7674
{
77-
//locationService.AllowsBackgroundUpdates = true;
7875
locator.DesiredAccuracy = 50;
7976

77+
// Delay getting the position to ensure that the UI has finished updating
78+
await Task.Delay(2000);
8079
var position = await locator.GetPositionAsync();
8180

8281
_settingsService.Latitude = position.Latitude.ToString();

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
}

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Marketing/CampaignMockService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ public class CampaignMockService : ICampaignService
3838

3939
public async Task<ObservableCollection<CampaignItem>> GetAllCampaignsAsync(string token)
4040
{
41-
await Task.Delay(500);
41+
await Task.Delay(10);
4242
return _mockCampaign;
4343
}
4444

4545
public async Task<CampaignItem> GetCampaignByIdAsync(int campaignId, string token)
4646
{
47-
await Task.Delay(500);
47+
await Task.Delay(10);
4848
return _mockCampaign.SingleOrDefault(c => c.Id == campaignId);
4949
}
5050
}

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/Order/OrderMockService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public class OrderMockService : IOrderService
7474

7575
public async Task<ObservableCollection<Models.Orders.Order>> GetOrdersAsync(string token)
7676
{
77-
await Task.Delay(500);
77+
await Task.Delay(10);
7878

7979
if (!string.IsNullOrEmpty(token))
8080
{
@@ -88,7 +88,7 @@ public class OrderMockService : IOrderService
8888

8989
public async Task<Models.Orders.Order> GetOrderAsync(int orderId, string token)
9090
{
91-
await Task.Delay(500);
91+
await Task.Delay(10);
9292

9393
if (!string.IsNullOrEmpty(token))
9494
return MockOrders
@@ -99,7 +99,7 @@ public class OrderMockService : IOrderService
9999

100100
public async Task CreateOrderAsync(Models.Orders.Order newOrder, string token)
101101
{
102-
await Task.Delay(500);
102+
await Task.Delay(10);
103103

104104
if (!string.IsNullOrEmpty(token))
105105
{

src/Mobile/eShopOnContainers/eShopOnContainers.Core/Services/User/UserMockService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class UserMockService : IUserService
2828

2929
public async Task<UserInfo> GetUserInfoAsync(string authToken)
3030
{
31-
await Task.Delay(500);
31+
await Task.Delay(10);
3232
return MockUserInfo;
3333
}
3434
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private async Task MockSignInAsync()
160160
{
161161
try
162162
{
163-
await Task.Delay(1000);
163+
await Task.Delay(10);
164164

165165
isAuthenticated = true;
166166
}
@@ -189,7 +189,7 @@ private async Task SignInAsync()
189189
{
190190
IsBusy = true;
191191

192-
await Task.Delay(500);
192+
await Task.Delay(10);
193193

194194
LoginUrl = _identityService.CreateAuthorizationRequest();
195195

src/Mobile/eShopOnContainers/eShopOnContainers.iOS/Services/GeolocationSingleUpdateDelegate.cs

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,19 @@ namespace eShopOnContainers.iOS.Services
1010
{
1111
internal class GeolocationSingleUpdateDelegate : CLLocationManagerDelegate
1212
{
13-
bool _haveHeading;
1413
bool _haveLocation;
1514
readonly Position _position = new Position();
16-
CLHeading _bestHeading;
17-
1815
readonly double _desiredAccuracy;
19-
readonly bool _includeHeading;
2016
readonly TaskCompletionSource<Position> _tcs;
2117
readonly CLLocationManager _manager;
2218

2319
public Task<Position> Task => _tcs?.Task;
2420

25-
public GeolocationSingleUpdateDelegate(CLLocationManager manager, double desiredAccuracy, bool includeHeading, int timeout, CancellationToken cancelToken)
21+
public GeolocationSingleUpdateDelegate(CLLocationManager manager, double desiredAccuracy, int timeout, CancellationToken cancelToken)
2622
{
2723
_manager = manager;
2824
_tcs = new TaskCompletionSource<Position>(manager);
2925
_desiredAccuracy = desiredAccuracy;
30-
_includeHeading = includeHeading;
3126

3227
if (timeout != Timeout.Infinite)
3328
{
@@ -76,8 +71,6 @@ public override void Failed(CLLocationManager manager, NSError error)
7671
}
7772
}
7873

79-
public override bool ShouldDisplayHeadingCalibration(CLLocationManager manager) => true;
80-
8174
public override void UpdatedLocation(CLLocationManager manager, CLLocation newLocation, CLLocation oldLocation)
8275
{
8376
if (newLocation.HorizontalAccuracy < 0)
@@ -97,31 +90,13 @@ public override void UpdatedLocation(CLLocationManager manager, CLLocation newLo
9790
{
9891
_position.Timestamp = new DateTimeOffset((DateTime)newLocation.Timestamp);
9992
}
100-
catch (Exception ex)
93+
catch (Exception)
10194
{
10295
_position.Timestamp = DateTimeOffset.UtcNow;
10396
}
10497
_haveLocation = true;
10598

106-
if ((!_includeHeading || _haveHeading) && _position.Accuracy <= _desiredAccuracy)
107-
{
108-
_tcs.TrySetResult(new Position(_position));
109-
StopListening();
110-
}
111-
}
112-
113-
public override void UpdatedHeading(CLLocationManager manager, CLHeading newHeading)
114-
{
115-
if (newHeading.HeadingAccuracy < 0)
116-
return;
117-
if (_bestHeading != null && newHeading.HeadingAccuracy >= _bestHeading.HeadingAccuracy)
118-
return;
119-
120-
_bestHeading = newHeading;
121-
_position.Heading = newHeading.TrueHeading;
122-
_haveHeading = true;
123-
124-
if (_haveLocation && _position.Accuracy <= _desiredAccuracy)
99+
if (_position.Accuracy <= _desiredAccuracy)
125100
{
126101
_tcs.TrySetResult(new Position(_position));
127102
StopListening();
@@ -130,9 +105,6 @@ public override void UpdatedHeading(CLLocationManager manager, CLHeading newHead
130105

131106
private void StopListening()
132107
{
133-
if (CLLocationManager.HeadingAvailable)
134-
_manager.StopUpdatingHeading();
135-
136108
_manager.StopUpdatingLocation();
137109
}
138110
}

src/Mobile/eShopOnContainers/eShopOnContainers.iOS/Services/LocationServiceImplementation.cs

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ namespace eShopOnContainers.iOS.Services
1515
{
1616
public class LocationServiceImplementation : ILocationServiceImplementation
1717
{
18-
bool _deferringUpdates;
19-
readonly CLLocationManager _manager;
20-
Position _lastPosition;
21-
22-
public event EventHandler<PositionErrorEventArgs> PositionError;
23-
public event EventHandler<PositionEventArgs> PositionChanged;
2418
public double DesiredAccuracy { get; set; }
2519
public bool IsGeolocationAvailable => true;
2620
public bool IsGeolocationEnabled
@@ -32,21 +26,11 @@ public bool IsGeolocationEnabled
3226
}
3327
}
3428

35-
public bool SupportsHeading => CLLocationManager.HeadingAvailable;
36-
3729
public LocationServiceImplementation()
3830
{
3931
DesiredAccuracy = 100;
40-
//_manager = GetManager();
41-
//_manager.AuthorizationChanged += OnAuthorizationChanged;
42-
//_manager.Failed += OnFailed;
43-
//_manager.UpdatedLocation += OnUpdatedLocation;
44-
//_manager.UpdatedHeading += OnUpdatedHeading;
45-
//_manager.DeferredUpdatesFinished += OnDeferredUpdatesFinished;
4632
}
4733

48-
void OnDeferredUpdatesFinished(object sender, NSErrorEventArgs e) => _deferringUpdates = false;
49-
5034
#region Internal Implementation
5135

5236
async Task<bool> CheckPermissions(Permission permission)
@@ -78,7 +62,7 @@ CLLocationManager GetManager()
7862

7963
#region ILocationServiceImplementation
8064

81-
public async Task<Position> GetPositionAsync(TimeSpan? timeout, CancellationToken? cancelToken = null, bool includeHeading = false)
65+
public async Task<Position> GetPositionAsync(TimeSpan? timeout, CancellationToken? cancelToken = null)
8266
{
8367
var permission = Permission.LocationWhenInUse;
8468
var hasPermission = await CheckPermissions(permission);
@@ -96,53 +80,16 @@ public async Task<Position> GetPositionAsync(TimeSpan? timeout, CancellationToke
9680
var manager = GetManager();
9781
manager.DesiredAccuracy = DesiredAccuracy;
9882

99-
// Permit background updates if background location mode is enabled
100-
if (UIDevice.CurrentDevice.CheckSystemVersion(9, 0))
101-
{
102-
var backgroundModes = NSBundle.MainBundle.InfoDictionary[(NSString)"UIBackgroundModes"] as NSArray;
103-
manager.AllowsBackgroundLocationUpdates = backgroundModes != null && (backgroundModes.Contains((NSString)"Location") || backgroundModes.Contains((NSString)"location"));
104-
}
105-
10683
// Always prevent location update pausing since we're only listening for a single update
10784
if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0))
10885
manager.PausesLocationUpdatesAutomatically = false;
10986

11087
tcs = new TaskCompletionSource<Position>(manager);
111-
var singleListener = new GeolocationSingleUpdateDelegate(manager, DesiredAccuracy, includeHeading, timeoutMilliseconds, cancelToken.Value);
88+
var singleListener = new GeolocationSingleUpdateDelegate(manager, DesiredAccuracy, timeoutMilliseconds, cancelToken.Value);
11289
manager.Delegate = singleListener;
11390
manager.StartUpdatingLocation();
11491

115-
if (includeHeading && SupportsHeading)
116-
manager.StartUpdatingHeading();
117-
11892
return await singleListener.Task;
119-
120-
//tcs = new TaskCompletionSource<Position>();
121-
//if (_lastPosition == null)
122-
//{
123-
// if (cancelToken != CancellationToken.None)
124-
// cancelToken.Value.Register(() => tcs.TrySetCanceled());
125-
126-
// EventHandler<PositionErrorEventArgs> gotError = null;
127-
// gotError = (s, e) =>
128-
// {
129-
// tcs.TrySetException(new GeolocationException(e.Error));
130-
// PositionError -= gotError;
131-
// };
132-
// PositionError += gotError;
133-
134-
// EventHandler<PositionEventArgs> gotPosition = null;
135-
// gotPosition = (s, e) =>
136-
// {
137-
// tcs.TrySetResult(e.Position);
138-
// PositionChanged += gotPosition;
139-
// };
140-
// PositionChanged += gotPosition;
141-
//}
142-
//else
143-
// tcs.SetResult(_lastPosition);
144-
145-
//return await tcs.Task;
14693
}
14794

14895
#endregion

src/Mobile/eShopOnContainers/eShopOnContainers.iOS/Services/PermissionsService.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ PermissionStatus GetLocationPermissionStatus(Permission permission)
1919
return PermissionStatus.Disabled;
2020

2121
var status = CLLocationManager.Status;
22-
2322
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
2423
{
2524
switch (status)
@@ -77,22 +76,6 @@ Task<PermissionStatus> RequestLocationPermissionAsync(Permission permission = Pe
7776
locationManager.AuthorizationChanged += authCallback;
7877

7978
var info = NSBundle.MainBundle.InfoDictionary;
80-
//if (permission == Permission.Location)
81-
//{
82-
// if (info.ContainsKey(new NSString("NSLocationAlwaysUsageDescription")))
83-
// locationManager.RequestAlwaysAuthorization();
84-
// else if (info.ContainsKey(new NSString("NSLocationWhenInUseUsageDescription")))
85-
// locationManager.RequestWhenInUseAuthorization();
86-
// else
87-
// throw new UnauthorizedAccessException("On iOS 8.0 and higher you must set either NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in your Info.plist file to enable Authorization Requests for Location updates!");
88-
//}
89-
//else if (permission == Permission.LocationAlways)
90-
//{
91-
// if (info.ContainsKey(new NSString("NSLocationAlwaysUsageDescription")))
92-
// locationManager.RequestAlwaysAuthorization();
93-
// else
94-
// throw new UnauthorizedAccessException("On iOS 8.0 and higher you must set either NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in your Info.plist file to enable Authorization Requests for Location updates!");
95-
//}
9679
if (permission == Permission.LocationWhenInUse)
9780
{
9881
if (info.ContainsKey(new NSString("NSLocationWhenInUseUsageDescription")))

0 commit comments

Comments
 (0)