Search in sources :

Example 46 with ObservableOnSubscribe

use of io.reactivex.ObservableOnSubscribe in project MusicLake by caiyonglong.

the class NeteaseApiServiceImpl method getTopMusicList.

public static Observable<List<Music>> getTopMusicList(int ids) {
    return getApiService().getTopList(ids).flatMap(topList -> {
        List<Music> musicList = new ArrayList<>();
        for (NeteaseMusic songInfo : topList.getResult().getTracks()) {
            Music music = new Music();
            music.setType(Music.Type.NETEASE);
            music.setOnline(true);
            music.setId(String.valueOf(songInfo.getId()));
            music.setAlbum(songInfo.getAlbum().getName());
            music.setAlbumId(songInfo.getAlbum().getId());
            music.setArtist(songInfo.getArtists().get(0).getName());
            // music.setArtistId(songInfo.getArtists().get(0).getId());
            music.setTitle(songInfo.getName());
            music.setCoverSmall(songInfo.getAlbum().getPicUrl());
            music.setCoverUri(songInfo.getAlbum().getBlurPicUrl());
            music.setCoverBig(songInfo.getAlbum().getPicUrl());
            musicList.add(music);
        }
        return Observable.create((ObservableOnSubscribe<List<Music>>) e -> {
            try {
                e.onNext(musicList);
                e.onComplete();
            } catch (Exception ep) {
                e.onError(ep);
            }
        });
    });
}
Also used : List(java.util.List) Music(com.cyl.musiclake.bean.Music) LogUtil(com.cyl.musiclake.utils.LogUtil) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) Map(java.util.Map) HashMap(java.util.HashMap) Observable(io.reactivex.Observable) ApiManager(com.cyl.musiclake.net.ApiManager) ArrayList(java.util.ArrayList) Music(com.cyl.musiclake.bean.Music) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 47 with ObservableOnSubscribe

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

the class FavoritePresenter method exportData.

@Override
public void exportData(final boolean onlyUrl) {
    Observable.create(new ObservableOnSubscribe<List<UnLimit91PornItem>>() {

        @Override
        public void subscribe(ObservableEmitter<List<UnLimit91PornItem>> e) throws Exception {
            List<UnLimit91PornItem> unLimit91PornItems = dataManager.loadAllLimit91PornItems();
            e.onNext(unLimit91PornItems);
            e.onComplete();
        }
    }).map(new Function<List<UnLimit91PornItem>, String>() {

        @Override
        public String apply(List<UnLimit91PornItem> unLimit91PornItems) throws Exception {
            File file = new File(SDCardUtils.EXPORT_FILE);
            if (file.exists()) {
                if (!file.delete()) {
                    throw new Exception("导出失败,因为删除原文件失败了");
                }
            }
            if (!file.createNewFile()) {
                throw new Exception("导出失败,创建新文件失败了");
            }
            if (onlyUrl) {
                for (UnLimit91PornItem unLimit91PornItem : unLimit91PornItems) {
                    CharSequence data = unLimit91PornItem.getVideoResult().getVideoUrl() + "\r\n\r\n";
                    if (TextUtils.isEmpty(data)) {
                        continue;
                    }
                    FileUtils.writeChars(file, "UTF-8", data);
                }
            } else {
                for (UnLimit91PornItem unLimit91PornItem : unLimit91PornItems) {
                    String title = unLimit91PornItem.getTitle();
                    String videoUrl = unLimit91PornItem.getVideoResult().getVideoUrl();
                    CharSequence data = title + "\r\n" + videoUrl + "\r\n\r\n";
                    if (TextUtils.isEmpty(data)) {
                        continue;
                    }
                    FileUtils.writeChars(file, "UTF-8", data);
                }
            }
            return "导出成功";
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).compose(provider.<String>bindUntilEvent(Lifecycle.Event.ON_STOP)).subscribe(new CallBackWrapper<String>() {

        @Override
        public void onBegin(Disposable d) {
        }

        @Override
        public void onSuccess(final String s) {
            ifViewAttached(new ViewAction<FavoriteView>() {

                @Override
                public void run(@NonNull FavoriteView view) {
                    view.showMessage(s, TastyToast.SUCCESS);
                }
            });
        }

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

                @Override
                public void run(@NonNull FavoriteView view) {
                    view.showMessage(msg, TastyToast.ERROR);
                }
            });
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) ApiException(com.dante.exception.ApiException) NonNull(android.support.annotation.NonNull) List(java.util.List) UnLimit91PornItem(com.dante.data.model.UnLimit91PornItem) File(java.io.File) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 48 with ObservableOnSubscribe

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

the class AboutPresenter method cleanCacheFile.

@Override
public void cleanCacheFile(final List<File> fileDirList, final Context context) {
    Observable.create(new ObservableOnSubscribe<Boolean>() {

        @Override
        public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
            boolean result = true;
            for (File fileDir : fileDirList) {
                if (fileDir.getAbsolutePath().contains("glide")) {
                    Logger.d("开始清图片缓存");
                    GlideApp.get(context).clearDiskCache();
                    result = true;
                    break;
                } else {
                    result = AppCacheUtils.cleanCacheFile(fileDir);
                }
            }
            if (result) {
                emitter.onNext(true);
                emitter.onComplete();
            } else {
                emitter.onError(new Throwable("clean cache file failure"));
            }
        }
    }).subscribeOn(Schedulers.io()).delay(1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).compose(provider.<Boolean>bindUntilEvent(Lifecycle.Event.ON_DESTROY)).subscribe(new CallBackWrapper<Boolean>() {

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

                @Override
                public void run(@NonNull AboutView view) {
                    view.showCleanDialog("清除缓存中,请稍后...");
                }
            });
        }

        @Override
        public void onSuccess(Boolean aBoolean) {
            ifViewAttached(new ViewAction<AboutView>() {

                @Override
                public void run(@NonNull AboutView view) {
                    view.cleanCacheSuccess("清除缓存成功!");
                }
            });
        }

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

                @Override
                public void run(@NonNull AboutView view) {
                    view.cleanCacheFailure("清除缓存失败!");
                }
            });
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) NonNull(android.support.annotation.NonNull) File(java.io.File) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 49 with ObservableOnSubscribe

use of io.reactivex.ObservableOnSubscribe in project RxCache by VictorAlbertos.

the class EvictExpirableRecordsPersistence method oEvictingTask.

private Observable<String> oEvictingTask() {
    Observable<String> oEvictingTask = Observable.create(new ObservableOnSubscribe<String>() {

        @Override
        public void subscribe(ObservableEmitter<String> emitter) throws Exception {
            if (!couldBeExpirableRecords) {
                emitter.onNext(Locale.RECORD_CAN_NOT_BE_EVICTED_BECAUSE_NO_ONE_IS_EXPIRABLE);
                emitter.onComplete();
                return;
            }
            int storedMB = persistence.storedMB();
            if (!reachedPercentageMemoryToStart(storedMB)) {
                emitter.onComplete();
                return;
            }
            List<String> allKeys = persistence.allKeys();
            float releasedMBSoFar = 0f;
            for (String key : allKeys) {
                if (reachedPercentageMemoryToStop(storedMB, releasedMBSoFar)) {
                    break;
                }
                Record record = persistence.retrieveRecord(key, isEncrypted, encryptKey);
                if (record == null)
                    continue;
                if (!record.getExpirable())
                    continue;
                persistence.evict(key);
                emitter.onNext(key);
                releasedMBSoFar += record.getSizeOnMb();
            }
            couldBeExpirableRecords = reachedPercentageMemoryToStop(storedMB, releasedMBSoFar);
            emitter.onComplete();
        }
    }).subscribeOn((Schedulers.io())).observeOn(Schedulers.io()).doOnError(new Consumer<Throwable>() {

        @Override
        public void accept(Throwable throwable) throws Exception {
            throwable.printStackTrace();
        }
    });
    return oEvictingTask.share();
}
Also used : ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) Record(io.rx_cache2.internal.Record) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 50 with ObservableOnSubscribe

use of io.reactivex.ObservableOnSubscribe in project okhttp-OkGo by jeasonlzy.

the class RxFileDownloadActivity method fileDownload2.

@OnClick(R.id.fileDownload2)
public void fileDownload2(View view) {
    Observable.create(new ObservableOnSubscribe<Progress>() {

        @Override
        public void subscribe(@NonNull final ObservableEmitter<Progress> e) throws Exception {
            String etString = etUrl.getText().toString();
            String url = TextUtils.isEmpty(etString) ? Urls.URL_DOWNLOAD : etString;
            // 
            OkGo.<File>get(url).headers("aaa", // 
            "111").params("bbb", // 
            "222").execute(new FileCallback() {

                @Override
                public void onSuccess(Response<File> response) {
                    e.onComplete();
                }

                @Override
                public void onError(Response<File> response) {
                    e.onError(response.getException());
                }

                @Override
                public void downloadProgress(Progress progress) {
                    e.onNext(progress);
                }
            });
        }
    }).doOnSubscribe(new Consumer<Disposable>() {

        @Override
        public void accept(@NonNull Disposable disposable) throws Exception {
            btnFileDownload2.setText("正在下载中...");
        }
    }).observeOn(// 
    AndroidSchedulers.mainThread()).subscribe(new Observer<Progress>() {

        @Override
        public void onSubscribe(@NonNull Disposable d) {
            addDisposable(d);
        }

        @Override
        public void onNext(@NonNull Progress progress) {
            String downloadLength = Formatter.formatFileSize(getApplicationContext(), progress.currentSize);
            String totalLength = Formatter.formatFileSize(getApplicationContext(), progress.totalSize);
            tvDownloadSize.setText(downloadLength + "/" + totalLength);
            String speed = Formatter.formatFileSize(getApplicationContext(), progress.speed);
            tvNetSpeed.setText(String.format("%s/s", speed));
            tvProgress.setText(numberFormat.format(progress.fraction));
            pbProgress.setMax(10000);
            pbProgress.setProgress((int) (progress.fraction * 10000));
        }

        @Override
        public void onError(@NonNull Throwable e) {
            e.printStackTrace();
            btnFileDownload2.setText("下载出错");
            showToast(e.getMessage());
        }

        @Override
        public void onComplete() {
            btnFileDownload2.setText("下载完成");
        }
    });
}
Also used : Disposable(io.reactivex.disposables.Disposable) Progress(com.lzy.okgo.model.Progress) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) FileCallback(com.lzy.okgo.callback.FileCallback) NonNull(io.reactivex.annotations.NonNull) File(java.io.File) ObservableEmitter(io.reactivex.ObservableEmitter) OnClick(butterknife.OnClick)

Aggregations

ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)58 ObservableEmitter (io.reactivex.ObservableEmitter)48 Disposable (io.reactivex.disposables.Disposable)34 Observable (io.reactivex.Observable)13 ArrayList (java.util.ArrayList)12 List (java.util.List)11 SupportSQLiteDatabase (androidx.sqlite.db.SupportSQLiteDatabase)9 File (java.io.File)8 IOException (java.io.IOException)8 Intent (android.content.Intent)6 Bundle (android.os.Bundle)6 Consumer (io.reactivex.functions.Consumer)6 Bitmap (android.graphics.Bitmap)5 Uri (android.net.Uri)5 Observer (io.reactivex.Observer)5 NonNull (io.reactivex.annotations.NonNull)5 HashMap (java.util.HashMap)5 Log (android.util.Log)4 View (android.view.View)4 BindView (butterknife.BindView)4