Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[shared_preferences] update List<String> encode/decode and other simple breaking changes #8335

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 3.0.0

* Updates non-supported type errors to `ArgumentError`.
* Removes support for reading `BigInt` and `Set<String>`.
* Moves `List<String>` encoding to dart `JSON` package.

## 2.4.0

* Adds `SharedPreferences` support within `SharedPreferencesAsyncAndroid` API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/** LegacySharedPreferencesPlugin */
public class LegacySharedPreferencesPlugin implements FlutterPlugin, SharedPreferencesApi {
private static final String TAG = "SharedPreferencesPlugin";
private static final String SHARED_PREFERENCES_NAME = "FlutterSharedPreferences";
private static final String JSON_LIST_IDENTIFIER = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBhIGxpc3Qu!";
private static final String LIST_IDENTIFIER = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBhIGxpc3Qu";
private static final String BIG_INTEGER_PREFIX = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBCaWdJbnRlZ2Vy";
private static final String DOUBLE_PREFIX = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBEb3VibGUu";

private SharedPreferences preferences;
private SharedPreferencesListEncoder listEncoder;
private final SharedPreferencesListEncoder listEncoder;

public LegacySharedPreferencesPlugin() {
this(new ListEncoder());
Expand Down Expand Up @@ -73,13 +73,6 @@ public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding bin

@Override
public @NonNull Boolean setString(@NonNull String key, @NonNull String value) {
// TODO (tarrinneal): Move this string prefix checking logic to dart code and make it an Argument Error.
if (value.startsWith(LIST_IDENTIFIER)
|| value.startsWith(BIG_INTEGER_PREFIX)
|| value.startsWith(DOUBLE_PREFIX)) {
throw new RuntimeException(
"StorageError: This string cannot be stored as it clashes with special identifier prefixes");
}
return preferences.edit().putString(key, value).commit();
}

Expand All @@ -99,6 +92,7 @@ public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding bin
return preferences.edit().remove(key).commit();
}

// Deprecated, for testing purposes only.
@Override
public @NonNull Boolean setStringList(@NonNull String key, @NonNull List<String> value)
throws RuntimeException {
Expand Down Expand Up @@ -131,14 +125,13 @@ public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding bin

// Gets all shared preferences, filtered to only those set with the given prefix.
// Optionally filtered also to only those items in the optional [allowList].
@SuppressWarnings("unchecked")
private @NonNull Map<String, Object> getAllPrefs(
@NonNull String prefix, @Nullable Set<String> allowList) throws RuntimeException {
Map<String, ?> allPrefs = preferences.getAll();
Map<String, Object> filteredPrefs = new HashMap<>();
for (String key : allPrefs.keySet()) {
if (key.startsWith(prefix) && (allowList == null || allowList.contains(key))) {
filteredPrefs.put(key, transformPref(key, allPrefs.get(key)));
filteredPrefs.put(key, transformPref(key, Objects.requireNonNull(allPrefs.get(key))));
}
}

Expand All @@ -148,32 +141,14 @@ public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding bin
private Object transformPref(@NonNull String key, @NonNull Object value) {
if (value instanceof String) {
String stringValue = (String) value;
if (stringValue.startsWith(LIST_IDENTIFIER)) {
if (stringValue.startsWith(JSON_LIST_IDENTIFIER)) {
return value;
} else if (stringValue.startsWith(LIST_IDENTIFIER)) {
return listEncoder.decode(stringValue.substring(LIST_IDENTIFIER.length()));
} else if (stringValue.startsWith(BIG_INTEGER_PREFIX)) {
// TODO (tarrinneal): Remove all BigInt code.
// https://github.com/flutter/flutter/issues/124420
String encoded = stringValue.substring(BIG_INTEGER_PREFIX.length());
return new BigInteger(encoded, Character.MAX_RADIX);
} else if (stringValue.startsWith(DOUBLE_PREFIX)) {
String doubleStr = stringValue.substring(DOUBLE_PREFIX.length());
return Double.valueOf(doubleStr);
}
} else if (value instanceof Set) {
// TODO (tarrinneal): Remove Set code.
// https://github.com/flutter/flutter/issues/124420

// This only happens for previous usage of setStringSet. The app expects a list.
@SuppressWarnings("unchecked")
List<String> listValue = new ArrayList<>((Set<String>) value);
// Let's migrate the value too while we are at it.
preferences
.edit()
.remove(key)
.putString(key, LIST_IDENTIFIER + listEncoder.encode(listValue))
.apply();

return listValue;
}
return value;
}
Expand Down
Loading
Loading