-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[shared_preferences] update List<String> encode/decode #8335
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
Changes from all commits
e772224
089d11d
29f3662
c0c08df
2ef0a01
bad5268
7a013b1
2c10fb7
614cbe4
e622f04
d317354
79ebe43
3a45c25
a49fb9f
e1f8fab
96d129b
e89421c
88ce597
7e28547
1cdce8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| ## 2.4.3 | ||
|
|
||
| * Migrates `List<String>` value encoding to JSON. | ||
|
|
||
| ## 2.4.2 | ||
|
|
||
| * Bumps gradle-plugin to 2.1.0. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,18 +25,22 @@ | |
| 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"; | ||
| // All identifiers must match the SharedPreferencesPlugin.kt file, as well as the strings.dart file. | ||
| private static final String LIST_IDENTIFIER = "VGhpcyBpcyB0aGUgcHJlZml4IGZvciBhIGxpc3Qu"; | ||
| // The symbol `!` was chosen as it cannot be created by the base 64 encoding used with LIST_IDENTIFIER. | ||
| private static final String JSON_LIST_IDENTIFIER = LIST_IDENTIFIER + "!"; | ||
| 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()); | ||
|
|
@@ -100,7 +104,15 @@ public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding bin | |
| } | ||
|
|
||
| @Override | ||
| public @NonNull Boolean setStringList(@NonNull String key, @NonNull List<String> value) | ||
| public @NonNull Boolean setEncodedStringList(@NonNull String key, @NonNull String value) | ||
| throws RuntimeException { | ||
| return preferences.edit().putString(key, value).commit(); | ||
| } | ||
|
|
||
| // Deprecated, for testing purposes only. | ||
tarrinneal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @Deprecated | ||
| @Override | ||
| public @NonNull Boolean setDeprecatedStringList(@NonNull String key, @NonNull List<String> value) | ||
| throws RuntimeException { | ||
| return preferences.edit().putString(key, LIST_IDENTIFIER + listEncoder.encode(value)).commit(); | ||
| } | ||
|
|
@@ -131,14 +143,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)))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What was requireNonNull needed here? Also this code is getting a bit difficult to parse. (A loop with a conditional that sets some transformed filtered data) Consider breaking parts out into methods or adding comments.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a change suggested by a warning I was getting in Android Studio. Since the method being called requires nonnull arguments and getting a value from a map doesn't programmatically guarantee a value. In this context, there would always be a value though. |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -149,7 +160,13 @@ private Object transformPref(@NonNull String key, @NonNull Object value) { | |
| if (value instanceof String) { | ||
| String stringValue = (String) value; | ||
| if (stringValue.startsWith(LIST_IDENTIFIER)) { | ||
| return listEncoder.decode(stringValue.substring(LIST_IDENTIFIER.length())); | ||
| // The JSON-encoded lists use an extended prefix to distinguish them from | ||
| // lists that are encoded on the platform. | ||
| if (stringValue.startsWith(JSON_LIST_IDENTIFIER)) { | ||
| return value; | ||
| } else { | ||
| 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 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.