From a86a11694a49f6fa98d139fb68cba304ea1c89de Mon Sep 17 00:00:00 2001 From: David Britch Date: Tue, 16 Jan 2018 16:00:25 +0000 Subject: [PATCH] SettingsService works on iOS. --- .../eShopOnContainers.Core/App.xaml.cs | 5 +- .../ViewModels/Base/ViewModelLocator.cs | 3 ++ .../ViewModels/SettingsViewModel.cs | 2 +- .../Services/SettingsServiceImplementation.cs | 54 ++++++++++--------- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/App.xaml.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/App.xaml.cs index 076ad16e5..a1178fbef 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/App.xaml.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/App.xaml.cs @@ -15,7 +15,6 @@ namespace eShopOnContainers public partial class App : Application { ISettingsService _settingsService; - bool _useMockServices; public App() { @@ -30,9 +29,9 @@ namespace eShopOnContainers private void InitApp() { - _useMockServices = true;//_settingsService.UseMocks; - ViewModelLocator.RegisterDependencies(_useMockServices); _settingsService = ViewModelLocator.Resolve(); + if (!_settingsService.UseMocks) + ViewModelLocator.UpdateDependencies(_settingsService.UseMocks); } private Task InitNavigation() diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/Base/ViewModelLocator.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/Base/ViewModelLocator.cs index 59b1f62a8..b7721017b 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/Base/ViewModelLocator.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/Base/ViewModelLocator.cs @@ -60,6 +60,9 @@ namespace eShopOnContainers.Core.ViewModels.Base _container.Register(); _container.Register(); _container.Register(); + _container.Register(); + _container.Register().AsSingleton(); + _container.Register().AsSingleton(); _container.Register().AsSingleton(); _container.Register().AsSingleton(); _container.Register().AsSingleton(); diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs index e2258ecc6..060d2ce69 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.Core/ViewModels/SettingsViewModel.cs @@ -35,7 +35,7 @@ namespace eShopOnContainers.Core.ViewModels _settingsService = settingsService; _locationService = locationService; - _useAzureServices = _settingsService.UseMocks; + _useAzureServices = !_settingsService.UseMocks; _endpoint = _settingsService.UrlBase; _latitude = double.Parse(_settingsService.Latitude, CultureInfo.CurrentCulture); _longitude = double.Parse(_settingsService.Longitude, CultureInfo.CurrentCulture); diff --git a/src/Mobile/eShopOnContainers/eShopOnContainers.iOS/Services/SettingsServiceImplementation.cs b/src/Mobile/eShopOnContainers/eShopOnContainers.iOS/Services/SettingsServiceImplementation.cs index 259074c24..429f367b7 100644 --- a/src/Mobile/eShopOnContainers/eShopOnContainers.iOS/Services/SettingsServiceImplementation.cs +++ b/src/Mobile/eShopOnContainers/eShopOnContainers.iOS/Services/SettingsServiceImplementation.cs @@ -2,7 +2,6 @@ using Foundation; using eShopOnContainers.Core.Services.Settings; using eShopOnContainers.iOS.Services; -using System.Globalization; [assembly: Xamarin.Forms.Dependency(typeof(SettingsServiceImplementation))] namespace eShopOnContainers.iOS.Services @@ -34,7 +33,7 @@ namespace eShopOnContainers.iOS.Services switch (typeCode) { case TypeCode.Boolean: - defaults.SetString(Convert.ToString(value, CultureInfo.InvariantCulture), key); + defaults.SetBool(Convert.ToBoolean(value), key); break; case TypeCode.String: defaults.SetString(Convert.ToString(value), key); @@ -57,34 +56,37 @@ namespace eShopOnContainers.iOS.Services T GetValueOrDefaultInternal(string key, T defaultValue = default(T)) { - var defaults = GetUserDefaults(); - - if (defaults[key] == null) + lock (locker) { - return defaultValue; - } + var defaults = GetUserDefaults(); - var type = typeof(T); - if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) - { - type = Nullable.GetUnderlyingType(type); - } + if (defaults[key] == null) + { + return defaultValue; + } - object value = null; - var typeCode = Type.GetTypeCode(type); - switch (typeCode) - { - case TypeCode.Boolean: - value = defaults.BoolForKey(key); - break; - case TypeCode.String: - value = defaults.StringForKey(key); - break; - default: - throw new ArgumentException($"Value of type {typeCode} is unsupported."); - } + var type = typeof(T); + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + type = Nullable.GetUnderlyingType(type); + } - return null != value ? (T)value : defaultValue; + object value = null; + var typeCode = Type.GetTypeCode(type); + switch (typeCode) + { + case TypeCode.Boolean: + value = defaults.BoolForKey(key); + break; + case TypeCode.String: + value = defaults.StringForKey(key); + break; + default: + throw new ArgumentException($"Value of type {typeCode} is unsupported."); + } + + return null != value ? (T)value : defaultValue; + } } #region ISettingsService Implementation