Search in sources :

Example 6 with FeedItem

use of gov.whitehouse.data.model.FeedItem in project wh-app-android by WhiteHouse.

the class FeedManager method updateFeedFromServer.

public static void updateFeedFromServer(String url, String title, String viewType) {
    final BehaviorSubject<Observable<List<FeedItem>>> subject;
    final OkHttpClient client = getClient();
    final Request request = getBaseRequest().url(url).get().build();
    if (sFeedItemsSubject.get(url) == null) {
        sFeedItemsSubject.put(url, BehaviorSubject.create());
    }
    subject = sFeedItemsSubject.get(url);
    subject.onNext(Observable.create((Subscriber<? super List<FeedItem>> op) -> {
        Response response;
        try {
            response = client.newCall(request).execute();
            if (response.code() >= 400) {
                op.onError(new IOException("Response code " + Integer.toString(response.code())));
            } else {
                SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
                FeedHandler handler = new FeedHandler(title, viewType);
                parser.parse(response.body().byteStream(), handler);
                op.onNext(handler.getFeedItems());
            }
            op.onCompleted();
        } catch (Exception e) {
            op.onError(e);
        }
    }));
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) FeedItem(gov.whitehouse.data.model.FeedItem) FeedHandler(gov.whitehouse.core.FeedHandler) Request(com.squareup.okhttp.Request) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) Observable(rx.Observable) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 7 with FeedItem

use of gov.whitehouse.data.model.FeedItem in project wh-app-android by WhiteHouse.

the class DataTest method createFeedItem.

@Test
public void createFeedItem() {
    FeedItem item = generateFeedItem();
    FeedItem dupe = generateFeedItem();
    assertThat(item).isEqualTo(item);
    assertThat(item).isEqualTo(dupe);
    assertThat(item.hashCode()).isEqualTo(dupe.hashCode());
    assertThat(item.creator()).isEqualTo("John Doe");
    assertThat(item.description()).isEqualTo("A feed item by John Doe");
    assertThat(item.feedTitle()).isEqualTo("John Doe's Feed");
    assertThat(item.guid()).isEqualTo("johndoeguid-1234");
    assertThat(item.link()).isEqualTo("http://www.example.com/link/john-doe/item");
    assertThat(item.pubDate()).isEqualTo(DateUtils.parseDate(TEST_DATE));
    assertThat(item.thumbnails()).isEqualTo(Collections.emptyMap());
    assertThat(item.title()).isEqualTo("An Item");
    assertThat(item.type()).isEqualTo(FeedType.TYPE_ARTICLE);
}
Also used : FeedItem(gov.whitehouse.data.model.FeedItem) Test(org.junit.Test)

Example 8 with FeedItem

use of gov.whitehouse.data.model.FeedItem in project wh-app-android by WhiteHouse.

the class DataTest method listOfFeedItemEqualsFeedItem.

@Test
public void listOfFeedItemEqualsFeedItem() {
    SoftAssertions softly = new SoftAssertions();
    FeedItem item = generateFeedItem();
    FeedItem dupe = generateFeedItem();
    List<FeedItem> list1 = new ArrayList<>(1);
    List<FeedItem> list2 = new ArrayList<>(1);
    list1.add(item);
    list2.add(item);
    softly.assertThat(list1).hasSameElementsAs(list2);
    softly.assertThat(list1).isEqualTo(list2);
    list2.clear();
    list2.add(dupe);
    softly.assertThat(list1).hasSameElementsAs(list2);
    softly.assertThat(list1).isEqualTo(list2);
    softly.assertAll();
}
Also used : FeedItem(gov.whitehouse.data.model.FeedItem) SoftAssertions(org.assertj.core.api.SoftAssertions) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 9 with FeedItem

use of gov.whitehouse.data.model.FeedItem 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)

Example 10 with FeedItem

use of gov.whitehouse.data.model.FeedItem in project wh-app-android by WhiteHouse.

the class GalleryFragment method newInstance.

public static GalleryFragment newInstance(List<FeedItem> feed, int index) {
    GalleryFragment f = new GalleryFragment();
    Bundle b = new Bundle(2);
    b.putParcelableArray(EXTRA_FEED, feed.toArray(new FeedItem[feed.size()]));
    b.putInt(EXTRA_INDEX, index);
    f.setArguments(b);
    return f;
}
Also used : FeedItem(gov.whitehouse.data.model.FeedItem) Bundle(android.os.Bundle)

Aggregations

FeedItem (gov.whitehouse.data.model.FeedItem)15 ArrayList (java.util.ArrayList)5 Bundle (android.os.Bundle)4 FavoritesMap (gov.whitehouse.data.model.FavoritesMap)4 Test (org.junit.Test)4 FileNotFoundException (java.io.FileNotFoundException)3 Intent (android.content.Intent)2 Fragment (android.support.v4.app.Fragment)2 FragmentManager (android.support.v4.app.FragmentManager)2 Gson (com.google.gson.Gson)2 BaseFragment (gov.whitehouse.app.BaseFragment)2 FeedItemData (gov.whitehouse.core.FeedItemData)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 List (java.util.List)2 Observable (rx.Observable)2 Subscriber (rx.Subscriber)2 Activity (android.app.Activity)1 Drawable (android.graphics.drawable.Drawable)1 Nullable (android.support.annotation.Nullable)1