Click subreddit names and user names in post content and comment content to start ViewSubredditDetailActiviy and ViewUserDetailActivity respectively. Use Chrome custom tab to open URL in post content and comment content.

This commit is contained in:
Alex Ning 2019-01-21 16:50:10 +08:00
parent 047e31936f
commit 86bc381906
9 changed files with 199 additions and 155 deletions

Binary file not shown.

View File

@ -14,9 +14,9 @@ public interface SubredditDao {
@Query("DELETE FROM subreddits") @Query("DELETE FROM subreddits")
void deleteAllSubreddits(); void deleteAllSubreddits();
@Query("SELECT * from subreddits WHERE name = :namePrefixed LIMIT 1") @Query("SELECT * from subreddits WHERE name = :namePrefixed COLLATE NOCASE LIMIT 1")
LiveData<SubredditData> getSubredditLiveDataByName(String namePrefixed); LiveData<SubredditData> getSubredditLiveDataByName(String namePrefixed);
@Query("SELECT * from subreddits WHERE name = :namePrefixed LIMIT 1") @Query("SELECT * from subreddits WHERE name = :namePrefixed COLLATE NOCASE LIMIT 1")
SubredditData getSubredditData(String namePrefixed); SubredditData getSubredditData(String namePrefixed);
} }

View File

@ -19,7 +19,7 @@ public interface SubscribedSubredditDao {
@Query("SELECT * from subscribed_subreddits ORDER BY name COLLATE NOCASE ASC") @Query("SELECT * from subscribed_subreddits ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits(); LiveData<List<SubscribedSubredditData>> getAllSubscribedSubreddits();
@Query("SELECT * from subscribed_subreddits WHERE name = :subredditName LIMIT 1") @Query("SELECT * from subscribed_subreddits WHERE name = :subredditName COLLATE NOCASE LIMIT 1")
SubscribedSubredditData getSubscribedSubreddit(String subredditName); SubscribedSubredditData getSubscribedSubreddit(String subredditName);
@Query("DELETE FROM subscribed_subreddits WHERE name = :subredditName") @Query("DELETE FROM subscribed_subreddits WHERE name = :subredditName")

View File

@ -19,7 +19,7 @@ public interface SubscribedUserDao {
@Query("SELECT * FROM subscribed_users ORDER BY name COLLATE NOCASE ASC") @Query("SELECT * FROM subscribed_users ORDER BY name COLLATE NOCASE ASC")
LiveData<List<SubscribedUserData>> getAllSubscribedUsers(); LiveData<List<SubscribedUserData>> getAllSubscribedUsers();
@Query("SELECT * FROM subscribed_users WHERE name = :userName LIMIT 1") @Query("SELECT * FROM subscribed_users WHERE name = :userName COLLATE NOCASE LIMIT 1")
SubscribedUserData getSubscribedUser(String userName); SubscribedUserData getSubscribedUser(String userName);
@Query("DELETE FROM subscribed_users WHERE name = :userName") @Query("DELETE FROM subscribed_users WHERE name = :userName")

View File

@ -14,9 +14,9 @@ public interface UserDao {
@Query("DELETE FROM users") @Query("DELETE FROM users")
void deleteAllUsers(); void deleteAllUsers();
@Query("SELECT * FROM users WHERE name = :userName LIMIT 1") @Query("SELECT * FROM users WHERE name = :userName COLLATE NOCASE LIMIT 1")
LiveData<UserData> getUserLiveData(String userName); LiveData<UserData> getUserLiveData(String userName);
@Query("SELECT * FROM users WHERE name = :userName LIMIT 1") @Query("SELECT * FROM users WHERE name = :userName COLLATE NOCASE LIMIT 1")
UserData getUserData(String userName); UserData getUserData(String userName);
} }

View File

@ -1,9 +1,12 @@
package ml.docilealligator.infinityforreddit; package ml.docilealligator.infinityforreddit;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.ColorFilter; import android.graphics.ColorFilter;
import android.net.Uri;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.customtabs.CustomTabsIntent;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -25,6 +28,7 @@ import java.util.Locale;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import ru.noties.markwon.SpannableConfiguration;
import ru.noties.markwon.view.MarkwonView; import ru.noties.markwon.view.MarkwonView;
class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter { class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
@ -66,7 +70,26 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
((CommentViewHolder) holder).authorTextView.setText(commentItem.getAuthor()); ((CommentViewHolder) holder).authorTextView.setText(commentItem.getAuthor());
((CommentViewHolder) holder).commentTimeTextView.setText(commentItem.getCommentTime()); ((CommentViewHolder) holder).commentTimeTextView.setText(commentItem.getCommentTime());
((CommentViewHolder) holder).commentMarkdownView.setMarkdown(commentItem.getCommentContent()); SpannableConfiguration spannableConfiguration = SpannableConfiguration.builder(mContext).linkResolver((view, link) -> {
if(link.startsWith("/u/")) {
Intent intent = new Intent(mContext, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, link.substring(3));
mContext.startActivity(intent);
} else if(link.startsWith("/r/")) {
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, link.substring(3));
mContext.startActivity(intent);
} else {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
// add share action to menu list
builder.addDefaultShareMenuItem();
builder.setToolbarColor(mContext.getResources().getColor(R.color.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(mContext, Uri.parse(link));
}
}).build();
((CommentViewHolder) holder).commentMarkdownView.setMarkdown(spannableConfiguration, commentItem.getCommentContent());
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore())); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
((CommentViewHolder) holder).verticalBlock.getLayoutParams().width = commentItem.getDepth() * 16; ((CommentViewHolder) holder).verticalBlock.getLayoutParams().width = commentItem.getDepth() * 16;
@ -74,44 +97,41 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
setExpandButton(((CommentViewHolder) holder).expandButton, commentItem.isExpanded()); setExpandButton(((CommentViewHolder) holder).expandButton, commentItem.isExpanded());
} }
((CommentViewHolder) holder).expandButton.setOnClickListener(new View.OnClickListener() { ((CommentViewHolder) holder).expandButton.setOnClickListener(view -> {
@Override if(commentItem.hasChildren() && commentItem.getChildren().size() > 0) {
public void onClick(View view) { mMultiLevelRecyclerView.toggleItemsGroup(holder.getAdapterPosition());
if(commentItem.hasChildren() && commentItem.getChildren().size() > 0) { setExpandButton(((CommentViewHolder) holder).expandButton, commentItem.isExpanded());
mMultiLevelRecyclerView.toggleItemsGroup(holder.getAdapterPosition()); } else {
setExpandButton(((CommentViewHolder) holder).expandButton, commentItem.isExpanded()); ((CommentViewHolder) holder).loadMoreCommentsProgressBar.setVisibility(View.VISIBLE);
} else { FetchComment.fetchComment(mRetrofit, subredditNamePrefixed, article, commentItem.getId(),
((CommentViewHolder) holder).loadMoreCommentsProgressBar.setVisibility(View.VISIBLE); new FetchComment.FetchCommentListener() {
FetchComment.fetchComment(mRetrofit, subredditNamePrefixed, article, commentItem.getId(), @Override
new FetchComment.FetchCommentListener() { public void onFetchCommentSuccess(String response) {
@Override ParseComment.parseComment(response, new ArrayList<CommentData>(),
public void onFetchCommentSuccess(String response) { locale, false, commentItem.getDepth(), new ParseComment.ParseCommentListener() {
ParseComment.parseComment(response, new ArrayList<CommentData>(), @Override
locale, false, commentItem.getDepth(), new ParseComment.ParseCommentListener() { public void onParseCommentSuccess(List<?> commentData, int moreCommentCount) {
@Override commentItem.addChildren((List<RecyclerViewItem>) commentData);
public void onParseCommentSuccess(List<?> commentData, int moreCommentCount) { ((CommentViewHolder) holder).loadMoreCommentsProgressBar
commentItem.addChildren((List<RecyclerViewItem>) commentData); .setVisibility(View.GONE);
((CommentViewHolder) holder).loadMoreCommentsProgressBar mMultiLevelRecyclerView.toggleItemsGroup(holder.getAdapterPosition());
.setVisibility(View.GONE); ((CommentViewHolder) holder).expandButton
mMultiLevelRecyclerView.toggleItemsGroup(holder.getAdapterPosition()); .setImageResource(R.drawable.ic_expand_less_black_20dp);
((CommentViewHolder) holder).expandButton }
.setImageResource(R.drawable.ic_expand_less_black_20dp);
}
@Override @Override
public void onParseCommentFail() { public void onParseCommentFail() {
((CommentViewHolder) holder).loadMoreCommentsProgressBar ((CommentViewHolder) holder).loadMoreCommentsProgressBar
.setVisibility(View.GONE); .setVisibility(View.GONE);
} }
}); });
} }
@Override @Override
public void onFetchCommentFail() { public void onFetchCommentFail() {
} }
}); });
}
} }
}); });
@ -126,120 +146,114 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
break; break;
} }
((CommentViewHolder) holder).upvoteButton.setOnClickListener(new View.OnClickListener() { ((CommentViewHolder) holder).upvoteButton.setOnClickListener(view -> {
@Override final boolean isDownvotedBefore = ((CommentViewHolder) holder).downvoteButton.getColorFilter() != null;
public void onClick(View view) { final ColorFilter minusButtonColorFilter = ((CommentViewHolder) holder).downvoteButton.getColorFilter();
final boolean isDownvotedBefore = ((CommentViewHolder) holder).downvoteButton.getColorFilter() != null; ((CommentViewHolder) holder).downvoteButton.clearColorFilter();
final ColorFilter minusButtonColorFilter = ((CommentViewHolder) holder).downvoteButton.getColorFilter();
((CommentViewHolder) holder).downvoteButton.clearColorFilter();
if (((CommentViewHolder) holder).upvoteButton.getColorFilter() == null) { if (((CommentViewHolder) holder).upvoteButton.getColorFilter() == null) {
((CommentViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
if(isDownvotedBefore) { if(isDownvotedBefore) {
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 2)); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 2));
} else {
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1));
}
VoteThing.voteThing(mOauthRetrofit,mSharedPreferences, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position) {
commentItem.setVoteType(1);
if(isDownvotedBefore) {
commentItem.setScore(commentItem.getScore() + 2);
} else {
commentItem.setScore(commentItem.getScore() + 1);
}
}
@Override
public void onVoteThingFail(int position) {
Toast.makeText(mContext, "Cannot upvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).upvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
((CommentViewHolder) holder).downvoteButton.setColorFilter(minusButtonColorFilter);
}
}, commentItem.getFullName(), RedditUtils.DIR_UPVOTE, ((CommentViewHolder) holder).getAdapterPosition());
} else { } else {
//Upvoted before ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1));
((CommentViewHolder) holder).upvoteButton.clearColorFilter(); }
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 1));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { VoteThing.voteThing(mOauthRetrofit,mSharedPreferences, new VoteThing.VoteThingListener() {
@Override @Override
public void onVoteThingSuccess(int position) { public void onVoteThingSuccess(int position1) {
commentItem.setVoteType(0); commentItem.setVoteType(1);
commentItem.setScore(commentItem.getScore() - 1); if(isDownvotedBefore) {
} commentItem.setScore(commentItem.getScore() + 2);
} else {
@Override
public void onVoteThingFail(int position) {
Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1));
((CommentViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
commentItem.setScore(commentItem.getScore() + 1); commentItem.setScore(commentItem.getScore() + 1);
} }
}, commentItem.getFullName(), RedditUtils.DIR_UNVOTE, ((CommentViewHolder) holder).getAdapterPosition()); }
}
@Override
public void onVoteThingFail(int position1) {
Toast.makeText(mContext, "Cannot upvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).upvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
((CommentViewHolder) holder).downvoteButton.setColorFilter(minusButtonColorFilter);
}
}, commentItem.getFullName(), RedditUtils.DIR_UPVOTE, ((CommentViewHolder) holder).getAdapterPosition());
} else {
//Upvoted before
((CommentViewHolder) holder).upvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 1));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position1) {
commentItem.setVoteType(0);
commentItem.setScore(commentItem.getScore() - 1);
}
@Override
public void onVoteThingFail(int position1) {
Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1));
((CommentViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
commentItem.setScore(commentItem.getScore() + 1);
}
}, commentItem.getFullName(), RedditUtils.DIR_UNVOTE, ((CommentViewHolder) holder).getAdapterPosition());
} }
}); });
((CommentViewHolder) holder).downvoteButton.setOnClickListener(new View.OnClickListener() { ((CommentViewHolder) holder).downvoteButton.setOnClickListener(view -> {
@Override final boolean isUpvotedBefore = ((CommentViewHolder) holder).upvoteButton.getColorFilter() != null;
public void onClick(View view) {
final boolean isUpvotedBefore = ((CommentViewHolder) holder).upvoteButton.getColorFilter() != null;
final ColorFilter upvoteButtonColorFilter = ((CommentViewHolder) holder).upvoteButton.getColorFilter(); final ColorFilter upvoteButtonColorFilter = ((CommentViewHolder) holder).upvoteButton.getColorFilter();
((CommentViewHolder) holder).upvoteButton.clearColorFilter(); ((CommentViewHolder) holder).upvoteButton.clearColorFilter();
if (((CommentViewHolder) holder).downvoteButton.getColorFilter() == null) { if (((CommentViewHolder) holder).downvoteButton.getColorFilter() == null) {
((CommentViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN); ((CommentViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
if (isUpvotedBefore) { if (isUpvotedBefore) {
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 2)); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 2));
} else { } else {
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 1)); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() - 1));
}
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() {
@Override
public void onVoteThingSuccess(int position12) {
commentItem.setVoteType(-1);
if(isUpvotedBefore) {
commentItem.setScore(commentItem.getScore() - 2);
} else {
commentItem.setScore(commentItem.getScore() - 1);
}
} }
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { @Override
@Override public void onVoteThingFail(int position12) {
public void onVoteThingSuccess(int position) { Toast.makeText(mContext, "Cannot downvote this comment", Toast.LENGTH_SHORT).show();
commentItem.setVoteType(-1); ((CommentViewHolder) holder).downvoteButton.clearColorFilter();
if(isUpvotedBefore) { ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
commentItem.setScore(commentItem.getScore() - 2); ((CommentViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter);
} else { }
commentItem.setScore(commentItem.getScore() - 1); }, commentItem.getFullName(), RedditUtils.DIR_DOWNVOTE, holder.getAdapterPosition());
} } else {
} //Down voted before
((CommentViewHolder) holder).downvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1));
@Override VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() {
public void onVoteThingFail(int position) { @Override
Toast.makeText(mContext, "Cannot downvote this comment", Toast.LENGTH_SHORT).show(); public void onVoteThingSuccess(int position12) {
((CommentViewHolder) holder).downvoteButton.clearColorFilter(); commentItem.setVoteType(0);
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore())); commentItem.setScore(commentItem.getScore());
((CommentViewHolder) holder).upvoteButton.setColorFilter(upvoteButtonColorFilter); }
}
}, commentItem.getFullName(), RedditUtils.DIR_DOWNVOTE, holder.getAdapterPosition());
} else {
//Down voted before
((CommentViewHolder) holder).downvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1));
VoteThing.voteThing(mOauthRetrofit, mSharedPreferences, new VoteThing.VoteThingListener() { @Override
@Override public void onVoteThingFail(int position12) {
public void onVoteThingSuccess(int position) { Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show();
commentItem.setVoteType(0); ((CommentViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
commentItem.setScore(commentItem.getScore()); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
} commentItem.setScore(commentItem.getScore());
}
@Override }, commentItem.getFullName(), RedditUtils.DIR_UNVOTE, holder.getAdapterPosition());
public void onVoteThingFail(int position) {
Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
commentItem.setScore(commentItem.getScore());
}
}, commentItem.getFullName(), RedditUtils.DIR_UNVOTE, holder.getAdapterPosition());
}
} }
}); });
} }

View File

@ -43,20 +43,27 @@ class ParseSubredditData {
String id = data.getString(JSONUtils.NAME_KEY); String id = data.getString(JSONUtils.NAME_KEY);
String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME); String subredditFullName = data.getString(JSONUtils.DISPLAY_NAME);
String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim(); String description = data.getString(JSONUtils.PUBLIC_DESCRIPTION_KEY).trim();
String bannerImageUrl = data.getString(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY);
if(bannerImageUrl.equals("") || bannerImageUrl.equals("null")) { String bannerImageUrl;
if(data.isNull(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY)) {
bannerImageUrl = "";
} else {
bannerImageUrl = data.getString(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY);
}
if(bannerImageUrl.equals("") && !data.isNull(JSONUtils.BANNER_IMG_KEY)) {
bannerImageUrl= data.getString(JSONUtils.BANNER_IMG_KEY); bannerImageUrl= data.getString(JSONUtils.BANNER_IMG_KEY);
if(bannerImageUrl.equals("null")) {
bannerImageUrl = "";
}
} }
String iconUrl = data.getString(JSONUtils.COMMUNITY_ICON_KEY);
if(iconUrl.equals("") || iconUrl.equals("null")) { String iconUrl;
if(data.isNull(JSONUtils.COMMUNITY_ICON_KEY)) {
iconUrl = "";
} else {
iconUrl = data.getString(JSONUtils.COMMUNITY_ICON_KEY);
}
if(iconUrl.equals("") && !data.isNull(JSONUtils.ICON_IMG_KEY)) {
iconUrl = data.getString(JSONUtils.ICON_IMG_KEY); iconUrl = data.getString(JSONUtils.ICON_IMG_KEY);
if(iconUrl.equals("null")) {
iconUrl = "";
}
} }
int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY); int nSubscribers = data.getInt(JSONUtils.SUBSCRIBERS_KEY);
int nCurrentOnlineSubscribers = data.getInt(JSONUtils.ACTIVE_USER_COUNT_KEY); int nCurrentOnlineSubscribers = data.getInt(JSONUtils.ACTIVE_USER_COUNT_KEY);
subredditData = new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description, nSubscribers); subredditData = new SubredditData(id, subredditFullName, iconUrl, bannerImageUrl, description, nSubscribers);

View File

@ -49,6 +49,7 @@ import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView; import de.hdodenhof.circleimageview.CircleImageView;
import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.BlurTransformation;
import retrofit2.Retrofit; import retrofit2.Retrofit;
import ru.noties.markwon.SpannableConfiguration;
import ru.noties.markwon.view.MarkwonView; import ru.noties.markwon.view.MarkwonView;
public class ViewPostDetailActivity extends AppCompatActivity { public class ViewPostDetailActivity extends AppCompatActivity {
@ -269,7 +270,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mTypeChip.setText("LINK"); mTypeChip.setText("LINK");
if(!mPost.getSelfText().equals("")) { if(!mPost.getSelfText().equals("")) {
mContentMarkdownView.setVisibility(View.VISIBLE); mContentMarkdownView.setVisibility(View.VISIBLE);
mContentMarkdownView.setMarkdown(mPost.getSelfText()); mContentMarkdownView.setMarkdown(getCustomSpannableConfiguration(), mPost.getSelfText());
} }
mNoPreviewLinkImageView.setVisibility(View.VISIBLE); mNoPreviewLinkImageView.setVisibility(View.VISIBLE);
mNoPreviewLinkImageView.setOnClickListener(view -> { mNoPreviewLinkImageView.setOnClickListener(view -> {
@ -285,7 +286,7 @@ public class ViewPostDetailActivity extends AppCompatActivity {
mTypeChip.setVisibility(View.GONE); mTypeChip.setVisibility(View.GONE);
if(!mPost.getSelfText().equals("")) { if(!mPost.getSelfText().equals("")) {
mContentMarkdownView.setVisibility(View.VISIBLE); mContentMarkdownView.setVisibility(View.VISIBLE);
mContentMarkdownView.setMarkdown(mPost.getSelfText()); mContentMarkdownView.setMarkdown(getCustomSpannableConfiguration(), mPost.getSelfText());
} }
break; break;
} }
@ -482,6 +483,27 @@ public class ViewPostDetailActivity extends AppCompatActivity {
} }
} }
private SpannableConfiguration getCustomSpannableConfiguration() {
return SpannableConfiguration.builder(this).linkResolver((view, link) -> {
if(link.startsWith("/u/")) {
Intent intent = new Intent(ViewPostDetailActivity.this, ViewUserDetailActivity.class);
intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, link.substring(3));
startActivity(intent);
} else if(link.startsWith("/r/")) {
Intent intent = new Intent(ViewPostDetailActivity.this, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, link.substring(3));
startActivity(intent);
} else {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
// add share action to menu list
builder.addDefaultShareMenuItem();
builder.setToolbarColor(getResources().getColor(R.color.colorPrimary));
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(ViewPostDetailActivity.this, Uri.parse(link));
}
}).build();
}
private void showRetrySnackbar() { private void showRetrySnackbar() {
Snackbar snackbar = Snackbar.make(mCoordinatorLayout, R.string.load_comment_failed, Snackbar.LENGTH_INDEFINITE); Snackbar snackbar = Snackbar.make(mCoordinatorLayout, R.string.load_comment_failed, Snackbar.LENGTH_INDEFINITE);
snackbar.setAction(R.string.retry, new View.OnClickListener() { snackbar.setAction(R.string.retry, new View.OnClickListener() {

View File

@ -104,8 +104,9 @@ public class ViewSubredditDetailActivity extends AppCompatActivity {
params.topMargin = statusBarHeight; params.topMargin = statusBarHeight;
String subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY); String subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY);
String title = "r/" + subredditName; String title = "r/" + subredditName;
subredditNameTextView.setText(title);
CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collapsing_toolbar_layout_view_subreddit_detail_activity); CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collapsing_toolbar_layout_view_subreddit_detail_activity);
AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout_view_subreddit_detail_activity); AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout_view_subreddit_detail_activity);