Search in sources :

Example 46 with Subscriber

use of rx.Subscriber in project PhotoNoter by yydcdut.

the class RxUser method saveEvernote.

public Observable<IUser> saveEvernote() {
    return Observable.create(new Observable.OnSubscribe<IUser>() {

        @Override
        public void call(Subscriber<? super IUser> subscriber) {
            if (mEvernoteSession.isLoggedIn()) {
                try {
                    User user = mEvernoteSession.getEvernoteClientFactory().getUserStoreClient().getUser();
                    mEvernoteUser = new EvernoteUser(user.getUsername());
                    SharedPreferences.Editor editor = mSharedPreferences.edit();
                    editor.putString(EVERNOTE_NAME, mEvernoteUser.getName());
                    editor.commit();
                    subscriber.onNext(mEvernoteUser);
                } catch (EDAMUserException e) {
                    YLog.e(e);
                    subscriber.onError(e);
                } catch (EDAMSystemException e) {
                    YLog.e(e);
                    subscriber.onError(e);
                } catch (TException e) {
                    YLog.e(e);
                    subscriber.onError(e);
                }
            } else {
                subscriber.onError(new RxException("没有登录"));
            }
            subscriber.onCompleted();
        }
    }).subscribeOn(Schedulers.io());
}
Also used : TException(com.evernote.thrift.TException) User(com.evernote.edam.type.User) EvernoteUser(com.yydcdut.note.entity.user.EvernoteUser) IUser(com.yydcdut.note.entity.user.IUser) QQUser(com.yydcdut.note.entity.user.QQUser) EDAMSystemException(com.evernote.edam.error.EDAMSystemException) Subscriber(rx.Subscriber) SharedPreferences(android.content.SharedPreferences) EvernoteUser(com.yydcdut.note.entity.user.EvernoteUser) IUser(com.yydcdut.note.entity.user.IUser) RxException(com.yydcdut.note.model.rx.exception.RxException) EDAMUserException(com.evernote.edam.error.EDAMUserException)

Example 47 with Subscriber

use of rx.Subscriber in project PhotoNoter by yydcdut.

the class RxGalleryPhotos method findByMedia.

@NonNull
public Observable<Map<String, MediaFolder>> findByMedia() {
    return Observable.create(new Observable.OnSubscribe<Map<String, MediaFolder>>() {

        @Override
        public void call(Subscriber<? super Map<String, MediaFolder>> subscriber) {
            if (mMediaCache == null || mMediaCache.size() == 0) {
                mMediaCache = new HashMap<>();
                MediaFolder mediaFolder4All = new MediaFolder(MediaFolder.ALL, new ArrayList<MediaPhoto>());
                mMediaCache.put(MediaFolder.ALL, mediaFolder4All);
                findInDatabase(mediaFolder4All);
            }
            subscriber.onNext(mMediaCache);
        }
    }).subscribeOn(Schedulers.io());
}
Also used : Subscriber(rx.Subscriber) MediaPhoto(com.yydcdut.note.entity.gallery.MediaPhoto) MediaFolder(com.yydcdut.note.entity.gallery.MediaFolder) HashMap(java.util.HashMap) Map(java.util.Map) NonNull(android.support.annotation.NonNull)

Example 48 with Subscriber

use of rx.Subscriber in project wh-app-android by WhiteHouse.

the class GalleryFragment method onFavoriteItemSelected.

private void onFavoriteItemSelected(boolean favorited) {
    Observable<Boolean> obsAction;
    FeedItem item = mFeedItems.get(mIndex);
    if (favorited) {
        obsAction = Observable.create((Subscriber<? super Boolean> subscriber) -> {
            try {
                FavoritesUtils.addToFavorites(getActivity(), FAVORITE_PHOTOS, item);
                mFeedItems.set(mIndex, new FeedItem.Builder(item).setFavorited(true).build());
                subscriber.onNext(true);
            } catch (Exception e) {
                Timber.w(e, "Unable to add FeedItem to favorites");
                subscriber.onError(e);
            }
            subscriber.onCompleted();
        });
        obsAction = AndroidObservable.bindFragment(this, obsAction).subscribeOn(Schedulers.io());
        bindSubscription(obsAction.subscribe(result -> {
            if (result) {
                Toast.makeText(getActivity(), R.string.added_to_favorites, Toast.LENGTH_SHORT).show();
            }
            configureOverflow(result);
        }));
    } else {
        obsAction = Observable.create((Subscriber<? super Boolean> subscriber) -> {
            try {
                FavoritesUtils.removeFromFavorites(getActivity(), FAVORITE_PHOTOS, item);
                mFeedItems.set(mIndex, new FeedItem.Builder(item).setFavorited(false).build());
                subscriber.onNext(false);
            } catch (Exception e) {
                Timber.w(e, "Unable to remove FeedItem from favorites");
                subscriber.onError(e);
            }
            subscriber.onCompleted();
        });
        obsAction = AndroidObservable.bindFragment(this, obsAction).subscribeOn(Schedulers.io());
        bindSubscription(obsAction.subscribe(result -> {
            if (!result) {
                Toast.makeText(getActivity(), R.string.removed_from_favorites, Toast.LENGTH_SHORT).show();
            }
            configureOverflow(result);
        }));
    }
}
Also used : Bundle(android.os.Bundle) ProgressBar(android.widget.ProgressBar) ButterKnife(butterknife.ButterKnife) ViewPager(android.support.v4.view.ViewPager) InjectView(butterknife.InjectView) Intent(android.content.Intent) ShareActionProvider(android.support.v7.widget.ShareActionProvider) FAVORITE_PHOTOS(gov.whitehouse.util.FavoritesUtils.FAVORITE_PHOTOS) MenuItem(android.view.MenuItem) ArrayList(java.util.ArrayList) Observable(rx.Observable) AndroidObservable(rx.android.observables.AndroidObservable) Toast(android.widget.Toast) Menu(android.view.Menu) Schedulers(rx.schedulers.Schedulers) View(android.view.View) BaseFragment(gov.whitehouse.app.BaseFragment) Icicle(icepick.Icicle) Subscriber(rx.Subscriber) FixedFragmentStatePagerAdapter(gov.whitehouse.widget.FixedFragmentStatePagerAdapter) LayoutInflater(android.view.LayoutInflater) MenuItemCompat(android.support.v4.view.MenuItemCompat) Fragment(android.support.v4.app.Fragment) PagerAdapter(android.support.v4.view.PagerAdapter) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) FavoritesUtils(gov.whitehouse.util.FavoritesUtils) FeedItem(gov.whitehouse.data.model.FeedItem) FragmentManager(android.support.v4.app.FragmentManager) List(java.util.List) TextView(android.widget.TextView) Toolbar(android.support.v7.widget.Toolbar) Nullable(android.support.annotation.Nullable) Activity(android.app.Activity) Collections(java.util.Collections) R(gov.whitehouse.R) FeedItem(gov.whitehouse.data.model.FeedItem)

Aggregations

Subscriber (rx.Subscriber)48 List (java.util.List)16 Observable (rx.Observable)15 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 Cursor (android.database.Cursor)5 ContextLife (com.yydcdut.note.injector.ContextLife)5 Map (java.util.Map)5 Inject (javax.inject.Inject)5 Subscription (rx.Subscription)5 Action0 (rx.functions.Action0)5