diff --git a/.vscode/settings.json b/.vscode/settings.json index 21cf8df..31a6f17 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -61,6 +61,8 @@ "typeinfo": "cpp", "variant": "cpp", "qstring": "cpp", - "qvariant": "cpp" + "qvariant": "cpp", + "qmetatype": "cpp", + "qdebug": "cpp" } } \ No newline at end of file diff --git a/packages/path_provider/path_provider_aurora/example/aurora/desktop/com.example.path_provider_aurora_example.desktop b/packages/path_provider/path_provider_aurora/example/aurora/desktop/com.example.path_provider_aurora_example.desktop index 39b28d4..69eeffc 100644 --- a/packages/path_provider/path_provider_aurora/example/aurora/desktop/com.example.path_provider_aurora_example.desktop +++ b/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 diff --git a/packages/shared_preferences/shared_preferences_aurora/README.md b/packages/shared_preferences/shared_preferences_aurora/README.md index 036dbb7..da8b050 100644 --- a/packages/shared_preferences/shared_preferences_aurora/README.md +++ b/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) \ No newline at end of file diff --git a/packages/shared_preferences/shared_preferences_aurora/aurora/include/shared_preferences_aurora/shared_preferences_aurora_plugin.h b/packages/shared_preferences/shared_preferences_aurora/aurora/include/shared_preferences_aurora/shared_preferences_aurora_plugin.h index c4a5afc..3f4e6d8 100644 --- a/packages/shared_preferences/shared_preferences_aurora/aurora/include/shared_preferences_aurora/shared_preferences_aurora_plugin.h +++ b/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 mapping; diff --git a/packages/shared_preferences/shared_preferences_aurora/aurora/shared_preferences_aurora_plugin.cpp b/packages/shared_preferences/shared_preferences_aurora/aurora/shared_preferences_aurora_plugin.cpp index dd7f3c5..348cf72 100644 --- a/packages/shared_preferences/shared_preferences_aurora/aurora/shared_preferences_aurora_plugin.cpp +++ b/packages/shared_preferences/shared_preferences_aurora/aurora/shared_preferences_aurora_plugin.cpp @@ -2,25 +2,37 @@ #include #include #include -#include #include +#include + +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 ({ - {"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 ®is 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 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 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 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); -} +} \ No newline at end of file diff --git a/packages/shared_preferences/shared_preferences_aurora/data/preview.png b/packages/shared_preferences/shared_preferences_aurora/data/preview.png new file mode 100644 index 0000000..c6f0708 Binary files /dev/null and b/packages/shared_preferences/shared_preferences_aurora/data/preview.png differ diff --git a/packages/shared_preferences/shared_preferences_aurora/example/lib/main.dart b/packages/shared_preferences/shared_preferences_aurora/example/lib/main.dart index 0a18ab3..9a92a43 100644 --- a/packages/shared_preferences/shared_preferences_aurora/example/lib/main.dart +++ b/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 { - int _valueInt = -1; - bool _valueBool = false; - double _valueDouble = 0.0; - String _valueString = ""; - List _valueStringList = []; - + int? _counter; + bool? _repeat; + double? _decimal; + String? _action; + List? _items; String? _error; - final _sharedPreferences = SharedPreferencesAurora(); - @override void initState() { super.initState(); @@ -33,60 +30,43 @@ class _MyAppState extends State { } Future initPlatformState() async { - int valueInt = -1; - bool valueBool = false; - double valueDouble = 0.0; - String valueString = ""; - List 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', ['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? 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 { 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, ), ], diff --git a/packages/shared_preferences/shared_preferences_aurora/example/pubspec.lock b/packages/shared_preferences/shared_preferences_aurora/example/pubspec.lock index c782233..5dc00eb 100644 --- a/packages/shared_preferences/shared_preferences_aurora/example/pubspec.lock +++ b/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" diff --git a/packages/shared_preferences/shared_preferences_aurora/example/pubspec.yaml b/packages/shared_preferences/shared_preferences_aurora/example/pubspec.yaml index ceb093e..b3e56b3 100644 --- a/packages/shared_preferences/shared_preferences_aurora/example/pubspec.yaml +++ b/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 diff --git a/packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora.dart b/packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora.dart index 62c2652..6d1de90 100644 --- a/packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora.dart +++ b/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 getInt(String key, int value) { - return SharedPreferencesAuroraPlatform.instance.getInt(key, value); - } - - Future setInt(String key, int value) { - return SharedPreferencesAuroraPlatform.instance.setInt(key, value); - } + late final SharedPreferencesAuroraPlatform _api; - Future 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 setBool(String key, bool value) { - return SharedPreferencesAuroraPlatform.instance.setBool(key, value); - } + static const String _defaultPrefix = 'flutter.'; - Future getDouble(String key, double value) { - return SharedPreferencesAuroraPlatform.instance.getDouble(key, value); + @override + Future remove(String key) async { + return _api.remove(key); } - Future setDouble(String key, double value) { - return SharedPreferencesAuroraPlatform.instance.setDouble(key, value); + @override + Future 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); + } + throw PlatformException( + code: 'InvalidOperation', + message: '"$valueType" is not a supported type.'); } - Future getString(String key, String value) { - return SharedPreferencesAuroraPlatform.instance.getString(key, value); + @override + Future clear() { + return clearWithPrefix(_defaultPrefix); } - Future setString(String key, String value) { - return SharedPreferencesAuroraPlatform.instance.setString(key, value); + @override + Future clearWithPrefix(String prefix) async { + return _api.clearWithPrefix(prefix); } - Future> getStringList(String key, List value) { - return SharedPreferencesAuroraPlatform.instance.getStringList(key, value); + @override + Future> getAll() { + return getAllWithPrefix(_defaultPrefix); } - Future setStringList(String key, List value) { - return SharedPreferencesAuroraPlatform.instance.setStringList(key, value); + @override + Future> getAllWithPrefix(String prefix) async { + final Map data = await _api.getAllWithPrefix(prefix); + return data.cast(); } } diff --git a/packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora_method_channel.dart b/packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora_method_channel.dart index f88abf5..4533262 100644 --- a/packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora_method_channel.dart +++ b/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 getInt(String key, int value) async { - final result = await methodChannel.invokeMethod('getInt', { - 'key': key, - 'value': value, - }); - return result ?? value; - } - @override Future setInt(String key, int value) async { return await methodChannel.invokeMethod('setInt', { @@ -28,15 +19,6 @@ class MethodChannelSharedPreferencesAurora false; } - @override - Future getBool(String key, bool value) async { - final result = await methodChannel.invokeMethod('getBool', { - 'key': key, - 'value': value, - }); - return result ?? value; - } - @override Future setBool(String key, bool value) async { return await methodChannel.invokeMethod('setBool', { @@ -46,15 +28,6 @@ class MethodChannelSharedPreferencesAurora false; } - @override - Future getDouble(String key, double value) async { - final result = await methodChannel.invokeMethod('getDouble', { - 'key': key, - 'value': value, - }); - return result ?? value; - } - @override Future setDouble(String key, double value) async { return await methodChannel.invokeMethod('setDouble', { @@ -65,17 +38,17 @@ class MethodChannelSharedPreferencesAurora } @override - Future getString(String key, String value) async { - final result = await methodChannel.invokeMethod('getString', { - 'key': key, - 'value': value, - }); - return result ?? value; + Future setString(String key, String value) async { + return await methodChannel.invokeMethod('setString', { + 'key': key, + 'value': value, + }) ?? + false; } @override - Future setString(String key, String value) async { - return await methodChannel.invokeMethod('setString', { + Future setStringList(String key, List value) async { + return await methodChannel.invokeMethod('setStringList', { 'key': key, 'value': value, }) ?? @@ -83,23 +56,27 @@ class MethodChannelSharedPreferencesAurora } @override - Future> getStringList(String key, List value) async { - final result = await methodChannel.invokeMethod?>( - 'getStringList', - { - 'key': key, - 'value': value, - }, - ); - return result?.map((e) => e as String).toList() ?? value; + Future clearWithPrefix(String prefix) async { + return await methodChannel.invokeMethod('clearWithPrefix', { + 'prefix': prefix, + }) ?? + false; } @override - Future setStringList(String key, List value) async { - return await methodChannel.invokeMethod('setStringList', { + Future remove(String key) async { + return await methodChannel.invokeMethod('remove', { 'key': key, - 'value': value, }) ?? false; } + + @override + Future> getAllWithPrefix(String prefix) async { + return await methodChannel + .invokeMethod?>('getAllWithPrefix', { + 'prefix': prefix, + }) ?? + {}; + } } diff --git a/packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora_platform_interface.dart b/packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora_platform_interface.dart index 8574ec7..180ffd0 100644 --- a/packages/shared_preferences/shared_preferences_aurora/lib/shared_preferences_aurora_platform_interface.dart +++ b/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 getInt(String key, int value) { - throw UnimplementedError('getInt() has not been implemented.'); - } - Future setInt(String key, int value) { throw UnimplementedError('setInt() has not been implemented.'); } - Future getBool(String key, bool value) { - throw UnimplementedError('getBool() has not been implemented.'); - } - Future setBool(String key, bool value) { throw UnimplementedError('setBool() has not been implemented.'); } - Future getDouble(String key, double value) { - throw UnimplementedError('getDouble() has not been implemented.'); - } - Future setDouble(String key, double value) { throw UnimplementedError('setDouble() has not been implemented.'); } - Future getString(String key, String value) { - throw UnimplementedError('getString() has not been implemented.'); - } - Future setString(String key, String value) { throw UnimplementedError('setString() has not been implemented.'); } - Future> getStringList(String key, List value) { - throw UnimplementedError('getStringList() has not been implemented.'); - } - Future setStringList(String key, List value) { throw UnimplementedError('setStringList() has not been implemented.'); } + + Future clearWithPrefix(String prefix) { + throw UnimplementedError('onClearWithPrefix() has not been implemented.'); + } + + Future remove(String key) { + throw UnimplementedError('remove() has not been implemented.'); + } + + Future> getAllWithPrefix(String prefix) { + throw UnimplementedError('getAllWithPrefix() has not been implemented.'); + } }