Android settings implementation.
This commit is contained in:
		
							parent
							
								
									a86a11694a
								
							
						
					
					
						commit
						648b1b3585
					
				@ -9,7 +9,5 @@
 | 
				
			|||||||
        bool AddOrUpdateValue(string key, string value);
 | 
					        bool AddOrUpdateValue(string key, string value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        void Remove(string key);
 | 
					        void Remove(string key);
 | 
				
			||||||
        void Clear();
 | 
					 | 
				
			||||||
        bool Contains(string key);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,145 @@
 | 
				
			|||||||
 | 
					using System;
 | 
				
			||||||
 | 
					using eShopOnContainers.Core.Services.Settings;
 | 
				
			||||||
 | 
					using Android.App;
 | 
				
			||||||
 | 
					using Android.Content;
 | 
				
			||||||
 | 
					using Android.Preferences;
 | 
				
			||||||
 | 
					using eShopOnContainers.Droid.Services;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[assembly: Xamarin.Forms.Dependency(typeof(SettingsServiceImplementation))]
 | 
				
			||||||
 | 
					namespace eShopOnContainers.Droid.Services
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public class SettingsServiceImplementation : ISettingsServiceImplementation
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        readonly object _locker = new object();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ISharedPreferences GetSharedPreference()
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            return PreferenceManager.GetDefaultSharedPreferences(Application.Context);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        bool AddOrUpdateValueInternal<T>(string key, T value)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            if (Application.Context == null)
 | 
				
			||||||
 | 
					                return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (value == null)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                Remove(key);
 | 
				
			||||||
 | 
					                return true;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            var type = typeof(T);
 | 
				
			||||||
 | 
					            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                type = Nullable.GetUnderlyingType(type);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            var typeCode = Type.GetTypeCode(type);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            lock (_locker)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                using (var sharedPrefs = GetSharedPreference())
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    using (var editor = sharedPrefs.Edit())
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        switch (typeCode)
 | 
				
			||||||
 | 
					                        {
 | 
				
			||||||
 | 
					                            case TypeCode.Boolean:
 | 
				
			||||||
 | 
					                                editor.PutBoolean(key, Convert.ToBoolean(value));
 | 
				
			||||||
 | 
					                                break;
 | 
				
			||||||
 | 
					                            case TypeCode.String:
 | 
				
			||||||
 | 
					                                editor.PutString(key, Convert.ToString(value));
 | 
				
			||||||
 | 
					                                break;
 | 
				
			||||||
 | 
					                            default:
 | 
				
			||||||
 | 
					                                throw new ArgumentException($"Value of type {typeCode} is not supported.");
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                        editor.Commit();
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            return true;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        T GetValueOrDefaultInternal<T>(string key, T defaultValue = default(T))
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            if (Application.Context == null)
 | 
				
			||||||
 | 
					                return defaultValue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (!Contains(key))
 | 
				
			||||||
 | 
					                return defaultValue;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            lock (_locker)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                using (var sharedPrefs = GetSharedPreference())
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    var type = typeof(T);
 | 
				
			||||||
 | 
					                    if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        type = Nullable.GetUnderlyingType(type);
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    object value = null;
 | 
				
			||||||
 | 
					                    var typeCode = Type.GetTypeCode(type);
 | 
				
			||||||
 | 
					                    switch (typeCode)
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        case TypeCode.Boolean:
 | 
				
			||||||
 | 
					                            value = sharedPrefs.GetBoolean(key, Convert.ToBoolean(defaultValue));
 | 
				
			||||||
 | 
					                            break;
 | 
				
			||||||
 | 
					                        case TypeCode.String:
 | 
				
			||||||
 | 
					                            value = sharedPrefs.GetString(key, Convert.ToString(defaultValue));
 | 
				
			||||||
 | 
					                            break;
 | 
				
			||||||
 | 
					                        default:
 | 
				
			||||||
 | 
					                            throw new ArgumentException($"Value of type {typeCode} is not supported.");
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    return null != value ? (T)value : defaultValue;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        bool Contains(string key)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            if (Application.Context == null)
 | 
				
			||||||
 | 
					                return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            lock (_locker)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                using (var sharedPrefs = GetSharedPreference())
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    if (sharedPrefs == null)
 | 
				
			||||||
 | 
					                        return false;
 | 
				
			||||||
 | 
					                    return sharedPrefs.Contains(key);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        #region ISettingsServiceImplementation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public bool AddOrUpdateValue(string key, bool value) => AddOrUpdateValueInternal(key, value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public bool AddOrUpdateValue(string key, string value) => AddOrUpdateValueInternal(key, value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public bool GetValueOrDefault(string key, bool defaultValue) => GetValueOrDefaultInternal(key, defaultValue);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public string GetValueOrDefault(string key, string defaultValue) => GetValueOrDefaultInternal(key, defaultValue);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        public void Remove(string key)
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            if (Application.Context == null)
 | 
				
			||||||
 | 
					                return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            lock (_locker)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                using (var sharedPrefs = GetSharedPreference())
 | 
				
			||||||
 | 
					                {
 | 
				
			||||||
 | 
					                    using (var editor = sharedPrefs.Edit())
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        editor.Remove(key);
 | 
				
			||||||
 | 
					                        editor.Commit();
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        #endregion
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -226,6 +226,7 @@
 | 
				
			|||||||
    <Compile Include="Effects\CircleEffect.cs" />
 | 
					    <Compile Include="Effects\CircleEffect.cs" />
 | 
				
			||||||
    <Compile Include="Effects\BaseContainerEffect.cs" />
 | 
					    <Compile Include="Effects\BaseContainerEffect.cs" />
 | 
				
			||||||
    <Compile Include="Activities\SplashActivity.cs" />
 | 
					    <Compile Include="Activities\SplashActivity.cs" />
 | 
				
			||||||
 | 
					    <Compile Include="Services\SettingsServiceImplementation.cs" />
 | 
				
			||||||
  </ItemGroup>
 | 
					  </ItemGroup>
 | 
				
			||||||
  <ItemGroup>
 | 
					  <ItemGroup>
 | 
				
			||||||
    <AndroidAsset Include="..\CommonResources\Fonts\Montserrat-Bold.ttf">
 | 
					    <AndroidAsset Include="..\CommonResources\Fonts\Montserrat-Bold.ttf">
 | 
				
			||||||
@ -379,6 +380,9 @@
 | 
				
			|||||||
      <Name>eShopOnContainers.Core</Name>
 | 
					      <Name>eShopOnContainers.Core</Name>
 | 
				
			||||||
    </ProjectReference>
 | 
					    </ProjectReference>
 | 
				
			||||||
  </ItemGroup>
 | 
					  </ItemGroup>
 | 
				
			||||||
 | 
					  <ItemGroup>
 | 
				
			||||||
 | 
					    <Folder Include="Services\" />
 | 
				
			||||||
 | 
					  </ItemGroup>
 | 
				
			||||||
  <Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
 | 
					  <Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
 | 
				
			||||||
  <Import Project="..\..\..\..\packages\Xamarin.Android.Support.Compat.25.4.0.2\build\MonoAndroid70\Xamarin.Android.Support.Compat.targets" Condition="Exists('..\..\..\..\packages\Xamarin.Android.Support.Compat.25.4.0.2\build\MonoAndroid70\Xamarin.Android.Support.Compat.targets')" />
 | 
					  <Import Project="..\..\..\..\packages\Xamarin.Android.Support.Compat.25.4.0.2\build\MonoAndroid70\Xamarin.Android.Support.Compat.targets" Condition="Exists('..\..\..\..\packages\Xamarin.Android.Support.Compat.25.4.0.2\build\MonoAndroid70\Xamarin.Android.Support.Compat.targets')" />
 | 
				
			||||||
  <Import Project="..\..\..\..\packages\Xamarin.Android.Support.Core.UI.25.4.0.2\build\MonoAndroid70\Xamarin.Android.Support.Core.UI.targets" Condition="Exists('..\..\..\..\packages\Xamarin.Android.Support.Core.UI.25.4.0.2\build\MonoAndroid70\Xamarin.Android.Support.Core.UI.targets')" />
 | 
					  <Import Project="..\..\..\..\packages\Xamarin.Android.Support.Core.UI.25.4.0.2\build\MonoAndroid70\Xamarin.Android.Support.Core.UI.targets" Condition="Exists('..\..\..\..\packages\Xamarin.Android.Support.Core.UI.25.4.0.2\build\MonoAndroid70\Xamarin.Android.Support.Core.UI.targets')" />
 | 
				
			||||||
 | 
				
			|||||||
@ -8,7 +8,7 @@ namespace eShopOnContainers.iOS.Services
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    public class SettingsServiceImplementation : ISettingsServiceImplementation
 | 
					    public class SettingsServiceImplementation : ISettingsServiceImplementation
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        readonly object locker = new object();
 | 
					        readonly object _locker = new object();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        NSUserDefaults GetUserDefaults() => NSUserDefaults.StandardUserDefaults;
 | 
					        NSUserDefaults GetUserDefaults() => NSUserDefaults.StandardUserDefaults;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -27,7 +27,7 @@ namespace eShopOnContainers.iOS.Services
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
            var typeCode = Type.GetTypeCode(type);
 | 
					            var typeCode = Type.GetTypeCode(type);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            lock (locker)
 | 
					            lock (_locker)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                var defaults = GetUserDefaults();
 | 
					                var defaults = GetUserDefaults();
 | 
				
			||||||
                switch (typeCode)
 | 
					                switch (typeCode)
 | 
				
			||||||
@ -56,7 +56,7 @@ namespace eShopOnContainers.iOS.Services
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        T GetValueOrDefaultInternal<T>(string key, T defaultValue = default(T))
 | 
					        T GetValueOrDefaultInternal<T>(string key, T defaultValue = default(T))
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            lock (locker)
 | 
					            lock (_locker)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                var defaults = GetUserDefaults();
 | 
					                var defaults = GetUserDefaults();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -89,7 +89,7 @@ namespace eShopOnContainers.iOS.Services
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        #region ISettingsService Implementation
 | 
					        #region ISettingsServiceImplementation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public bool AddOrUpdateValue(string key, bool value) => AddOrUpdateValueInternal(key, value);
 | 
					        public bool AddOrUpdateValue(string key, bool value) => AddOrUpdateValueInternal(key, value);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -101,7 +101,7 @@ namespace eShopOnContainers.iOS.Services
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        public void Remove(string key)
 | 
					        public void Remove(string key)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            lock (locker)
 | 
					            lock (_locker)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                var defaults = GetUserDefaults();
 | 
					                var defaults = GetUserDefaults();
 | 
				
			||||||
                try
 | 
					                try
 | 
				
			||||||
@ -119,48 +119,6 @@ namespace eShopOnContainers.iOS.Services
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        public void Clear()
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            lock (locker)
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                var defaults = GetUserDefaults();
 | 
					 | 
				
			||||||
                try
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    var items = defaults.ToDictionary();
 | 
					 | 
				
			||||||
                    foreach (var item in items.Keys)
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        if (item is NSString nsString)
 | 
					 | 
				
			||||||
                        {
 | 
					 | 
				
			||||||
                            defaults.RemoveObject(nsString);
 | 
					 | 
				
			||||||
                        }
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                    defaults.Synchronize();
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                catch (Exception ex)
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    Console.WriteLine("Unable to clear all defaults. Message: " + ex.Message);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        public bool Contains(string key)
 | 
					 | 
				
			||||||
        {
 | 
					 | 
				
			||||||
            lock (locker)
 | 
					 | 
				
			||||||
            {
 | 
					 | 
				
			||||||
                var defaults = GetUserDefaults();
 | 
					 | 
				
			||||||
                try
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    var setting = defaults[key];
 | 
					 | 
				
			||||||
                    return setting != null;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                catch (Exception ex)
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    Console.WriteLine("Unable to clear all defaults. Message: " + ex.Message);
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
                return false;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        #endregion
 | 
					        #endregion
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user