From 382aea9431d5cd9811ab8bf3fa2a7eb8ac1565c4 Mon Sep 17 00:00:00 2001 From: Tommy Parnell Date: Sun, 23 Feb 2014 12:51:09 -0500 Subject: [PATCH] init commit --- Extensions/ISharedPreferencesEditor.cs | 39 +++++++++++++ Properties/AssemblyInfo.cs | 34 ++++++++++++ Resources/AboutResources.txt | 50 +++++++++++++++++ Resources/Resource.Designer.cs | 73 +++++++++++++++++++++++++ Resources/Values/Strings.xml | 5 ++ Setting/CastingSettingException.cs | 14 +++++ Setting/SettingsKey.cs | 76 ++++++++++++++++++++++++++ SharedSettingsAbstraction.csproj | 64 ++++++++++++++++++++++ 8 files changed, 355 insertions(+) create mode 100644 Extensions/ISharedPreferencesEditor.cs create mode 100644 Properties/AssemblyInfo.cs create mode 100644 Resources/AboutResources.txt create mode 100644 Resources/Resource.Designer.cs create mode 100644 Resources/Values/Strings.xml create mode 100644 Setting/CastingSettingException.cs create mode 100644 Setting/SettingsKey.cs create mode 100644 SharedSettingsAbstraction.csproj diff --git a/Extensions/ISharedPreferencesEditor.cs b/Extensions/ISharedPreferencesEditor.cs new file mode 100644 index 0000000..f0d1f5c --- /dev/null +++ b/Extensions/ISharedPreferencesEditor.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using Android.App; +using Android.Content; +using Android.OS; +using Android.Runtime; +using Android.Views; +using Android.Widget; + +namespace SharedSettingsAbstraction.Extensions +{ + public static class AndroidExtensions + { + public static void SaveObject ( this ISharedPreferencesEditor ed, string key, object value) + { + var refType = value.GetType(); + + if (refType == typeof(string)) + { + ed.PutString(key,(String)value); + } + if (refType == typeof(bool)) + { + ed.PutBoolean(key, (bool)value); + } + if (refType == typeof(int)) + { + ed.PutInt(key, (int)value); + } + if (refType == typeof(float)) + { + ed.PutFloat(key, (float)value); + } + } + } +} \ No newline at end of file diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..f6d6ff9 --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,34 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using Android.App; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("SharedSettingsAbstraction")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("SharedSettingsAbstraction")] +[assembly: AssemblyCopyright("Copyright © 2014")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] +[assembly: ComVisible(false)] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] + +// Add some common permissions, these can be removed if not needed +[assembly: UsesPermission(Android.Manifest.Permission.Internet)] +[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)] diff --git a/Resources/AboutResources.txt b/Resources/AboutResources.txt new file mode 100644 index 0000000..f3e286e --- /dev/null +++ b/Resources/AboutResources.txt @@ -0,0 +1,50 @@ +Images, layout descriptions, binary blobs and string dictionaries can be included +in your application as resource files. Various Android APIs are designed to +operate on the resource IDs instead of dealing with images, strings or binary blobs +directly. + +For example, a sample Android app that contains a user interface layout (main.xml), +an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png) +would keep its resources in the "Resources" directory of the application: + +Resources/ + drawable-hdpi/ + icon.png + + drawable-ldpi/ + icon.png + + drawable-mdpi/ + icon.png + + layout/ + main.xml + + values/ + strings.xml + +In order to get the build system to recognize Android resources, set the build action to +"AndroidResource". The native Android APIs do not operate directly with filenames, but +instead operate on resource IDs. When you compile an Android application that uses resources, +the build system will package the resources for distribution and generate a class called +"Resource" that contains the tokens for each one of the resources included. For example, +for the above Resources layout, this is what the Resource class would expose: + +public class Resource { + public class drawable { + public const int icon = 0x123; + } + + public class layout { + public const int main = 0x456; + } + + public class strings { + public const int first_string = 0xabc; + public const int second_string = 0xbcd; + } +} + +You would then use R.drawable.icon to reference the drawable/icon.png file, or Resource.layout.main +to reference the layout/main.xml file, or Resource.strings.first_string to reference the first +string in the dictionary file values/strings.xml. \ No newline at end of file diff --git a/Resources/Resource.Designer.cs b/Resources/Resource.Designer.cs new file mode 100644 index 0000000..766aff4 --- /dev/null +++ b/Resources/Resource.Designer.cs @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:2.0.50727.4952 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace SharedSettingsAbstraction +{ + + + public partial class Resource + { + + public partial class Attribute + { + + private Attribute() + { + } + } + + public partial class Drawable + { + + // aapt resource value: 0x7f020000 + public const int Icon = 2130837504; + + private Drawable() + { + } + } + + public partial class Id + { + + // aapt resource value: 0x7f050000 + public const int MyButton = 2131034112; + + private Id() + { + } + } + + public partial class Layout + { + + // aapt resource value: 0x7f030000 + public const int Main = 2130903040; + + private Layout() + { + } + } + + public partial class String + { + + // aapt resource value: 0x7f040001 + public const int ApplicationName = 2130968577; + + // aapt resource value: 0x7f040000 + public const int Hello = 2130968576; + + private String() + { + } + } + } +} diff --git a/Resources/Values/Strings.xml b/Resources/Values/Strings.xml new file mode 100644 index 0000000..9d44e29 --- /dev/null +++ b/Resources/Values/Strings.xml @@ -0,0 +1,5 @@ + + + Hello World, Click Me! + $projectname$ + diff --git a/Setting/CastingSettingException.cs b/Setting/CastingSettingException.cs new file mode 100644 index 0000000..5846933 --- /dev/null +++ b/Setting/CastingSettingException.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace GravatarSyncro.Android.Client.Setting +{ + class CastingSettingException : Exception + { + public CastingSettingException() + : base("Error Casting Settings") { } + } + +} diff --git a/Setting/SettingsKey.cs b/Setting/SettingsKey.cs new file mode 100644 index 0000000..eb5d124 --- /dev/null +++ b/Setting/SettingsKey.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using Android.App; +using Android.Content; +using Android.OS; +using Android.Runtime; +using Android.Views; +using Android.Widget; +using SharedSettingsAbstraction.Extensions; + +namespace SharedSettingsAbstraction.Setting +{ + /// + /// Key Value Pair + /// + /// Value Type + public class SettingsKey + { + public string key { get; private set; } + public T value { private get; private set; } + public string Description { get; set; } + public string Title { get; set; } + private string _preferenceName; + private T _defaultValue; + public List SpinnerOptions { get; set; } + /// + /// Setup key + /// + /// + public SettingsKey(string _key, string preferenceName, T defaultValue) + { + key = _key; + _preferenceName = preferenceName; + _defaultValue = defaultValue; + } + + /// + /// Get Setting, if it cannot find it or cast properly it will return default setting + /// + /// Context + /// SettingsKey + public SettingsKey getSetting(Context con ) + { + var shared = con.GetSharedPreferences(_preferenceName, FileCreationMode.WorldReadable); + try + { + value = (T)shared.All.Where(x => x.Key == key).FirstOrDefault().Value; + if (value == null) SetSetting(con, _defaultValue); + } + catch (Exception) + { + value = (T)_defaultValue; + } + + return this; + } + /// + /// Set the setting with a new setting + /// + /// + /// + /// + public SettingsKey SetSetting(Context con, T val) + { + var shared = con.GetSharedPreferences(_preferenceName, FileCreationMode.WorldWriteable); + var edit = shared.Edit(); + edit.SaveObject(key, val); + edit.Commit(); + value = val; + return this; + } + } +} \ No newline at end of file diff --git a/SharedSettingsAbstraction.csproj b/SharedSettingsAbstraction.csproj new file mode 100644 index 0000000..5211535 --- /dev/null +++ b/SharedSettingsAbstraction.csproj @@ -0,0 +1,64 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {F8507427-451B-4B55-A5B5-52F1151324B3} + {EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Library + Properties + SharedSettingsAbstraction + SharedSettingsAbstraction + 512 + Resources\Resource.Designer.cs + Off + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file