Search in sources :

Example 1 with RxAdapterEvent

use of com.winsonchiu.reader.adapter.RxAdapterEvent in project Reader by TheKeeperOfPie.

the class ControllerComments method loadNestedComments.

public void loadNestedComments(final Comment moreComment) {
    setLoading(true);
    String children = "";
    List<String> childrenList = moreComment.getChildren();
    if (childrenList.isEmpty()) {
        int commentIndex = listingComments.getChildren().indexOf(moreComment);
        if (commentIndex >= 0) {
            listingComments.getChildren().remove(commentIndex);
            eventHolder.call(new RxAdapterEvent<>(getData(), RxAdapterEvent.Type.REMOVE, commentIndex + 1));
        }
        return;
    }
    for (String id : childrenList) {
        children += id + ",";
    }
    reddit.moreChildren(link.getName(), children.substring(0, children.length() - 1)).subscribe(new Observer<String>() {

        @Override
        public void onCompleted() {
            setLoading(false);
        }

        @Override
        public void onError(Throwable e) {
            e.printStackTrace();
        }

        @Override
        public void onNext(String response) {
            Log.d(TAG, "onNext() called with: " + "response = [" + response + "]");
            try {
                JsonNode nodeThings = ComponentStatic.getObjectMapper().readValue(response, JsonNode.class).get("json").get("data").get("things");
                Listing listing = new Listing();
                List<Thing> things = new ArrayList<>();
                List<Thing> comments = new ArrayList<>();
                for (JsonNode node : nodeThings) {
                    Comment comment = Comment.fromJson(node, moreComment.getLevel());
                    // For some reason Reddit doesn't report the link author, so we'll do it manually
                    comment.setLinkAuthor(link.getAuthor());
                    if (comment.getParentId().equals(link.getId())) {
                        comments.add(comment);
                    } else {
                        // TODO: Find a starting index to insert comments, without iterating the entire data list so many times
                        int commentIndex = -1;
                        for (int position = 0; position < comments.size(); position++) {
                            if (comments.get(position).getId().equals(comment.getParentId())) {
                                commentIndex = position;
                                break;
                            }
                        }
                        if (commentIndex >= 0) {
                            comment.setLevel(((Comment) comments.get(commentIndex)).getLevel() + 1);
                        }
                        comments.add(commentIndex + 1, comment);
                    }
                }
                if (comments.isEmpty()) {
                    int commentIndex = link.getComments().getChildren().indexOf(moreComment);
                    if (commentIndex >= 0) {
                        link.getComments().getChildren().remove(commentIndex);
                    }
                    commentIndex = listingComments.getChildren().indexOf(moreComment);
                    if (commentIndex >= 0) {
                        listingComments.getChildren().remove(commentIndex);
                        eventHolder.call(new RxAdapterEvent<>(getData(), RxAdapterEvent.Type.REMOVE, commentIndex + 1));
                    }
                } else {
                    things.addAll(comments);
                    listing.setChildren(things);
                    insertComments(moreComment, listing);
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    });
}
Also used : Comment(com.winsonchiu.reader.data.reddit.Comment) RxAdapterEvent(com.winsonchiu.reader.adapter.RxAdapterEvent) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Listing(com.winsonchiu.reader.data.reddit.Listing) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 2 with RxAdapterEvent

use of com.winsonchiu.reader.adapter.RxAdapterEvent in project Reader by TheKeeperOfPie.

the class ControllerSearch method loadMoreLinksSubreddit.

public Observable<Listing> loadMoreLinksSubreddit() {
    if (isLoadingLinksSubreddit()) {
        return Observable.empty();
    }
    Subreddit subreddit = controllerLinks.getSubreddit();
    if (subscriptionLinksSubreddit != null && !subscriptionLinksSubreddit.isUnsubscribed()) {
        subscriptionLinksSubreddit.unsubscribe();
        subscriptionLinksSubreddit = null;
    }
    String pathSubreddit = subreddit.getUrl();
    if (pathSubreddit.length() < 2) {
        pathSubreddit = "";
    }
    Observable<Listing> observable = reddit.search(pathSubreddit, query, sort.toString(), time.toString(), linksSubreddit.getAfter(), true).flatMap(UtilsRx.flatMapWrapError(response -> Listing.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class))));
    subscriptionLinksSubreddit = observable.subscribe(new FinalizingSubscriber<Listing>() {

        @Override
        public void start() {
            setLoadingLinksSubreddit(true);
        }

        @Override
        public void next(Listing listing) {
            if (listing.getChildren().isEmpty() || listing.getChildren().get(0) instanceof Subreddit) {
                return;
            }
            int startSize = linksSubreddit.getChildren().size();
            int positionStart = startSize + 1;
            linksSubreddit.addChildren(listing.getChildren());
            linksSubreddit.setAfter(listing.getAfter());
            eventHolder.callLinksSubreddit(new RxAdapterEvent<>(getLinksSubredditModel(), RxAdapterEvent.Type.INSERT, positionStart, linksSubreddit.getChildren().size() - startSize));
        }

        @Override
        public void finish() {
            setLoadingLinksSubreddit(false);
        }
    });
    return observable;
}
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) Listing(com.winsonchiu.reader.data.reddit.Listing) JsonNode(com.fasterxml.jackson.databind.JsonNode) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) FinalizingSubscriber(com.winsonchiu.reader.rx.FinalizingSubscriber)

Example 3 with RxAdapterEvent

use of com.winsonchiu.reader.adapter.RxAdapterEvent in project Reader by TheKeeperOfPie.

the class ControllerSearch method reloadLinksSubreddit.

public Observable<Listing> reloadLinksSubreddit() {
    if (subscriptionLinksSubreddit != null && !subscriptionLinksSubreddit.isUnsubscribed()) {
        subscriptionLinksSubreddit.unsubscribe();
        subscriptionLinksSubreddit = null;
    }
    Subreddit subreddit = controllerLinks.getSubreddit();
    String pathSubreddit = subreddit.getUrl();
    if (pathSubreddit.length() < 2) {
        pathSubreddit = "";
    }
    Observable<Listing> observable = reddit.search(pathSubreddit, query, sort.toString(), time.toString(), null, true).flatMap(UtilsRx.flatMapWrapError(response -> Listing.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class))));
    subscriptionSubreddits = observable.subscribe(new FinalizingSubscriber<Listing>() {

        @Override
        public void start() {
            setLoadingLinksSubreddit(true);
        }

        @Override
        public void next(Listing listing) {
            linksSubreddit = listing;
            eventHolder.callLinksSubreddit(new RxAdapterEvent<>(getLinksSubredditModel()));
            for (Listener listener : listeners) {
                listener.scrollToLinksSubreddit(0);
            }
        }

        @Override
        public void finish() {
            setLoadingLinksSubreddit(false);
        }
    });
    return observable;
}
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) Listing(com.winsonchiu.reader.data.reddit.Listing) JsonNode(com.fasterxml.jackson.databind.JsonNode) Subreddit(com.winsonchiu.reader.data.reddit.Subreddit) FinalizingSubscriber(com.winsonchiu.reader.rx.FinalizingSubscriber)

Example 4 with RxAdapterEvent

use of com.winsonchiu.reader.adapter.RxAdapterEvent in project Reader by TheKeeperOfPie.

the class ControllerHistory method reload.

public void reload() {
    lastIndex = 0;
    if (TextUtils.isEmpty(query)) {
        namesToFetch.clear();
        HistoryEntry currentEntry = historian.getFirst();
        while (currentEntry != null) {
            if (!currentEntry.isRemoved() && currentEntry.getTimestamp() > timeStart && currentEntry.getTimestamp() < timeEnd) {
                namesToFetch.add(currentEntry.getName());
            }
            currentEntry = currentEntry.getNext();
        }
    }
    if (namesToFetch.isEmpty()) {
        history = new Listing();
        eventHolder.call(new RxAdapterEvent<>(getData()));
        setIsLoading(false);
        return;
    }
    setIsLoading(true);
    StringBuilder builder = new StringBuilder();
    int finalIndex = lastIndex + 25 > namesToFetch.size() ? namesToFetch.size() : lastIndex + 25;
    while (lastIndex < finalIndex) {
        builder.append(namesToFetch.get(lastIndex));
        builder.append(SEPARATOR);
        lastIndex++;
    }
    reddit.info(builder.toString()).flatMap(UtilsRx.flatMapWrapError(response -> Listing.fromJson(ComponentStatic.getObjectMapper().readValue(response, JsonNode.class)))).subscribe(new FinalizingSubscriber<Listing>() {

        @Override
        public void start() {
            setIsLoading(true);
        }

        @Override
        public void next(Listing listing) {
            history = listing;
            eventHolder.call(new RxAdapterEvent<>(getData()));
        }

        @Override
        public void finish() {
            setIsLoading(false);
        }
    });
}
Also used : Listing(com.winsonchiu.reader.data.reddit.Listing) RxAdapterEvent(com.winsonchiu.reader.adapter.RxAdapterEvent)

Aggregations

RxAdapterEvent (com.winsonchiu.reader.adapter.RxAdapterEvent)4 Listing (com.winsonchiu.reader.data.reddit.Listing)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 AccountManager (android.accounts.AccountManager)2 Context (android.content.Context)2 SharedPreferences (android.content.SharedPreferences)2 PreferenceManager (android.preference.PreferenceManager)2 Nullable (android.support.annotation.Nullable)2 TextUtils (android.text.TextUtils)2 Log (android.util.Log)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 BehaviorRelay (com.jakewharton.rxrelay.BehaviorRelay)2 AppSettings (com.winsonchiu.reader.AppSettings)2 ControllerUser (com.winsonchiu.reader.ControllerUser)2 CustomApplication (com.winsonchiu.reader.CustomApplication)2 ComponentStatic (com.winsonchiu.reader.dagger.components.ComponentStatic)2 Link (com.winsonchiu.reader.data.reddit.Link)2