io.reactivex.MaybeOnSubscribe Java Examples

The following examples show how to use io.reactivex.MaybeOnSubscribe. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RxPermissions.java    From RuntimePermission with Apache License 2.0 6 votes vote down vote up
/**
 * use only request with an empty array to request all manifest permissions
 */
public Maybe<PermissionResult> requestAsMaybe(final List<String> permissions) {
    return Maybe.create(new MaybeOnSubscribe<PermissionResult>() {
        @Override
        public void subscribe(final MaybeEmitter<PermissionResult> emitter) throws Exception {
            runtimePermission
                    .request(permissions)
                    .onResponse(new ResponseCallback() {
                        @Override
                        public void onResponse(PermissionResult result) {
                            if (result.isAccepted()) {
                                emitter.onSuccess(result);
                            } else {
                                emitter.onError(new Error(result));
                            }
                        }
                    }).ask();
        }
    });
}
 
Example #2
Source File: ImageBrowserActivity.java    From Toutiao with Apache License 2.0 6 votes vote down vote up
private void saveImage() {
    if (ContextCompat.checkSelfPermission(mContext, Permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        requestPermission();
    } else {
        final String url = mImgList.get(mViewPager.getCurrentItem());
        Maybe.create((MaybeOnSubscribe<Boolean>) emitter -> emitter.onSuccess(DownloadUtil.saveImage(url, mContext)))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(this.bindAutoDispose())
                .subscribe(b -> {
                    String s = b ? getString(R.string.saved) : getString(R.string.error);
                    Toast.makeText(mContext, s, Toast.LENGTH_SHORT).show();
                }, ErrorAction.error());
    }
}
 
Example #3
Source File: Query.java    From sqlitemagic with Apache License 2.0 6 votes vote down vote up
/**
 * Creates {@link Maybe} that when subscribed to executes the query against a database
 * and emits query result to downstream.
 * <p>
 * The resulting stream will be empty if query result is {@code null}.
 * <dl>
 * <dt><b>Scheduler:</b></dt>
 * <dd>{@code run} does not operate by default on a particular {@link Scheduler}.</dd>
 * </dl>
 *
 * @return Deferred {@link Maybe} that when subscribed to executes the query and emits
 * its result to downstream
 * @see #runBlocking
 */
@NonNull
@CheckResult
public final Maybe<T> run() {
  return Maybe.create(new MaybeOnSubscribe<T>() {
    @Override
    public void subscribe(MaybeEmitter<T> emitter) {
      final Cursor cursor = rawQuery(true);
      if (emitter.isDisposed()) {
        if (cursor != null) {
          cursor.close();
        }
        return;
      }
      final T result = map(cursor);
      if (result != null) {
        emitter.onSuccess(result);
      } else {
        emitter.onComplete();
      }
    }
  });
}
 
Example #4
Source File: RealValueStore.java    From RxStore with Apache License 2.0 6 votes vote down vote up
@Override @NonNull public Maybe<T> get() {
  return Maybe.create(new MaybeOnSubscribe<T>() {
    @Override public void subscribe(final MaybeEmitter<T> emitter) throws Exception {
      runInReadLock(readWriteLock, new ThrowingRunnable() {
        @Override public void run() throws Exception {
          if (!file.exists()) {
            emitter.onComplete();
            return;
          }

          T value = converter.read(file, type);
          if (value == null) emitter.onComplete();
          emitter.onSuccess(value);
        }
      });
    }
  });
}
 
Example #5
Source File: SeleniumDownloader.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
@Override
public Maybe<Response> download(Request request) {

    return Maybe.create(new MaybeOnSubscribe<String>(){

        @Override
        public void subscribe(MaybeEmitter emitter) throws Exception {

            if (webDriver!=null) {
                webDriver.get(request.getUrl());

                if (Preconditions.isNotBlank(actions)) {

                    actions.forEach(
                            action-> action.perform(webDriver)
                    );
                }

                emitter.onSuccess(webDriver.getPageSource());
            }
        }
    })
    .compose(new DownloaderDelayTransformer(request))
    .map(new Function<String, Response>() {

        @Override
        public Response apply(String html) throws Exception {

            Response response = new Response();
            response.setContent(html.getBytes());
            response.setStatusCode(Constant.OK_STATUS_CODE);
            response.setContentType(getContentType(webDriver));
            return response;
        }
    });
}
 
Example #6
Source File: ImageBrowserActivity.java    From Toutiao with Apache License 2.0 5 votes vote down vote up
private void shareImage() {
    if (ContextCompat.checkSelfPermission(mContext, Permission.WRITE_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        requestPermission();
    } else {
        Maybe.create((MaybeOnSubscribe<Bitmap>) emitter -> {
            final String url = mImgList.get(mViewPager.getCurrentItem());
            Bitmap bitmap = Glide.with(mContext).asBitmap().load(url)
                    .submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                    .get();
            emitter.onSuccess(bitmap);
        })
                .subscribeOn(Schedulers.io())
                .filter(Objects::nonNull)
                .map(bitmap -> {
                    File appDir = new File(Environment.getExternalStorageDirectory(), "Toutiao");
                    if (!appDir.exists()) {
                        appDir.mkdir();
                    }
                    String fileName = "temporary_file.jpg";
                    File file = new File(appDir, fileName);
                    FileOutputStream fos = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    fos.flush();
                    fos.close();
                    return file;
                })
                .observeOn(AndroidSchedulers.mainThread())
                .as(this.bindAutoDispose())
                .subscribe(file -> IntentAction.sendImage(mContext, Uri.fromFile(file)), ErrorAction.error());
    }
}