Skip to content

Commit

Permalink
Ability to set default post sort
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumBadger committed Aug 3, 2016
1 parent d90381d commit 3b48e50
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 34 deletions.
1 change: 1 addition & 0 deletions assets/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Comment swipe actions
Ability to search comments (thanks to cpalasanu)
Ability to send and reply to private messages
Pull-down-to-refresh for posts/comments
Ability to set default post sort
Music is no longer paused when GIFs play (thanks to noughtmare and ccrama)
Fixed saving images with slashes in the path (thanks to 0xKD)
Action bar back button extended to allow clicking on text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public void onSelected(final PostListingURL url) {

if(twoPane) {

postListingController = new PostListingController(url);
postListingController = new PostListingController(url, this);
requestRefresh(RefreshableFragment.POSTS, false);

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void onCreate(final Bundle savedInstanceState) {
throw new RuntimeException(String.format("'%s' is not a post listing URL!", url.generateJsonUri()));
}

controller = new PostListingController((PostListingURL)url);
controller = new PostListingController((PostListingURL)url, this);

Bundle fragmentSavedInstanceState = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.quantumbadger.redreader.activities.OptionsMenuUtility;
import org.quantumbadger.redreader.fragments.MainMenuFragment;
import org.quantumbadger.redreader.io.WritableHashSet;
import org.quantumbadger.redreader.listingcontrollers.PostListingController;
import org.quantumbadger.redreader.reddit.prepared.RedditPreparedPost;
import org.quantumbadger.redreader.reddit.things.RedditSubreddit;
import org.quantumbadger.redreader.reddit.url.PostCommentListingURL;
Expand Down Expand Up @@ -394,6 +395,10 @@ public static CommentAction pref_behaviour_actions_comment_longclick(final Conte
return CommentAction.valueOf(General.asciiUppercase(getString(R.string.pref_behaviour_actions_comment_longclick_key, "action_menu", context, sharedPreferences)));
}

public static PostListingController.Sort pref_behaviour_postsort(final Context context, final SharedPreferences sharedPreferences) {
return PostListingController.Sort.valueOf(General.asciiUppercase(getString(R.string.pref_behaviour_postsort_key, "hot", context, sharedPreferences)));
}

public static PostCommentListingURL.Sort pref_behaviour_commentsort(final Context context, final SharedPreferences sharedPreferences) {
return PostCommentListingURL.Sort.valueOf(General.asciiUppercase(getString(R.string.pref_behaviour_commentsort_key, "best", context, sharedPreferences)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package org.quantumbadger.redreader.listingcontrollers;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import org.quantumbadger.redreader.cache.CacheRequest;
import org.quantumbadger.redreader.common.PrefsUtility;
import org.quantumbadger.redreader.fragments.PostListingFragment;
import org.quantumbadger.redreader.reddit.things.RedditSubreddit;
import org.quantumbadger.redreader.reddit.url.PostListingURL;
Expand All @@ -43,7 +46,14 @@ public UUID getSession() {
return session;
}

public PostListingController(PostListingURL url) {
public PostListingController(PostListingURL url, final Context context) {

if(url.pathType() == RedditURLParser.SUBREDDIT_POST_LISTING_URL) {
if(url.asSubredditPostListURL().order == null) {
url = url.asSubredditPostListURL().sort(defaultOrder(context));
}
}

this.url = url;
}

Expand Down Expand Up @@ -76,6 +86,10 @@ public final void setSort(final Sort order) {
}
}

private Sort defaultOrder(final Context context) {
return PrefsUtility.pref_behaviour_postsort(context, PreferenceManager.getDefaultSharedPreferences(context));
}

public final Sort getSort() {

if(url.pathType() == RedditURLParser.SUBREDDIT_POST_LISTING_URL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import android.content.Context;
import android.net.Uri;
import android.support.annotation.Nullable;
import org.quantumbadger.redreader.R;
import org.quantumbadger.redreader.common.Constants;
import org.quantumbadger.redreader.common.General;
Expand All @@ -31,11 +32,11 @@
public class SubredditPostListURL extends PostListingURL {

public static SubredditPostListURL getFrontPage() {
return new SubredditPostListURL(Type.FRONTPAGE, null, PostListingController.Sort.HOT, null, null, null);
return new SubredditPostListURL(Type.FRONTPAGE, null, null, null, null, null);
}

public static SubredditPostListURL getAll() {
return new SubredditPostListURL(Type.ALL, null, PostListingController.Sort.HOT, null, null, null);
return new SubredditPostListURL(Type.ALL, null, null, null, null, null);
}

public static RedditURLParser.RedditURL getSubreddit(String subreddit) throws RedditSubreddit.InvalidSubredditNameException {
Expand All @@ -56,11 +57,18 @@ public enum Type {
public final Type type;
public final String subreddit;

public final PostListingController.Sort order;
public final Integer limit;
public final String before, after;
@Nullable public final PostListingController.Sort order;
@Nullable public final Integer limit;
@Nullable public final String before, after;

private SubredditPostListURL(
Type type,
String subreddit,
@Nullable PostListingController.Sort order,
@Nullable Integer limit,
@Nullable String before,
@Nullable String after) {

SubredditPostListURL(Type type, String subreddit, PostListingController.Sort order, Integer limit, String before, String after) {
this.type = type;
this.subreddit = subreddit;
this.order = order;
Expand All @@ -85,7 +93,12 @@ public PostListingController.Sort getOrder() {
return order;
}

private static PostListingController.Sort getOrder(String sort, String t) {
@Nullable
private static PostListingController.Sort getOrder(@Nullable String sort, @Nullable String t) {

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

sort = sort.toLowerCase();
t = t != null ? t.toLowerCase() : null;
Expand Down Expand Up @@ -232,7 +245,7 @@ public static SubredditPostListURL parse(final Uri uri) {

switch(pathSegments.length) {
case 0:
return new SubredditPostListURL(Type.FRONTPAGE, null, PostListingController.Sort.HOT, limit, before, after);
return new SubredditPostListURL(Type.FRONTPAGE, null, null, limit, before, after);

case 1: {
if(order != null) {
Expand All @@ -252,7 +265,7 @@ public static SubredditPostListURL parse(final Uri uri) {
if(subreddit.equals("all")) {

if(pathSegments.length == 2) {
return new SubredditPostListURL(Type.ALL, null, PostListingController.Sort.HOT, limit, before, after);
return new SubredditPostListURL(Type.ALL, null, null, limit, before, after);

} else if(order != null) {
return new SubredditPostListURL(Type.ALL, null, order, limit, before, after);
Expand All @@ -264,7 +277,7 @@ public static SubredditPostListURL parse(final Uri uri) {
} else if(subreddit.matches("all(\\-[\\w\\.]+)+")) {

if(pathSegments.length == 2) {
return new SubredditPostListURL(Type.ALL_SUBTRACTION, subreddit, PostListingController.Sort.HOT, limit, before, after);
return new SubredditPostListURL(Type.ALL_SUBTRACTION, subreddit, null, limit, before, after);

} else if(order != null) {
return new SubredditPostListURL(Type.ALL_SUBTRACTION, subreddit, order, limit, before, after);
Expand All @@ -276,7 +289,7 @@ public static SubredditPostListURL parse(final Uri uri) {
} else if(subreddit.matches("\\w+(\\+[\\w\\.]+)+")) {

if(pathSegments.length == 2) {
return new SubredditPostListURL(Type.SUBREDDIT_COMBINATION, subreddit, PostListingController.Sort.HOT, limit, before, after);
return new SubredditPostListURL(Type.SUBREDDIT_COMBINATION, subreddit, null, limit, before, after);

} else if(order != null) {
return new SubredditPostListURL(Type.SUBREDDIT_COMBINATION, subreddit, order, limit, before, after);
Expand All @@ -288,7 +301,7 @@ public static SubredditPostListURL parse(final Uri uri) {
} else if(subreddit.matches("[\\w\\.]+")) {

if(pathSegments.length == 2) {
return new SubredditPostListURL(Type.SUBREDDIT, subreddit, PostListingController.Sort.HOT, limit, before, after);
return new SubredditPostListURL(Type.SUBREDDIT, subreddit, null, limit, before, after);

} else if(order != null) {
return new SubredditPostListURL(Type.SUBREDDIT, subreddit, order, limit, before, after);
Expand All @@ -307,6 +320,29 @@ public static SubredditPostListURL parse(final Uri uri) {
}
}

@Override
public String humanReadablePath() {

String path = super.humanReadablePath();

if(order == null) {
return path;
}

switch(order) {
case TOP_HOUR:
case TOP_DAY:
case TOP_WEEK:
case TOP_MONTH:
case TOP_YEAR:
case TOP_ALL:
return path + "?t=" + order.name().split("_")[1].toLowerCase();

default:
return path;
}
}

@Override
public String humanReadableName(Context context, boolean shorter) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void onCreate(final Bundle savedInstanceState) {
R.string.pref_behaviour_actions_comment_tap_key,
R.string.pref_behaviour_actions_comment_longclick_key,
R.string.pref_behaviour_commentsort_key,
R.string.pref_behaviour_postsort_key,
R.string.pref_appearance_langforce_key,
R.string.pref_behaviour_postcount_key,
R.string.pref_behaviour_bezel_toolbar_swipezone_key,
Expand Down
66 changes: 47 additions & 19 deletions src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,54 @@
<item>disabled</item>
</string-array>

<string-array name="pref_behaviour_commentsort_array">
<item>@string/sort_comments_best</item>
<item>@string/sort_comments_hot</item>
<item>@string/sort_comments_new</item>
<item>@string/sort_comments_old</item>
<item>@string/sort_comments_controversial</item>
<item>@string/sort_comments_top</item>
<item>@string/sort_comments_qa</item>
</string-array>
<string-array name="pref_behaviour_postsort_array">
<item>@string/sort_posts_hot</item>
<item>@string/sort_posts_new</item>
<item>@string/sort_posts_rising</item>
<item>@string/sort_posts_controversial</item>
<item>@string/sort_posts_top_hour</item>
<item>@string/sort_posts_top_today</item>
<item>@string/sort_posts_top_week</item>
<item>@string/sort_posts_top_month</item>
<item>@string/sort_posts_top_year</item>
<item>@string/sort_posts_top_all</item>
</string-array>

<!-- Constants. Do not change. -->
<string-array name="pref_behaviour_commentsort_array_return">
<item>best</item>
<item>hot</item>
<item>new</item>
<item>old</item>
<item>controversial</item>
<item>top</item>
<item>qa</item>
</string-array>
<!-- Constants. Do not change. -->
<string-array name="pref_behaviour_postsort_array_return">
<item>hot</item>
<item>new</item>
<item>rising</item>
<item>controversial</item>
<item>top_hour</item>
<item>top_day</item>
<item>top_week</item>
<item>top_month</item>
<item>top_year</item>
<item>top_all</item>

</string-array>

<string-array name="pref_behaviour_commentsort_array">
<item>@string/sort_comments_best</item>
<item>@string/sort_comments_hot</item>
<item>@string/sort_comments_new</item>
<item>@string/sort_comments_old</item>
<item>@string/sort_comments_controversial</item>
<item>@string/sort_comments_top</item>
<item>@string/sort_comments_qa</item>
</string-array>

<!-- Constants. Do not change. -->
<string-array name="pref_behaviour_commentsort_array_return">
<item>best</item>
<item>hot</item>
<item>new</item>
<item>old</item>
<item>controversial</item>
<item>top</item>
<item>qa</item>
</string-array>

<string-array name="pref_behaviour_actions_comment">
<item>@string/action_collapse</item>
Expand Down
3 changes: 3 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -860,4 +860,7 @@
<string name="action_copy_text">Copy Text</string>
<string name="action_copy_link">Copy Link</string>

<string name="pref_behaviour_postsort_key">pref_behaviour_postsort</string>
<string name="pref_behaviour_postsort">Default Post Sort</string>

</resources>
6 changes: 6 additions & 0 deletions src/main/res/xml/prefs_behaviour.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@

<PreferenceCategory android:title="@string/pref_behaviour_sort_header">

<ListPreference android:title="@string/pref_behaviour_postsort"
android:key="@string/pref_behaviour_postsort_key"
android:entries="@array/pref_behaviour_postsort_array"
android:entryValues="@array/pref_behaviour_postsort_array_return"
android:defaultValue="hot"/>

<ListPreference android:title="@string/pref_behaviour_commentsort"
android:key="@string/pref_behaviour_commentsort_key"
android:entries="@array/pref_behaviour_commentsort_array"
Expand Down

0 comments on commit 3b48e50

Please sign in to comment.