use of com.winsonchiu.reader.data.reddit.Subreddit in project Reader by TheKeeperOfPie.
the class ControllerSearch method reloadSubredditsRecommended.
public void reloadSubredditsRecommended() {
for (Subscription subscription : subscriptionsSubredditsRecommended) {
subscription.unsubscribe();
}
subscriptionsSubredditsRecommended.clear();
subredditsRecommended.getChildren().clear();
for (Listener listener : listeners) {
listener.getAdapterSearchSubredditsRecommended().notifyDataSetChanged();
}
StringBuilder builderOmit = new StringBuilder();
for (Thing thing : subredditsSubscribed.getChildren()) {
Subreddit subreddit = (Subreddit) thing;
builderOmit.append(subreddit.getDisplayName());
builderOmit.append(",");
}
currentSubreddit = controllerLinks.getSubreddit().getDisplayName();
reddit.recommend(currentSubreddit, builderOmit.toString()).flatMap(UtilsRx.flatMapWrapError(response -> {
final JSONArray jsonArray = new JSONArray(response);
List<String> names = new ArrayList<>(jsonArray.length());
for (int index = 0; index < jsonArray.length(); index++) {
/*
No idea why the API returns a {"sr_name": "subreddit"} rather than an
array of Strings, but we'll convert it.
*/
JSONObject dataSubreddit = jsonArray.getJSONObject(index);
names.add(dataSubreddit.optString("sr_name"));
}
return names;
})).flatMap(Observable::from).flatMap(next -> reddit.about("/r/" + next + "/")).flatMap(UtilsRx.flatMapWrapError(response -> Subreddit.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class)))).subscribe(new FinalizingSubscriber<Subreddit>() {
@Override
public void start() {
subredditsRecommended.getChildren().clear();
}
@Override
public void next(Subreddit next) {
subredditsRecommended.getChildren().add(next);
}
@Override
public void finish() {
Collections.sort(subredditsRecommended.getChildren(), new Comparator<Thing>() {
@Override
public int compare(Thing lhs, Thing rhs) {
return ((Subreddit) lhs).getDisplayName().compareToIgnoreCase(((Subreddit) rhs).getDisplayName());
}
});
for (Listener listener : listeners) {
listener.getAdapterSearchSubredditsRecommended().notifyDataSetChanged();
}
}
});
}
Aggregations