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);
}
});
});
}
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);
}
});
}
});
}
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("清除缓存失败!");
}
});
}
});
}
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();
}
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("下载完成");
}
});
}
Aggregations