简体   繁体   中英

How can I use dagger 2 so that I can use an abstract representation of shared preferences?

How can I use dagger 2 so that I can use an abstract representation of shared preferences instead of having to initialize the interface every time I want to use it? I have a wrapper for shared preferences in the data layer like this:

public interface SharedPrefsWrapper {

    void putBoolean(String key, boolean value);

    boolean getBoolean(String key);

    void putString(String key, String value);

    String getString(String key);

    void putInt(String key, int value);

    int getInt(String  key);

    void putFloat(String key, float value);

    float getFloat(String key);

    void putLong(String key, long value);

    long getLong(String key);


}

Then I have it's implementation in the app layer:

public class SharedPreferenceImpl implements SharedPrefsWrapper {

private SharedPreferences sharedPreferences;

private Context context;
private static final String PREFS= "Wyat_prefs";

public SharedPreferenceImpl(Context context) {
    this.context = context;
}

@Override
public void putBoolean(String key, boolean value) {

}

@Override
public boolean getBoolean(String key) {
    return false;
}

@Override
public void putString(String key, String value) {

    SharedPreferences.Editor editor = context.getSharedPreferences(PREFS,0).edit();
    editor.putString(key,value);
    editor.apply();

}

@Override
public String getString(String key) {

   sharedPreferences = context.getSharedPreferences(PREFS,0);


    return sharedPreferences.getString(key,null);
}

I did the dependency injection with dagger like this:

@Provides @Singleton
SharedPreferenceImpl providesSharedPreference(WyatApplication wyatApplication){
    return new SharedPreferenceImpl(wyatApplication);
}

@Provides @Singleton
SharedPrefsWrapper providesSharedPrefsWrapper(SharedPreferenceImpl sharedPreference){
    return  sharedPreference;
}
@Singleton @Component(modules = AppModule.class)
public interface AppComponent {

WyatApplication wyatapplication();

WyatRepository wyatrepository();

SharedPrefsWrapper sharedPreference();

}

When I tried to save something in the shared preferences from the presenter I got a null pointer I am trying to save it like this:

sharedPrefsWrapper.putString("token",accessToken.getAccessToken());

so that the abstraction is used.I have checked and I can see the access token when I log it so it isn't the issue. Have I forgotten to do something or am I using the wrong approach?

EDIT: I injected the wrapper like this into the presenter

public class LoginPresenter implements Presenter<LoginView> {

   private GetAccessTokenUseCase getAccessTokenUseCase;
    private Subscription subscription;
    private String email;
    private String password;


     @Inject
     SharedPrefsWrapper sharedPrefsWrapper ;

In order to tell dagger to inject the field into the LoginPresenter class you have 2 options.

Either call inject(LoginPresenter) which tells dagger to set instance variables of the LoginPresenter class with objects provided in modules. Note that this requires you to declare this method in the component interface if there isn't one already.

Or alternatively, which is also the encouraged way when possible, annotate the constructor with @Inject and add SharedPerfsWrapper to the constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM