Search in sources :

Example 11 with Subreddit

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();
            }
        }
    });
}
Also used : Context(android.content.Context) CustomApplication(com.winsonchiu.reader.CustomApplication) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) ControllerLinks(com.winsonchiu.reader.links.ControllerLinks) ListIterator(java.util.ListIterator) UtilsRx(com.winsonchiu.reader.utils.UtilsRx) AppSettings(com.winsonchiu.reader.AppSettings) LinksModel(com.winsonchiu.reader.links.LinksModel) ArrayList(java.util.ArrayList) Observable(rx.Observable) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Reddit(com.winsonchiu.reader.data.reddit.Reddit) JSONObject(org.json.JSONObject) ComponentStatic(com.winsonchiu.reader.dagger.components.ComponentStatic) Link(com.winsonchiu.reader.data.reddit.Link) JsonNode(com.fasterxml.jackson.databind.JsonNode) PreferenceManager(android.preference.PreferenceManager) Log(android.util.Log) AccountManager(android.accounts.AccountManager) BehaviorRelay(com.jakewharton.rxrelay.BehaviorRelay) Iterator(java.util.Iterator) ControllerUser(com.winsonchiu.reader.ControllerUser) Sort(com.winsonchiu.reader.data.reddit.Sort) Listing(com.winsonchiu.reader.data.reddit.Listing) Set(java.util.Set) TextUtils(android.text.TextUtils) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FinalizingSubscriber(com.winsonchiu.reader.rx.FinalizingSubscriber) IOException(java.io.IOException) Thing(com.winsonchiu.reader.data.reddit.Thing) Observer(rx.Observer) URLEncoder(java.net.URLEncoder) List(java.util.List) SharedPreferences(android.content.SharedPreferences) Replyable(com.winsonchiu.reader.data.reddit.Replyable) Time(com.winsonchiu.reader.data.reddit.Time) RxAdapterEvent(com.winsonchiu.reader.adapter.RxAdapterEvent) Nullable(android.support.annotation.Nullable) Comparator(java.util.Comparator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Collections(java.util.Collections) Subscription(rx.Subscription) JSONArray(org.json.JSONArray) JSONArray(org.json.JSONArray) Comparator(java.util.Comparator) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(rx.Subscription) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) Thing(com.winsonchiu.reader.data.reddit.Thing)

Aggregations

Subreddit (com.winsonchiu.reader.data.reddit.Subreddit)11 Thing (com.winsonchiu.reader.data.reddit.Thing)7 Link (com.winsonchiu.reader.data.reddit.Link)6 Listing (com.winsonchiu.reader.data.reddit.Listing)6 Sort (com.winsonchiu.reader.data.reddit.Sort)6 Time (com.winsonchiu.reader.data.reddit.Time)6 SharedPreferences (android.content.SharedPreferences)5 Nullable (android.support.annotation.Nullable)5 TextUtils (android.text.TextUtils)5 Log (android.util.Log)5 AppSettings (com.winsonchiu.reader.AppSettings)5 ControllerUser (com.winsonchiu.reader.ControllerUser)5 Reddit (com.winsonchiu.reader.data.reddit.Reddit)5 UtilsRx (com.winsonchiu.reader.utils.UtilsRx)5 Comparator (java.util.Comparator)5 Inject (javax.inject.Inject)5 AccountManager (android.accounts.AccountManager)4 Context (android.content.Context)4 PreferenceManager (android.preference.PreferenceManager)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4