forked from flutter-tizen/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_preferences_tizen.dart
141 lines (122 loc) · 4.12 KB
/
shared_preferences_tizen.dart
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2020 Samsung Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
import 'src/bindings.dart';
/// The Tizen implementation of [SharedPreferencesStorePlatform].
///
/// This class implements the `package:shared_preferences` functionality for Tizen.
class SharedPreferencesPlugin extends SharedPreferencesStorePlatform {
/// Registers this class as the default instance of [SharedPreferencesStorePlatform].
static void register() {
SharedPreferencesStorePlatform.instance = SharedPreferencesPlugin();
}
static Map<String, Object>? _cachedPreferences;
static const String _separator = '␞';
static int _preferenceItemCallback(Pointer<Utf8> pKey, Pointer<Void> data) {
final String key = pKey.toDartString();
using((Arena arena) {
final Pointer<Int8> pBool = arena();
if (bindings.getBoolean(pKey, pBool) == 0) {
_cachedPreferences![key] = pBool.value == 1;
return;
}
final Pointer<Double> pDouble = arena();
if (bindings.getDouble(pKey, pDouble) == 0) {
_cachedPreferences![key] = pDouble.value;
return;
}
final Pointer<Int32> pInt = arena();
if (bindings.getInt(pKey, pInt) == 0) {
_cachedPreferences![key] = pInt.value;
return;
}
final Pointer<Pointer<Utf8>> ppString = arena();
if (bindings.getString(pKey, ppString) == 0) {
final Pointer<Utf8> pString = ppString.value;
final String stringValue = pString.toDartString();
if (stringValue == _separator) {
_cachedPreferences![key] = <String>[];
} else if (stringValue.contains(_separator)) {
final List<String> list = stringValue.split(_separator);
_cachedPreferences![key] = list.getRange(1, list.length - 1);
} else {
_cachedPreferences![key] = stringValue;
}
malloc.free(pString);
return;
}
});
return 1;
}
Map<String, Object> get _preferences {
if (_cachedPreferences != null) {
return _cachedPreferences!;
}
_cachedPreferences = <String, Object>{};
final int ret = bindings.foreachItem(
Pointer.fromFunction(_preferenceItemCallback, 0), nullptr);
if (ret == 0) {
return _cachedPreferences!;
}
return <String, Object>{};
}
@override
Future<bool> clear() async {
_preferences.clear();
return bindings.removeAll() == 0;
}
@override
Future<Map<String, Object>> getAll() async => _preferences;
@override
Future<bool> remove(String key) async {
return using((Arena arena) {
final bool ret = bindings.remove(key.toNativeUtf8(allocator: arena)) == 0;
if (ret) {
_preferences.remove(key);
}
return ret;
});
}
@override
Future<bool> setValue(String valueType, String key, Object value) async {
final int ret = using((Arena arena) {
final Pointer<Utf8> pKey = key.toNativeUtf8(allocator: arena);
switch (valueType) {
case 'Bool':
return bindings.setBoolean(pKey, (value as bool) ? 1 : 0);
case 'Double':
return bindings.setDouble(pKey, value as double);
case 'Int':
return bindings.setInt(pKey, value as int);
case 'String':
return bindings.setString(
pKey,
(value as String).toNativeUtf8(allocator: arena),
);
case 'StringList':
return bindings.setString(
pKey,
_joinStringList(value as List<String>)
.toNativeUtf8(allocator: arena),
);
default:
print('Not implemented : valueType[' + valueType + ']');
return -1;
}
});
if (ret == 0) {
_preferences[key] = value;
return true;
}
return false;
}
String _joinStringList(List<String> list) {
return list.isEmpty
? _separator
: _separator + list.join(_separator) + _separator;
}
}