Skip to content
This repository was archived by the owner on May 15, 2024. It is now read-only.

Commit 88f8434

Browse files
authored
Merge pull request #2 from xamarin/preferences
Preferences
2 parents 96c9800 + e0e5007 commit 88f8434

14 files changed

+495
-77
lines changed

F50.Tests/UnitTest1.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
using Xunit;
33

44
namespace F50.Tests
5-
{
6-
public class UnitTest1
7-
{
8-
[Fact]
9-
public void Test1()
10-
{
11-
12-
}
13-
}
5+
{
6+
public class UnitTest1
7+
{
8+
[Fact]
9+
public void Test1()
10+
{
11+
12+
}
13+
}
1414
}

F50/F50.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard1.0;netstandard2.0;Xamarin.iOS10;MonoAndroid80;uap10.0.16299</TargetFrameworks>
4+
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">netstandard1.0;netstandard2.0;Xamarin.iOS10;MonoAndroid80;uap10.0.16299</TargetFrameworks>
5+
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard1.0;netstandard2.0;Xamarin.iOS10;MonoAndroid80</TargetFrameworks>
56
<AssemblyName>Xamarin.F50</AssemblyName>
67
<RootNamespace>Xamarin.F50</RootNamespace>
78
<PackageId>Xamarin.F50</PackageId>
@@ -22,7 +23,7 @@
2223
<Copyright>Copyright 2018</Copyright>
2324
<RepositoryUrl>https://github.com/xamarin/f50</RepositoryUrl>
2425
<PackageReleaseNotes>See: https://github.com/xamarin/f50 </PackageReleaseNotes>
25-
<LangVersion>default</LangVersion>
26+
<LangVersion>7.1</LangVersion>
2627
<DefineConstants>$(DefineConstants);</DefineConstants>
2728
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2829
<GenerateDocumentationFile Condition=" '$(Configuration)' == 'Release' ">true</GenerateDocumentationFile>

F50/Platform/Platform.android.cs

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using Android.App;
2+
using Android.Content;
3+
using Android.OS;
4+
using System;
5+
6+
namespace Xamarin.F50
7+
{
8+
public partial class Platform
9+
{
10+
static ActivityLifecycleContextListener lifecycleListener;
11+
12+
internal static Context CurrentContext =>
13+
lifecycleListener?.Context ?? Application.Context;
14+
15+
internal static Activity CurrentActivity =>
16+
lifecycleListener?.Activity;
17+
18+
public static void Init(Activity activity, Bundle bundle)
19+
{
20+
lifecycleListener = new ActivityLifecycleContextListener();
21+
activity.Application.RegisterActivityLifecycleCallbacks(lifecycleListener);
22+
}
23+
}
24+
25+
class ActivityLifecycleContextListener : Java.Lang.Object, Application.IActivityLifecycleCallbacks
26+
{
27+
WeakReference<Activity> currentActivity = new WeakReference<Activity>(null);
28+
29+
public Context Context =>
30+
Activity ?? Application.Context;
31+
32+
public Activity Activity
33+
{
34+
get
35+
{
36+
Activity a;
37+
if (currentActivity.TryGetTarget(out a))
38+
return a;
39+
return null;
40+
}
41+
}
42+
43+
public void OnActivityCreated(Activity activity, Bundle savedInstanceState)
44+
{
45+
}
46+
47+
public void OnActivityDestroyed(Activity activity)
48+
{
49+
}
50+
51+
public void OnActivityPaused(Activity activity)
52+
{
53+
currentActivity.SetTarget(null);
54+
}
55+
56+
public void OnActivityResumed(Activity activity)
57+
{
58+
currentActivity.SetTarget(activity);
59+
}
60+
61+
public void OnActivitySaveInstanceState(Activity activity, Bundle outState)
62+
{
63+
}
64+
65+
public void OnActivityStarted(Activity activity)
66+
{
67+
}
68+
69+
public void OnActivityStopped(Activity activity)
70+
{
71+
}
72+
}
73+
}

F50/Platform/Platform.ios.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Xamarin.F50
2+
{
3+
public partial class Platform
4+
{
5+
}
6+
}

F50/Platform/Platform.netstandard.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Xamarin.F50
2+
{
3+
public partial class Platform
4+
{
5+
}
6+
}

F50/Platform/Platform.shared.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Xamarin.F50
2+
{
3+
public partial class Platform
4+
{
5+
}
6+
}

F50/Platform/Platform.uwp.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Xamarin.F50
2+
{
3+
public partial class Platform
4+
{
5+
}
6+
}
+130-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,140 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
2+
using System.Globalization;
3+
using Android.App;
4+
using Android.Content;
5+
using Android.Preferences;
46

57
namespace Xamarin.F50
68
{
79
public partial class Preferences
810
{
9-
public void Set(string key, string val)
10-
{
11+
static readonly object locker = new object();
1112

13+
public bool ContainsKey(string key)
14+
{
15+
lock (locker)
16+
{
17+
using (var sharedPreferences = GetSharedPreferences())
18+
{
19+
return sharedPreferences.Contains(key);
20+
}
21+
}
22+
}
23+
24+
public void Remove(string key)
25+
{
26+
lock (locker)
27+
{
28+
using (var sharedPreferences = GetSharedPreferences())
29+
using (var editor = sharedPreferences.Edit())
30+
{
31+
editor.Remove(key).Commit();
32+
}
33+
}
34+
}
35+
36+
public void Clear()
37+
{
38+
lock (locker)
39+
{
40+
using (var sharedPreferences = GetSharedPreferences())
41+
using (var editor = sharedPreferences.Edit())
42+
{
43+
editor.Clear().Commit();
44+
}
45+
}
46+
}
47+
48+
void Set<T>(string key, T value)
49+
{
50+
lock (locker)
51+
{
52+
using (var sharedPreferences = GetSharedPreferences())
53+
using (var editor = sharedPreferences.Edit())
54+
{
55+
switch (value)
56+
{
57+
case string s:
58+
editor.PutString(key, s);
59+
break;
60+
case int i:
61+
editor.PutInt(key, i);
62+
break;
63+
case bool b:
64+
editor.PutBoolean(key, b);
65+
break;
66+
case long l:
67+
editor.PutLong(key, l);
68+
break;
69+
case double d:
70+
var valueString = Convert.ToString(value, CultureInfo.InvariantCulture);
71+
editor.PutString(key, valueString);
72+
break;
73+
case float f:
74+
editor.PutFloat(key, f);
75+
break;
76+
}
77+
editor.Apply();
78+
}
79+
}
80+
}
81+
82+
T Get<T>(string key, T defaultValue)
83+
{
84+
lock (locker)
85+
{
86+
object value = null;
87+
using (var sharedPreferences = GetSharedPreferences())
88+
{
89+
switch (defaultValue)
90+
{
91+
case string s:
92+
value = sharedPreferences.GetString(key, s);
93+
break;
94+
case int i:
95+
value = sharedPreferences.GetInt(key, i);
96+
break;
97+
case bool b:
98+
value = sharedPreferences.GetBoolean(key, b);
99+
break;
100+
case long l:
101+
value = sharedPreferences.GetLong(key, l);
102+
break;
103+
case double d:
104+
var savedDouble = sharedPreferences.GetString(key, null);
105+
if (string.IsNullOrWhiteSpace(savedDouble))
106+
{
107+
value = defaultValue;
108+
}
109+
else
110+
{
111+
double outDouble;
112+
if (!double.TryParse(savedDouble, out outDouble))
113+
{
114+
var maxString = Convert.ToString(double.MaxValue, CultureInfo.InvariantCulture);
115+
outDouble = savedDouble.Equals(maxString) ? double.MaxValue : double.MinValue;
116+
}
117+
118+
value = outDouble;
119+
}
120+
break;
121+
case float f:
122+
value = sharedPreferences.GetFloat(key, f);
123+
break;
124+
}
125+
}
126+
127+
return (T)value;
128+
}
129+
}
130+
131+
ISharedPreferences GetSharedPreferences()
132+
{
133+
var context = Application.Context;
134+
135+
return string.IsNullOrWhiteSpace(SharedName) ?
136+
PreferenceManager.GetDefaultSharedPreferences(context) :
137+
context.GetSharedPreferences(SharedName, FileCreationMode.Private);
12138
}
13139
}
14140
}

0 commit comments

Comments
 (0)