使用Dagger 2和RxJava2包装SharedPreferences(Wrapping SharedPreferences with Dagger 2 and RxJava2)

编程入门 行业动态 更新时间:2024-10-14 16:21:29
使用Dagger 2和RxJava2包装SharedPreferences(Wrapping SharedPreferences with Dagger 2 and RxJava2)

我是否需要包装Android SharedPreferences类? 如果是的话,能否请您提供一个简单的工作示例?

我知道如何使用SharedPreferences ,但是当它包装它并提供Dagger 2和RxJava2时,我很困惑。

Do I need to wrap the Android SharedPreferences class or not? If yes, can you please provide me a simple working example?

I know how to use the SharedPreferences, but when it comes to wrapping it and providing it with Dagger 2 and RxJava2 I am confused.

最满意答案

我通常只使用名为LocalStorage的接口或类似的东西来包装它。 然后将一个Context注入实现并像往常一样实现SharedPreferences。 如果你想使用Rx,只需确保你的接口方法返回Observables 。

然后,只要你需要使用SharedPeferences,只需注入一个LocalStorage就可以了。

编辑:我不知道你需要多少代码,但这是一个示例。

首先定义一个接口

public interface LocalStorage { void writeMessage(String message); Observable<String> readMessage(); }

然后使用SharedPreferences编写此接口的实现。 那么我们需要做些什么呢? 好吧,我们真正需要的只是一个Context ,但是我们不用担心,当我们在dagger 2模块中创建LoginStorage时,我们将通过构造函数传递一个。

public class SharedPrefStorage implements LocalStorage { private Context context; public SharedPrefStorage(Context context) { this.context = context; } @Override public void writeMessage(String message) { context.getSharedPreferences("sharedprefs", Context.MODE_PRIVATE) .edit().putString("myMessage", message).apply(); } @Override public Observable<String> readMessage() { return Observable.fromCallable(new Callable<String>() { @Override public String call() throws Exception { return context.getSharedPreferences("sharedprefs", Context.MODE_PRIVATE) .getString("myMessage", ""); } } }); }

如你所见,我们只是像往常一样读取和写入值,没什么特别的。 现在我们将它添加到我们的Dagger 2 AppModule(或者你决定称之为的任何东西)。 我们已经知道我们需要一个上下文来使SharedPrefStorage工作,所以把它作为一个参数,dagger会注入它(前提是你有一个带有上下文的@Provides方法,例如你的应用程序类)。

@Module public class AppModule { private MyApplication app; public AppModule(MyApplication app) { this.app = app; } @Provides @Singleton public MyApplication provideApp() { return app; } @Provides @Singleton public LocalStorage provideLocalStorage(MyApplication context) return new SharedPrefStorage(context); } }

然后,当然,让我们将它添加到我们的Dagger 2组件中,以便我们实际公开LocalStorage并将其注入我们想要的任何地方。

@Singleton @Component(modules = AppModule.class) public interface AppComponent { LocalStorage localStorage(); }

现在我们可以在需要的时候注入LocalStorage。 我希望这可以解决问题。

I usually just wrap it using an interface called LocalStorage or something similar. Then inject a Context into the implementation and implement your SharedPreferences as usual. If you want to use Rx just make sure your interface methods return Observables.

Then whenever you need to use SharedPeferences just have a LocalStorage injected and you're all set.

Edit: Im not sure how much code you need but here's a sample.

Start off by defining an interface

public interface LocalStorage { void writeMessage(String message); Observable<String> readMessage(); }

Then write the implementation of this interface using SharedPreferences. So what do we need for this to work? Well, all we really need is a Context, but let's not worry about that, we will pass one through the constructor when we create the LoginStorage in the dagger 2 module.

public class SharedPrefStorage implements LocalStorage { private Context context; public SharedPrefStorage(Context context) { this.context = context; } @Override public void writeMessage(String message) { context.getSharedPreferences("sharedprefs", Context.MODE_PRIVATE) .edit().putString("myMessage", message).apply(); } @Override public Observable<String> readMessage() { return Observable.fromCallable(new Callable<String>() { @Override public String call() throws Exception { return context.getSharedPreferences("sharedprefs", Context.MODE_PRIVATE) .getString("myMessage", ""); } } }); }

As you see we simply read and write the values as usual, nothing fancy. Now we just add this to our Dagger 2 AppModule (or whatever you decided to call it). We already know that we will need a Context for the SharedPrefStorage to work, so put it as a parameter and dagger will inject it (provided you have a @Provides method with a context, for example your application class).

@Module public class AppModule { private MyApplication app; public AppModule(MyApplication app) { this.app = app; } @Provides @Singleton public MyApplication provideApp() { return app; } @Provides @Singleton public LocalStorage provideLocalStorage(MyApplication context) return new SharedPrefStorage(context); } }

And then, of course, let's add it to our Dagger 2 component so that we actually expose the LocalStorage and can have it injected wherever we want it.

@Singleton @Component(modules = AppModule.class) public interface AppComponent { LocalStorage localStorage(); }

Now we can just have our LocalStorage injected whenever we need one. I hope this clears things up.

更多推荐

本文发布于:2023-08-07 07:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463019.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Dagger   Wrapping   SharedPreferences

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!