Skip to content

Commit fc6b494

Browse files
committed
feat: settings to filter out posts with few comments
When the checkbox is enabled, the user is asked for the minimum number of comments. This info is shown as the summary of the checkbox, when it is enabled. A toast is shown when comments are filtered, just as a reminder that this filter is in effect.
1 parent ea0b596 commit fc6b494

File tree

6 files changed

+154
-3
lines changed

6 files changed

+154
-3
lines changed

src/main/java/org/quantumbadger/redreader/adapters/PostListingManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
package org.quantumbadger.redreader.adapters;
1919

2020
import android.content.Context;
21+
import org.quantumbadger.redreader.common.PrefsUtility;
2122
import org.quantumbadger.redreader.reddit.RedditPostListItem;
2223

24+
import java.util.ArrayList;
2325
import java.util.Collection;
2426
import java.util.Collections;
2527

src/main/java/org/quantumbadger/redreader/common/PrefsUtility.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ public static boolean isRefreshRequired(final Context context, final String key)
115115
|| key.equals(context.getString(R.string.pref_behaviour_nsfw_key))
116116
|| key.equals(context.getString(R.string.pref_behaviour_postcount_key))
117117
|| key.equals(context.getString(R.string.pref_behaviour_comment_min_key))
118+
|| key.equals(context.getString(R.string.pref_behaviour_hide_few_comments_key))
119+
|| key.equals(context.getString(R.string.pref_behaviour_min_comments_key))
118120
|| key.equals(context.getString(R.string.pref_behaviour_pinned_subredditsort_key))
119121
|| key.equals(context.getString(
120122
R.string.pref_behaviour_blocked_subredditsort_key))
@@ -806,7 +808,6 @@ public static boolean pref_behaviour_video_frame_step() {
806808
return getBoolean(R.string.pref_behaviour_video_frame_step_key,
807809
false);
808810
}
809-
810811
public static boolean pref_behaviour_video_mute_default() {
811812
return getBoolean(
812813
R.string.pref_behaviour_video_mute_default_key,
@@ -1171,6 +1172,28 @@ public static boolean pref_behaviour_mark_posts_as_read() {
11711172
true);
11721173
}
11731174

1175+
public static boolean pref_behaviour_hide_few_comments() {
1176+
return getBoolean(
1177+
R.string.pref_behaviour_hide_few_comments_key,
1178+
false);
1179+
}
1180+
1181+
public static int pref_behaviour_min_comments() {
1182+
try {
1183+
return Integer.parseInt(getString(
1184+
R.string.pref_behaviour_min_comments_key,
1185+
"10"));
1186+
} catch(final Throwable e) {
1187+
return 10;
1188+
}
1189+
}
1190+
1191+
public static void pref_behaviour_min_comments(final int value) {
1192+
sharedPrefs.edit()
1193+
.putString(getPrefKey(R.string.pref_behaviour_min_comments_key), String.valueOf(value))
1194+
.apply();
1195+
}
1196+
11741197
public enum SharingDomain {
11751198
STANDARD_REDDIT("reddit.com"),
11761199
SHORT_REDDIT("redd.it"),

src/main/java/org/quantumbadger/redreader/fragments/PostListingFragment.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,15 @@ public void onDataStreamComplete(
713713
&& mPostListingURL.pathType()
714714
!= RedditURLParser.USER_POST_LISTING_URL;
715715

716+
final boolean hideFewComments
717+
= PrefsUtility.pref_behaviour_hide_few_comments();
718+
719+
final int minComments = hideFewComments
720+
? PrefsUtility.pref_behaviour_min_comments()
721+
: 0;
722+
723+
final AtomicInteger filteredCommentCount = new AtomicInteger(0);
724+
716725
final boolean isConnectionWifi = General.isConnectionWifi(activity);
717726

718727
final boolean inlinePreviews
@@ -856,6 +865,13 @@ public void onDataStreamComplete(
856865
continue;
857866
}
858867

868+
// Skip adding this post if it has too few comments
869+
if(hideFewComments && post.getNum_comments() < minComments) {
870+
mPostsNotShown = true;
871+
filteredCommentCount.incrementAndGet();
872+
continue;
873+
}
874+
859875
if(precacheComments) {
860876
precacheComments(activity, preparedPost, positionInList);
861877
}
@@ -944,6 +960,16 @@ public void onSuccess(final ImageInfo info) {
944960
mPostListingManager.addViewToItems(emptyView);
945961
}
946962

963+
// Show toast if posts were filtered due to few comments (only on first download)
964+
if(firstDownload && filteredCommentCount.get() > 0) {
965+
final String message = getContext().getResources().getQuantityString(
966+
R.plurals.posts_filtered_few_comments,
967+
filteredCommentCount.get(),
968+
filteredCommentCount.get(),
969+
minComments);
970+
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
971+
}
972+
947973
onPostsAdded();
948974

949975
mRequest = null;

src/main/java/org/quantumbadger/redreader/settings/SettingsFragment.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,31 @@ public void onCreatePreferences(
261261
}
262262
}
263263

264+
{
265+
final CheckBoxPreference hideFewCommentsPref =
266+
findPreference(getString(R.string.pref_behaviour_hide_few_comments_key));
267+
268+
if (hideFewCommentsPref != null) {
269+
// Update summary to show current minimum when enabled
270+
updateHideFewCommentsSummary(hideFewCommentsPref);
271+
272+
hideFewCommentsPref.setOnPreferenceChangeListener((preference, newValue) -> {
273+
final boolean isChecked = (Boolean) newValue;
274+
275+
if (isChecked) {
276+
// Show dialog to ask for minimum comment count
277+
showMinCommentsDialog(hideFewCommentsPref);
278+
return false; // Don't change the preference yet
279+
} else {
280+
// Clear the summary when unchecked
281+
hideFewCommentsPref.setSummary(null);
282+
return true;
283+
}
284+
});
285+
}
286+
}
287+
288+
264289

265290
final Preference testNotificationPref =
266291
findPreference(getString(R.string.pref_developer_test_notification_key));
@@ -796,4 +821,56 @@ public void run() {
796821
}.start();
797822

798823
}
824+
825+
private void updateHideFewCommentsSummary(final CheckBoxPreference preference) {
826+
if (preference.isChecked()) {
827+
final Context context = getActivity();
828+
final int minComments = PrefsUtility.pref_behaviour_min_comments();
829+
final String summary = context.getResources().getQuantityString(
830+
R.plurals.pref_behaviour_hide_few_comments_summary,
831+
minComments,
832+
minComments);
833+
preference.setSummary(summary);
834+
} else {
835+
preference.setSummary(null);
836+
}
837+
}
838+
839+
private void showMinCommentsDialog(final CheckBoxPreference preference) {
840+
final Context context = getActivity();
841+
final int currentValue = PrefsUtility.pref_behaviour_min_comments();
842+
843+
// Create an EditText for number input
844+
final android.widget.EditText editText = new android.widget.EditText(context);
845+
editText.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
846+
editText.setText(String.valueOf(currentValue));
847+
editText.setSelection(editText.getText().length());
848+
849+
new MaterialAlertDialogBuilder(context)
850+
.setTitle(R.string.pref_behaviour_min_comments_dialog_title)
851+
.setView(editText)
852+
.setPositiveButton(android.R.string.ok, (dialog, which) -> {
853+
try {
854+
final String input = editText.getText().toString().trim();
855+
final int minComments = Integer.parseInt(input);
856+
857+
if (minComments >= 0 && minComments <= 100000) {
858+
// Save the minimum comments value
859+
PrefsUtility.pref_behaviour_min_comments(minComments);
860+
861+
// Enable the checkbox
862+
preference.setChecked(true);
863+
864+
// Update the summary
865+
updateHideFewCommentsSummary(preference);
866+
} else {
867+
General.quickToast(context, R.string.error_invalid_number_range);
868+
}
869+
} catch (NumberFormatException e) {
870+
General.quickToast(context, R.string.error_invalid_number);
871+
}
872+
})
873+
.setNegativeButton(android.R.string.cancel, null)
874+
.show();
875+
}
799876
}

src/main/res/values/strings.xml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,22 @@
10571057
<string name="pref_behaviour_hide_read_posts_key" translatable="false">pref_behaviour_hide_read_posts</string>
10581058
<string name="pref_behaviour_hide_read_posts_title">Hide read posts</string>
10591059

1060+
<string name="pref_behaviour_hide_few_comments_key" translatable="false">pref_behaviour_hide_few_comments</string>
1061+
<string name="pref_behaviour_hide_few_comments_title">Hide posts with few comments</string>
1062+
<string name="pref_behaviour_min_comments_key" translatable="false">pref_behaviour_min_comments</string>
1063+
<string name="pref_behaviour_min_comments_title">Minimum comments</string>
1064+
<string name="pref_behaviour_min_comments_dialog_title">Minimum number of comments to show post</string>
1065+
1066+
<plurals name="posts_filtered_few_comments">
1067+
<item quantity="one">%1$d post filtered (fewer than %2$d comments)</item>
1068+
<item quantity="other">%1$d posts filtered (fewer than %2$d comments)</item>
1069+
</plurals>
1070+
1071+
<plurals name="pref_behaviour_hide_few_comments_summary">
1072+
<item quantity="one">Minimum %1$d comment</item>
1073+
<item quantity="other">Minimum %1$d comments</item>
1074+
</plurals>
1075+
10601076
<!-- 2018-10-06 -->
10611077
<string name="pref_menus_mainmenu_shortcutitems_title">Shortcuts</string>
10621078
<string name="pref_menus_mainmenu_shortcutitems_key" translatable="false">pref_menus_mainmenu_shortcutitems_key</string>
@@ -1507,7 +1523,7 @@
15071523

15081524
<string name="restore_preferences_error_version_warning_title">Warning</string>
15091525
<string name="restore_preferences_error_version_warning_message">The selected file was created by a newer version of RedReader. Restoring the preferences from this file may lead to crashes and instability.</string>
1510-
1526+
15111527
<string name="button_continue_anyway">Continue anyway</string>
15121528
<string name="button_cancel">Cancel</string>
15131529
<string name="button_restore_preferences">Restore</string>
@@ -1879,7 +1895,7 @@
18791895
<string name="album_pref_all_settings">All settings</string>
18801896

18811897
<string name="album_link_button">Link</string>
1882-
1898+
18831899
<string name="radio_button_selected">Selected</string>
18841900
<string name="radio_button_not_selected">Not selected</string>
18851901

@@ -1891,6 +1907,9 @@
18911907
<string name="error_invalid_account_title">Invalid account</string>
18921908
<string name="error_invalid_account_message">Selected account is not currently logged in</string>
18931909

1910+
<string name="error_invalid_number">Invalid number</string>
1911+
<string name="error_invalid_number_range">Number must be between 0 and 100000</string>
1912+
18941913
<!-- 2024-08-04 -->
18951914
<string name="album_more_options">More settings</string>
18961915

src/main/res/xml/prefs_behaviour.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@
179179
android:key="@string/pref_behaviour_mark_posts_as_read_key"
180180
android:defaultValue="true"/>
181181

182+
<CheckBoxPreference android:title="@string/pref_behaviour_hide_few_comments_title"
183+
android:key="@string/pref_behaviour_hide_few_comments_key"
184+
android:defaultValue="false"/>
185+
182186
<ListPreference android:title="@string/pref_behaviour_postcount_title"
183187
android:key="@string/pref_behaviour_postcount_key"
184188
android:entries="@array/pref_behaviour_postcount_items"

0 commit comments

Comments
 (0)