Skip to content

Commit

Permalink
Accessibility: header for comments
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumBadger committed Nov 20, 2020
1 parent 8e5bfe0 commit 253090f
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,42 @@ public CharSequence getHeader(
return header;
}

@Override
public String getAccessibilityHeader(
final RRThemeAttributes theme,
final RedditChangeDataManager changeDataManager,
final Context context,
final int commentAgeUnits,
final long postCreated,
final long parentCommentCreated,
final boolean collapsed) {

final StringBuilder accessibilityHeader = new StringBuilder();
final String separator = " \n";

if(src.author != null) {
accessibilityHeader
.append(context.getString(
R.string.accessibility_subtitle_author_withperiod,
src.author))
.append(separator);
}

accessibilityHeader
.append(context.getString(
R.string.accessibility_subtitle_age_withperiod,
RRTime.formatDurationFrom(
context,
src.created_utc * 1000L,
R.string.time_ago,
PrefsUtility.appearance_inbox_age_units(
context,
General.getSharedPrefs(context)))))
.append(separator);

return accessibilityHeader.toString();
}

@Override
public View getBody(
final BaseActivity activity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import androidx.annotation.NonNull;
import org.quantumbadger.redreader.R;
import org.quantumbadger.redreader.account.RedditAccountManager;
import org.quantumbadger.redreader.activities.BaseActivity;
Expand Down Expand Up @@ -219,42 +220,13 @@ public CharSequence getHeader(

if(theme.shouldShow(PrefsUtility.AppearanceCommentHeaderItem.AGE)) {
final long commentTime = rawComment.created_utc * 1000L;
final String formattedAge;

//In addition to enforcing the user's prefs, the lower mode cases also act as fallbacks
switch(commentAgeMode) {
case RELATIVE_PARENT:
if(parentCommentCreated != NO_TIMESTAMP) {
formattedAge = RRTime.formatDurationFrom(
context,
parentCommentCreated * 1000L,
commentTime,
R.string.time_after_reply,
commentAgeUnits);
break;
}
//Top-level comment (or unable to get the parent comment's creation time)
case RELATIVE_POST:
if(postCreated != NO_TIMESTAMP) {
formattedAge = RRTime.formatDurationFrom(
context,
postCreated * 1000L,
commentTime,
R.string.time_after,
commentAgeUnits);
break;
}
//Unable to get post creation date, resorting to absolute age
case ABSOLUTE:
formattedAge = RRTime.formatDurationFrom(
context,
commentTime,
R.string.time_ago,
commentAgeUnits);
break;
default:
throw new IllegalStateException("Unexpected value: " + commentAgeMode);
}
final String formattedAge = formatAge(
context,
commentAgeMode,
commentAgeUnits,
commentTime,
postCreated,
parentCommentCreated);

sb.append(
formattedAge,
Expand All @@ -276,6 +248,149 @@ public CharSequence getHeader(
return sb.get();
}

@Override
public String getAccessibilityHeader(
final RRThemeAttributes theme,
final RedditChangeDataManager changeDataManager,
final Context context,
final int commentAgeUnits,
final long postCreated,
final long parentCommentCreated,
final boolean collapsed) {

final PrefsUtility.CommentAgeMode commentAgeMode = PrefsUtility.appearance_comment_age_mode(
context,
General.getSharedPrefs(context));

final StringBuilder accessibilityHeader = new StringBuilder();

final RedditComment rawComment = mComment.getRawComment();

final String separator = " \n";

if(collapsed) {
accessibilityHeader
.append(context.getString(R.string.accessibility_subtitle_comment_collapsed))
.append(separator);
}

if(theme.shouldShow(PrefsUtility.AppearanceCommentHeaderItem.AUTHOR)) {
accessibilityHeader
.append(context.getString(
R.string.accessibility_subtitle_author_withperiod,
rawComment.author))
.append(separator);
}

final String flair = mComment.getFlair();

if(theme.shouldShow(PrefsUtility.AppearanceCommentHeaderItem.FLAIR)
&& flair != null
&& flair.length() > 0) {

accessibilityHeader
.append(context.getString(
R.string.accessibility_subtitle_flair_withperiod,
flair + General.LTR_OVERRIDE_MARK))
.append(separator);
}

if(theme.shouldShow(PrefsUtility.AppearanceCommentHeaderItem.SCORE) && mShowScore) {

if(Boolean.TRUE.equals(rawComment.score_hidden)) {
accessibilityHeader
.append(context.getString(
R.string.accessibility_subtitle_points_unknown_withperiod))
.append(separator);

} else {
accessibilityHeader
.append(context.getString(
R.string.accessibility_subtitle_points_withperiod,
computeScore(changeDataManager)))
.append(separator);
}
}

if(theme.shouldShow(PrefsUtility.AppearanceCommentHeaderItem.GOLD)) {

if(rawComment.gilded > 0) {
accessibilityHeader
.append(context.getString(
R.string.accessibility_subtitle_gold_withperiod,
rawComment.gilded))
.append(separator);
}
}

if(theme.shouldShow(PrefsUtility.AppearanceCommentHeaderItem.AGE)) {
final long commentTime = rawComment.created_utc * 1000L;
final String formattedAge = formatAge(
context,
commentAgeMode,
commentAgeUnits,
commentTime,
postCreated,
parentCommentCreated);

accessibilityHeader
.append(context.getString(
R.string.accessibility_subtitle_age_withperiod,
formattedAge))
.append(separator);

if(rawComment.edited instanceof Long) {
accessibilityHeader
.append(context.getString(
R.string.accessibility_subtitle_edited_since_being_posted))
.append(separator);
}
}

return accessibilityHeader.toString();
}

@NonNull
private String formatAge(
@NonNull final Context context,
@NonNull final PrefsUtility.CommentAgeMode commentAgeMode,
@NonNull final int commentAgeUnits,
final long commentTime,
final long postCreated,
final long parentCommentCreated) {
//In addition to enforcing the user's prefs, the lower mode cases also act as fallbacks
switch(commentAgeMode) {
case RELATIVE_PARENT:
if(parentCommentCreated != NO_TIMESTAMP) {
return RRTime.formatDurationFrom(
context,
parentCommentCreated * 1000L,
commentTime,
R.string.time_after_reply,
commentAgeUnits);
}
//Top-level comment (or unable to get the parent comment's creation time)
case RELATIVE_POST:
if(postCreated != NO_TIMESTAMP) {
return RRTime.formatDurationFrom(
context,
postCreated * 1000L,
commentTime,
R.string.time_after,
commentAgeUnits);
}
//Unable to get post creation date, resorting to absolute age
case ABSOLUTE:
return RRTime.formatDurationFrom(
context,
commentTime,
R.string.time_ago,
commentAgeUnits);
default:
throw new IllegalStateException("Unexpected value: " + commentAgeMode);
}
}

@Override
public View getBody(
final BaseActivity activity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ CharSequence getHeader(
final long postCreated,
final long parentCommentCreated);

String getAccessibilityHeader(
final RRThemeAttributes theme,
final RedditChangeDataManager changeDataManager,
final Context context,
final int commentAgeUnits,
final long postCreated,
final long parentCommentCreated,
final boolean collapsed);

View getBody(
final BaseActivity activity,
final Integer textColor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,22 +366,40 @@ public void reset(

final RedditRenderableComment renderableComment = mComment.asComment();

final int ageUnits = PrefsUtility.appearance_comment_age_units(
activity,
General.getSharedPrefs(activity));

final long postTimestamp = (mFragment != null && mFragment.getPost() != null)
? mFragment.getPost().src.getCreatedTimeSecsUTC()
: RedditRenderableComment.NO_TIMESTAMP;

final long parentCommentTimestamp = mComment.getParent() != null
? mComment.getParent().asComment().getParsedComment().getRawComment().created_utc
: RedditRenderableComment.NO_TIMESTAMP;

final boolean isCollapsed = mComment.isCollapsed(mChangeDataManager);

final CharSequence headerText = renderableComment.getHeader(
mTheme,
mChangeDataManager,
activity,
PrefsUtility.appearance_comment_age_units(
activity,
General.getSharedPrefs(activity)),
(mFragment != null && mFragment.getPost() != null)
? mFragment.getPost().src.getCreatedTimeSecsUTC()
: RedditRenderableComment.NO_TIMESTAMP,
mComment.getParent() != null
? mComment.getParent()
.asComment().getParsedComment().getRawComment().created_utc
: RedditRenderableComment.NO_TIMESTAMP);

if(mComment.isCollapsed(mChangeDataManager)) {
ageUnits,
postTimestamp,
parentCommentTimestamp);

final String accessibilityHeader = renderableComment.getAccessibilityHeader(
mTheme,
mChangeDataManager,
activity,
ageUnits,
postTimestamp,
parentCommentTimestamp,
isCollapsed);

mHeader.setContentDescription(accessibilityHeader);

if(isCollapsed) {
setFlingingEnabled(false);
//noinspection SetTextI18n
mHeader.setText("[ + ] "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ public void reset(
RedditRenderableComment.NO_TIMESTAMP,
RedditRenderableComment.NO_TIMESTAMP));

mHeader.setContentDescription(item.getAccessibilityHeader(
theme,
changeDataManager,
context,
PrefsUtility.appearance_inbox_age_units(context, General.getSharedPrefs(context)),
RedditRenderableComment.NO_TIMESTAMP,
RedditRenderableComment.NO_TIMESTAMP,
false));

final View body = item.getBody(
context,
mTheme.rrCommentBodyCol,
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 @@ -1391,6 +1391,9 @@
<string name="accessibility_subtitle_subreddit_withperiod">Sub-reddit: %s.</string>
<string name="accessibility_subtitle_domain_withperiod">Domain: %s.</string>
<string name="accessibility_subtitle_selfpost_withperiod">Text post.</string>
<string name="accessibility_subtitle_points_unknown_withperiod">Score hidden.</string>
<string name="accessibility_subtitle_edited_since_being_posted">Text edited since being posted.</string>
<string name="accessibility_subtitle_comment_collapsed">Comment text hidden.</string>

<string name="accessibility_subtitle_domain_i_redd_it">Reddit images</string>
<string name="accessibility_subtitle_domain_v_redd_it">Reddit videos</string>
Expand Down

0 comments on commit 253090f

Please sign in to comment.