Search in sources :

Example 1 with SingleSource

use of io.reactivex.SingleSource in project gh4a by slapperwan.

the class RxUtils method wrapWithProgressDialog.

public static <T> SingleTransformer<T, T> wrapWithProgressDialog(final FragmentActivity activity, @StringRes final int messageResId) {
    return new SingleTransformer<T, T>() {

        private ProgressDialogFragment mFragment;

        @Override
        public SingleSource<T> apply(Single<T> upstream) {
            return upstream.doOnSubscribe(disposable -> showDialog()).doOnError(throwable -> hideDialog()).doOnSuccess(result -> hideDialog());
        }

        private void showDialog() {
            Bundle args = new Bundle();
            args.putInt("message_res", messageResId);
            mFragment = new ProgressDialogFragment();
            mFragment.setArguments(args);
            mFragment.show(activity.getSupportFragmentManager(), "progressdialog");
        }

        private void hideDialog() {
            if (mFragment.getActivity() != null) {
                mFragment.dismissAllowingStateLoss();
            }
            mFragment = null;
        }
    };
}
Also used : CoordinatorLayout(android.support.design.widget.CoordinatorLayout) Bundle(android.os.Bundle) SingleSource(io.reactivex.SingleSource) Dialog(android.app.Dialog) StringRes(android.support.annotation.StringRes) Response(retrofit2.Response) DialogFragment(android.support.v4.app.DialogFragment) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) ArrayList(java.util.ArrayList) Page(com.meisolsson.githubsdk.model.Page) R(com.gh4a.R) Schedulers(io.reactivex.schedulers.Schedulers) ApiRequestException(com.gh4a.ApiRequestException) Predicate(io.reactivex.functions.Predicate) List(java.util.List) SingleTransformer(io.reactivex.SingleTransformer) FragmentActivity(android.support.v4.app.FragmentActivity) Function(io.reactivex.functions.Function) Snackbar(android.support.design.widget.Snackbar) SearchPage(com.meisolsson.githubsdk.model.SearchPage) Comparator(java.util.Comparator) PublishProcessor(io.reactivex.processors.PublishProcessor) BaseActivity(com.gh4a.BaseActivity) Collections(java.util.Collections) SingleTransformer(io.reactivex.SingleTransformer) Single(io.reactivex.Single) Bundle(android.os.Bundle)

Example 2 with SingleSource

use of io.reactivex.SingleSource in project developmentDependencyLibrary by MatchlessBrother.

the class BasePhotoActivity method onActivityResult.

/**
 ***********************************获取图片以及裁剪图片的处理过程**********************************
 */
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_IMAGE_CAMERA) {
        final String imgPath = compressImagePixel_AllAcion(mImgFilePath);
        if (!StringTools.isEmpty(imgPath)) {
            Intent intent = new Intent(this, PhotoCropperActivity.class);
            intent.putExtra(PhotoCropperActivity.TITLECOLOR, mTitleBgColor);
            intent.putExtra(PhotoCropperActivity.BITMAPPATH, imgPath);
            intent.putExtra(PhotoCropperActivity.ROUNDCROPPER, mIsRoundCropper);
            intent.putExtra(PhotoCropperActivity.TITLETEXTCOLOR, mTitleTextColor);
            startActivityForResult(intent, REQUEST_CODE_IMAGE_CROPPER);
        } else
            PromptBoxTools.showToast(this, "亲,获取拍照的图片失败了!\n因为在指定路径无法查找到图片哟!");
    } else if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_IMAGE_GALLERY && data != null) {
        final ArrayList<String> imgList = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
        if (imgList.size() == 1) {
            String imgPath = imgList.get(0);
            if (!StringTools.isEmpty(imgPath)) {
                File bitmapFile = new File(imgPath);
                /**
                 ******超过120KB的图片需要进行像素压缩处理*******
                 */
                if (bitmapFile.length() >= 120 * 1024)
                    imgPath = compressImagePixel_AllAcion(imgPath);
                else /**
                 ***不超过120的图片则不需要进行像素压缩处理****
                 */
                {
                    try {
                        mResultBitmapQualitySize = 100d;
                        FileInputStream fileInputStream = new FileInputStream(bitmapFile);
                        byte[] fileInputStreamSz = new byte[(int) bitmapFile.length()];
                        fileInputStream.read(fileInputStreamSz);
                        File newBitmapFile = new File(mImgFilePath);
                        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(newBitmapFile));
                        outputStream.write(fileInputStreamSz);
                        outputStream.flush();
                        outputStream.close();
                        imgPath = mImgFilePath;
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                Intent intent = new Intent(this, PhotoCropperActivity.class);
                intent.putExtra(PhotoCropperActivity.TITLECOLOR, mTitleBgColor);
                intent.putExtra(PhotoCropperActivity.BITMAPPATH, imgPath);
                intent.putExtra(PhotoCropperActivity.ROUNDCROPPER, mIsRoundCropper);
                intent.putExtra(PhotoCropperActivity.TITLETEXTCOLOR, mTitleTextColor);
                startActivityForResult(intent, REQUEST_CODE_IMAGE_CROPPER);
            } else
                PromptBoxTools.showToast(this, "亲,获取选定的图片失败了!\n因为在指定路径无法查找到图片哟!");
        } else {
            Single.just("only").flatMap(new Function<String, SingleSource<LinkedList<String>>>() {

                public SingleSource<LinkedList<String>> apply(@io.reactivex.annotations.NonNull String s) throws Exception {
                    for (int index = 0; index < imgList.size(); index++) {
                        String imgPath = imgList.get(index);
                        File bitmapFile = new File(imgPath);
                        /**
                         *******为了不破坏原有图片,我们尽量生成新图片来进行操作*******
                         */
                        /**
                         ******超过120KB的图片需要进行像素压缩以及质量压缩的处理******
                         */
                        mImgFilePath = mCachePath + File.separator + new SimpleDateFormat("yyyy_MM_dd_HH_mm").format(new Date()) + "_" + index + ".jpg";
                        if (bitmapFile.length() >= 120 * 1024)
                            imgPath = compressImageQuality_Complete(compressImagePixel_AllAcion(imgPath), (int) mResultBitmapQualitySize.doubleValue());
                        else
                            /**
                             *不超过120KB的图片我们则直接使用原图片,避免消耗多余的空间**
                             */
                            imgPath = imgPath;
                        /**
                         *至此对于图片必须的像素压缩完成
                         */
                        imgList.set(index, imgPath);
                    }
                    LinkedList<String> dataList = new LinkedList<>();
                    dataList.addAll(imgList);
                    return Single.just(dataList);
                }
            }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Consumer<LinkedList<String>>() {

                public void accept(@io.reactivex.annotations.NonNull LinkedList<String> dataList) throws Exception {
                    setOnNewImgPathListener(dataList);
                }
            });
        }
    } else if (requestCode == REQUEST_CODE_IMAGE_CROPPER && data != null) {
        String cropperBitmapPath = data.getStringExtra(PhotoCropperActivity.BITMAPPATH);
        File bitmapFile = new File(cropperBitmapPath);
        if (!StringTools.isEmpty(cropperBitmapPath) && bitmapFile.exists()) {
            /**
             **************************************超过120KB的图片需要进行质量压缩处理***************************************
             */
            if (bitmapFile.length() >= 120 * 1024)
                cropperBitmapPath = compressImageQuality_Complete(cropperBitmapPath, (int) mResultBitmapQualitySize.doubleValue());
            LinkedList<String> dataList = new LinkedList<>();
            dataList.add(cropperBitmapPath);
            setOnNewImgPathListener(dataList);
        } else
            PromptBoxTools.showToast(this, "亲,手机发生异常情况导致裁剪\n图片失败了哟!请您稍后重试!");
    }
    super.onActivityResult(requestCode, resultCode, data);
}
Also used : SingleSource(io.reactivex.SingleSource) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Intent(android.content.Intent) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Date(java.util.Date) LinkedList(java.util.LinkedList) Consumer(io.reactivex.functions.Consumer) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with SingleSource

use of io.reactivex.SingleSource in project ForPDA by RadiationX.

the class NotificationsService method sendNotification.

public void sendNotification(NotificationEvent event) {
    Log.e("kulolo", "sendNotification " + event.notifyId());
    if (notificationPreferencesHolder.getMainAvatarsEnabled()) {
        SchedulersProvider schedulers = App.get().Di().getSchedulers();
        Disposable disposable = avatarRepository.getAvatar(event.getUserId(), event.getUserNick()).flatMap((Function<String, SingleSource<Bitmap>>) s -> {
            return Single.fromCallable(() -> ImageLoader.getInstance().loadImageSync(s)).subscribeOn(schedulers.io()).observeOn(schedulers.ui());
        }).onErrorReturn(throwable -> ImageLoader.getInstance().loadImageSync("assets://av.png")).map(bitmap -> {
            if (bitmap != null) {
                Resources res = App.getContext().getResources();
                int height = (int) res.getDimension(android.R.dimen.notification_large_icon_height);
                int width = (int) res.getDimension(android.R.dimen.notification_large_icon_width);
                boolean isCircle = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
                bitmap = BitmapUtils.centerCrop(bitmap, width, height, 1.0f);
                bitmap = BitmapUtils.createAvatar(bitmap, width, height, isCircle);
            }
            return bitmap;
        }).subscribe(avatar -> sendNotification(event, avatar), Throwable::printStackTrace);
        addToDisposable(disposable);
    } else {
        sendNotification(event, null);
    }
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Context(android.content.Context) SingleSource(io.reactivex.SingleSource) NotificationCompat(androidx.core.app.NotificationCompat) Uri(android.net.Uri) Intent(android.content.Intent) BitmapUtils(forpdateam.ru.forpda.common.BitmapUtils) PendingIntent(android.app.PendingIntent) DrawableRes(androidx.annotation.DrawableRes) EventsRepository(forpdateam.ru.forpda.model.repository.events.EventsRepository) Single(io.reactivex.Single) IBinder(android.os.IBinder) SuppressLint(android.annotation.SuppressLint) Handler(android.os.Handler) Toast(android.widget.Toast) NotificationChannel(android.app.NotificationChannel) NotificationManagerCompat(androidx.core.app.NotificationManagerCompat) Build(android.os.Build) Log(android.util.Log) NotificationPreferencesHolder(forpdateam.ru.forpda.model.preferences.NotificationPreferencesHolder) NotificationManager(android.app.NotificationManager) AlarmManager(android.app.AlarmManager) Service(android.app.Service) Messenger(android.os.Messenger) ImageLoader(com.nostra13.universalimageloader.core.ImageLoader) NotificationEvent(forpdateam.ru.forpda.entity.remote.events.NotificationEvent) SchedulersProvider(forpdateam.ru.forpda.model.SchedulersProvider) List(java.util.List) MainActivity(forpdateam.ru.forpda.ui.activities.MainActivity) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) ApiUtils(forpdateam.ru.forpda.model.data.remote.api.ApiUtils) Message(android.os.Message) Bitmap(android.graphics.Bitmap) Function(io.reactivex.functions.Function) AvatarRepository(forpdateam.ru.forpda.model.repository.avatar.AvatarRepository) App(forpdateam.ru.forpda.App) R(forpdateam.ru.forpda.R) Resources(android.content.res.Resources) Function(io.reactivex.functions.Function) Bitmap(android.graphics.Bitmap) SchedulersProvider(forpdateam.ru.forpda.model.SchedulersProvider) Resources(android.content.res.Resources)

Aggregations

SingleSource (io.reactivex.SingleSource)3 Intent (android.content.Intent)2 Single (io.reactivex.Single)2 Function (io.reactivex.functions.Function)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 SuppressLint (android.annotation.SuppressLint)1 AlarmManager (android.app.AlarmManager)1 Dialog (android.app.Dialog)1 NotificationChannel (android.app.NotificationChannel)1 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Service (android.app.Service)1 Context (android.content.Context)1 Resources (android.content.res.Resources)1 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1 Build (android.os.Build)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1