Added Preferences implementation for Android

Also added placeholders for other platform implementations that need to be completed.
This commit is contained in:
Jonathan Dick 2018-02-20 21:36:44 -05:00
Родитель 9c0b83f29d
Коммит 411fb05d02
6 изменённых файлов: 186 добавлений и 19 удалений

Просмотреть файл

@ -22,7 +22,7 @@
<Copyright>Copyright 2018</Copyright>
<RepositoryUrl>https://github.com/xamarin/f50</RepositoryUrl>
<PackageReleaseNotes>See: https://github.com/xamarin/f50 </PackageReleaseNotes>
<LangVersion>default</LangVersion>
<LangVersion>7.1</LangVersion>
<DefineConstants>$(DefineConstants);</DefineConstants>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile Condition=" '$(Configuration)' == 'Release' ">true</GenerateDocumentationFile>

Просмотреть файл

@ -1,14 +1,130 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using Android.App;
using Android.Content;
using Android.Preferences;
namespace Xamarin.F50
{
public partial class Preferences
{
public void Set(string key, string val)
{
public bool ContainsKey(string key)
{
using (var sharedPreferences = GetSharedPreferences())
{
if (sharedPreferences == null)
return false;
return sharedPreferences.Contains(key);
}
}
public bool Remove(string key)
{
using (var sharedPreferences = GetSharedPreferences())
{
if (sharedPreferences == null)
return false;
return sharedPreferences.Edit().Remove(key).Commit();
}
}
public bool Clear()
{
using (var sharedPreferences = GetSharedPreferences())
{
if (sharedPreferences == null)
return false;
using (var editor = sharedPreferences.Edit())
{
return editor.Clear().Commit();
}
}
}
bool Set<T>(string key, T value)
{
using (var sharedPreferences = GetSharedPreferences())
{
if (sharedPreferences == null)
return false;
using (var editor = sharedPreferences.Edit())
{
switch (value)
{
case string s:
editor.PutString(key, s);
break;
case int i:
editor.PutInt(key, i);
break;
case bool b:
editor.PutBoolean(key, b);
break;
case long l:
editor.PutLong(key, l);
break;
case double d:
var strDbl = Convert.ToString(d, CultureInfo.InvariantCulture);
editor.PutString(key, strDbl);
break;
case float f:
editor.PutFloat(key, f);
break;
}
return editor.Commit();
}
}
}
T Get<T> (string key, T defaultValue)
{
object value = null;
using (var sharedPreferences = GetSharedPreferences())
{
if (sharedPreferences == null)
return default;
switch (defaultValue)
{
case string s:
value = sharedPreferences.GetString(key, s);
break;
case int i:
value = sharedPreferences.GetInt(key, i);
break;
case bool b:
value = sharedPreferences.GetBoolean(key, b);
break;
case long l:
value = sharedPreferences.GetLong(key, l);
break;
case double d:
double dbl;
if (double.TryParse(sharedPreferences.GetString(key, null), out dbl))
value = dbl;
break;
case float f:
value = sharedPreferences.GetFloat(key, f);
break;
}
}
return (T)value;
}
ISharedPreferences GetSharedPreferences ()
{
var context = Platform.CurrentContext;
if (context == null)
return null;
return string.IsNullOrWhiteSpace(SharedName) ?
PreferenceManager.GetDefaultSharedPreferences(Application.Context) :
Application.Context.GetSharedPreferences(SharedName, FileCreationMode.Private);
}
}
}

Просмотреть файл

@ -6,9 +6,19 @@ namespace Xamarin.F50
{
public partial class Preferences
{
public void Set(string key, string val)
{
public bool ContainsKey(string key) =>
throw new NotImplentedInReferenceAssembly();
}
}
public bool Remove(string key) =>
throw new NotImplentedInReferenceAssembly();
public bool Clear() =>
throw new NotImplentedInReferenceAssembly();
bool Set<T>(string key, T value) =>
throw new NotImplentedInReferenceAssembly();
T Get<T>(string key, T defaultValue) =>
throw new NotImplentedInReferenceAssembly();
}
}

Просмотреть файл

@ -6,7 +6,19 @@ namespace Xamarin.F50
{
public partial class Preferences
{
public void Set(string key, string val) =>
throw new NotImplentedInReferenceAssembly();
public bool ContainsKey(string key) =>
throw new NotImplentedInReferenceAssembly();
public bool Remove(string key) =>
throw new NotImplentedInReferenceAssembly();
public bool Clear() =>
throw new NotImplentedInReferenceAssembly();
bool Set<T>(string key, T value) =>
throw new NotImplentedInReferenceAssembly();
T Get<T>(string key, T defaultValue) =>
throw new NotImplentedInReferenceAssembly();
}
}

Просмотреть файл

@ -10,10 +10,29 @@ namespace Xamarin.F50
/// </summary>
public partial class Preferences
{
public static void DoStuff()
{
public Preferences()
{
}
}
}
public Preferences(string sharedName)
{
SharedName = sharedName;
}
public string SharedName { get; private set; }
public string Get(string key, string defaultValue)
=> Get<string>(key, defaultValue);
public bool Get(string key, bool defaultValue)
=> Get<bool>(key, defaultValue);
public int Get(string key, int defaultValue)
=> Get<int>(key, defaultValue);
public double Get(string key, double defaultValue)
=> Get<double>(key, defaultValue);
public float Get(string key, float defaultValue)
=> Get<float>(key, defaultValue);
public long Get(string key, long defaultValue)
=> Get<long>(key, defaultValue);
}
}

Просмотреть файл

@ -6,9 +6,19 @@ namespace Xamarin.F50
{
public partial class Preferences
{
public void Set(string key, string val)
{
public bool ContainsKey(string key) =>
throw new NotImplentedInReferenceAssembly();
}
}
public bool Remove(string key) =>
throw new NotImplentedInReferenceAssembly();
public bool Clear() =>
throw new NotImplentedInReferenceAssembly();
bool Set<T>(string key, T value) =>
throw new NotImplentedInReferenceAssembly();
T Get<T>(string key, T defaultValue) =>
throw new NotImplentedInReferenceAssembly();
}
}