Search in sources :

Example 1 with ShareBean

use of com.mnnyang.gzuclassschedule.data.beanv2.ShareBean in project GzuClassSchedule by mnnyang.

the class HomePresenter method createShare.

/**
 * 建立分享
 */
@Override
public void createShare(final long groupId, final String groupName) {
    mView.showLoading("建立分享中");
    Observable.create(new ObservableOnSubscribe<ShareBean>() {

        @Override
        public void subscribe(final ObservableEmitter<ShareBean> emitter) throws Exception {
            List<CourseV2> list = Cache.instance().getCourseV2Dao().queryBuilder().where(CourseV2Dao.Properties.CouCgId.eq(groupId)).where(CourseV2Dao.Properties.CouDeleted.eq(false)).list();
            if (list != null && !list.isEmpty()) {
                String json = buildJsonOfGroups(list, groupName);
                MyHttpUtils utils = new MyHttpUtils();
                utils.uploadShare(json, new HttpCallback<ShareBean>() {

                    @Override
                    public void onSuccess(ShareBean bean) {
                        emitter.onNext(bean);
                    }

                    @Override
                    public void onFail(String errMsg) {
                        emitter.onError(new Exception(errMsg));
                    }
                });
            } else {
                emitter.onError(new Exception("该课表没有课程"));
            }
        }
    }).map(new Function<ShareBean, Bitmap>() {

        @Override
        public Bitmap apply(ShareBean bean) throws Exception {
            if (bean == null || TextUtils.isEmpty(bean.getData())) {
                return null;
            }
            Bitmap logo = BitmapFactory.decodeResource(Cache.instance().getContext().getResources(), R.mipmap.ic_launcher_round);
            final int width = ScreenUtils.dp2px(150);
            String content = Url.URL_SHARE + "?id=" + bean.getData();
            return new QRCode().makeQRCodeImage(content, width, width, logo);
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Bitmap>() {

        @Override
        public void onSubscribe(Disposable d) {
        }

        @Override
        public void onNext(Bitmap bitmap) {
            if (!mView.isActive()) {
                return;
            }
            mView.stopLoading();
            if (bitmap == null) {
                mView.showMassage("分享失败!");
                return;
            }
            mView.createQRCodeSucceed(bitmap);
        }

        @Override
        public void onError(Throwable e) {
            if (!mView.isActive()) {
                return;
            }
            mView.stopLoading();
            mView.showMassage(e.getMessage());
        }

        @Override
        public void onComplete() {
        }
    });
}
Also used : MyHttpUtils(com.mnnyang.gzuclassschedule.data.http.MyHttpUtils) Disposable(io.reactivex.disposables.Disposable) HttpCallback(com.mnnyang.gzuclassschedule.data.http.HttpCallback) JSONException(org.json.JSONException) Function(io.reactivex.functions.Function) ShareBean(com.mnnyang.gzuclassschedule.data.beanv2.ShareBean) Bitmap(android.graphics.Bitmap) QRCode(com.mnnyang.gzuclassschedule.utils.spec.QRCode) List(java.util.List)

Aggregations

Bitmap (android.graphics.Bitmap)1 ShareBean (com.mnnyang.gzuclassschedule.data.beanv2.ShareBean)1 HttpCallback (com.mnnyang.gzuclassschedule.data.http.HttpCallback)1 MyHttpUtils (com.mnnyang.gzuclassschedule.data.http.MyHttpUtils)1 QRCode (com.mnnyang.gzuclassschedule.utils.spec.QRCode)1 Disposable (io.reactivex.disposables.Disposable)1 Function (io.reactivex.functions.Function)1 List (java.util.List)1 JSONException (org.json.JSONException)1