Search in sources :

Example 1 with FAVORITE_PHOTOS

use of gov.whitehouse.util.FavoritesUtils.FAVORITE_PHOTOS 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

Activity (android.app.Activity)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 Nullable (android.support.annotation.Nullable)1 Fragment (android.support.v4.app.Fragment)1 FragmentManager (android.support.v4.app.FragmentManager)1 MenuItemCompat (android.support.v4.view.MenuItemCompat)1 PagerAdapter (android.support.v4.view.PagerAdapter)1 ViewPager (android.support.v4.view.ViewPager)1 ShareActionProvider (android.support.v7.widget.ShareActionProvider)1 Toolbar (android.support.v7.widget.Toolbar)1 LayoutInflater (android.view.LayoutInflater)1 Menu (android.view.Menu)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 ProgressBar (android.widget.ProgressBar)1 TextView (android.widget.TextView)1 Toast (android.widget.Toast)1 ButterKnife (butterknife.ButterKnife)1