Skip to content

Commit

Permalink
Enable sorting of user comment listing (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaudecker authored and QuantumBadger committed Feb 18, 2017
1 parent 9cc82a6 commit b2fe389
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.quantumbadger.redreader.reddit.prepared.RedditPreparedPost;
import org.quantumbadger.redreader.reddit.url.PostCommentListingURL;
import org.quantumbadger.redreader.reddit.url.RedditURLParser;
import org.quantumbadger.redreader.reddit.url.UserCommentListingURL;
import org.quantumbadger.redreader.views.RedditPostView;

import java.util.UUID;
Expand Down Expand Up @@ -133,6 +134,7 @@ public boolean onCreateOptionsMenu(final Menu menu) {
true,
false,
false,
controller.isUserCommentListing(),
false,
controller.isSortable(),
null,
Expand Down Expand Up @@ -179,6 +181,11 @@ public void onSortSelected(final PostCommentListingURL.Sort order) {
requestRefresh(RefreshableFragment.COMMENTS, false);
}

public void onSortSelected(final UserCommentListingURL.Sort order) {
controller.setSort(order);
requestRefresh(RefreshableFragment.COMMENTS, false);
}

@Override
public void onSearchComments() {
DialogUtils.showSearchDialog(this, new DialogUtils.OnSearchListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.quantumbadger.redreader.reddit.url.PostListingURL;
import org.quantumbadger.redreader.reddit.url.RedditURLParser;
import org.quantumbadger.redreader.reddit.url.SubredditPostListURL;
import org.quantumbadger.redreader.reddit.url.UserCommentListingURL;
import org.quantumbadger.redreader.reddit.url.UserPostListingURL;
import org.quantumbadger.redreader.reddit.url.UserProfileURL;
import org.quantumbadger.redreader.views.RedditPostView;
Expand Down Expand Up @@ -670,7 +671,7 @@ public boolean onCreateOptionsMenu(final Menu menu) {
commentsVisible,
false,
false,
postsSortable,
false, postsSortable,
commentsSortable,
subredditSubscriptionState,
postsVisible && subredditDescription != null && subredditDescription.length() > 0,
Expand Down Expand Up @@ -700,6 +701,11 @@ public void onSortSelected(final PostCommentListingURL.Sort order) {
requestRefresh(RefreshableFragment.COMMENTS, false);
}

public void onSortSelected(final UserCommentListingURL.Sort order) {
commentListingController.setSort(order);
requestRefresh(RefreshableFragment.COMMENTS, false);
}

@Override
public void onSearchComments() {
DialogUtils.showSearchDialog(this, R.string.action_search_comments, new DialogUtils.OnSearchListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.quantumbadger.redreader.reddit.prepared.RedditPreparedPost;
import org.quantumbadger.redreader.reddit.url.PostCommentListingURL;
import org.quantumbadger.redreader.reddit.url.RedditURLParser;
import org.quantumbadger.redreader.reddit.url.UserCommentListingURL;
import org.quantumbadger.redreader.views.RedditPostView;

import java.util.ArrayList;
Expand Down Expand Up @@ -97,7 +98,7 @@ public void onCreate(final Bundle savedInstanceState) {

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
OptionsMenuUtility.prepare(this, menu, false, false, true, false, false, false, false, null, false, false, null, null);
OptionsMenuUtility.prepare(this, menu, false, false, true, false, false, false, false, false, null, false, false, null, null);

if(mFragment != null) {
mFragment.onCreateOptionsMenu(menu);
Expand Down Expand Up @@ -140,6 +141,9 @@ public void onPastComments() {}
@Override
public void onSortSelected(final PostCommentListingURL.Sort order) {}

@Override
public void onSortSelected(final UserCommentListingURL.Sort order) {}

@Override
public void onSearchComments() {
DialogUtils.showSearchDialog(this, new DialogUtils.OnSearchListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.quantumbadger.redreader.reddit.PostSort;
import org.quantumbadger.redreader.reddit.api.RedditSubredditSubscriptionManager;
import org.quantumbadger.redreader.reddit.url.PostCommentListingURL;
import org.quantumbadger.redreader.reddit.url.UserCommentListingURL;
import org.quantumbadger.redreader.settings.SettingsActivity;

import java.util.EnumSet;
Expand Down Expand Up @@ -72,7 +73,7 @@ public static <E extends BaseActivity & OptionsMenuListener> void prepare(
final E activity, final Menu menu,
final boolean subredditsVisible, final boolean postsVisible, final boolean commentsVisible,
final boolean areSearchResults, final boolean isUserPostListing,
final boolean postsSortable, final boolean commentsSortable,
final boolean isUserCommentListing, final boolean postsSortable, final boolean commentsSortable,
final RedditSubredditSubscriptionManager.SubredditSubscriptionState subredditSubscriptionState,
final boolean subredditHasSidebar,
final boolean pastCommentsSupported,
Expand Down Expand Up @@ -122,7 +123,10 @@ else if(isUserPostListing)
if(subredditHasSidebar) add(activity, menu, Option.SIDEBAR, false);

} else if(!subredditsVisible && !postsVisible && commentsVisible) {
if(commentsSortable) addAllCommentSorts(activity, menu, true);
if(commentsSortable && !isUserCommentListing)
addAllCommentSorts(activity, menu, true);
else if(commentsSortable && isUserCommentListing)
addAllUserCommentSorts(activity, menu, true);
add(activity, menu, Option.REFRESH_COMMENTS, false);
if(optionsMenuItemsPrefs.contains(OptionsMenuItemsPref.SEARCH)) add(activity, menu, Option.SEARCH, false);
if(pastCommentsSupported) {
Expand Down Expand Up @@ -557,6 +561,40 @@ public boolean onMenuItemClick(final MenuItem item) {
});
}

private static void addAllUserCommentSorts(final AppCompatActivity activity, final Menu menu, final boolean icon) {

final SubMenu sortComments = menu.addSubMenu(R.string.options_sort_comments);

if(icon) {
sortComments.getItem().setIcon(R.drawable.ic_sort_dark);
sortComments.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}

addSort(activity, sortComments, R.string.sort_comments_hot, UserCommentListingURL.Sort.HOT);
addSort(activity, sortComments, R.string.sort_comments_new, UserCommentListingURL.Sort.NEW);
addSort(activity, sortComments, R.string.sort_comments_controversial, UserCommentListingURL.Sort.CONTROVERSIAL);

final SubMenu sortCommentsTop = sortComments.addSubMenu(R.string.sort_comments_top);

addSort(activity, sortCommentsTop, R.string.sort_posts_top_hour, UserCommentListingURL.Sort.TOP_HOUR);
addSort(activity, sortCommentsTop, R.string.sort_posts_top_today, UserCommentListingURL.Sort.TOP_DAY);
addSort(activity, sortCommentsTop, R.string.sort_posts_top_week, UserCommentListingURL.Sort.TOP_WEEK);
addSort(activity, sortCommentsTop, R.string.sort_posts_top_month, UserCommentListingURL.Sort.TOP_MONTH);
addSort(activity, sortCommentsTop, R.string.sort_posts_top_year, UserCommentListingURL.Sort.TOP_YEAR);
addSort(activity, sortCommentsTop, R.string.sort_posts_top_all, UserCommentListingURL.Sort.TOP_ALL);
}

private static void addSort(final AppCompatActivity activity, final Menu menu, final int name, final UserCommentListingURL.Sort order) {

menu.add(activity.getString(name)).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
((OptionsMenuCommentsListener)activity).onSortSelected(order);
return true;
}
});
}

private interface OptionsMenuListener {
}

Expand Down Expand Up @@ -597,6 +635,8 @@ public interface OptionsMenuCommentsListener extends OptionsMenuListener {

void onSortSelected(PostCommentListingURL.Sort order);

void onSortSelected(UserCommentListingURL.Sort order);

void onSearchComments();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public boolean onCreateOptionsMenu(final Menu menu) {
false,
controller.isSearchResults(),
controller.isUserPostListing(),
controller.isSortable(),
false, controller.isSortable(),
true,
subredditSubscriptionState,
subredditDescription != null && subredditDescription.length() > 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.quantumbadger.redreader.reddit.url.CommentListingURL;
import org.quantumbadger.redreader.reddit.url.PostCommentListingURL;
import org.quantumbadger.redreader.reddit.url.RedditURLParser;
import org.quantumbadger.redreader.reddit.url.UserCommentListingURL;

import java.util.UUID;

Expand All @@ -52,6 +53,10 @@ public CommentListingController(RedditURLParser.RedditURL url, final Context con
if(url.asPostCommentListURL().order == null) {
url = url.asPostCommentListURL().order(defaultOrder(context));
}
} else if(url.pathType() == RedditURLParser.USER_COMMENT_LISTING_URL) {
if(url.asUserCommentListURL().order == null) {
url = url.asUserCommentListURL().order(UserCommentListingURL.Sort.NEW);
}
}

if(!(url instanceof CommentListingURL)) {
Expand All @@ -71,6 +76,12 @@ public void setSort(final PostCommentListingURL.Sort s) {
}
}

public void setSort(final UserCommentListingURL.Sort s) {
if(mUrl.pathType() == RedditURLParser.USER_COMMENT_LISTING_URL) {
mUrl = mUrl.asUserCommentListURL().order(s);
}
}

public PostCommentListingURL.Sort getSort() {

if(mUrl.pathType() == RedditURLParser.POST_COMMENT_LISTING_URL) {
Expand Down Expand Up @@ -108,6 +119,11 @@ public CommentListingFragment get(final AppCompatActivity parent, final boolean
}

public boolean isSortable() {
return mUrl.pathType() == RedditURLParser.POST_COMMENT_LISTING_URL;
return mUrl.pathType() == RedditURLParser.POST_COMMENT_LISTING_URL
|| mUrl.pathType() == RedditURLParser.USER_COMMENT_LISTING_URL;
}

public boolean isUserCommentListing() {
return mUrl.pathType() == RedditURLParser.USER_COMMENT_LISTING_URL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.quantumbadger.redreader.R;
import org.quantumbadger.redreader.common.Constants;
import org.quantumbadger.redreader.common.General;
Expand All @@ -29,23 +32,29 @@
public class UserCommentListingURL extends CommentListingURL {

public final String user;
public final Sort order;
public final Integer limit;
public final String after;

UserCommentListingURL(String user, Integer limit, String after) {
UserCommentListingURL(String user, Sort order, Integer limit, String after) {
this.user = user;
this.order = order;
this.limit = limit;
this.after = after;
}

@Override
public UserCommentListingURL after(String newAfter) {
return new UserCommentListingURL(user, limit, newAfter);
return new UserCommentListingURL(user, order, limit, newAfter);
}

@Override
public UserCommentListingURL limit(Integer newLimit) {
return new UserCommentListingURL(user, newLimit, after);
return new UserCommentListingURL(user, order, newLimit, after);
}

public UserCommentListingURL order(Sort newOrder) {
return new UserCommentListingURL(user, newOrder, limit, after);
}

public static UserCommentListingURL parse(Uri uri) {
Expand All @@ -69,6 +78,13 @@ public static UserCommentListingURL parse(Uri uri) {
pathSegments = pathSegmentsFiltered.toArray(new String[pathSegmentsFiltered.size()]);
}

final Sort order;
if(pathSegments.length > 0) {
order = Sort.parse(uri.getQueryParameter("sort"), uri.getQueryParameter("t"));
} else {
order = null;
}

if(pathSegments.length < 3) {
return null;
}
Expand Down Expand Up @@ -100,7 +116,7 @@ public static UserCommentListingURL parse(Uri uri) {
}
}

return new UserCommentListingURL(username, limit, after);
return new UserCommentListingURL(username, order, limit, after);
}

@Override
Expand All @@ -113,6 +129,10 @@ public Uri generateJsonUri() {
builder.appendPath(user);
builder.appendEncodedPath("comments");

if(order != null) {
order.addToUserCommentListingUri(builder);
}

if(after != null) {
builder.appendQueryParameter("after", after);
}
Expand Down Expand Up @@ -142,4 +162,65 @@ public String humanReadableName(Context context, boolean shorter) {
return String.format("%s (%s)", name, user);
}
}

public enum Sort {
NEW, HOT, CONTROVERSIAL, TOP, TOP_HOUR, TOP_DAY, TOP_WEEK, TOP_MONTH, TOP_YEAR, TOP_ALL;

@Nullable
public static Sort parse(@Nullable String sort, @Nullable String t) {

if(sort == null) {
return null;
}

sort = General.asciiLowercase(sort);
t = t != null ? General.asciiLowercase(t) : null;

if(sort.equals("hot")) {
return HOT;

} else if(sort.equals("new")) {
return NEW;

} else if(sort.equals("controversial")) {
return CONTROVERSIAL;

} else if(sort.equals("top")) {

if(t == null) return TOP_ALL;
else if(t.equals("all")) return TOP_ALL;
else if(t.equals("hour")) return TOP_HOUR;
else if(t.equals("day")) return TOP_DAY;
else if(t.equals("week")) return TOP_WEEK;
else if(t.equals("month")) return TOP_MONTH;
else if(t.equals("year")) return TOP_YEAR;
else return TOP_ALL;

} else {
return null;
}
}

public void addToUserCommentListingUri(@NonNull final Uri.Builder builder) {

switch(this) {
case HOT:
case NEW:
case CONTROVERSIAL:
builder.appendQueryParameter("sort", General.asciiLowercase(name()));
break;

case TOP_HOUR:
case TOP_DAY:
case TOP_WEEK:
case TOP_MONTH:
case TOP_YEAR:
case TOP_ALL:
final String parts[] = name().split("_");
builder.appendQueryParameter("sort", General.asciiLowercase(parts[0]));
builder.appendQueryParameter("t", General.asciiLowercase(parts[1]));
break;
}
}
}
}

0 comments on commit b2fe389

Please sign in to comment.