Search in sources :

Example 6 with ObservableSource

use of io.reactivex.ObservableSource in project android-mvp-architecture by MindorksOpenSource.

the class AppDataManager method seedDatabaseQuestions.

@Override
public Observable<Boolean> seedDatabaseQuestions() {
    GsonBuilder builder = new GsonBuilder().excludeFieldsWithoutExposeAnnotation();
    final Gson gson = builder.create();
    return mDbHelper.isQuestionEmpty().concatMap(new Function<Boolean, ObservableSource<? extends Boolean>>() {

        @Override
        public ObservableSource<? extends Boolean> apply(Boolean isEmpty) throws Exception {
            if (isEmpty) {
                Type type = $Gson$Types.newParameterizedTypeWithOwner(null, List.class, Question.class);
                List<Question> questionList = gson.fromJson(CommonUtils.loadJSONFromAsset(mContext, AppConstants.SEED_DATABASE_QUESTIONS), type);
                return saveQuestionList(questionList);
            }
            return Observable.just(false);
        }
    });
}
Also used : Type(java.lang.reflect.Type) GsonBuilder(com.google.gson.GsonBuilder) ObservableSource(io.reactivex.ObservableSource) Gson(com.google.gson.Gson) List(java.util.List) Question(com.mindorks.framework.mvp.data.db.model.Question)

Example 7 with ObservableSource

use of io.reactivex.ObservableSource in project 91Pop by DanteAndroid.

the class SettingPresenter method moveOldDownloadVideoToNewDir.

@Override
public void moveOldDownloadVideoToNewDir(final String newDirPath, final QMUICommonListItemView qmuiCommonListItemView) {
    Observable.fromCallable(new Callable<File[]>() {

        @Override
        public File[] call() throws Exception {
            File file = new File(dataManager.getCustomDownloadVideoDirPath());
            return file.listFiles();
        }
    }).flatMap(new Function<File[], ObservableSource<File>>() {

        @Override
        public ObservableSource<File> apply(File[] files) throws Exception {
            return Observable.fromArray(files);
        }
    }).filter(new Predicate<File>() {

        @Override
        public boolean test(File file) throws Exception {
            return file.getName().endsWith(".mp4");
        }
    }).map(new Function<File, String>() {

        @Override
        public String apply(File file) throws Exception {
            FileUtils.move(file, new File(newDirPath, file.getName()));
            return file.getAbsolutePath();
        }
    }).delay(1, TimeUnit.SECONDS).compose(RxSchedulersHelper.<String>ioMainThread()).compose(provider.<String>bindUntilEvent(Lifecycle.Event.ON_DESTROY)).subscribe(new CallBackWrapper<String>() {

        @Override
        public void onBegin(Disposable d) {
            ifViewAttached(new ViewAction<SettingView>() {

                @Override
                public void run(@NonNull SettingView view) {
                    view.beginMoveOldDirDownloadVideoToNewDir();
                }
            });
        }

        @Override
        public void onSuccess(final String s) {
            Logger.t(TAG).d("正在移动到:" + s);
        }

        @Override
        public void onError(String msg, int code) {
            ifViewAttached(new ViewAction<SettingView>() {

                @Override
                public void run(@NonNull SettingView view) {
                    view.setNewDownloadVideoDirError("移动文件失败,无法设置新目录");
                }
            });
        }

        @Override
        public void onComplete() {
            super.onComplete();
            ifViewAttached(new ViewAction<SettingView>() {

                @Override
                public void run(@NonNull SettingView view) {
                    dataManager.setCustomDownloadVideoDirPath(newDirPath);
                    qmuiCommonListItemView.setDetailText(newDirPath);
                    view.setNewDownloadVideoDirSuccess("移动文件完成,设置新目录成功");
                }
            });
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) Callable(java.util.concurrent.Callable) Predicate(io.reactivex.functions.Predicate) ObservableSource(io.reactivex.ObservableSource) NonNull(android.support.annotation.NonNull) File(java.io.File)

Example 8 with ObservableSource

use of io.reactivex.ObservableSource in project Store by NYTimes.

the class StoreActivity method loadPosts.

@SuppressWarnings("CheckReturnValue")
public void loadPosts() {
    BarCode awwRequest = new BarCode(RedditData.class.getSimpleName(), "aww");
    /*
        First call to get(awwRequest) will use the network, then save response in the in-memory
        cache. Subsequent calls will retrieve the cached version of the data.

        But, since the policy of this store is to expire after 10 seconds, the cache will
        only be used for subsequent requests that happen within 10 seconds of the initial request.
        After that, the request will use the network.
         */
    this.nonPersistedStore.get(awwRequest).flatMapObservable(new Function<RedditData, ObservableSource<Post>>() {

        @Override
        public ObservableSource<Post> apply(@NonNull RedditData redditData) throws Exception {
            return sanitizeData(redditData);
        }
    }).toList().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(this::showPosts, throwable -> {
        Log.e(StoreActivity.class.getSimpleName(), throwable.getMessage(), throwable);
    });
}
Also used : RedditData(com.nytimes.android.sample.data.model.RedditData) ObservableSource(io.reactivex.ObservableSource) BarCode(com.nytimes.android.external.store3.base.impl.BarCode)

Aggregations

ObservableSource (io.reactivex.ObservableSource)8 List (java.util.List)4 Predicate (io.reactivex.functions.Predicate)3 ServiceFactory (app.main.wangliwei.enablehands.http.ServiceFactory)2 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 Disposable (io.reactivex.disposables.Disposable)2 Type (java.lang.reflect.Type)2 Retrofit (retrofit2.Retrofit)2 RemoteException (android.os.RemoteException)1 NonNull (android.support.annotation.NonNull)1 NewsInfo (app.main.wangliwei.enablehands.bean.NewsInfo)1 Weixin (app.main.wangliwei.enablehands.bean.Weixin)1 NewsService (app.main.wangliwei.enablehands.http.NewsService)1 WeixinService (app.main.wangliwei.enablehands.http.WeixinService)1 TypeToken (com.google.gson.reflect.TypeToken)1 Option (com.mindorks.framework.mvp.data.db.model.Option)1 Question (com.mindorks.framework.mvp.data.db.model.Question)1 BarCode (com.nytimes.android.external.store3.base.impl.BarCode)1 RedditData (com.nytimes.android.sample.data.model.RedditData)1