-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prefs.uno
97 lines (83 loc) · 2.68 KB
/
Prefs.uno
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using Uno;
using Uno.UX;
using Uno.Compiler.ExportTargetInterop;
using Fuse;
using Fuse.Scripting;
[extern(Android) ForeignInclude(Language.Java, "android.content.SharedPreferences", "android.content.Context", "android.preference.PreferenceManager")]
[UXGlobalModule]
public sealed class FusePrefs : NativeModule {
static readonly FusePrefs _instance;
public FusePrefs()
{
if (_instance != null) return;
Uno.UX.Resource.SetGlobalKey(_instance = this, "FusePrefs");
AddMember(new NativeFunction("write", Write));
AddMember(new NativeFunction("read", Read));
}
static object Write(Fuse.Scripting.Context context, object[] args)
{
if (args.Length == 2)
{
var key = args[0] as string;
var val = args[1] as string;
if (key != null && val != null)
{
return WriteImpl(key, val);
}
}
return null;
}
private static extern(!mobile) bool WriteImpl(string key, string val)
{
return false;
}
[Foreign(Language.Java)]
private static extern(Android) bool WriteImpl(string key, string val)
@{
Context context = com.fuse.Activity.getRootActivity();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, val);
editor.commit();
return true;
@}
[Foreign(Language.ObjC)]
private static extern(iOS) bool WriteImpl(string key, string val)
@{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:val forKey:key];
[prefs synchronize];
return true;
@}
static object Read(Fuse.Scripting.Context context, object[] args)
{
if (args.Length == 1)
{
var key = args[0] as string;
if (key != null)
{
return ReadImpl(key);
}
}
return null;
}
private static extern(!mobile) bool ReadImpl(string key)
{
return false;
}
[Foreign(Language.Java)]
private static extern(Android) string ReadImpl(string key)
@{
Context context = com.fuse.Activity.getRootActivity();
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String val = preferences.getString(key, "");
return val;
@}
[Foreign(Language.ObjC)]
private static extern(iOS) string ReadImpl(string key)
@{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *val = [prefs stringForKey:key];
return val;
@}
}