Search in sources :

Example 1 with Thing

use of com.winsonchiu.reader.data.reddit.Thing in project Reader by TheKeeperOfPie.

the class ControllerComments method expandComment.

private void expandComment(int position) {
    List<Thing> commentList = link.getComments().getChildren();
    int index = commentList.indexOf(listingComments.getChildren().get(position));
    if (index < 0) {
        return;
    }
    List<Comment> commentsToInsert = new LinkedList<>();
    Comment comment = (Comment) commentList.get(index);
    int numAdded = 0;
    while (++index < commentList.size() && ((Comment) commentList.get(index)).getLevel() > comment.getLevel()) {
        commentsToInsert.add((Comment) commentList.get(index));
    }
    for (int insertIndex = commentsToInsert.size() - 1; insertIndex >= 0; insertIndex--) {
        listingComments.getChildren().add(position + 1, commentsToInsert.get(insertIndex));
        numAdded++;
    }
    comment.setCollapsed(0);
    eventHolder.call(new RxAdapterEvent<>(getData(), RxAdapterEvent.Type.CHANGE, position + 1));
    eventHolder.call(new RxAdapterEvent<>(getData(), RxAdapterEvent.Type.INSERT, position + 2, numAdded));
}
Also used : Comment(com.winsonchiu.reader.data.reddit.Comment) Thing(com.winsonchiu.reader.data.reddit.Thing) LinkedList(java.util.LinkedList)

Example 2 with Thing

use of com.winsonchiu.reader.data.reddit.Thing in project Reader by TheKeeperOfPie.

the class ControllerComments method toggleComment.

/**
 * Toggles children of comment
 *
 * @param comment
 * @return true if comment is now expanded, false if collapsed
 */
@Override
public boolean toggleComment(Comment comment) {
    int position = indexOf(comment);
    if (position == listingComments.getChildren().size() - 1) {
        expandComment(position);
        return true;
    }
    List<Thing> commentList = listingComments.getChildren();
    comment = (Comment) commentList.get(position);
    Comment nextComment = (Comment) commentList.get(position + 1);
    if (comment.getLevel() >= nextComment.getLevel()) {
        expandComment(position);
        return true;
    } else {
        collapseComment(position);
        return false;
    }
}
Also used : Comment(com.winsonchiu.reader.data.reddit.Comment) Thing(com.winsonchiu.reader.data.reddit.Thing)

Example 3 with Thing

use of com.winsonchiu.reader.data.reddit.Thing in project Reader by TheKeeperOfPie.

the class ControllerComments method collapseComment.

private void collapseComment(int position, boolean notify) {
    List<Thing> commentList = listingComments.getChildren();
    Comment comment = (Comment) commentList.get(position);
    position++;
    int numRemoved = 0;
    while (position < commentList.size() && ((Comment) commentList.get(position)).getLevel() > comment.getLevel()) {
        commentList.remove(position);
        numRemoved++;
    }
    if (numRemoved > 0) {
        comment.setCollapsed(numRemoved);
    }
    if (notify) {
        eventHolder.call(new RxAdapterEvent<>(getData(), RxAdapterEvent.Type.CHANGE, position));
        eventHolder.call(new RxAdapterEvent<>(getData(), RxAdapterEvent.Type.INSERT, position + 1, numRemoved));
    }
}
Also used : Comment(com.winsonchiu.reader.data.reddit.Comment) Thing(com.winsonchiu.reader.data.reddit.Thing)

Example 4 with Thing

use of com.winsonchiu.reader.data.reddit.Thing in project Reader by TheKeeperOfPie.

the class TableLink method queryListing.

public List<Thing> queryListing(final List<String> ids) {
    // StringBuilder statement = new StringBuilder("SELECT * from " + NAME + " inner join (select " + ids.get(0) + " as " + _ID);
    // 
    // String union = " union all select ";
    // 
    // for (int index = 1; index < ids.size(); index++) {
    // statement.append(union)
    // .append(DatabaseUtils.sqlEscapeString(ids.get(index)));
    // }
    // 
    // statement.append(") as x on " + NAME + "." + _ID + " = x." + _ID);
    List<String> parameters = new ArrayList<>(ids.size());
    for (int index = 0; index < ids.size(); index++) {
        parameters.add("?");
    }
    List<Thing> links = new ArrayList<>();
    ObjectMapper objectMapper = ComponentStatic.getObjectMapper();
    String[] idArray = new String[ids.size()];
    for (int index = 0; index < idArray.length; index++) {
        idArray[index] = ids.get(index);
    }
    Cursor query = sqLiteDatabase.query(NAME, new String[] { COLUMN_JSON, COLUMN_COMMENTS }, _ID + " IN (" + TextUtils.join(",", parameters) + ")", idArray, null, null, null);
    if (query != null) {
        if (query.moveToFirst()) {
            do {
                String json = query.getString(query.getColumnIndex(COLUMN_JSON));
                String comments = query.getString(query.getColumnIndex(COLUMN_COMMENTS));
                try {
                    Link link = Link.fromJson(objectMapper.readValue(json, JsonNode.class));
                    if (!TextUtils.isEmpty(comments)) {
                        link.setComments(Listing.fromJson(objectMapper.readValue(comments, JsonNode.class)));
                    }
                    links.add(link);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } while (query.moveToNext());
        }
        query.close();
    }
    Collections.sort(links, new Comparator<Thing>() {

        @Override
        public int compare(Thing lhs, Thing rhs) {
            int first = ids.indexOf(lhs.getId());
            int second = ids.indexOf(rhs.getId());
            return first < second ? -1 : (first == second ? 0 : 1);
        }
    });
    return links;
}
Also used : ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) Cursor(android.database.Cursor) Thing(com.winsonchiu.reader.data.reddit.Thing) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Link(com.winsonchiu.reader.data.reddit.Link)

Example 5 with Thing

use of com.winsonchiu.reader.data.reddit.Thing in project Reader by TheKeeperOfPie.

the class ControllerProfile method setReplyText.

@Override
public boolean setReplyText(String name, String text, boolean collapsed) {
    if (topLink != null && name.equals(topLink.getName())) {
        topLink.setReplyText(text);
        topLink.setReplyExpanded(!collapsed);
        for (Listener listener : listeners) {
            listener.getAdapter().notifyItemChanged(2);
        }
        return true;
    }
    if (topComment != null && name.equals(topComment.getName())) {
        topComment.setReplyText(text);
        topComment.setReplyExpanded(!collapsed);
        for (Listener listener : listeners) {
            listener.getAdapter().notifyItemChanged(4);
        }
        return true;
    }
    for (int index = 0; index < data.getChildren().size(); index++) {
        Thing thing = data.getChildren().get(index);
        if (thing.getName().equals(name)) {
            ((Replyable) thing).setReplyText(text);
            ((Replyable) thing).setReplyExpanded(!collapsed);
            for (Listener listener : listeners) {
                listener.getAdapter().notifyItemChanged(index + 6);
            }
            return true;
        }
    }
    return false;
}
Also used : ControllerListener(com.winsonchiu.reader.utils.ControllerListener) Replyable(com.winsonchiu.reader.data.reddit.Replyable) Thing(com.winsonchiu.reader.data.reddit.Thing)

Aggregations

Thing (com.winsonchiu.reader.data.reddit.Thing)21 Replyable (com.winsonchiu.reader.data.reddit.Replyable)8 Comment (com.winsonchiu.reader.data.reddit.Comment)7 Link (com.winsonchiu.reader.data.reddit.Link)7 Listing (com.winsonchiu.reader.data.reddit.Listing)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 Subreddit (com.winsonchiu.reader.data.reddit.Subreddit)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)3 Comparator (java.util.Comparator)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