Search in sources :

Example 41 with Subscriber

use of rx.Subscriber in project mosby by sockeqwe.

the class MailProviderTest method changeLabel.

@Test
public void changeLabel() {
    mailProvider.getMail(1).subscribe(new Action1<Mail>() {

        @Override
        public void call(Mail mail) {
            final AtomicBoolean changed = new AtomicBoolean(false);
            final String newLabel = "foo";
            mailProvider.setLabel(mail.getId(), newLabel).subscribe(new Subscriber<Mail>() {

                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    Assert.fail("Error while changing label");
                }

                @Override
                public void onNext(Mail mail) {
                    Assert.assertEquals(mail.getLabel(), newLabel);
                    changed.set(true);
                }
            });
            Assert.assertTrue(changed.get());
        }
    });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Subscriber(rx.Subscriber) Test(org.junit.Test)

Example 42 with Subscriber

use of rx.Subscriber in project PhotoNoter by yydcdut.

the class SplashPresenterImpl method checkDisks.

private void checkDisks() {
    if (!mLocalStorageUtils.isFirstTime()) {
        initFiles();
        Observable.create(new Observable.OnSubscribe<File[]>() {

            @Override
            public void call(Subscriber<? super File[]> subscriber) {
                File f = new File(FilePathUtils.getPath());
                if (f.exists()) {
                    subscriber.onNext(f.listFiles());
                    subscriber.onCompleted();
                } else {
                    subscriber.onError(new RxException(""));
                }
            }
        }).flatMap(fileList -> Observable.from(fileList)).subscribeOn(Schedulers.io()).observeOn(Schedulers.computation()).filter(file1 -> !file1.isDirectory()).filter(file -> file.getName().toLowerCase().endsWith(".jpg") || file.getName().toLowerCase().endsWith(".png") || file.getName().toLowerCase().endsWith(".jpeg")).count().subscribe((fileNumber -> {
            mRxSandBox.getNumber().subscribe((integer -> {
                if (fileNumber != integer) {
                    Intent checkIntent = new Intent(mContext, CheckService.class);
                    mContext.startService(checkIntent);
                }
            }), (throwable -> YLog.e(throwable)));
        }), (throwable -> YLog.e(throwable)));
    }
}
Also used : Context(android.content.Context) Subscriber(rx.Subscriber) LocalStorageUtils(com.yydcdut.note.utils.LocalStorageUtils) Intent(android.content.Intent) ISplashPresenter(com.yydcdut.note.presenters.home.ISplashPresenter) File(java.io.File) Observable(rx.Observable) Inject(javax.inject.Inject) RxException(com.yydcdut.note.model.rx.exception.RxException) Message(android.os.Message) Permission(com.yydcdut.note.utils.permission.Permission) RxSandBox(com.yydcdut.note.model.rx.RxSandBox) Handler(android.os.Handler) ContextLife(com.yydcdut.note.injector.ContextLife) ISplashView(com.yydcdut.note.views.home.ISplashView) FilePathUtils(com.yydcdut.note.utils.FilePathUtils) Schedulers(rx.schedulers.Schedulers) CheckService(com.yydcdut.note.service.CheckService) AspectPermission(com.yydcdut.note.aspect.permission.AspectPermission) Activity(android.app.Activity) PermissionUtils(com.yydcdut.note.utils.PermissionUtils) YLog(com.yydcdut.note.utils.YLog) IView(com.yydcdut.note.views.IView) Subscriber(rx.Subscriber) RxException(com.yydcdut.note.model.rx.exception.RxException) Intent(android.content.Intent) File(java.io.File)

Example 43 with Subscriber

use of rx.Subscriber in project PhotoNoter by yydcdut.

the class AlbumPresenterImpl method savePhotosFromGallery.

@Override
public void savePhotosFromGallery(ArrayList<String> pathList) {
    List<PhotoNote> photoNotes = new ArrayList<>(pathList.size());
    for (String path : pathList) {
        PhotoNote photoNote = new PhotoNote(System.currentTimeMillis() + ".jpg", System.currentTimeMillis(), System.currentTimeMillis(), "", "", System.currentTimeMillis(), System.currentTimeMillis(), mCategoryId);
        photoNote.setTag(path);
        photoNotes.add(photoNote);
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            YLog.e(e);
        }
    }
    mRxPhotoNote.savePhotoNotes(photoNotes).flatMap(//注意看这里是photoNotes
    photoNotes1 -> Observable.from(photoNotes)).map(photoNote -> {
        String path = (String) photoNote.getTag();
        if (!TextUtils.isEmpty(path)) {
            try {
                FilePathUtils.copyFile(path, photoNote.getBigPhotoPathWithoutFile());
                FilePathUtils.saveSmallPhotoFromBigPhoto(photoNote.getBigPhotoPathWithFile(), photoNote.getPhotoName());
                photoNote.setPaletteColor(Utils.getPaletteColor(ImageLoaderManager.loadImageSync(photoNote.getBigPhotoPathWithFile())));
            } catch (IOException e) {
                YLog.e(e);
            }
        }
        return photoNote;
    }).lift(new Observable.Operator<Integer, PhotoNote>() {

        @Override
        public Subscriber<? super PhotoNote> call(Subscriber<? super Integer> subscriber) {
            return new Subscriber<PhotoNote>() {

                @Override
                public void onCompleted() {
                    subscriber.onNext(mCategoryId);
                    subscriber.onCompleted();
                }

                @Override
                public void onError(Throwable e) {
                }

                @Override
                public void onNext(PhotoNote photoNote) {
                }
            };
        }
    }).doOnSubscribe(() -> mAlbumView.showProgressBar()).subscribeOn(AndroidSchedulers.mainThread()).subscribe(integer -> {
        EventBus.getDefault().post(new PhotoNoteCreateEvent());
        mRxPhotoNote.refreshByCategoryId(mCategoryId, mAlbumSortKind).observeOn(AndroidSchedulers.mainThread()).subscribe(photoNoteList -> {
            mAlbumView.updateData(photoNoteList);
            mAlbumView.notifyDataSetChanged();
            mAlbumView.hideProgressBar();
        }, (throwable -> YLog.e(throwable)));
    }, (throwable -> YLog.e(throwable)));
}
Also used : RxPhotoNote(com.yydcdut.note.model.rx.RxPhotoNote) PhotoNote(com.yydcdut.note.entity.PhotoNote) Context(android.content.Context) RxPhotoNote(com.yydcdut.note.model.rx.RxPhotoNote) Uri(android.net.Uri) PhotoNoteDeleteEvent(com.yydcdut.note.bus.PhotoNoteDeleteEvent) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) ArrayList(java.util.ArrayList) Observable(rx.Observable) Inject(javax.inject.Inject) IAlbumView(com.yydcdut.note.views.home.IAlbumView) ContentResolver(android.content.ContentResolver) RxSandBox(com.yydcdut.note.model.rx.RxSandBox) Handler(android.os.Handler) ContextLife(com.yydcdut.note.injector.ContextLife) EventBus(org.greenrobot.eventbus.EventBus) Map(java.util.Map) Category(com.yydcdut.note.entity.Category) FilePathUtils(com.yydcdut.note.utils.FilePathUtils) R(com.yydcdut.note.R) Utils(com.yydcdut.note.utils.Utils) PermissionUtils(com.yydcdut.note.utils.PermissionUtils) Fragment(android.app.Fragment) RxCategory(com.yydcdut.note.model.rx.RxCategory) CategoryMoveEvent(com.yydcdut.note.bus.CategoryMoveEvent) Subscriber(rx.Subscriber) PhotoNoteCreateEvent(com.yydcdut.note.bus.PhotoNoteCreateEvent) LocalStorageUtils(com.yydcdut.note.utils.LocalStorageUtils) TextUtils(android.text.TextUtils) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) IAlbumPresenter(com.yydcdut.note.presenters.home.IAlbumPresenter) List(java.util.List) Permission(com.yydcdut.note.utils.permission.Permission) TreeMap(java.util.TreeMap) ComparatorFactory(com.yydcdut.note.model.compare.ComparatorFactory) ImageLoaderManager(com.yydcdut.note.utils.ImageManager.ImageLoaderManager) CategoryCreateEvent(com.yydcdut.note.bus.CategoryCreateEvent) PhotoNote(com.yydcdut.note.entity.PhotoNote) Comparator(java.util.Comparator) YLog(com.yydcdut.note.utils.YLog) IView(com.yydcdut.note.views.IView) PhotoNoteCreateEvent(com.yydcdut.note.bus.PhotoNoteCreateEvent) Subscriber(rx.Subscriber) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 44 with Subscriber

use of rx.Subscriber in project PhotoNoter by yydcdut.

the class RxCategory method updateOrder.

/**
     * 更新顺序
     *
     * @return
     */
public Observable<List<Category>> updateOrder() {
    return Observable.create(new Observable.OnSubscribe<List<Category>>() {

        @Override
        public void call(Subscriber<? super List<Category>> subscriber) {
            for (int i = 0; i < mCache.size(); i++) {
                Category category = mCache.get(i);
                category.setSort(i);
                mCategoryDB.update(category);
            }
            subscriber.onNext(mCache);
            subscriber.onCompleted();
        }
    }).subscribeOn(Schedulers.io());
}
Also used : Category(com.yydcdut.note.entity.Category) Subscriber(rx.Subscriber) List(java.util.List)

Example 45 with Subscriber

use of rx.Subscriber in project PhotoNoter by yydcdut.

the class RxFeedBack method doObservable.

public Observable<Map<String, String>> doObservable() {
    return Observable.create(new Observable.OnSubscribe<String>() {

        @Override
        public void call(Subscriber<? super String> subscriber) {
            if (TextUtils.isEmpty(mLocalStorageUtils.getUmengUid())) {
                try {
                    StringBuilder sb = new StringBuilder("http://fb.umeng.com/api/v2/user/getuid");
                    sb.append("?");
                    Iterator iterator = mDeviceInfo.keys();
                    String uid;
                    while (iterator.hasNext()) {
                        String entry = (String) iterator.next();
                        uid = mDeviceInfo.get(entry).toString();
                        sb.append(URLEncoder.encode(entry, "UTF-8") + "=" + URLEncoder.encode(uid, "UTF-8") + "&");
                    }
                    if (38 == sb.charAt(sb.length() - 1)) {
                        sb.deleteCharAt(sb.length() - 1);
                    }
                    JSONObject json = httpConnection(sb.toString());
                    if (judgeStatus(json)) {
                        uid = json.getJSONObject("data").getString("uid");
                        mLocalStorageUtils.setUmengUid(uid);
                        subscriber.onNext(uid);
                    }
                } catch (JSONException e) {
                    YLog.e(e);
                    subscriber.onError(e);
                } catch (UnsupportedEncodingException e) {
                    YLog.e(e);
                    subscriber.onError(e);
                } catch (IOException e) {
                    YLog.e(e);
                    subscriber.onError(e);
                } finally {
                    subscriber.onCompleted();
                }
            } else {
                subscriber.onNext(mLocalStorageUtils.getUmengUid());
                subscriber.onCompleted();
            }
        }
    }).subscribeOn(Schedulers.io()).lift(new Observable.Operator<JSONObject, String>() {

        @Override
        public Subscriber<? super String> call(Subscriber<? super JSONObject> subscriber) {
            return new Subscriber<String>() {

                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    subscriber.onError(e);
                }

                @Override
                public void onNext(String s) {
                    StringBuilder sb = new StringBuilder(mEmail);
                    sb.append("<---联系方式   ").append(mType).append("   反馈内容--->").append(mContent);
                    try {
                        mDeviceInfo.put("content", sb.toString());
                        mDeviceInfo.put("feedback_id", mFeedbackId);
                        mDeviceInfo.put("reply_id", System.currentTimeMillis() + "");
                        mDeviceInfo.put("device_uuid", mLocalStorageUtils.getDeviceUuid());
                        mDeviceInfo.put("type", "new_feedback");
                        mDeviceInfo.put("uid", s);
                        subscriber.onNext(mDeviceInfo);
                    } catch (JSONException e) {
                        YLog.e(e);
                        subscriber.onError(e);
                    }
                }
            };
        }
    }).lift(new Observable.Operator<JSONObject, JSONObject>() {

        @Override
        public Subscriber<? super JSONObject> call(Subscriber<? super JSONObject> subscriber) {
            return new Subscriber<JSONObject>() {

                @Override
                public void onCompleted() {
                }

                @Override
                public void onError(Throwable e) {
                    subscriber.onError(e);
                }

                @Override
                public void onNext(JSONObject jsonObject) {
                    try {
                        JSONObject json = httpConnection(jsonObject, "http://fb.umeng.com/api/v2/feedback/reply/new");
                        subscriber.onNext(json);
                    } catch (IOException e) {
                        YLog.e(e);
                        subscriber.onError(e);
                    }
                }
            };
        }
    }).map(jsonObject -> setData(jsonObject));
}
Also used : JSONException(org.json.JSONException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Observable(rx.Observable) JSONObject(org.json.JSONObject) Subscriber(rx.Subscriber) Iterator(java.util.Iterator)

Aggregations

Subscriber (rx.Subscriber)50 List (java.util.List)16 Observable (rx.Observable)16 IOException (java.io.IOException)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 Action1 (rx.functions.Action1)10 Schedulers (rx.schedulers.Schedulers)10 Context (android.content.Context)8 ArrayList (java.util.ArrayList)8 OnSubscribe (rx.Observable.OnSubscribe)8 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)7 RxException (com.yydcdut.note.model.rx.exception.RxException)7 Notification (rx.Notification)7 TestSubscriber (rx.observers.TestSubscriber)7 File (java.io.File)6 Cursor (android.database.Cursor)5 View (android.view.View)5 ContextLife (com.yydcdut.note.injector.ContextLife)5 Map (java.util.Map)5 Inject (javax.inject.Inject)5