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();
}
}
});
}
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;
}
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;
}
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);
}
});
}
Aggregations