Browse Source

[shared_pref] done implementation

merge-requests/2/head
Vitaliy Zarubin 2 years ago
parent
commit
99ff12d573
  1. 4
      .vscode/settings.json
  2. 2
      packages/path_provider/path_provider_aurora/example/aurora/desktop/com.example.path_provider_aurora_example.desktop
  3. 20
      packages/shared_preferences/shared_preferences_aurora/README.md
  4. 26
      packages/shared_preferences/shared_preferences_aurora/aurora/include/shared_preferences_aurora/shared_preferences_aurora_plugin.h
  5. 249
      packages/shared_preferences/shared_preferences_aurora/aurora/shared_preferences_aurora_plugin.cpp
  6. BIN
      packages/shared_preferences/shared_preferences_aurora/data/preview.png
  7. 120
      packages/shared_preferences/shared_preferences_aurora/example/lib/main.dart
  8. 117
      packages/shared_preferences/shared_preferences_aurora/example/pubspec.lock
  9. 2
      packages/shared_preferences/shared_preferences_aurora/example/pubspec.yaml
  10. 71
      packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora.dart
  11. 71
      packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora_method_channel.dart
  12. 32
      packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora_platform_interface.dart

4
.vscode/settings.json vendored

@ -61,6 +61,8 @@
"typeinfo": "cpp",
"variant": "cpp",
"qstring": "cpp",
"qvariant": "cpp"
"qvariant": "cpp",
"qmetatype": "cpp",
"qdebug": "cpp"
}
}

2
packages/path_provider/path_provider_aurora/example/aurora/desktop/com.example.path_provider_aurora_example.desktop

@ -7,6 +7,6 @@ Exec=/usr/bin/com.example.path_provider_aurora_example
X-Nemo-Application-Type=silica-qt5
[X-Application]
Permissions=
Permissions=UserDirs
OrganizationName=com.example
ApplicationName=path_provider_aurora_example

20
packages/shared_preferences/shared_preferences_aurora/README.md

@ -1,15 +1,17 @@
# shared_preferences_aurora
A new Flutter plugin project.
The Aurora implementation of [`shared_preferences`][https://pub.dev/packages/shared_preferences].
## Getting Started
## Usage
This package is not an _endorsed_ implementation of `shared_preferences`.
Therefore, you have to include `path_provider_aurora` alongside `shared_preferences` as dependencies in your `pubspec.yaml` file.
This project is a starting point for a Flutter
[plug-in package](https://flutter.dev/developing-packages/),
a specialized package that includes platform-specific implementation code for
Android and/or iOS.
```yaml
dependencies:
shared_preferences: ^2.1.1
shared_preferences_aurora: ^0.0.0 # @todo Not published
```
For help getting started with Flutter development, view the
[online documentation](https://flutter.dev/docs), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
### Preview example
![preview.png](data%2Fpreview.png)

26
packages/shared_preferences/shared_preferences_aurora/aurora/include/shared_preferences_aurora/shared_preferences_aurora_plugin.h

@ -20,38 +20,44 @@ public:
struct ARGS
{
QString prefix;
QString key;
Encodable value;
};
enum Types
{
Int,
Bool,
Double,
String,
List
};
enum Methods
{
getInt,
setInt,
getBool,
setBool,
getDouble,
setDouble,
getString,
setString,
getStringList,
setStringList,
clearWithPrefix,
remove,
getAllWithPrefix,
};
private:
void onMethodCall(const MethodCall &call);
void unimplemented(const MethodCall &call);
ARGS getArguments(const MethodCall &call);
void onGetInt(const MethodCall &call);
void onSetInt(const MethodCall &call);
void onGetBool(const MethodCall &call);
void onSetBool(const MethodCall &call);
void onGetDouble(const MethodCall &call);
void onSetDouble(const MethodCall &call);
void onGetString(const MethodCall &call);
void onSetString(const MethodCall &call);
void onGetStringList(const MethodCall &call);
void onSetStringList(const MethodCall &call);
void onClearWithPrefix(const MethodCall &call);
void onRemove(const MethodCall &call);
void onGetAllWithPrefix(const MethodCall &call);
QSettings settings;
std::map <std::string, int> mapping;

249
packages/shared_preferences/shared_preferences_aurora/aurora/shared_preferences_aurora_plugin.cpp

@ -2,25 +2,37 @@
#include <flutter/method-channel.h>
#include <flutter/application.h>
#include <QSettings>
#include <QMetaType>
#include <QVariant>
#include <QDir>
namespace {
QString defaultSettingsFile()
{
const auto [orgname, appname] = Application::GetID();
return QStringLiteral("%1/.local/share/%2/%3/.flutter_shared_preferences.conf")
.arg(QDir::homePath())
.arg(QString::fromStdString(orgname))
.arg(QString::fromStdString(appname));
}
} /* namespace */
SharedPreferencesAuroraPlugin::SharedPreferencesAuroraPlugin(): settings(
QString::fromStdString(Application::GetID().orgname),
QString::fromStdString(Application::GetID().appname)
defaultSettingsFile(),
QSettings::NativeFormat
) {
// map methods
this->mapping = std::map <std::string, int>({
{"getInt", Methods::getInt},
{"setInt", Methods::setInt},
{"getBool", Methods::getBool},
{"setBool", Methods::setBool},
{"getDouble", Methods::getDouble},
{"setDouble", Methods::setDouble},
{"getString", Methods::getString},
{"setString", Methods::setString},
{"getStringList", Methods::getStringList},
{"setStringList", Methods::setStringList},
{"clearWithPrefix", Methods::clearWithPrefix},
{"remove", Methods::remove},
{"getAllWithPrefix", Methods::getAllWithPrefix},
});
}
@ -33,9 +45,14 @@ void SharedPreferencesAuroraPlugin::RegisterWithRegistrar(PluginRegistrar &regis
SharedPreferencesAuroraPlugin::ARGS SharedPreferencesAuroraPlugin::getArguments(const MethodCall &call)
{
QString key = QString::fromStdString(call.GetArguments()["key"].GetString());
Encodable value = call.GetArguments()["value"];
ARGS args;
args.key = QString::fromStdString(call.GetArguments()["key"].GetString());
args.value = call.GetArguments()["value"];
args.prefix = key.split('.').first();
args.key = key.mid(args.prefix.length() + 1, key.length());
args.value = value;
return args;
}
@ -43,113 +60,197 @@ void SharedPreferencesAuroraPlugin::onMethodCall(const MethodCall &call)
{
const auto &method = call.GetMethod();
switch (this->mapping[method]) {
case Methods::getInt: onGetInt(call); return;
case Methods::setInt: onSetInt(call); return;
case Methods::getBool: onGetBool(call); return;
case Methods::setBool: onSetBool(call); return;
case Methods::getDouble: onGetDouble(call); return;
case Methods::setDouble: onSetDouble(call); return;
case Methods::getString: onGetString(call); return;
case Methods::setString: onSetString(call); return;
case Methods::getStringList: onGetStringList(call); return;
case Methods::setStringList: onSetStringList(call); return;
switch (this->mapping[method])
{
case Methods::setInt:
onSetInt(call);
return;
case Methods::setBool:
onSetBool(call);
return;
case Methods::setDouble:
onSetDouble(call);
return;
case Methods::setString:
onSetString(call);
return;
case Methods::setStringList:
onSetStringList(call);
return;
case Methods::clearWithPrefix:
onClearWithPrefix(call);
return;
case Methods::remove:
onRemove(call);
return;
case Methods::getAllWithPrefix:
onGetAllWithPrefix(call);
return;
}
unimplemented(call);
}
void SharedPreferencesAuroraPlugin::onGetInt(const MethodCall &call)
{
const auto [key, value] = this->getArguments(call);
call.SendSuccessResponse(settings.value(key, value.GetInt()).toInt());
}
void SharedPreferencesAuroraPlugin::onSetInt(const MethodCall &call)
{
const auto [key, value] = this->getArguments(call);
settings.setValue(key, value.GetInt());
call.SendSuccessResponse(true);
}
const auto [prefix, key, value] = this->getArguments(call);
void SharedPreferencesAuroraPlugin::onGetBool(const MethodCall &call)
{
const auto [key, value] = this->getArguments(call);
call.SendSuccessResponse(settings.value(key, value.GetBoolean()).toBool());
settings.beginGroup(prefix);
settings.setValue(
QStringLiteral("%1:").arg(Types::Int) + key,
value.GetInt()
);
settings.sync();
settings.endGroup();
call.SendSuccessResponse(true);
}
void SharedPreferencesAuroraPlugin::onSetBool(const MethodCall &call)
{
const auto [key, value] = this->getArguments(call);
settings.setValue(key, value.GetBoolean());
const auto [prefix, key, value] = this->getArguments(call);
settings.beginGroup(prefix);
settings.setValue(
QStringLiteral("%1:").arg(Types::Bool) + key,
value.GetBoolean()
);
settings.sync();
settings.endGroup();
call.SendSuccessResponse(true);
}
void SharedPreferencesAuroraPlugin::onGetDouble(const MethodCall &call)
void SharedPreferencesAuroraPlugin::onSetDouble(const MethodCall &call)
{
const auto [key, value] = this->getArguments(call);
call.SendSuccessResponse(settings.value(key, value.GetFloat()).toDouble());
const auto [prefix, key, value] = this->getArguments(call);
settings.beginGroup(prefix);
settings.setValue(
QStringLiteral("%1:").arg(Types::Double) + key,
value.GetFloat()
);
settings.sync();
settings.endGroup();
call.SendSuccessResponse(true);
}
void SharedPreferencesAuroraPlugin::onSetDouble(const MethodCall &call)
void SharedPreferencesAuroraPlugin::onSetString(const MethodCall &call)
{
const auto [key, value] = this->getArguments(call);
settings.setValue(key, value.GetFloat());
const auto [prefix, key, value] = this->getArguments(call);
settings.beginGroup(prefix);
settings.setValue(
QStringLiteral("%1:").arg(Types::String) + key,
QString::fromStdString(value.GetString())
);
settings.sync();
settings.endGroup();
call.SendSuccessResponse(true);
}
void SharedPreferencesAuroraPlugin::onGetString(const MethodCall &call)
void SharedPreferencesAuroraPlugin::onSetStringList(const MethodCall &call)
{
const auto [key, value] = this->getArguments(call);
call.SendSuccessResponse(settings.value(
key,
QString::fromStdString(value.GetString())).toString().toStdString()
const auto [prefix, key, value] = this->getArguments(call);
const auto vec = value.GetList();
QStringList strings;
for (const auto& item : vec)
{
strings.append(QString::fromStdString(item.GetString()));
}
settings.beginGroup(prefix);
settings.setValue(
QStringLiteral("%1:").arg(Types::List) + key,
strings
);
settings.sync();
settings.endGroup();
call.SendSuccessResponse(true);
}
void SharedPreferencesAuroraPlugin::onSetString(const MethodCall &call)
void SharedPreferencesAuroraPlugin::onClearWithPrefix(const MethodCall &call)
{
const auto [key, value] = this->getArguments(call);
settings.setValue(key, QString::fromStdString(value.GetString()));
const auto prefix = QString::fromStdString(call.GetArguments()["prefix"].GetString())
.replace(".", "");
settings.beginGroup(prefix);
settings.remove("");
settings.sync();
settings.endGroup();
call.SendSuccessResponse(true);
}
void SharedPreferencesAuroraPlugin::onGetStringList(const MethodCall &call)
void SharedPreferencesAuroraPlugin::onRemove(const MethodCall &call)
{
const auto [key, value] = this->getArguments(call);
QString raw = QString::fromStdString(call.GetArguments()["key"].GetString());
const auto prefix = raw.split('.').first();
const auto key = raw.mid(prefix.length() + 1, raw.length());
std::vector<Encodable> vec;
QStringList list = settings.value(key, {}).toStringList();
settings.beginGroup(prefix);
settings.remove(QStringLiteral("%1:").arg(Types::Int) + key);
settings.remove(QStringLiteral("%1:").arg(Types::Bool) + key);
settings.remove(QStringLiteral("%1:").arg(Types::Double) + key);
settings.remove(QStringLiteral("%1:").arg(Types::String) + key);
settings.remove(QStringLiteral("%1:").arg(Types::List) + key);
settings.sync();
settings.endGroup();
for (const auto& item : list)
{
vec.push_back(Encodable(item.toStdString()));
}
if (vec.size() > 0) {
call.SendSuccessResponse(vec);
} else {
call.SendSuccessResponse(value.GetList());
}
call.SendSuccessResponse(true);
}
void SharedPreferencesAuroraPlugin::onSetStringList(const MethodCall &call)
void SharedPreferencesAuroraPlugin::onGetAllWithPrefix(const MethodCall &call)
{
const auto [key, value] = this->getArguments(call);
const auto vec = value.GetList();
const auto prefix = QString::fromStdString(call.GetArguments()["prefix"].GetString())
.replace(".", "");
QStringList strings;
std::map<Encodable, Encodable> map;
for (const auto& item : vec)
settings.beginGroup(prefix);
for (const auto& item : settings.allKeys())
{
strings.append(QString::fromStdString(item.GetString()));
const auto type = item.split(":").first();
const auto key = prefix + "." + item.mid(type.length() + 1, item.length());
const auto variant = settings.value(item);
switch (type.toInt())
{
case Types::Int:
map[key.toStdString()] = Encodable(variant.toInt());
break;
case Types::Bool:
map[key.toStdString()] = Encodable(variant.toBool());
break;
case Types::Double:
map[key.toStdString()] = Encodable(variant.toDouble());
break;
case Types::String:
map[key.toStdString()] = Encodable(variant.toString().toStdString());
break;
case Types::List:
std::vector<Encodable> vec;
QStringList list = variant.toStringList();
for (const auto& item : list)
{
vec.push_back(item.toStdString());
}
map[key.toStdString()] = vec;
}
}
settings.setValue(key, strings);
call.SendSuccessResponse(true);
settings.endGroup();
call.SendSuccessResponse(map);
}
void SharedPreferencesAuroraPlugin::unimplemented(const MethodCall &call)
{
call.SendSuccessResponse(nullptr);
}
}

BIN
packages/shared_preferences/shared_preferences_aurora/data/preview.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 91 KiB

120
packages/shared_preferences/shared_preferences_aurora/example/lib/main.dart

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:shared_preferences_aurora/shared_preferences_aurora.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
runApp(const MyApp());
@ -16,16 +16,13 @@ class MyApp extends StatefulWidget {
}
class _MyAppState extends State<MyApp> {
int _valueInt = -1;
bool _valueBool = false;
double _valueDouble = 0.0;
String _valueString = "";
List<String> _valueStringList = [];
int? _counter;
bool? _repeat;
double? _decimal;
String? _action;
List<String>? _items;
String? _error;
final _sharedPreferences = SharedPreferencesAurora();
@override
void initState() {
super.initState();
@ -33,60 +30,43 @@ class _MyAppState extends State<MyApp> {
}
Future<void> initPlatformState() async {
int valueInt = -1;
bool valueBool = false;
double valueDouble = 0.0;
String valueString = "";
List<String> valueStringList = [];
String error = "";
try {
await _sharedPreferences.setInt('_valueInt', 77);
await _sharedPreferences.setBool('_valueBool', true);
await _sharedPreferences.setDouble('_valueDouble', 0.77);
await _sharedPreferences.setString('_valueString', "String");
await _sharedPreferences.setStringList('_valueStringList', [
"Item 1",
"Item 2",
]);
valueInt = await _sharedPreferences.getInt(
'_valueInt',
_valueInt,
);
valueBool = await _sharedPreferences.getBool(
'_valueBool',
_valueBool,
);
valueDouble = await _sharedPreferences.getDouble(
'_valueDouble',
_valueDouble,
);
valueString = await _sharedPreferences.getString(
'_valueString',
_valueString,
);
valueStringList = await _sharedPreferences.getStringList(
'_valueStringList',
_valueStringList,
);
final SharedPreferences prefs = await SharedPreferences.getInstance();
// Save an integer value to 'counter' key.
await prefs.setInt('counter', 10);
// Save an boolean value to 'repeat' key.
await prefs.setBool('repeat', true);
// Save an double value to 'decimal' key.
await prefs.setDouble('decimal', 1.5);
// Save an String value to 'action' key.
await prefs.setString('action', 'Start');
// Save an list of strings to 'items' key.
await prefs.setStringList('items', <String>['Earth', 'Moon', 'Sun']);
// Try reading data from the 'counter' key. If it doesn't exist, returns null.
final int? counter = prefs.getInt('counter');
// Try reading data from the 'repeat' key. If it doesn't exist, returns null.
final bool? repeat = prefs.getBool('repeat');
// Try reading data from the 'decimal' key. If it doesn't exist, returns null.
final double? decimal = prefs.getDouble('decimal');
// Try reading data from the 'action' key. If it doesn't exist, returns null.
final String? action = prefs.getString('action');
// Try reading data from the 'items' key. If it doesn't exist, returns null.
final List<String>? items = prefs.getStringList('items');
setState(() {
_counter = counter;
_repeat = repeat;
_decimal = decimal;
_action = action;
_items = items;
});
} on PlatformException {
error = 'Platform exception';
setState(() {
_error = 'Platform exception';
});
}
if (!mounted) return;
setState(() {
if (error.isEmpty) {
_valueInt = valueInt;
_valueBool = valueBool;
_valueDouble = valueDouble;
_valueString = valueString;
_valueStringList = valueStringList;
} else {
_error = error;
}
});
}
@override
@ -151,56 +131,56 @@ class _MyAppState extends State<MyApp> {
const SizedBox(height: 30),
const Text(
'setInt/getInt',
'Counter / int',
style: textStyleTitle,
),
spaceSmall,
Text(
_valueInt.toString(),
_counter.toString(),
style: textStylePath,
),
spaceMedium,
const Text(
'setBool/getBool',
'Repeat / bool',
style: textStyleTitle,
),
spaceSmall,
Text(
_valueBool.toString(),
_repeat.toString(),
style: textStylePath,
),
spaceMedium,
const Text(
'setDouble/getDouble',
'Decimal / double',
style: textStyleTitle,
),
spaceSmall,
Text(
_valueDouble.toString(),
_decimal.toString(),
style: textStylePath,
),
spaceMedium,
const Text(
'setString/getString',
'Action / String',
style: textStyleTitle,
),
spaceSmall,
Text(
_valueString,
_action.toString(),
style: textStylePath,
),
spaceMedium,
const Text(
'setStringList/getStringList',
'Items / String List',
style: textStyleTitle,
),
spaceSmall,
Text(
_valueStringList.toString(),
_items.toString(),
style: textStylePath,
),
],

117
packages/shared_preferences/shared_preferences_aurora/example/pubspec.lock

@ -50,6 +50,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
ffi:
dependency: transitive
description:
name: ffi
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
file:
dependency: transitive
description:
name: file
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.4"
flutter:
dependency: "direct main"
description: flutter
@ -67,6 +81,18 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.4"
lints:
dependency: transitive
description:
@ -102,6 +128,34 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
path_provider_linux:
dependency: transitive
description:
name: path_provider_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.10"
path_provider_platform_interface:
dependency: transitive
description:
name: path_provider_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.6"
path_provider_windows:
dependency: transitive
description:
name: path_provider_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.6"
platform:
dependency: transitive
description:
name: platform
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
plugin_platform_interface:
dependency: transitive
description:
@ -109,6 +163,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
process:
dependency: transitive
description:
name: process
url: "https://pub.dartlang.org"
source: hosted
version: "4.2.4"
shared_preferences:
dependency: "direct main"
description:
name: shared_preferences
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.4"
shared_preferences_aurora:
dependency: "direct main"
description:
@ -116,6 +191,20 @@ packages:
relative: true
source: path
version: "0.0.1"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.2"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
shared_preferences_platform_interface:
dependency: transitive
description:
@ -123,6 +212,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
sky_engine:
dependency: transitive
description: flutter
@ -177,6 +280,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
win32:
dependency: transitive
description:
name: win32
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.4"
xdg_directories:
dependency: transitive
description:
name: xdg_directories
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
sdks:
dart: ">=2.18.6 <3.0.0"
flutter: ">=3.0.0"

2
packages/shared_preferences/shared_preferences_aurora/example/pubspec.yaml

@ -17,7 +17,7 @@ environment:
dependencies:
flutter:
sdk: flutter
shared_preferences: ^2.1.1
shared_preferences_aurora:
# When depending on this package from a real application you should use:
# shared_preferences_aurora: ^x.y.z

71
packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora.dart

@ -1,45 +1,64 @@
import 'shared_preferences_aurora_platform_interface.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart';
class SharedPreferencesAurora {
static void registerWith() {}
class SharedPreferencesAurora extends SharedPreferencesStorePlatform {
SharedPreferencesAurora({
@visibleForTesting SharedPreferencesAuroraPlatform? api,
}) : _api = api ?? SharedPreferencesAuroraPlatform.instance;
Future<int> getInt(String key, int value) {
return SharedPreferencesAuroraPlatform.instance.getInt(key, value);
}
Future<bool> setInt(String key, int value) {
return SharedPreferencesAuroraPlatform.instance.setInt(key, value);
}
late final SharedPreferencesAuroraPlatform _api;
Future<bool> getBool(String key, bool value) {
return SharedPreferencesAuroraPlatform.instance.getBool(key, value);
/// Registers this class as the default instance of [SharedPreferencesStorePlatform].
static void registerWith() {
SharedPreferencesStorePlatform.instance = SharedPreferencesAurora();
}
Future<bool> setBool(String key, bool value) {
return SharedPreferencesAuroraPlatform.instance.setBool(key, value);
}
static const String _defaultPrefix = 'flutter.';
Future<double> getDouble(String key, double value) {
return SharedPreferencesAuroraPlatform.instance.getDouble(key, value);
@override
Future<bool> remove(String key) async {
return _api.remove(key);
}
Future<bool> setDouble(String key, double value) {
return SharedPreferencesAuroraPlatform.instance.setDouble(key, value);
@override
Future<bool> setValue(String valueType, String key, Object value) async {
switch (valueType) {
case 'String':
return _api.setString(key, value as String);
case 'Bool':
return _api.setBool(key, value as bool);
case 'Int':
return _api.setInt(key, value as int);
case 'Double':
return _api.setDouble(key, value as double);
case 'StringList':
return _api.setStringList(key, value as List<String>);
}
throw PlatformException(
code: 'InvalidOperation',
message: '"$valueType" is not a supported type.');
}
Future<String> getString(String key, String value) {
return SharedPreferencesAuroraPlatform.instance.getString(key, value);
@override
Future<bool> clear() {
return clearWithPrefix(_defaultPrefix);
}
Future<bool> setString(String key, String value) {
return SharedPreferencesAuroraPlatform.instance.setString(key, value);
@override
Future<bool> clearWithPrefix(String prefix) async {
return _api.clearWithPrefix(prefix);
}
Future<List<String>> getStringList(String key, List<String> value) {
return SharedPreferencesAuroraPlatform.instance.getStringList(key, value);
@override
Future<Map<String, Object>> getAll() {
return getAllWithPrefix(_defaultPrefix);
}
Future<bool> setStringList(String key, List<String> value) {
return SharedPreferencesAuroraPlatform.instance.setStringList(key, value);
@override
Future<Map<String, Object>> getAllWithPrefix(String prefix) async {
final Map<Object?, Object?> data = await _api.getAllWithPrefix(prefix);
return data.cast<String, Object>();
}
}

71
packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora_method_channel.dart

@ -10,15 +10,6 @@ class MethodChannelSharedPreferencesAurora
@visibleForTesting
final methodChannel = const MethodChannel('shared_preferences_aurora');
@override
Future<int> getInt(String key, int value) async {
final result = await methodChannel.invokeMethod<int?>('getInt', {
'key': key,
'value': value,
});
return result ?? value;
}
@override
Future<bool> setInt(String key, int value) async {
return await methodChannel.invokeMethod<bool?>('setInt', {
@ -28,15 +19,6 @@ class MethodChannelSharedPreferencesAurora
false;
}
@override
Future<bool> getBool(String key, bool value) async {
final result = await methodChannel.invokeMethod<bool?>('getBool', {
'key': key,
'value': value,
});
return result ?? value;
}
@override
Future<bool> setBool(String key, bool value) async {
return await methodChannel.invokeMethod<bool?>('setBool', {
@ -46,15 +28,6 @@ class MethodChannelSharedPreferencesAurora
false;
}
@override
Future<double> getDouble(String key, double value) async {
final result = await methodChannel.invokeMethod<double?>('getDouble', {
'key': key,
'value': value,
});
return result ?? value;
}
@override
Future<bool> setDouble(String key, double value) async {
return await methodChannel.invokeMethod<bool?>('setDouble', {
@ -65,17 +38,17 @@ class MethodChannelSharedPreferencesAurora
}
@override
Future<String> getString(String key, String value) async {
final result = await methodChannel.invokeMethod<String?>('getString', {
'key': key,
'value': value,
});
return result ?? value;
Future<bool> setString(String key, String value) async {
return await methodChannel.invokeMethod<bool?>('setString', {
'key': key,
'value': value,
}) ??
false;
}
@override
Future<bool> setString(String key, String value) async {
return await methodChannel.invokeMethod<bool?>('setString', {
Future<bool> setStringList(String key, List<String> value) async {
return await methodChannel.invokeMethod<bool?>('setStringList', {
'key': key,
'value': value,
}) ??
@ -83,23 +56,27 @@ class MethodChannelSharedPreferencesAurora
}
@override
Future<List<String>> getStringList(String key, List<String> value) async {
final result = await methodChannel.invokeMethod<List<Object?>?>(
'getStringList',
{
'key': key,
'value': value,
},
);
return result?.map((e) => e as String).toList() ?? value;
Future<bool> clearWithPrefix(String prefix) async {
return await methodChannel.invokeMethod<bool?>('clearWithPrefix', {
'prefix': prefix,
}) ??
false;
}
@override
Future<bool> setStringList(String key, List<String> value) async {
return await methodChannel.invokeMethod<bool?>('setStringList', {
Future<bool> remove(String key) async {
return await methodChannel.invokeMethod<bool?>('remove', {
'key': key,
'value': value,
}) ??
false;
}
@override
Future<Map<Object?, Object?>> getAllWithPrefix(String prefix) async {
return await methodChannel
.invokeMethod<Map<Object?, Object?>?>('getAllWithPrefix', {
'prefix': prefix,
}) ??
{};
}
}

32
packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora_platform_interface.dart

@ -24,43 +24,35 @@ abstract class SharedPreferencesAuroraPlatform extends PlatformInterface {
_instance = instance;
}
Future<int> getInt(String key, int value) {
throw UnimplementedError('getInt() has not been implemented.');
}
Future<bool> setInt(String key, int value) {
throw UnimplementedError('setInt() has not been implemented.');
}
Future<bool> getBool(String key, bool value) {
throw UnimplementedError('getBool() has not been implemented.');
}
Future<bool> setBool(String key, bool value) {
throw UnimplementedError('setBool() has not been implemented.');
}
Future<double> getDouble(String key, double value) {
throw UnimplementedError('getDouble() has not been implemented.');
}
Future<bool> setDouble(String key, double value) {
throw UnimplementedError('setDouble() has not been implemented.');
}
Future<String> getString(String key, String value) {
throw UnimplementedError('getString() has not been implemented.');
}
Future<bool> setString(String key, String value) {
throw UnimplementedError('setString() has not been implemented.');
}
Future<List<String>> getStringList(String key, List<String> value) {
throw UnimplementedError('getStringList() has not been implemented.');
}
Future<bool> setStringList(String key, List<String> value) {
throw UnimplementedError('setStringList() has not been implemented.');
}
Future<bool> clearWithPrefix(String prefix) {
throw UnimplementedError('onClearWithPrefix() has not been implemented.');
}
Future<bool> remove(String key) {
throw UnimplementedError('remove() has not been implemented.');
}
Future<Map<Object?, Object?>> getAllWithPrefix(String prefix) {
throw UnimplementedError('getAllWithPrefix() has not been implemented.');
}
}

Loading…
Cancel
Save