Skip to content

Commit 52fa931

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 f7c9b90 commit 52fa931

File tree

6 files changed

+154
-2
lines changed

6 files changed

+154
-2
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ public static boolean isRefreshRequired(final Context context, final String key)
114114
|| key.equals(context.getString(R.string.pref_behaviour_nsfw_key))
115115
|| key.equals(context.getString(R.string.pref_behaviour_postcount_key))
116116
|| key.equals(context.getString(R.string.pref_behaviour_comment_min_key))
117+
|| key.equals(context.getString(R.string.pref_behaviour_hide_few_comments_key))
118+
|| key.equals(context.getString(R.string.pref_behaviour_min_comments_key))
117119
|| key.equals(context.getString(R.string.pref_behaviour_pinned_subredditsort_key))
118120
|| key.equals(context.getString(
119121
R.string.pref_behaviour_blocked_subredditsort_key))
@@ -1163,6 +1165,28 @@ public static boolean pref_behaviour_hide_read_posts() {
11631165
false);
11641166
}
11651167

1168+
public static boolean pref_behaviour_hide_few_comments() {
1169+
return getBoolean(
1170+
R.string.pref_behaviour_hide_few_comments_key,
1171+
false);
1172+
}
1173+
1174+
public static int pref_behaviour_min_comments() {
1175+
try {
1176+
return Integer.parseInt(getString(
1177+
R.string.pref_behaviour_min_comments_key,
1178+
"10"));
1179+
} catch(final Throwable e) {
1180+
return 10;
1181+
}
1182+
}
1183+
1184+
public static void pref_behaviour_min_comments(final int value) {
1185+
sharedPrefs.edit()
1186+
.putString(getPrefKey(R.string.pref_behaviour_min_comments_key), String.valueOf(value))
1187+
.apply();
1188+
}
1189+
11661190
public enum SharingDomain {
11671191
STANDARD_REDDIT("reddit.com"),
11681192
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
@@ -732,6 +732,15 @@ public void onDataStreamComplete(
732732
&& mPostListingURL.pathType()
733733
!= RedditURLParser.USER_POST_LISTING_URL;
734734

735+
final boolean hideFewComments
736+
= PrefsUtility.pref_behaviour_hide_few_comments();
737+
738+
final int minComments = hideFewComments
739+
? PrefsUtility.pref_behaviour_min_comments()
740+
: 0;
741+
742+
final AtomicInteger filteredCommentCount = new AtomicInteger(0);
743+
735744
final boolean isConnectionWifi = General.isConnectionWifi(activity);
736745

737746
final boolean inlinePreviews
@@ -875,6 +884,13 @@ public void onDataStreamComplete(
875884
continue;
876885
}
877886

887+
// Skip adding this post if it has too few comments
888+
if(hideFewComments && post.getNum_comments() < minComments) {
889+
mPostsNotShown = true;
890+
filteredCommentCount.incrementAndGet();
891+
continue;
892+
}
893+
878894
if(precacheComments) {
879895
precacheComments(activity, preparedPost, positionInList);
880896
}
@@ -963,6 +979,16 @@ public void onSuccess(final ImageInfo info) {
963979
mPostListingManager.addViewToItems(emptyView);
964980
}
965981

982+
// Show toast if posts were filtered due to few comments (only on first download)
983+
if(firstDownload && filteredCommentCount.get() > 0) {
984+
final String message = getContext().getResources().getQuantityString(
985+
R.plurals.posts_filtered_few_comments,
986+
filteredCommentCount.get(),
987+
filteredCommentCount.get(),
988+
minComments);
989+
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
990+
}
991+
966992
onPostsAdded();
967993

968994
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
@@ -1062,6 +1062,22 @@
10621062
<string name="pref_behaviour_hide_read_posts_key" translatable="false">pref_behaviour_hide_read_posts</string>
10631063
<string name="pref_behaviour_hide_read_posts_title">Hide read posts</string>
10641064

1065+
<string name="pref_behaviour_hide_few_comments_key" translatable="false">pref_behaviour_hide_few_comments</string>
1066+
<string name="pref_behaviour_hide_few_comments_title">Hide posts with few comments</string>
1067+
<string name="pref_behaviour_min_comments_key" translatable="false">pref_behaviour_min_comments</string>
1068+
<string name="pref_behaviour_min_comments_title">Minimum comments</string>
1069+
<string name="pref_behaviour_min_comments_dialog_title">Minimum number of comments to show post</string>
1070+
1071+
<plurals name="posts_filtered_few_comments">
1072+
<item quantity="one">%1$d post filtered (fewer than %2$d comments)</item>
1073+
<item quantity="other">%1$d posts filtered (fewer than %2$d comments)</item>
1074+
</plurals>
1075+
1076+
<plurals name="pref_behaviour_hide_few_comments_summary">
1077+
<item quantity="one">Minimum %1$d comment</item>
1078+
<item quantity="other">Minimum %1$d comments</item>
1079+
</plurals>
1080+
10651081
<!-- 2018-10-06 -->
10661082
<string name="pref_menus_mainmenu_shortcutitems_title">Shortcuts</string>
10671083
<string name="pref_menus_mainmenu_shortcutitems_key" translatable="false">pref_menus_mainmenu_shortcutitems_key</string>
@@ -1511,7 +1527,7 @@
15111527

15121528
<string name="restore_preferences_error_version_warning_title">Warning</string>
15131529
<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>
1514-
1530+
15151531
<string name="button_continue_anyway">Continue anyway</string>
15161532
<string name="button_cancel">Cancel</string>
15171533
<string name="button_restore_preferences">Restore</string>
@@ -1883,7 +1899,7 @@
18831899
<string name="album_pref_all_settings">All settings</string>
18841900

18851901
<string name="album_link_button">Link</string>
1886-
1902+
18871903
<string name="radio_button_selected">Selected</string>
18881904
<string name="radio_button_not_selected">Not selected</string>
18891905

@@ -1895,6 +1911,9 @@
18951911
<string name="error_invalid_account_title">Invalid account</string>
18961912
<string name="error_invalid_account_message">Selected account is not currently logged in</string>
18971913

1914+
<string name="error_invalid_number">Invalid number</string>
1915+
<string name="error_invalid_number_range">Number must be between 0 and 100000</string>
1916+
18981917
<!-- 2024-08-04 -->
18991918
<string name="album_more_options">More settings</string>
19001919

src/main/res/xml/prefs_behaviour.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@
175175
android:key="@string/pref_behaviour_hide_read_posts_key"
176176
android:defaultValue="false"/>
177177

178+
<CheckBoxPreference android:title="@string/pref_behaviour_hide_few_comments_title"
179+
android:key="@string/pref_behaviour_hide_few_comments_key"
180+
android:defaultValue="false"/>
181+
178182
<ListPreference android:title="@string/pref_behaviour_postcount_title"
179183
android:key="@string/pref_behaviour_postcount_key"
180184
android:entries="@array/pref_behaviour_postcount_items"

0 commit comments

Comments
 (0)