diff --git a/packages/shared_preferences/CHANGELOG.md b/packages/shared_preferences/CHANGELOG.md index 0a38bd785..cc8680c41 100644 --- a/packages/shared_preferences/CHANGELOG.md +++ b/packages/shared_preferences/CHANGELOG.md @@ -1,6 +1,12 @@ -## NEXT +## 2.2.0 * Increase the minimum Flutter version to 3.3. +* Update shared_preferences to 2.2.0. +* Update shared_preferences_interface to 2.3.0. +* Add `clearWithParameters` and `getAllWithParameters`. +* Update `clear` to use `clearWithParameters` +* Update `getAll` to use `getAllWithParameters` +* Update integration_test. ## 2.1.0 diff --git a/packages/shared_preferences/README.md b/packages/shared_preferences/README.md index 56f799a2f..1993a73a3 100644 --- a/packages/shared_preferences/README.md +++ b/packages/shared_preferences/README.md @@ -10,8 +10,8 @@ This package is not an _endorsed_ implementation of `shared_preferences`. Theref ```yaml dependencies: - shared_preferences: ^2.0.9 - shared_preferences_tizen: ^2.1.0 + shared_preferences: ^2.2.0 + shared_preferences_tizen: ^2.2.0 ``` Then you can import `shared_preferences` in your Dart code: diff --git a/packages/shared_preferences/example/integration_test/shared_preferences_test.dart b/packages/shared_preferences/example/integration_test/shared_preferences_test.dart index 7244efe99..443ac6fc4 100644 --- a/packages/shared_preferences/example/integration_test/shared_preferences_test.dart +++ b/packages/shared_preferences/example/integration_test/shared_preferences_test.dart @@ -9,29 +9,21 @@ import 'package:shared_preferences/shared_preferences.dart'; void main() { IntegrationTestWidgetsFlutterBinding.ensureInitialized(); - group('$SharedPreferences', () { - const String testString = 'hello world'; - const bool testBool = true; - const int testInt = 42; - const double testDouble = 3.14159; - const List testList = ['foo', 'bar']; + const String testString = 'hello world'; + const bool testBool = true; + const int testInt = 42; + const double testDouble = 3.14159; + const List testList = ['foo', 'bar']; - const String testString2 = 'goodbye world'; - const bool testBool2 = false; - const int testInt2 = 1337; - const double testDouble2 = 2.71828; - const List testList2 = ['baz', 'quox']; + const String testString2 = 'goodbye world'; + const bool testBool2 = false; + const int testInt2 = 1337; + const double testDouble2 = 2.71828; + const List testList2 = ['baz', 'quox']; - late SharedPreferences preferences; - - setUp(() async { - preferences = await SharedPreferences.getInstance(); - }); - - tearDown(() { - preferences.clear(); - }); + late SharedPreferences preferences; + void runAllTests() { testWidgets('reading', (WidgetTester _) async { expect(preferences.get('String'), isNull); expect(preferences.get('bool'), isNull); @@ -97,5 +89,77 @@ void main() { // The last write should win. expect(preferences.getInt('int'), writeCount); }); + } + + group('SharedPreferences', () { + setUp(() async { + preferences = await SharedPreferences.getInstance(); + }); + + tearDown(() async { + await preferences.clear(); + SharedPreferences.resetStatic(); + }); + + runAllTests(); + }); + + group('setPrefix', () { + setUp(() async { + SharedPreferences.resetStatic(); + SharedPreferences.setPrefix('prefix.'); + preferences = await SharedPreferences.getInstance(); + }); + + tearDown(() async { + await preferences.clear(); + SharedPreferences.resetStatic(); + }); + + runAllTests(); + }); + + group('setNoPrefix', () { + setUp(() async { + SharedPreferences.resetStatic(); + SharedPreferences.setPrefix(''); + preferences = await SharedPreferences.getInstance(); + }); + + tearDown(() async { + await preferences.clear(); + SharedPreferences.resetStatic(); + }); + + runAllTests(); + }); + + testWidgets('allowList only gets allowed items', (WidgetTester _) async { + const String allowedString = 'stringKey'; + const String allowedBool = 'boolKey'; + const String notAllowedDouble = 'doubleKey'; + const String resultString = 'resultString'; + + const Set allowList = {allowedString, allowedBool}; + + SharedPreferences.resetStatic(); + SharedPreferences.setPrefix('', allowList: allowList); + + final SharedPreferences prefs = await SharedPreferences.getInstance(); + + await prefs.setString(allowedString, resultString); + await prefs.setBool(allowedBool, true); + await prefs.setDouble(notAllowedDouble, 3.14); + + await prefs.reload(); + + final String? testString = prefs.getString(allowedString); + expect(testString, resultString); + + final bool? testBool = prefs.getBool(allowedBool); + expect(testBool, true); + + final double? testDouble = prefs.getDouble(notAllowedDouble); + expect(testDouble, null); }); } diff --git a/packages/shared_preferences/example/pubspec.yaml b/packages/shared_preferences/example/pubspec.yaml index 6bf98c195..43021b29f 100644 --- a/packages/shared_preferences/example/pubspec.yaml +++ b/packages/shared_preferences/example/pubspec.yaml @@ -9,7 +9,7 @@ environment: dependencies: flutter: sdk: flutter - shared_preferences: ^2.0.9 + shared_preferences: ^2.2.0 shared_preferences_tizen: path: ../ diff --git a/packages/shared_preferences/lib/shared_preferences_tizen.dart b/packages/shared_preferences/lib/shared_preferences_tizen.dart index 95f976561..8dd4e47ab 100644 --- a/packages/shared_preferences/lib/shared_preferences_tizen.dart +++ b/packages/shared_preferences/lib/shared_preferences_tizen.dart @@ -7,6 +7,7 @@ import 'dart:ffi'; import 'package:ffi/ffi.dart'; import 'package:shared_preferences_platform_interface/shared_preferences_platform_interface.dart'; +import 'package:shared_preferences_platform_interface/types.dart'; import 'package:tizen_interop/4.0/tizen.dart'; /// The Tizen implementation of [SharedPreferencesStorePlatform]. @@ -16,6 +17,8 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform { SharedPreferencesStorePlatform.instance = SharedPreferencesPlugin(); } + static const String _defaultPrefix = 'flutter.'; + static Map? _cachedPreferences; static const String _separator = '␞'; @@ -77,13 +80,44 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform { @override Future clear() async { - _preferences.clear(); + return clearWithParameters(ClearParameters( + filter: PreferencesFilter(prefix: _defaultPrefix), + )); + } - return tizen.preference_remove_all() == 0; + @override + Future clearWithParameters(ClearParameters parameters) async { + final PreferencesFilter filter = parameters.filter; + final List keys = List.of(_preferences.keys); + + for (final String key in keys) { + if (key.startsWith(filter.prefix) && + (filter.allowList == null || filter.allowList!.contains(key))) { + if (!(await remove(key))) { + return false; + } + } + } + return true; } @override - Future> getAll() async => _preferences; + Future> getAll() async { + return getAllWithParameters( + GetAllParameters(filter: PreferencesFilter(prefix: _defaultPrefix)), + ); + } + + @override + Future> getAllWithParameters( + GetAllParameters parameters) async { + final PreferencesFilter filter = parameters.filter; + final Map withPrefix = + Map.from(_preferences); + withPrefix.removeWhere((String key, _) => !(key.startsWith(filter.prefix) && + (filter.allowList?.contains(key) ?? true))); + return withPrefix; + } @override Future remove(String key) async { diff --git a/packages/shared_preferences/pubspec.yaml b/packages/shared_preferences/pubspec.yaml index f3927cb32..3d3ff391b 100644 --- a/packages/shared_preferences/pubspec.yaml +++ b/packages/shared_preferences/pubspec.yaml @@ -2,7 +2,7 @@ name: shared_preferences_tizen description: Tizen implementation of the shared_preferences plugin. homepage: https://github.com/flutter-tizen/plugins repository: https://github.com/flutter-tizen/plugins/tree/master/packages/shared_preferences -version: 2.1.0 +version: 2.2.0 environment: sdk: ">=2.18.0 <4.0.0" @@ -18,5 +18,5 @@ dependencies: ffi: ^2.0.1 flutter: sdk: flutter - shared_preferences_platform_interface: ^2.0.0 + shared_preferences_platform_interface: ^2.3.0 tizen_interop: ^0.2.0