From 6a02eb31c91edd31ded93821f9dcbfdf33772596 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Wed, 21 Jun 2017 17:30:19 +0200 Subject: [PATCH] Add gps usage options --- .../eShopOnContainers.Core/App.xaml.cs | 16 +- .../Helpers/Settings.cs | 8 + .../ViewModels/SettingsViewModel.cs | 166 +++++++++++++----- .../Views/SettingsView.xaml | 50 +++++- 4 files changed, 187 insertions(+), 53 deletions(-) diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/App.xaml.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/App.xaml.cs index b1b24d758..22dcafce0 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/App.xaml.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/App.xaml.cs @@ -50,7 +50,7 @@ namespace eShopOnContainers await InitNavigation(); } - if (!Settings.UseFakeLocation) + if (Settings.GpsUsage && !Settings.UseFakeLocation) { await GetRealLocation(); } @@ -71,13 +71,17 @@ namespace eShopOnContainers private async Task GetRealLocation() { var locator = CrossGeolocator.Current; - locator.AllowsBackgroundUpdates = true; - locator.DesiredAccuracy = 50; - var position = await locator.GetPositionAsync(20000); + if (locator.IsGeolocationEnabled && locator.IsGeolocationAvailable) + { + locator.AllowsBackgroundUpdates = true; + locator.DesiredAccuracy = 50; - Settings.Latitude = position.Latitude; - Settings.Longitude = position.Longitude; + var position = await locator.GetPositionAsync(20000); + + Settings.Latitude = position.Latitude; + Settings.Longitude = position.Longitude; + } } private async Task SendCurrentLocation() diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Helpers/Settings.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Helpers/Settings.cs index 243659d09..70e1ee040 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Helpers/Settings.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Helpers/Settings.cs @@ -28,10 +28,12 @@ namespace eShopOnContainers.Core.Helpers private const string IdUseFakeLocation = "use_fake_location"; private const string IdLatitude = "latitude"; private const string IdLongitude = "flongitude"; + private const string IdGpsUsage = "gps_usage"; private static readonly string AccessTokenDefault = string.Empty; private static readonly string IdTokenDefault = string.Empty; private static readonly bool UseMocksDefault = true; private static readonly bool UseFakeLocationDefault = false; + private static readonly bool GpsUsageValue = false; private static readonly double FakeLatitudeValue = 47.604610d; private static readonly double FakeLongitudeValue = -122.315752d; private static readonly string UrlBaseDefault = GlobalSetting.Instance.BaseEndpoint; @@ -84,5 +86,11 @@ namespace eShopOnContainers.Core.Helpers get => AppSettings.GetValueOrDefault(IdLongitude, FakeLongitudeValue); set => AppSettings.AddOrUpdateValue(IdLongitude, value); } + + public static bool GpsUsage + { + get => AppSettings.GetValueOrDefault(IdGpsUsage, GpsUsageValue); + set => AppSettings.AddOrUpdateValue(IdGpsUsage, value); + } } } \ No newline at end of file diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs index 9ffb4f830..d3db564f3 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs @@ -1,15 +1,14 @@ -using eShopOnContainers.Core.ViewModels.Base; -using System.Windows.Input; -using Xamarin.Forms; -using System.Threading.Tasks; -using eShopOnContainers.Core.Helpers; -using eShopOnContainers.Core.Models.User; -using System; -using eShopOnContainers.Core.Models.Location; -using eShopOnContainers.Core.Services.Location; - -namespace eShopOnContainers.Core.ViewModels +namespace eShopOnContainers.Core.ViewModels { + using System.Windows.Input; + using Xamarin.Forms; + using System.Threading.Tasks; + using Helpers; + using Models.User; + using Base; + using Models.Location; + using Services.Location; + public class SettingsViewModel : ViewModelBase { private string _titleUseAzureServices; @@ -17,24 +16,31 @@ namespace eShopOnContainers.Core.ViewModels private bool _useAzureServices; private string _titleUseFakeLocation; private string _descriptionUseFakeLocation; + private bool _gpsUsage; + private string _titleGpsUsage; + private string _descriptionGpsUsage; private bool _useFakeLocation; private string _endpoint; private double _latitude; private double _longitude; - private ILocationService _locationService; + + private readonly ILocationService _locationService; public SettingsViewModel(ILocationService locationService) { - UseAzureServices = !Settings.UseMocks; - _useFakeLocation = Settings.UseFakeLocation; + _locationService = locationService; + + _useAzureServices = !Settings.UseMocks; + _endpoint = Settings.UrlBase; _latitude = Settings.Latitude; _longitude = Settings.Longitude; - _locationService = locationService; + _useFakeLocation = Settings.UseFakeLocation; + _gpsUsage = Settings.GpsUsage; } public string TitleUseAzureServices { - get { return _titleUseAzureServices; } + get => _titleUseAzureServices; set { _titleUseAzureServices = value; @@ -44,7 +50,7 @@ namespace eShopOnContainers.Core.ViewModels public string DescriptionUseAzureServices { - get { return _descriptionUseAzureServices; } + get => _descriptionUseAzureServices; set { _descriptionUseAzureServices = value; @@ -54,20 +60,20 @@ namespace eShopOnContainers.Core.ViewModels public bool UseAzureServices { - get { return _useAzureServices; } + get => _useAzureServices; set { _useAzureServices = value; - // Save use mocks services to local storage - Settings.UseMocks = !_useAzureServices; + UpdateUseAzureServices(); + RaisePropertyChanged(() => UseAzureServices); } } public string TitleUseFakeLocation { - get { return _titleUseFakeLocation; } + get => _titleUseFakeLocation; set { _titleUseFakeLocation = value; @@ -77,7 +83,7 @@ namespace eShopOnContainers.Core.ViewModels public string DescriptionUseFakeLocation { - get { return _descriptionUseFakeLocation; } + get => _descriptionUseFakeLocation; set { _descriptionUseFakeLocation = value; @@ -87,27 +93,47 @@ namespace eShopOnContainers.Core.ViewModels public bool UseFakeLocation { - get { return _useFakeLocation; } + get => _useFakeLocation; set { _useFakeLocation = value; - // Save use fake location services to local storage - Settings.UseFakeLocation = _useFakeLocation; + UpdateFakeLocation(); + RaisePropertyChanged(() => UseFakeLocation); } } + public string TitleGpsUsage + { + get => _titleGpsUsage; + set + { + _titleGpsUsage = value; + RaisePropertyChanged(() => TitleGpsUsage); + } + } + + public string DescriptionGpsUsage + { + get => _descriptionGpsUsage; + set + { + _descriptionGpsUsage = value; + RaisePropertyChanged(() => DescriptionGpsUsage); + } + } + public string Endpoint { - get { return _endpoint; } + get => _endpoint; set { _endpoint = value; if(!string.IsNullOrEmpty(_endpoint)) { - UpdateEndpoint(_endpoint); + UpdateEndpoint(); } RaisePropertyChanged(() => Endpoint); @@ -116,12 +142,12 @@ namespace eShopOnContainers.Core.ViewModels public double Latitude { - get { return _latitude; } + get => _latitude; set { _latitude = value; - UpdateLatitude(_latitude); + UpdateLatitude(); RaisePropertyChanged(() => Latitude); } @@ -129,41 +155,53 @@ namespace eShopOnContainers.Core.ViewModels public double Longitude { - get { return _longitude; } + get => _longitude; set { _longitude = value; - UpdateLongitude(_longitude); + UpdateLongitude(); RaisePropertyChanged(() => Longitude); } } + public bool GpsUsage + { + get => _gpsUsage; + set + { + _gpsUsage = value; + + UpdateGpsUsage(); + + RaisePropertyChanged(() => GpsUsage); + } + } + public bool UserIsLogged => !string.IsNullOrEmpty(Settings.AuthAccessToken); public ICommand ToggleMockServicesCommand => new Command(async () => await ToggleMockServicesAsync()); - public ICommand ToggleFakeLocationCommand => new Command(() => ToggleFakeLocationAsync()); + public ICommand ToggleFakeLocationCommand => new Command(ToggleFakeLocationAsync); public ICommand ToggleSendLocationCommand => new Command(async () => await ToggleSendLocationAsync()); + public ICommand ToggleGpsUsageCommand => new Command(ToggleGpsUsage); + public override Task InitializeAsync(object navigationData) { - UpdateInfo(); + UpdateInfoUseAzureServices(); UpdateInfoFakeLocation(); - - Endpoint = Settings.UrlBase; - _latitude = Settings.Latitude; - _longitude = Settings.Longitude; - _useFakeLocation = Settings.UseFakeLocation; + UpdateInfoGpsUsage(); + return base.InitializeAsync(navigationData); } private async Task ToggleMockServicesAsync() { ViewModelLocator.RegisterDependencies(!UseAzureServices); - UpdateInfo(); + UpdateInfoUseAzureServices(); var previousPageViewModel = NavigationService.PreviousPageViewModel; if (previousPageViewModel != null) @@ -204,7 +242,12 @@ namespace eShopOnContainers.Core.ViewModels } } - private void UpdateInfo() + private void ToggleGpsUsage() + { + UpdateInfoGpsUsage(); + } + + private void UpdateInfoUseAzureServices() { if (!UseAzureServices) { @@ -232,22 +275,53 @@ namespace eShopOnContainers.Core.ViewModels } } - private void UpdateEndpoint(string endpoint) + private void UpdateInfoGpsUsage() + { + if (!GpsUsage) + { + TitleGpsUsage = "Enable GPS"; + DescriptionGpsUsage = "When enabling the use of device gps you will get the location campaigns through your real location."; + } + else + { + TitleGpsUsage = "Disable GPS"; + DescriptionGpsUsage = "When disabling the use of device gps you won't get the location campaigns through your real location."; + } + } + + + private void UpdateUseAzureServices() + { + // Save use mocks services to local storage + Settings.UseMocks = !_useAzureServices; + } + + private void UpdateEndpoint() { // Update remote endpoint (save to local storage) - Settings.UrlBase = endpoint; + GlobalSetting.Instance.BaseEndpoint = Settings.UrlBase = _endpoint; } - private void UpdateLatitude(double latitude) + private void UpdateFakeLocation() + { + Settings.UseFakeLocation = _useFakeLocation; + } + + private void UpdateLatitude() { // Update fake latitude (save to local storage) - Settings.Latitude = latitude; + Settings.Latitude = _latitude; } - private void UpdateLongitude(double longitude) + private void UpdateLongitude() { // Update fake longitude (save to local storage) - Settings.Longitude = longitude; + Settings.Longitude = _longitude; + } + + private void UpdateGpsUsage() + { + Settings.GpsUsage = _gpsUsage; } } } \ No newline at end of file diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Views/SettingsView.xaml b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Views/SettingsView.xaml index e5c5ccdd3..76cb81b72 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Views/SettingsView.xaml +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/Views/SettingsView.xaml @@ -100,6 +100,9 @@ + + + - + + + + + + + + + + + + + + +