question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose preference key to Converter

See original GitHub issue

The change from Adapter to Converter interface (#75) breaks some functionality use cases where serializing/deserializing from shared preferences depends on the preference key. For example, I have an EncryptedPreferenceAdapter that worked as follows with the previous Adapter interface:

    @Override
    public Optional<T> get(@NonNull String key, @NonNull SharedPreferences preferences) {
            final String serialized = preferences.getString(key, null);
            if (serialized == null)
                return Optional.empty();
            final String decrypted = Obfuscator.decryptFromStorage(key, serialized);
            return Optional.ofNullable(decrypted);
    }

    @Override
    public void set(@NonNull String key, @NonNull Optional<T> optionalValue, @NonNull SharedPreferences.Editor editor) {
            if (optionalValue.isPresent()) {
                editor.putString(key, Obfuscator.encryptForStorage(key, optionalValue.get()));
            }
            else {
                editor.putString(key, null);
            }
    }

Can Converters be updated to provide the preference key? Or can adapters be resurrected since there may be apps out in the wild that have adapters serializing to non-string types?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
m4xp1commented, Nov 8, 2017

I think the converter interface is very good. And your case is quite specific. Because I think that for you it will be a good option that something like this:

  public class KeyConverter implements Preference.Converter<String> {

        private final String key;

        public KeyConverter(String key) {
            this.key = key;
        }

        @Override
        public @NonNull String deserialize(@NonNull String serialized) {
            return Obfuscator.decryptFromStorage(key, serialized);
        }

        @Override
        public @NonNull String serialize(@NonNull String value) {
            return Obfuscator.encryptForStorage(key, value);
        }
    }

What say?

0reactions
f2prateekcommented, Dec 13, 2017

Gonna close this out, don’t have any plans to change this soon and I think there are a few options to make this work if needed (e.g. https://github.com/f2prateek/rx-preferences/issues/100#issuecomment-342676597).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Inspecting the View Tree – Part 1: PreferenceKey
Introducing PreferenceKey · Value: is a typealias that indicates what type of information we want to expose through the preference. · defaultValue ...
Read more >
Working with Preferences DataStore - Android Developers
DataStore is a new and improved data storage solution aimed at replacing SharedPreferences. Built on Kotlin coroutines and Flow, DataStore ...
Read more >
Add a preference key to NSViewPepresentable ...
I have an instance of a custom AppKit/UIKit view, wrapped into an **ViewRepresentable that needs to communicate some diagnostic information up ...
Read more >
IVIanuu/auto-rx-preferences: [DEPRECATED] Annotation ... - GitHub
Create a class and annotate it with the @Preferences annotation. Create some fields and annotate them with the @Key annotation. The class should...
Read more >
EXPOSE to ETH Converter | ETH to EXPOSE Calculator | Nomics
Want Daily Price Updates? · Select your delivery preferences · Verify your email · Customize your newsletter and track additional cryptocurrencies.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found