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);
}
}));
}
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);
}
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();
}
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);
}));
}
}
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;
}
Aggregations