Preferences DataStore------JAVA

DataStore  |  Android 开发者  |  Android Developers

本文做个demo



public class TestDataStore {
    private static TestDataStore instance;
    private RxDataStore<Preferences> dataStore;

    public static TestDataStore getInstance() {
        if (instance == null) {
            synchronized (YshpDataStore.class) {
                if (instance == null) {
                    instance = new TestDataStore();
                }
            }
        }
        return instance;
    }

    private TestDataStore() {
        dataStore = new RxPreferenceDataStoreBuilder(BaseApplication.getContext(), /*name=*/ "settings").build();
    }

    public void getIntValue(String key, GetResult<Integer> result) {
        Preferences.Key<Integer> IntKey = PreferencesKeys.intKey(key);
        Flowable<Integer> exampleCounterFlow = dataStore.data().map(prefs -> prefs.get(IntKey));
        final DisponseHandler disponseHandler = new DisponseHandler();

        disponseHandler.disposable = exampleCounterFlow
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Throwable {
                        result.onSuccess(integer);
                        if (disponseHandler.disposable != null && !disponseHandler.disposable.isDisposed()){
                            disponseHandler.disposable.dispose();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {
                        result.onFail();
                        if (disponseHandler.disposable != null && !disponseHandler.disposable.isDisposed()){
                            disponseHandler.disposable.dispose();
                        }
                    }
                });
    }

    public int getIntValue(String key) {
        Preferences.Key<Integer> IntKey = PreferencesKeys.intKey(key);
        Flowable<Integer> exampleCounterFlow = dataStore.data().map(prefs -> prefs.get(IntKey));
        return exampleCounterFlow.blockingFirst();
    }

    public void putIntValue(String key, int value) {
        Preferences.Key<Integer> IntKey = PreferencesKeys.intKey(key);
        Single<Preferences> updateResult = dataStore.updateDataAsync(prefsIn -> {
            MutablePreferences mutablePreferences = prefsIn.toMutablePreferences();
            mutablePreferences.set(IntKey, value);
            return Single.just(mutablePreferences);
        });
        updateResult.subscribe();
    }


    public  <T> T getValue(DataStoreKey key, Class<T> cls){
        Preferences.Key<String> dstKey = PreferencesKeys.stringKey(key.getKey());
        Flowable<String> currentFlow = dataStore.data().map(prefs -> prefs.get(dstKey));
        String content = currentFlow.blockingFirst();
        Gson gson = new Gson();
        T result = gson.fromJson(content, cls);
        return result;
    }


    public  <T> void getValueAsync(YshpDataStoreKey key, GetResult<T> callback, Class<T> cls){
        Preferences.Key<String> dstKey = PreferencesKeys.stringKey(key.getKey());
        Flowable<String> currentFlow = dataStore.data().map(prefs -> prefs.get(dstKey));
        final DisponseHandler disponseHandler = new DisponseHandler();
        disponseHandler.disposable = currentFlow
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String data) throws Throwable {
                        String content = currentFlow.blockingFirst();
                        Gson gson = new Gson();
                        T result = gson.fromJson(content, cls);
                        callback.onSuccess(result);
                        if (disponseHandler.disposable != null && !disponseHandler.disposable.isDisposed()){
                            disponseHandler.disposable.dispose();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Throwable {
                        callback.onFail();
                        if (disponseHandler.disposable != null && !disponseHandler.disposable.isDisposed()){
                            disponseHandler.disposable.dispose();
                        }
                    }
                });

    }


    public <T> void putValue(YshpDataStoreKey key, T value) {
        Preferences.Key<String> dstKey = PreferencesKeys.stringKey(key.getKey());
        Single<Preferences> updateResult = dataStore.updateDataAsync(prefsIn -> {
            MutablePreferences mutablePreferences = prefsIn.toMutablePreferences();
            Gson gson = new Gson();
            String content = gson.toJson(value);
            mutablePreferences.set(dstKey, content);
            return Single.just(mutablePreferences);
        });
        updateResult.subscribe();
    }


    class DisponseHandler{
        Disposable disposable;
    }


    public interface GetResult<T>{
        void onSuccess(T result);
        void onFail();
    }


}

使用

data class User(val name:String, val age:String, val sex:Sex){
    override fun toString(): String {
        return "User(name='$name', age='$age', sex=$sex)"
    }
}
public enum DataStoreKey {
    TEST_KEY("test_key");
    private String key;

    DataStoreKey(String key) {
        this.key = key;
    }

    public String getKey() {
        return key;
    }
}

保存

                TestDataStore.getInstance().putValue(DataStoreKey.TEST_KEY, user);

获取

异步
        TestDataStore.getInstance().getValueAsync(DataStoreKey.TEST_KEY, new YshpDataStore.GetResult<User>() {

            @Override
            public void onSuccess(User result) {
                binding.textView4.setText(result + "");
            }

            @Override
            public void onFail() {

            }
        }, User.class);


同步
TestDataStore.getInstance().getValue(DataStoreKey.TEST_KEY, User.class)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值