Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Add setting to collapse parents #2433

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
}
{
SwitchCompat single = (SwitchCompat) findViewById(R.id.collapse_parents);
single.setChecked(SettingValues.collapseParents);
single.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SettingValues.collapseParents = isChecked;
SettingValues.prefs.edit().putBoolean(SettingValues.PREF_COLLAPSE_PARENTS, isChecked).apply();
}
});
}
{
SwitchCompat single = (SwitchCompat) findViewById(R.id.collapse_comments_default);
single.setChecked(SettingValues.collapseCommentsDefault);
Expand Down
176 changes: 105 additions & 71 deletions app/src/main/java/me/ccrama/redditslide/Adapters/CommentAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public class CommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
long lastSeen = 0;
public ArrayList<String> approved = new ArrayList<>();
public ArrayList<String> removed = new ArrayList<>();
private Map<String, CommentViewHolder> topLevels = new HashMap<>();

public CommentAdapter(CommentPage mContext, SubmissionComments dataSet, RecyclerView listView,
Submission submission, FragmentManager fm) {
Expand Down Expand Up @@ -301,6 +302,9 @@ public void onBindViewHolder(final RecyclerView.ViewHolder firstHolder, int old)

final CommentNode baseNode = currentComments.get(datasetPosition).comment;
final Comment comment = baseNode.getComment();
if (baseNode.isTopLevel()) {
topLevels.put(comment.getFullName(), holder);
}

if (pos == getItemCount() - 1) {
holder.itemView.setPadding(0, 0, 0, (int) mContext.getResources()
Expand Down Expand Up @@ -1728,38 +1732,11 @@ public void doOnClick(CommentViewHolder holder, Comment comment, CommentNode bas

public void doOnClick(final CommentViewHolder holder, final CommentNode baseNode,
final Comment comment) {

if (currentlyEditing != null
&& !currentlyEditing.getText().toString().isEmpty()
&& holder.getAdapterPosition() <= editingPosition) {
new AlertDialogWrapper.Builder(mContext).setTitle(R.string.discard_comment_title)
.setMessage(R.string.comment_discard_msg)
.setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

currentlyEditing = null;
editingPosition = -1;
if (SettingValues.fastscroll) {
mPage.fastScroll.setVisibility(View.VISIBLE);
}
if (mPage.fab != null) mPage.fab.setVisibility(View.VISIBLE);
mPage.overrideFab = false;
currentlyEditingId = "";
backedText = "";
View view = ((Activity) mContext).getCurrentFocus();
if (view != null) {
InputMethodManager imm =
(InputMethodManager) mContext.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

doOnClick(holder, baseNode, comment);

}
})
.setNegativeButton(R.string.btn_no, null)
.show();
collapseEditingComment(holder, baseNode, comment);

} else {
if (isClicking) {
Expand All @@ -1768,56 +1745,106 @@ public void onClick(DialogInterface dialog, int which) {
isHolder.itemView.findViewById(R.id.menu).setVisibility(View.GONE);
} else {
if (hiddenPersons.contains(comment.getFullName())) {
hiddenPersons.remove(comment.getFullName());
unhideAll(baseNode, holder.getAdapterPosition() + 1);
unhideComments(holder, baseNode, comment);

if (toCollapse.contains(comment.getFullName())
&& SettingValues.collapseComments) {
setViews(comment.getDataNode().get("body_html").asText(),
submission.getSubredditName(), holder);
}
} else {
hideComments(holder, baseNode, comment);
}
clickpos = holder.getAdapterPosition() + 1;
}
}
}

CommentAdapterHelper.hideChildrenObject(holder.childrenNumber);
if (!holder.firstTextView.getText().toString().isEmpty()) {
holder.firstTextView.setVisibility(View.VISIBLE);
} else {
holder.firstTextView.setVisibility(View.GONE);
}
holder.commentOverflow.setVisibility(View.VISIBLE);
private void hideComments(CommentViewHolder cvh, CommentNode cn, Comment c) {
final CommentNode node = (SettingValues.collapseParents
? getTopParent(cn)
: cn);
final Comment comment = (SettingValues.collapseParents
? node.getComment()
: c);
final CommentViewHolder holder = (SettingValues.collapseParents
? topLevels.get(comment.getFullName())
: cvh);
int childNumber = getChildNumber(node);
if (childNumber > 0) {
hideAll(node, holder.getAdapterPosition() + 1);
if (!hiddenPersons.contains(comment.getFullName())) {
hiddenPersons.add(comment.getFullName());
}
if (childNumber > 0) {
CommentAdapterHelper.showChildrenObject(holder.childrenNumber);
holder.childrenNumber.setText("+" + childNumber);
}
}
toCollapse.add(comment.getFullName());
if ((holder.firstTextView.getVisibility() == View.VISIBLE
|| holder.commentOverflow.getVisibility() == View.VISIBLE)
&& SettingValues.collapseComments) {
holder.firstTextView.setVisibility(View.GONE);
holder.commentOverflow.setVisibility(View.GONE);
} else if (SettingValues.collapseComments) {
if (!holder.firstTextView.getText().toString().isEmpty()) {
holder.firstTextView.setVisibility(View.VISIBLE);
} else {
holder.firstTextView.setVisibility(View.GONE);
}
holder.commentOverflow.setVisibility(View.VISIBLE);
}
}

private void unhideComments(CommentViewHolder holder, CommentNode baseNode, Comment comment) {
hiddenPersons.remove(comment.getFullName());
unhideAll(baseNode, holder.getAdapterPosition() + 1);

toCollapse.remove(comment.getFullName());
if (toCollapse.contains(comment.getFullName())
&& SettingValues.collapseComments) {
setViews(comment.getDataNode().get("body_html").asText(),
submission.getSubredditName(), holder);
}

} else {
int childNumber = getChildNumber(baseNode);
if (childNumber > 0) {
hideAll(baseNode, holder.getAdapterPosition() + 1);
if (!hiddenPersons.contains(comment.getFullName())) {
hiddenPersons.add(comment.getFullName());
}
if (childNumber > 0) {
CommentAdapterHelper.showChildrenObject(holder.childrenNumber);
holder.childrenNumber.setText("+" + childNumber);
CommentAdapterHelper.hideChildrenObject(holder.childrenNumber);
if (!holder.firstTextView.getText().toString().isEmpty()) {
holder.firstTextView.setVisibility(View.VISIBLE);
} else {
holder.firstTextView.setVisibility(View.GONE);
}
holder.commentOverflow.setVisibility(View.VISIBLE);


toCollapse.remove(comment.getFullName());
}

private void collapseEditingComment(final CommentViewHolder holder, final CommentNode baseNode,
final Comment comment) {
new AlertDialogWrapper.Builder(mContext).setTitle(R.string.discard_comment_title)
.setMessage(R.string.comment_discard_msg)
.setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

currentlyEditing = null;
editingPosition = -1;
if (SettingValues.fastscroll) {
mPage.fastScroll.setVisibility(View.VISIBLE);
}
}
toCollapse.add(comment.getFullName());
if ((holder.firstTextView.getVisibility() == View.VISIBLE
|| holder.commentOverflow.getVisibility() == View.VISIBLE)
&& SettingValues.collapseComments) {
holder.firstTextView.setVisibility(View.GONE);
holder.commentOverflow.setVisibility(View.GONE);
} else if (SettingValues.collapseComments) {
if (!holder.firstTextView.getText().toString().isEmpty()) {
holder.firstTextView.setVisibility(View.VISIBLE);
} else {
holder.firstTextView.setVisibility(View.GONE);
if (mPage.fab != null) mPage.fab.setVisibility(View.VISIBLE);
mPage.overrideFab = false;
currentlyEditingId = "";
backedText = "";
View view = ((Activity) mContext).getCurrentFocus();
if (view != null) {
InputMethodManager imm =
(InputMethodManager) mContext.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
holder.commentOverflow.setVisibility(View.VISIBLE);

doOnClick(holder, baseNode, comment);

}
}
clickpos = holder.getAdapterPosition() + 1;
}
}
})
.setNegativeButton(R.string.btn_no, null)
.show();
}

private int getChildNumber(CommentNode user) {
Expand Down Expand Up @@ -2435,4 +2462,11 @@ private RedditClient getAuthenticatedClient(String profileName) {
Authentication.doVerify(token, reddit, true, mContext);
return reddit;
}

private CommentNode getTopParent(CommentNode comment) {
if (comment.isTopLevel()) {
return comment;
}
return getTopParent(comment.getParent());
}
}
3 changes: 3 additions & 0 deletions app/src/main/java/me/ccrama/redditslide/SettingValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class SettingValues {
public static final String PREF_COMMENT_PAGER = "commentPager";
public static final String PREF_COLLAPSE_COMMENTS = "collapseCOmments";
public static final String PREF_COLLAPSE_COMMENTS_DEFAULT = "collapseCommentsDefault";
public static final String PREF_COLLAPSE_PARENTS = "collapseParents";
public static final String PREF_RIGHT_HANDED_COMMENT_MENU = "rightHandedCommentMenu";
public static final String PREF_DUAL_PORTRAIT = "dualPortrait";
public static final String PREF_CROP_IMAGE = "cropImage";
Expand Down Expand Up @@ -215,6 +216,7 @@ public class SettingValues {
public static boolean colorIcon;
public static boolean peek;
public static boolean largeLinks;
public static boolean collapseParents;

public static void setAllValues(SharedPreferences settings) {
prefs = settings;
Expand Down Expand Up @@ -270,6 +272,7 @@ public static void setAllValues(SharedPreferences settings) {
alwaysZoom = prefs.getBoolean(PREF_ZOOM_DEFAULT, true);
collapseComments = prefs.getBoolean(PREF_COLLAPSE_COMMENTS, false);
collapseCommentsDefault = prefs.getBoolean(PREF_COLLAPSE_COMMENTS_DEFAULT, false);
collapseParents = prefs.getBoolean(PREF_COLLAPSE_PARENTS, false);
rightHandedCommentMenu = prefs.getBoolean(PREF_RIGHT_HANDED_COMMENT_MENU, false);
commentAutoHide = prefs.getBoolean(PREF_AUTOHIDE_COMMENTS, false);

Expand Down
44 changes: 44 additions & 0 deletions app/src/main/res/layout/activity_settings_comments.xml
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,50 @@
android:hapticFeedbackEnabled="true"
android:textColor="?attr/font"
android:textColorHint="?attr/font" />

</RelativeLayout>

<View
android:layout_width="match_parent"
android:background="?attr/tint"
android:alpha=".25"
android:layout_height="0.25dp"/>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="?android:selectableItemBackground"
android:paddingStart="16dp">

<LinearLayout
android:layout_marginEnd="64dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings_comment_collapse_parents"
android:textColor="?attr/font"
android:textSize="14sp" />
</LinearLayout>

<android.support.v7.widget.SwitchCompat
android:id="@+id/collapse_parents"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingEnd="16dp"
android:backgroundTint="?attr/tint"
android:button="@null"
android:buttonTint="?attr/tint"
android:hapticFeedbackEnabled="true"
android:textColor="?attr/font"
android:textColorHint="?attr/font" />

</RelativeLayout>
</LinearLayout>
</ScrollView>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1161,5 +1161,6 @@
<string name="theme_pixel">Pixel</string>
<string name="save_images_to_subreddit_specific_subfolders">Save images to subreddit-specific subfolders</string>
<string name="description_save_subfolders">Works in submission and comment views</string>
<string name="settings_comment_collapse_parents">Collapse parents</string>

</resources>