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,9 +97,7 @@ 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
public void onClick(View view) {
if(commentItem.hasChildren() && commentItem.getChildren().size() > 0) { if(commentItem.hasChildren() && commentItem.getChildren().size() > 0) {
mMultiLevelRecyclerView.toggleItemsGroup(holder.getAdapterPosition()); mMultiLevelRecyclerView.toggleItemsGroup(holder.getAdapterPosition());
setExpandButton(((CommentViewHolder) holder).expandButton, commentItem.isExpanded()); setExpandButton(((CommentViewHolder) holder).expandButton, commentItem.isExpanded());
@ -112,7 +133,6 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
} }
}); });
} }
}
}); });
switch (commentItem.getVoteType()) { switch (commentItem.getVoteType()) {
@ -126,9 +146,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
break; break;
} }
((CommentViewHolder) holder).upvoteButton.setOnClickListener(new View.OnClickListener() { ((CommentViewHolder) holder).upvoteButton.setOnClickListener(view -> {
@Override
public void onClick(View view) {
final boolean isDownvotedBefore = ((CommentViewHolder) holder).downvoteButton.getColorFilter() != null; final boolean isDownvotedBefore = ((CommentViewHolder) holder).downvoteButton.getColorFilter() != null;
final ColorFilter minusButtonColorFilter = ((CommentViewHolder) holder).downvoteButton.getColorFilter(); final ColorFilter minusButtonColorFilter = ((CommentViewHolder) holder).downvoteButton.getColorFilter();
((CommentViewHolder) holder).downvoteButton.clearColorFilter(); ((CommentViewHolder) holder).downvoteButton.clearColorFilter();
@ -143,7 +161,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
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(1); commentItem.setVoteType(1);
if(isDownvotedBefore) { if(isDownvotedBefore) {
commentItem.setScore(commentItem.getScore() + 2); commentItem.setScore(commentItem.getScore() + 2);
@ -153,7 +171,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
} }
@Override @Override
public void onVoteThingFail(int position) { public void onVoteThingFail(int position1) {
Toast.makeText(mContext, "Cannot upvote this comment", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "Cannot upvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).upvoteButton.clearColorFilter(); ((CommentViewHolder) holder).upvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore())); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
@ -167,13 +185,13 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
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(0);
commentItem.setScore(commentItem.getScore() - 1); commentItem.setScore(commentItem.getScore() - 1);
} }
@Override @Override
public void onVoteThingFail(int position) { public void onVoteThingFail(int position1) {
Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore() + 1)); ((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); ((CommentViewHolder) holder).upvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.colorPrimary), android.graphics.PorterDuff.Mode.SRC_IN);
@ -181,12 +199,9 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
} }
}, commentItem.getFullName(), RedditUtils.DIR_UNVOTE, ((CommentViewHolder) holder).getAdapterPosition()); }, commentItem.getFullName(), RedditUtils.DIR_UNVOTE, ((CommentViewHolder) holder).getAdapterPosition());
} }
}
}); });
((CommentViewHolder) holder).downvoteButton.setOnClickListener(new View.OnClickListener() { ((CommentViewHolder) holder).downvoteButton.setOnClickListener(view -> {
@Override
public void onClick(View view) {
final boolean isUpvotedBefore = ((CommentViewHolder) holder).upvoteButton.getColorFilter() != null; final boolean isUpvotedBefore = ((CommentViewHolder) holder).upvoteButton.getColorFilter() != null;
final ColorFilter upvoteButtonColorFilter = ((CommentViewHolder) holder).upvoteButton.getColorFilter(); final ColorFilter upvoteButtonColorFilter = ((CommentViewHolder) holder).upvoteButton.getColorFilter();
@ -202,7 +217,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
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 position12) {
commentItem.setVoteType(-1); commentItem.setVoteType(-1);
if(isUpvotedBefore) { if(isUpvotedBefore) {
commentItem.setScore(commentItem.getScore() - 2); commentItem.setScore(commentItem.getScore() - 2);
@ -212,7 +227,7 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
} }
@Override @Override
public void onVoteThingFail(int position) { public void onVoteThingFail(int position12) {
Toast.makeText(mContext, "Cannot downvote this comment", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "Cannot downvote this comment", Toast.LENGTH_SHORT).show();
((CommentViewHolder) holder).downvoteButton.clearColorFilter(); ((CommentViewHolder) holder).downvoteButton.clearColorFilter();
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore())); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
@ -226,13 +241,13 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
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 position12) {
commentItem.setVoteType(0); commentItem.setVoteType(0);
commentItem.setScore(commentItem.getScore()); commentItem.setScore(commentItem.getScore());
} }
@Override @Override
public void onVoteThingFail(int position) { public void onVoteThingFail(int position12) {
Toast.makeText(mContext, "Cannot unvote this comment", Toast.LENGTH_SHORT).show(); 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).downvoteButton.setColorFilter(ContextCompat.getColor(mContext, R.color.minusButtonColor), android.graphics.PorterDuff.Mode.SRC_IN);
((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore())); ((CommentViewHolder) holder).scoreTextView.setText(Integer.toString(commentItem.getScore()));
@ -240,7 +255,6 @@ class CommentMultiLevelRecyclerViewAdapter extends MultiLevelAdapter {
} }
}, commentItem.getFullName(), RedditUtils.DIR_UNVOTE, holder.getAdapterPosition()); }, 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;
bannerImageUrl= data.getString(JSONUtils.BANNER_IMG_KEY); if(data.isNull(JSONUtils.BANNER_BACKGROUND_IMAGE_KEY)) {
if(bannerImageUrl.equals("null")) {
bannerImageUrl = ""; 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);
} }
String iconUrl = data.getString(JSONUtils.COMMUNITY_ICON_KEY);
if(iconUrl.equals("") || iconUrl.equals("null")) { String iconUrl;
iconUrl = data.getString(JSONUtils.ICON_IMG_KEY); if(data.isNull(JSONUtils.COMMUNITY_ICON_KEY)) {
if(iconUrl.equals("null")) {
iconUrl = ""; 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);
} }
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);