Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[shared_preferences] Unhardcode Flutter prefix in platform code #2011

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ Audrius Karosevicius <[email protected]>
Lukasz Piliszczuk <[email protected]>
SoundReply Solutions GmbH <[email protected]>
Rafal Wachol <[email protected]>
Nino Uzelac <[email protected]>
4 changes: 4 additions & 0 deletions packages/shared_preferences/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.5.4

* Remove hardcoded prefix from internal platform calls

## 0.5.3+4

* Copy `List` instances when reading and writing values to prevent mutations from propagating.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ private String encodeList(List<String> list) throws IOException {
}

// Filter preferences to only those set by the flutter app.
private Map<String, Object> getAllPrefs() throws IOException {
private Map<String, Object> getAllPrefs(String prefix) throws IOException {
Map<String, ?> allPrefs = preferences.getAll();
Map<String, Object> filteredPrefs = new HashMap<>();
for (String key : allPrefs.keySet()) {
if (key.startsWith("flutter.")) {
if (key.startsWith(prefix)) {
Object value = allPrefs.get(key);
if (value instanceof String) {
String stringValue = (String) value;
Expand Down Expand Up @@ -179,13 +179,13 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
result.success(true);
break;
case "getAll":
result.success(getAllPrefs());
result.success(getAllPrefs((String) call.argument("prefix")));
return;
case "remove":
commitAsync(preferences.edit().remove(key), result);
break;
case "clear":
Set<String> keySet = getAllPrefs().keySet();
Set<String> keySet = getAllPrefs((String) call.argument("prefix")).keySet();
Editor clearEditor = preferences.edit();
for (String keyToDelete : keySet) {
clearEditor.remove(keyToDelete);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
NSDictionary *arguments = [call arguments];

if ([method isEqualToString:@"getAll"]) {
result(getAllPrefs());
NSString *prefix = arguments[@"prefix"];
result(getAllPrefs(prefix));
} else if ([method isEqualToString:@"setBool"]) {
NSString *key = arguments[@"key"];
NSNumber *value = arguments[@"value"];
Expand Down Expand Up @@ -54,7 +55,8 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
result(@YES);
} else if ([method isEqualToString:@"clear"]) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
for (NSString *key in getAllPrefs()) {
NSString *prefix = arguments[@"prefix"];
for (NSString *key in getAllPrefs(prefix)) {
[defaults removeObjectForKey:key];
}
result(@YES);
Expand All @@ -66,13 +68,13 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {

#pragma mark - Private

static NSMutableDictionary *getAllPrefs() {
static NSMutableDictionary *getAllPrefs(NSString *prefix) {
NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
NSDictionary *prefs = [[NSUserDefaults standardUserDefaults] persistentDomainForName:appDomain];
NSMutableDictionary *filteredPrefs = [NSMutableDictionary dictionary];
if (prefs != nil) {
for (NSString *candidateKey in prefs) {
if ([candidateKey hasPrefix:@"flutter."]) {
if ([candidateKey hasPrefix:prefix]) {
[filteredPrefs setObject:prefs[candidateKey] forKey:candidateKey];
}
}
Expand Down
10 changes: 8 additions & 2 deletions packages/shared_preferences/lib/shared_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,11 @@ class SharedPreferences {

/// Completes with true once the user preferences for the app has been cleared.
Future<bool> clear() async {
final Map<String, dynamic> params = <String, dynamic>{
'prefix': '$_prefix',
};
_preferenceCache.clear();
return await _kChannel.invokeMethod<bool>('clear');
return await _kChannel.invokeMethod<bool>('clear', params);
}

/// Fetches the latest values from the host platform.
Expand All @@ -164,8 +167,11 @@ class SharedPreferences {
}

static Future<Map<String, Object>> _getSharedPreferencesMap() async {
final Map<String, dynamic> params = <String, dynamic>{
'prefix': '$_prefix',
};
final Map<String, Object> fromSystem =
await _kChannel.invokeMapMethod<String, Object>('getAll');
await _kChannel.invokeMapMethod<String, Object>('getAll', params);
assert(fromSystem != null);
// Strip the flutter. prefix from the returned preferences.
final Map<String, Object> preferencesMap = <String, Object>{};
Expand Down
2 changes: 1 addition & 1 deletion packages/shared_preferences/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Flutter plugin for reading and writing simple key-value pairs.
Wraps NSUserDefaults on iOS and SharedPreferences on Android.
author: Flutter Team <[email protected]>
homepage: https://github.com/flutter/plugins/tree/master/packages/shared_preferences
version: 0.5.3+4
version: 0.5.4

flutter:
plugin:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ void main() {
expect(preferences.getInt('int'), null);
expect(preferences.getDouble('double'), null);
expect(preferences.getStringList('List'), null);
expect(log, <Matcher>[isMethodCall('clear', arguments: null)]);
expect(log, <Matcher>[
isMethodCall('clear', arguments: <String, dynamic>{
'prefix': 'flutter.',
})
]);
});

test('reloading', () async {
Expand Down