Skip to content

Commit

Permalink
[shared_preferences] Update to support shared_preferences 2.2.0 (flut…
Browse files Browse the repository at this point in the history
  • Loading branch information
kazakan authored Jul 27, 2023
1 parent f575fe4 commit 246d6b0
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 29 deletions.
8 changes: 7 additions & 1 deletion packages/shared_preferences/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions packages/shared_preferences/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> testList = <String>['foo', 'bar'];
const String testString = 'hello world';
const bool testBool = true;
const int testInt = 42;
const double testDouble = 3.14159;
const List<String> testList = <String>['foo', 'bar'];

const String testString2 = 'goodbye world';
const bool testBool2 = false;
const int testInt2 = 1337;
const double testDouble2 = 2.71828;
const List<String> testList2 = <String>['baz', 'quox'];
const String testString2 = 'goodbye world';
const bool testBool2 = false;
const int testInt2 = 1337;
const double testDouble2 = 2.71828;
const List<String> testList2 = <String>['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);
Expand Down Expand Up @@ -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<String> allowList = <String>{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);
});
}
2 changes: 1 addition & 1 deletion packages/shared_preferences/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
dependencies:
flutter:
sdk: flutter
shared_preferences: ^2.0.9
shared_preferences: ^2.2.0
shared_preferences_tizen:
path: ../

Expand Down
40 changes: 37 additions & 3 deletions packages/shared_preferences/lib/shared_preferences_tizen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand All @@ -16,6 +17,8 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform {
SharedPreferencesStorePlatform.instance = SharedPreferencesPlugin();
}

static const String _defaultPrefix = 'flutter.';

static Map<String, Object>? _cachedPreferences;
static const String _separator = '␞';

Expand Down Expand Up @@ -77,13 +80,44 @@ class SharedPreferencesPlugin extends SharedPreferencesStorePlatform {

@override
Future<bool> clear() async {
_preferences.clear();
return clearWithParameters(ClearParameters(
filter: PreferencesFilter(prefix: _defaultPrefix),
));
}

return tizen.preference_remove_all() == 0;
@override
Future<bool> clearWithParameters(ClearParameters parameters) async {
final PreferencesFilter filter = parameters.filter;
final List<String> keys = List<String>.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<Map<String, Object>> getAll() async => _preferences;
Future<Map<String, Object>> getAll() async {
return getAllWithParameters(
GetAllParameters(filter: PreferencesFilter(prefix: _defaultPrefix)),
);
}

@override
Future<Map<String, Object>> getAllWithParameters(
GetAllParameters parameters) async {
final PreferencesFilter filter = parameters.filter;
final Map<String, Object> withPrefix =
Map<String, Object>.from(_preferences);
withPrefix.removeWhere((String key, _) => !(key.startsWith(filter.prefix) &&
(filter.allowList?.contains(key) ?? true)));
return withPrefix;
}

@override
Future<bool> remove(String key) async {
Expand Down
4 changes: 2 additions & 2 deletions packages/shared_preferences/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

0 comments on commit 246d6b0

Please sign in to comment.