use of gov.whitehouse.data.model.FavoritesMap in project wh-app-android by WhiteHouse.
the class FavoritesUtils method removeFromFavorites.
public static void removeFromFavorites(final Context context, final String type, final FeedItem item) {
FavoritesMap map;
try {
map = getGson().fromJson(new InputStreamReader(getInputStream(context)), FavoritesMap.class);
} catch (FileNotFoundException e) {
map = null;
}
if (map == null) {
return;
}
if (FAVORITE_ARTICLES.equals(type) && (map.articles() == null || map.articles().size() < 1)) {
return;
} else if (FAVORITE_PHOTOS.equals(type) && (map.photos() == null || map.photos().size() < 1)) {
return;
} else if (FAVORITE_VIDEOS.equals(type) && (map.videos() == null || map.videos().size() < 1)) {
return;
}
final LinkedList<FeedItem> llist;
switch(type) {
case FAVORITE_ARTICLES:
llist = new LinkedList<>(map.articles());
break;
case FAVORITE_PHOTOS:
llist = new LinkedList<>(map.photos());
break;
case FAVORITE_VIDEOS:
llist = new LinkedList<>(map.videos());
break;
default:
llist = new LinkedList<>();
break;
}
for (Iterator itr = llist.iterator(); itr.hasNext(); ) {
FeedItem i = (FeedItem) itr.next();
if (i.pubDate().equals(item.pubDate()) && i.link().equals(item.link())) {
itr.remove();
}
}
switch(type) {
case FAVORITE_ARTICLES:
map.articles().clear();
map.articles().addAll(llist);
break;
case FAVORITE_PHOTOS:
map.photos().clear();
map.photos().addAll(llist);
break;
case FAVORITE_VIDEOS:
map.videos().clear();
map.videos().addAll(llist);
break;
}
OutputStreamWriter out = new OutputStreamWriter(getOutputStream(context));
try {
out.write(getGson().toJson(map));
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
use of gov.whitehouse.data.model.FavoritesMap in project wh-app-android by WhiteHouse.
the class FavoritesUtils method addToFavorites.
public static void addToFavorites(final Context context, final String type, final FeedItem item) {
Timber.d(">> addToFavorites");
FavoritesMap map;
try {
map = getGson().fromJson(new InputStreamReader(getInputStream(context)), FavoritesMap.class);
} catch (FileNotFoundException e) {
map = null;
}
if (map == null) {
map = FavoritesMap.create(new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
}
switch(type) {
case FAVORITE_ARTICLES:
map.articles().add(item);
break;
case FAVORITE_PHOTOS:
map.photos().add(item);
break;
case FAVORITE_VIDEOS:
map.videos().add(item);
break;
}
OutputStreamWriter out = new OutputStreamWriter(getOutputStream(context));
try {
String json = getGson().toJson(map);
Timber.d("Converted the following Map:");
Timber.d(map.toString());
Timber.d("to the following Json:");
Timber.d(json);
out.write(json);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
Timber.d("<< addToFavorites");
}
use of gov.whitehouse.data.model.FavoritesMap in project wh-app-android by WhiteHouse.
the class FeedManager method updateFavorites.
public static void updateFavorites(Context ctx) {
sFavoritesSubject.onNext(Observable.create((Subscriber<? super List<FeedItem>> op) -> {
final FavoritesMap map;
final InputStreamReader isr;
final List<FeedItem> favorites = new ArrayList<>();
final Gson gson = GsonUtils.createGsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
try {
isr = new InputStreamReader(FavoritesUtils.getInputStream(ctx));
map = gson.fromJson(isr, FavoritesMap.class);
if (map.articles() != null) {
processAndStoreFavorites(map.articles(), favorites, FeedType.TYPE_ARTICLE);
}
if (map.photos() != null) {
processAndStoreFavorites(map.photos(), favorites, FeedType.TYPE_PHOTO);
}
if (map.videos() != null) {
processAndStoreFavorites(map.videos(), favorites, FeedType.TYPE_VIDEO);
}
Collections.sort(favorites, (lhs, rhs) -> {
if (lhs.feedTitle() != null && rhs.feedTitle() != null) {
return lhs.feedTitle().compareTo(rhs.feedTitle());
}
return 1;
});
} catch (FileNotFoundException ignored) {
}
op.onNext(favorites);
op.onCompleted();
}).subscribeOn(Schedulers.io()));
}
use of gov.whitehouse.data.model.FavoritesMap in project wh-app-android by WhiteHouse.
the class DataTest method mapFavoritesToJson.
@Test
public void mapFavoritesToJson() {
final Gson gson = createGson();
String json;
FeedItem item = generateFeedItem();
FavoritesMap convertedMap;
FavoritesMap map = FavoritesMap.create(new ArrayList<>(1), new ArrayList<>(), new ArrayList<>());
map.articles().add(item);
System.out.println("Map toString():\n" + map.toString() + "\n");
json = gson.toJson(map);
System.out.println("Map JSON:\n" + json + "\n");
convertedMap = gson.fromJson(json, FavoritesMap.class);
System.out.println("Converted Map toString():\n" + convertedMap.toString() + "\n");
System.out.println("Item hashCode(): " + item.hashCode());
printFeedItemFieldHashes(item);
System.out.println("Converted Item hashCode(): " + convertedMap.articles().get(0).hashCode());
printFeedItemFieldHashes(convertedMap.articles().get(0));
assertThat(convertedMap.articles()).hasSameSizeAs(map.articles());
assertThat(convertedMap.photos()).hasSameSizeAs(map.photos());
assertThat(convertedMap.videos()).hasSameSizeAs(map.videos());
assertThat(convertedMap.articles()).contains(item);
}
use of gov.whitehouse.data.model.FavoritesMap in project wh-app-android by WhiteHouse.
the class DataTest method createFavoritesMap.
@Test
public void createFavoritesMap() {
FeedItem item = generateFeedItem();
FavoritesMap map = FavoritesMap.create(new ArrayList<>(1), new ArrayList<>(), new ArrayList<>());
map.articles().add(item);
assertThat(map.articles()).hasSize(1);
assertThat(map.photos()).hasSize(0);
assertThat(map.videos()).hasSize(0);
}
Aggregations