Skip to content

Commit f217471

Browse files
committed
Merge branch 'feature/track-user-location-mongodb' of https://github.com/dotnet-architecture/eShopOnContainers into feature/track-user-location-mongodb
2 parents ea6084d + c82b5f3 commit f217471

9 files changed

Lines changed: 312 additions & 24 deletions

File tree

src/Mobile/eShopOnContainers/eShopOnContainers.Core/GlobalSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public string BaseEndpoint
4343

4444
public string IdentityEndpoint { get; set; }
4545

46+
public string LocationEndpoint { get; set; }
47+
4648
public string UserInfoEndpoint { get; set; }
4749

4850
public string LogoutEndpoint { get; set; }
@@ -62,6 +64,7 @@ private void UpdateEndpoint(string baseEndpoint)
6264
LogoutEndpoint = string.Format("{0}:5105/connect/endsession", baseEndpoint);
6365
IdentityCallback = string.Format("{0}:5105/xamarincallback", baseEndpoint);
6466
LogoutCallback = string.Format("{0}:5105/Account/Redirecting", baseEndpoint);
67+
LocationEndpoint = string.Format("{0}:5109", baseEndpoint);
6568
}
6669
}
6770
}

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ private static ISettings AppSettings
2424
private const string IdToken = "id_token";
2525
private const string IdUseMocks = "use_mocks";
2626
private const string IdUrlBase = "url_base";
27+
private const string IdUseFakeLocation = "use_fake_location";
28+
private const string IdFakeLatitude = "fake_latitude";
29+
private const string IdFakeLongitude = "fake_longitude";
2730
private static readonly string AccessTokenDefault = string.Empty;
2831
private static readonly string IdTokenDefault = string.Empty;
2932
private static readonly bool UseMocksDefault = true;
33+
private static readonly bool UseFakeLocationDefault = false;
3034
private static readonly string UrlBaseDefault = GlobalSetting.Instance.BaseEndpoint;
3135

3236
#endregion
@@ -78,5 +82,40 @@ public static string UrlBase
7882
AppSettings.AddOrUpdateValue<string>(IdUrlBase, value);
7983
}
8084
}
85+
86+
public static bool UseFakeLocation
87+
{
88+
get
89+
{
90+
return AppSettings.GetValueOrDefault<bool>(IdUseFakeLocation, UseFakeLocationDefault);
91+
}
92+
set
93+
{
94+
AppSettings.AddOrUpdateValue<bool>(IdUseFakeLocation, value);
95+
}
96+
}
97+
98+
public static double FakeLatitude
99+
{
100+
get
101+
{
102+
return AppSettings.GetValueOrDefault<double>(IdFakeLatitude);
103+
}
104+
set
105+
{
106+
AppSettings.AddOrUpdateValue<double>(IdFakeLatitude, value);
107+
}
108+
}
109+
public static double FakeLongitude
110+
{
111+
get
112+
{
113+
return AppSettings.GetValueOrDefault<double>(IdFakeLongitude);
114+
}
115+
set
116+
{
117+
AppSettings.AddOrUpdateValue<double>(IdFakeLongitude, value);
118+
}
119+
}
81120
}
82121
}
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 class LocationRequest
4+
{
5+
public double Longitude { get; set; }
6+
public double Latitude { get; set; }
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace eShopOnContainers.Core.Services.Location
2+
{
3+
using System.Threading.Tasks;
4+
using eShopOnContainers.Core.Models.Location;
5+
6+
public interface ILocationService
7+
{
8+
Task UpdateUserLocation(LocationRequest newLocReq);
9+
}
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace eShopOnContainers.Core.Services.Location
2+
{
3+
using eShopOnContainers.Core.Models.Location;
4+
using eShopOnContainers.Core.Services.RequestProvider;
5+
using System;
6+
using System.Threading.Tasks;
7+
8+
public class LocationService : ILocationService
9+
{
10+
private readonly IRequestProvider _requestProvider;
11+
12+
public LocationService(IRequestProvider requestProvider)
13+
{
14+
_requestProvider = requestProvider;
15+
}
16+
17+
public async Task UpdateUserLocation(LocationRequest newLocReq)
18+
{
19+
UriBuilder builder = new UriBuilder(GlobalSetting.Instance.LocationEndpoint);
20+
21+
builder.Path = "api/v1/locations";
22+
23+
string uri = builder.ToString();
24+
25+
var result = await _requestProvider.PostAsync(uri, newLocReq);
26+
}
27+
}
28+
}

src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/Base/ViewModelLocator.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using eShopOnContainers.Core.Services.Order;
1212
using eShopOnContainers.Core.Services.User;
1313
using Xamarin.Forms;
14+
using eShopOnContainers.Core.Services.Location;
1415

1516
namespace eShopOnContainers.Core.ViewModels.Base
1617
{
@@ -53,24 +54,25 @@ public static void RegisterDependencies(bool useMockServices)
5354
builder.RegisterType<OpenUrlService>().As<IOpenUrlService>();
5455
builder.RegisterType<IdentityService>().As<IIdentityService>();
5556
builder.RegisterType<RequestProvider>().As<IRequestProvider>();
57+
builder.RegisterType<LocationService>().As<ILocationService>().SingleInstance();
5658

57-
if (useMockServices)
59+
if (useMockServices)
5860
{
5961
builder.RegisterInstance(new CatalogMockService()).As<ICatalogService>();
6062
builder.RegisterInstance(new BasketMockService()).As<IBasketService>();
6163
builder.RegisterInstance(new OrderMockService()).As<IOrderService>();
6264
builder.RegisterInstance(new UserMockService()).As<IUserService>();
6365

64-
UseMockService = true;
66+
UseMockService = true;
6567
}
6668
else
6769
{
6870
builder.RegisterType<CatalogService>().As<ICatalogService>().SingleInstance();
6971
builder.RegisterType<BasketService>().As<IBasketService>().SingleInstance();
7072
builder.RegisterType<OrderService>().As<IOrderService>().SingleInstance();
71-
builder.RegisterType<UserService>().As<IUserService>().SingleInstance();
73+
builder.RegisterType<UserService>().As<IUserService>().SingleInstance();
7274

73-
UseMockService = false;
75+
UseMockService = false;
7476
}
7577

7678
if (_container != null)

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

Lines changed: 140 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,51 @@
44
using System.Threading.Tasks;
55
using eShopOnContainers.Core.Helpers;
66
using eShopOnContainers.Core.Models.User;
7+
using System;
8+
using eShopOnContainers.Core.Models.Location;
9+
using eShopOnContainers.Core.Services.Location;
710

811
namespace eShopOnContainers.Core.ViewModels
912
{
1013
public class SettingsViewModel : ViewModelBase
1114
{
12-
private string _title;
13-
private string _description;
15+
private string _titleUseAzureServices;
16+
private string _descriptionUseAzureServices;
1417
private bool _useAzureServices;
18+
private string _titleUseFakeLocation;
19+
private string _descriptionUseFakeLocation;
20+
private bool _useFakeLocation;
1521
private string _endpoint;
22+
private double _latitude;
23+
private double _longitude;
24+
private ILocationService _locationService;
1625

17-
public SettingsViewModel()
26+
public SettingsViewModel(ILocationService locationService)
1827
{
1928
UseAzureServices = !Settings.UseMocks;
29+
_useFakeLocation = Settings.UseFakeLocation;
30+
_latitude = Settings.FakeLatitude;
31+
_longitude = Settings.FakeLongitude;
32+
_locationService = locationService;
2033
}
2134

22-
public string Title
35+
public string TitleUseAzureServices
2336
{
24-
get { return _title; }
37+
get { return _titleUseAzureServices; }
2538
set
2639
{
27-
_title = value;
28-
RaisePropertyChanged(() => Title);
40+
_titleUseAzureServices = value;
41+
RaisePropertyChanged(() => TitleUseAzureServices);
2942
}
3043
}
3144

32-
public string Description
45+
public string DescriptionUseAzureServices
3346
{
34-
get { return _description; }
47+
get { return _descriptionUseAzureServices; }
3548
set
3649
{
37-
_description = value;
38-
RaisePropertyChanged(() => Description);
50+
_descriptionUseAzureServices = value;
51+
RaisePropertyChanged(() => DescriptionUseAzureServices);
3952
}
4053
}
4154

@@ -52,6 +65,39 @@ public bool UseAzureServices
5265
}
5366
}
5467

68+
public string TitleUseFakeLocation
69+
{
70+
get { return _titleUseFakeLocation; }
71+
set
72+
{
73+
_titleUseFakeLocation = value;
74+
RaisePropertyChanged(() => TitleUseFakeLocation);
75+
}
76+
}
77+
78+
public string DescriptionUseFakeLocation
79+
{
80+
get { return _descriptionUseFakeLocation; }
81+
set
82+
{
83+
_descriptionUseFakeLocation = value;
84+
RaisePropertyChanged(() => DescriptionUseFakeLocation);
85+
}
86+
}
87+
88+
public bool UseFakeLocation
89+
{
90+
get { return _useFakeLocation; }
91+
set
92+
{
93+
_useFakeLocation = value;
94+
95+
// Save use fake location services to local storage
96+
Settings.UseFakeLocation = _useFakeLocation;
97+
RaisePropertyChanged(() => UseFakeLocation);
98+
}
99+
}
100+
55101
public string Endpoint
56102
{
57103
get { return _endpoint; }
@@ -68,12 +114,47 @@ public string Endpoint
68114
}
69115
}
70116

117+
public double Latitude
118+
{
119+
get { return _latitude; }
120+
set
121+
{
122+
_latitude = value;
123+
124+
UpdateLatitude(_latitude);
125+
126+
RaisePropertyChanged(() => Latitude);
127+
}
128+
}
129+
130+
public double Longitude
131+
{
132+
get { return _longitude; }
133+
set
134+
{
135+
_longitude = value;
136+
137+
UpdateLongitude(_longitude);
138+
139+
RaisePropertyChanged(() => Longitude);
140+
}
141+
}
142+
71143
public ICommand ToggleMockServicesCommand => new Command(async () => await ToggleMockServicesAsync());
72144

145+
public ICommand ToggleFakeLocationCommand => new Command(() => ToggleFakeLocationAsync());
146+
147+
public ICommand ToggleSendLocationCommand => new Command(async () => await ToggleSendLocationAsync());
148+
73149
public override Task InitializeAsync(object navigationData)
74150
{
75151
UpdateInfo();
152+
UpdateInfoFakeLocation();
153+
76154
Endpoint = Settings.UrlBase;
155+
_latitude = Settings.FakeLatitude;
156+
_longitude = Settings.FakeLongitude;
157+
_useFakeLocation = Settings.UseFakeLocation;
77158
return base.InitializeAsync(navigationData);
78159
}
79160

@@ -100,17 +181,48 @@ private async Task ToggleMockServicesAsync()
100181
}
101182
}
102183

103-
private void UpdateInfo()
184+
private void ToggleFakeLocationAsync()
185+
{
186+
ViewModelLocator.RegisterDependencies(!UseAzureServices);
187+
UpdateInfoFakeLocation();
188+
}
189+
190+
private async Task ToggleSendLocationAsync()
191+
{
192+
LocationRequest locationRequest = new LocationRequest
193+
{
194+
Latitude = _latitude,
195+
Longitude = _longitude
196+
};
197+
198+
await _locationService.UpdateUserLocation(locationRequest);
199+
}
200+
201+
private void UpdateInfo()
104202
{
105203
if (!UseAzureServices)
106204
{
107-
Title = "Use Mock Services";
108-
Description = "Mock Services are simulated objects that mimic the behavior of real services using a controlled approach.";
205+
TitleUseAzureServices = "Use Mock Services";
206+
DescriptionUseAzureServices = "Mock Services are simulated objects that mimic the behavior of real services using a controlled approach.";
109207
}
110208
else
111209
{
112-
Title = "Use Microservices/Containers from eShopOnContainers";
113-
Description = "When enabling the use of microservices/containers, the app will attempt to use real services deployed as Docker containers at the specified base endpoint, which will must be reachable through the network.";
210+
TitleUseAzureServices = "Use Microservices/Containers from eShopOnContainers";
211+
DescriptionUseAzureServices = "When enabling the use of microservices/containers, the app will attempt to use real services deployed as Docker containers at the specified base endpoint, which will must be reachable through the network.";
212+
}
213+
}
214+
215+
private void UpdateInfoFakeLocation()
216+
{
217+
if (!UseFakeLocation)
218+
{
219+
TitleUseFakeLocation = "Use Fake Location";
220+
DescriptionUseFakeLocation = "Fake Location are added for marketing campaign testing.";
221+
}
222+
else
223+
{
224+
TitleUseFakeLocation = "Use Real Location";
225+
DescriptionUseFakeLocation = "When enabling the use of real location, the app will attempt to use real location from the device.";
114226
}
115227
}
116228

@@ -119,5 +231,17 @@ private void UpdateEndpoint(string endpoint)
119231
// Update remote endpoint (save to local storage)
120232
Settings.UrlBase = endpoint;
121233
}
234+
235+
private void UpdateLatitude(double latitude)
236+
{
237+
// Update fake latitude (save to local storage)
238+
Settings.FakeLatitude = latitude;
239+
}
240+
241+
private void UpdateLongitude(double longitude)
242+
{
243+
// Update fake longitude (save to local storage)
244+
Settings.FakeLongitude = longitude;
245+
}
122246
}
123247
}

0 commit comments

Comments
 (0)