diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-09-22 13:28:28 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-09-22 13:28:28 +0000 |
commit | 52b27971ffff6e57247ce06a7363cbaf37495d82 (patch) | |
tree | a51f119e7e4e22bbb53607eba74390298450f2bd /app/src/main/java | |
parent | 9ca494d4b5409b12b8262194f736284036858c01 (diff) | |
download | infinity-for-reddit-52b27971ffff6e57247ce06a7363cbaf37495d82.tar infinity-for-reddit-52b27971ffff6e57247ce06a7363cbaf37495d82.tar.gz infinity-for-reddit-52b27971ffff6e57247ce06a7363cbaf37495d82.tar.bz2 infinity-for-reddit-52b27971ffff6e57247ce06a7363cbaf37495d82.tar.lz infinity-for-reddit-52b27971ffff6e57247ce06a7363cbaf37495d82.tar.xz infinity-for-reddit-52b27971ffff6e57247ce06a7363cbaf37495d82.tar.zst infinity-for-reddit-52b27971ffff6e57247ce06a7363cbaf37495d82.zip |
Display comments count in the post. Display a comment and a save button in a post. Minor bugs fixed.
Diffstat (limited to '')
10 files changed, 253 insertions, 88 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java index 5104bef8..46f1c850 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentActivity.java @@ -150,8 +150,8 @@ public class CommentActivity extends AppCompatActivity { }) .usePlugin(LinkifyPlugin.create(Linkify.WEB_URLS)) .build(); - markwon.setMarkdown(commentParentMarkwonView, intent.getExtras().getString(EXTRA_COMMENT_PARENT_TEXT_KEY)); - parentFullname = intent.getExtras().getString(EXTRA_PARENT_FULLNAME_KEY); + markwon.setMarkdown(commentParentMarkwonView, intent.getStringExtra(EXTRA_COMMENT_PARENT_TEXT_KEY)); + parentFullname = intent.getStringExtra(EXTRA_PARENT_FULLNAME_KEY); parentDepth = intent.getExtras().getInt(EXTRA_PARENT_DEPTH_KEY); parentPosition = intent.getExtras().getInt(EXTRA_PARENT_POSITION_KEY); isReplying = intent.getExtras().getBoolean(EXTRA_IS_REPLYING_KEY); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java index ada28849..efb920e4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/CommentAndPostRecyclerViewAdapter.java @@ -33,7 +33,6 @@ import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; -import com.google.android.material.chip.Chip; import com.libRG.CustomTextView; import com.santalu.aspectratioimageview.AspectRatioImageView; @@ -366,7 +365,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie } if(mPost.isNSFW()) { - ((PostDetailViewHolder) holder).mNSFWChip.setOnClickListener(view -> { + ((PostDetailViewHolder) holder).mNSFWTextView.setOnClickListener(view -> { Intent intent = new Intent(mActivity, FilteredThingActivity.class); intent.putExtra(FilteredThingActivity.EXTRA_NAME, mSubredditNamePrefixed.substring(2)); intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); @@ -374,15 +373,15 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie intent.putExtra(FilteredThingActivity.EXTRA_FILTER, Post.NSFW_TYPE); mActivity.startActivity(intent); }); - ((PostDetailViewHolder) holder).mNSFWChip.setVisibility(View.VISIBLE); + ((PostDetailViewHolder) holder).mNSFWTextView.setVisibility(View.VISIBLE); } else { - ((PostDetailViewHolder) holder).mNSFWChip.setVisibility(View.GONE); + ((PostDetailViewHolder) holder).mNSFWTextView.setVisibility(View.GONE); } String scoreWithVote = Integer.toString(mPost.getScore() + mPost.getVoteType()); ((PostDetailViewHolder) holder).mScoreTextView.setText(scoreWithVote); - ((PostDetailViewHolder) holder).mTypeChip.setOnClickListener(view -> { + ((PostDetailViewHolder) holder).mTypeTextView.setOnClickListener(view -> { Intent intent = new Intent(mActivity, FilteredThingActivity.class); intent.putExtra(FilteredThingActivity.EXTRA_NAME, mSubredditNamePrefixed.substring(2)); intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); @@ -394,7 +393,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie switch (mPost.getPostType()) { case Post.IMAGE_TYPE: - ((PostDetailViewHolder) holder).mTypeChip.setText("IMAGE"); + ((PostDetailViewHolder) holder).mTypeTextView.setText("IMAGE"); ((PostDetailViewHolder) holder).mImageView.setOnClickListener(view -> { Intent intent = new Intent(mActivity, ViewImageActivity.class); @@ -406,7 +405,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie }); break; case Post.LINK_TYPE: - ((PostDetailViewHolder) holder).mTypeChip.setText("LINK"); + ((PostDetailViewHolder) holder).mTypeTextView.setText("LINK"); ((PostDetailViewHolder) holder).linkTextView.setVisibility(View.VISIBLE); String domain = Uri.parse(mPost.getUrl()).getHost(); @@ -424,7 +423,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie }); break; case Post.GIF_VIDEO_TYPE: - ((PostDetailViewHolder) holder).mTypeChip.setText("GIF"); + ((PostDetailViewHolder) holder).mTypeTextView.setText("GIF"); final Uri gifVideoUri = Uri.parse(mPost.getVideoUrl()); ((PostDetailViewHolder) holder).mImageView.setOnClickListener(view -> { @@ -437,7 +436,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie }); break; case Post.VIDEO_TYPE: - ((PostDetailViewHolder) holder).mTypeChip.setText("VIDEO"); + ((PostDetailViewHolder) holder).mTypeTextView.setText("VIDEO"); final Uri videoUri = Uri.parse(mPost.getVideoUrl()); ((PostDetailViewHolder) holder).mImageView.setOnClickListener(view -> { @@ -450,7 +449,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie }); break; case Post.NO_PREVIEW_LINK_TYPE: - ((PostDetailViewHolder) holder).mTypeChip.setText("LINK"); + ((PostDetailViewHolder) holder).mTypeTextView.setText("LINK"); ((PostDetailViewHolder) holder).linkTextView.setVisibility(View.VISIBLE); String noPreviewLinkDomain = Uri.parse(mPost.getUrl()).getHost(); @@ -474,7 +473,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie }); break; case Post.TEXT_TYPE: - ((PostDetailViewHolder) holder).mTypeChip.setText("TEXT"); + ((PostDetailViewHolder) holder).mTypeTextView.setText("TEXT"); if(!mPost.getSelfText().equals("")) { ((PostDetailViewHolder) holder).mContentMarkdownView.setVisibility(View.VISIBLE); @@ -482,6 +481,77 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie } break; } + + ((PostDetailViewHolder) holder).commentButton.setOnClickListener(view -> { + if(mAccessToken == null) { + Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); + return; + } + + Intent intent = new Intent(mActivity, CommentActivity.class); + intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, mPost.getFullName()); + intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TEXT_KEY, mPost.getTitle()); + intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, false); + intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, 0); + mActivity.startActivity(intent); + }); + + ((PostDetailViewHolder) holder).commentsCountTextView.setText(Integer.toString(mPost.getnComments())); + + if(mPost.isSaved()) { + ((PostDetailViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_24px); + } else { + ((PostDetailViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_border_24px); + } + + ((PostDetailViewHolder) holder).saveButton.setOnClickListener(view -> { + if(mAccessToken == null) { + Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show(); + return; + } + + if(mPost.isSaved()) { + ((PostDetailViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_border_24px); + SaveThing.unsaveThing(mOauthRetrofit, mAccessToken, mPost.getFullName(), + new SaveThing.SaveThingListener() { + @Override + public void success() { + mPost.setSaved(false); + ((PostDetailViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_border_24px); + Toast.makeText(mActivity, R.string.post_unsaved_success, Toast.LENGTH_SHORT).show(); + mCommentRecyclerViewAdapterCallback.updatePost(mPost); + } + + @Override + public void failed() { + mPost.setSaved(true); + ((PostDetailViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_24px); + Toast.makeText(mActivity, R.string.post_unsaved_failed, Toast.LENGTH_SHORT).show(); + mCommentRecyclerViewAdapterCallback.updatePost(mPost); + } + }); + } else { + ((PostDetailViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_24px); + SaveThing.saveThing(mOauthRetrofit, mAccessToken, mPost.getFullName(), + new SaveThing.SaveThingListener() { + @Override + public void success() { + mPost.setSaved(true); + ((PostDetailViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_24px); + Toast.makeText(mActivity, R.string.post_saved_success, Toast.LENGTH_SHORT).show(); + mCommentRecyclerViewAdapterCallback.updatePost(mPost); + } + + @Override + public void failed() { + mPost.setSaved(false); + ((PostDetailViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_border_24px); + Toast.makeText(mActivity, R.string.post_saved_failed, Toast.LENGTH_SHORT).show(); + mCommentRecyclerViewAdapterCallback.updatePost(mPost); + } + }); + } + }); } else if(holder.getItemViewType() == VIEW_TYPE_COMMENT) { CommentData comment; if(mIsSingleCommentThreadMode) { @@ -1100,7 +1170,7 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie ((PostDetailViewHolder) holder).spoilerFlairlinearLayout.setVisibility(View.GONE); ((PostDetailViewHolder) holder).flairTextView.setVisibility(View.GONE); ((PostDetailViewHolder) holder).spoilerTextView.setVisibility(View.GONE); - ((PostDetailViewHolder) holder).mNSFWChip.setVisibility(View.GONE); + ((PostDetailViewHolder) holder).mNSFWTextView.setVisibility(View.GONE); } else if(holder instanceof LoadMoreChildCommentsViewHolder) { ((LoadMoreChildCommentsViewHolder) holder).itemView.setPadding(0, 0, 0, 0); ViewGroup.LayoutParams params = ((LoadMoreChildCommentsViewHolder) holder).verticalBlock.getLayoutParams(); @@ -1137,13 +1207,13 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie @BindView(R.id.post_time_text_view_item_post_detail) TextView mPostTimeTextView; @BindView(R.id.title_text_view_item_post_detail) TextView mTitleTextView; @BindView(R.id.content_markdown_view_item_post_detail) TextView mContentMarkdownView; - @BindView(R.id.type_text_view_item_post_detail) Chip mTypeChip; + @BindView(R.id.type_text_view_item_post_detail) CustomTextView mTypeTextView; @BindView(R.id.gilded_image_view_item_post_detail) ImageView mGildedImageView; @BindView(R.id.gilded_number_text_view_item_post_detail) TextView mGildedNumberTextView; @BindView(R.id.crosspost_image_view_item_post_detail) ImageView mCrosspostImageView; @BindView(R.id.archived_image_view_item_post_detail) ImageView mArchivedImageView; @BindView(R.id.locked_image_view_item_post_detail) ImageView mLockedImageView; - @BindView(R.id.nsfw_text_view_item_post_detail) Chip mNSFWChip; + @BindView(R.id.nsfw_text_view_item_post_detail) CustomTextView mNSFWTextView; @BindView(R.id.spoiler_flair_linear_layout_item_post_detail) LinearLayout spoilerFlairlinearLayout; @BindView(R.id.spoiler_custom_text_view_item_post_detail) CustomTextView spoilerTextView; @BindView(R.id.flair_custom_text_view_item_post_detail) CustomTextView flairTextView; @@ -1157,6 +1227,9 @@ class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVie @BindView(R.id.plus_button_item_post_detail) ImageView mUpvoteButton; @BindView(R.id.score_text_view_item_post_detail) TextView mScoreTextView; @BindView(R.id.minus_button_item_post_detail) ImageView mDownvoteButton; + @BindView(R.id.comment_button_item_post_detail) ImageView commentButton; + @BindView(R.id.comments_count_item_post_detail) TextView commentsCountTextView; + @BindView(R.id.save_button_item_post_detail) ImageView saveButton; @BindView(R.id.share_button_item_post_detail) ImageView mShareButton; PostDetailViewHolder(@NonNull View itemView) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index f18da1cf..69d1fa9b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -165,6 +165,7 @@ class ParsePost { int score = data.getInt(JSONUtils.SCORE_KEY); int voteType; int gilded = data.getInt(JSONUtils.GILDED_KEY); + int nComments = data.getInt(JSONUtils.NUM_COMMENTS_KEY); boolean hidden = data.getBoolean(JSONUtils.HIDDEN_KEY); boolean spoiler = data.getBoolean(JSONUtils.SPOILER_KEY); boolean nsfw = data.getBoolean(JSONUtils.NSFW_KEY); @@ -207,23 +208,23 @@ class ParsePost { Post crosspostParent = parseBasicData(data, locale); Post post = parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, previewWidth, previewHeight, - score, voteType, gilded, flair, hidden, spoiler, nsfw, stickied, archived, locked, - saved, true); + score, voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, + archived, locked, saved, true); post.setCrosspostParentId(crosspostParent.getId()); return post; } else { return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, previewWidth, previewHeight, - score, voteType, gilded, flair, hidden, spoiler, nsfw, stickied, archived, locked, - saved, false); + score, voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, + archived, locked, saved, false); } } private static Post parseData(JSONObject data, String permalink, String id, String fullName, String subredditName, String subredditNamePrefixed, String author, String formattedPostTime, String title, String previewUrl, int previewWidth, - int previewHeight, int score, int voteType, int gilded, String flair, - boolean hidden, boolean spoiler, boolean nsfw, boolean stickied, + int previewHeight, int score, int voteType, int gilded, int nComments, + String flair, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean locked, boolean saved, boolean isCrosspost) throws JSONException { Post post; @@ -235,8 +236,8 @@ class ParsePost { //Text post int postType = Post.TEXT_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, - title, permalink, score, postType, voteType, gilded, flair, hidden, spoiler, nsfw, - stickied, archived, locked, saved, isCrosspost); + title, permalink, score, postType, voteType, gilded, nComments, flair, hidden, + spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_KEY)) { post.setSelfText(""); } else { @@ -246,8 +247,8 @@ class ParsePost { //No preview link post int postType = Post.NO_PREVIEW_LINK_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, - title, previewUrl, url, permalink, score, postType, voteType, gilded, flair, - hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); + title, previewUrl, url, permalink, score, postType, voteType, gilded, nComments, + flair, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_KEY)) { post.setSelfText(""); } else { @@ -267,8 +268,8 @@ class ParsePost { String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.HLS_URL_KEY)).toString(); post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, - title, previewUrl, permalink, score, postType, voteType, gilded, flair, hidden, - spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); + title, previewUrl, permalink, score, postType, voteType, gilded, nComments, + flair, hidden, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); @@ -282,8 +283,8 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, - gilded, flair, hidden, spoiler, nsfw, stickied, archived, locked, saved, - isCrosspost); + gilded, nComments, flair, hidden, spoiler, nsfw, stickied, archived, + locked, saved, isCrosspost); post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); post.setVideoUrl(videoUrl); @@ -294,8 +295,8 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, url, url, permalink, score, postType, - voteType, gilded, flair, hidden, spoiler, nsfw, stickied, archived, - locked, saved, isCrosspost); + voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, + archived, locked, saved, isCrosspost); post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); @@ -306,8 +307,8 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, permalink, score, postType, voteType, - gilded, flair, hidden, spoiler, nsfw, stickied, archived, locked, - saved, isCrosspost); + gilded, nComments, flair, hidden, spoiler, nsfw, stickied, archived, + locked, saved, isCrosspost); post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); @@ -323,8 +324,8 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, url, permalink, score, postType, - voteType, gilded, flair, hidden, spoiler, nsfw, stickied, archived, - locked, saved, isCrosspost); + voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, + archived, locked, saved, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_KEY)) { post.setSelfText(""); } else { @@ -343,8 +344,8 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, url, permalink, score, postType, - voteType, gilded, flair, hidden, spoiler, nsfw, stickied, archived, - locked, saved, isCrosspost); + voteType, gilded, nComments, flair, hidden, spoiler, nsfw, stickied, + archived, locked, saved, isCrosspost); post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); } else { @@ -353,8 +354,8 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, url, url, permalink, score, postType, voteType, - gilded, flair, hidden, spoiler, nsfw, stickied, archived, locked, saved, - isCrosspost); + gilded, nComments, flair, hidden, spoiler, nsfw, stickied, archived, locked, + saved, isCrosspost); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java index 3a522d29..7f1ba645 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java @@ -38,6 +38,7 @@ class Post implements Parcelable { private int gilded; private int previewWidth; private int previewHeight; + private int nComments; private boolean hidden; private boolean spoiler; private boolean nsfw; @@ -50,8 +51,8 @@ class Post implements Parcelable { Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author, String postTime, String title, String previewUrl, String permalink, int score, int postType, - int voteType, int gilded, String flair, boolean hidden, boolean spoiler, boolean nsfw, - boolean stickied, boolean archived, boolean locked, boolean saved, boolean isCrosspost) { + int voteType, int gilded, int nComments, String flair, boolean hidden, boolean spoiler, + boolean nsfw, boolean stickied, boolean archived, boolean locked, boolean saved, boolean isCrosspost) { this.id = id; this.fullName = fullName; this.subredditName = subredditName; @@ -66,6 +67,7 @@ class Post implements Parcelable { this.postType = postType; this.voteType = voteType; this.gilded = gilded; + this.nComments = nComments; this.flair = flair; this.hidden = hidden; this.spoiler = spoiler; @@ -79,8 +81,9 @@ class Post implements Parcelable { Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author, String postTime, String title, String previewUrl, String url, String permalink, int score, - int postType, int voteType, int gilded, String flair, boolean hidden, boolean spoiler, - boolean nsfw, boolean stickied, boolean archived, boolean locked, boolean saved, boolean isCrosspost) { + int postType, int voteType, int gilded, int nComments, String flair, boolean hidden, + boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean locked, + boolean saved, boolean isCrosspost) { this.id = id; this.fullName = fullName; this.subredditName = subredditName; @@ -96,6 +99,7 @@ class Post implements Parcelable { this.postType = postType; this.voteType = voteType; this.gilded = gilded; + this.nComments = nComments; this.flair = flair; this.hidden = hidden; this.spoiler = spoiler; @@ -109,8 +113,8 @@ class Post implements Parcelable { Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author, String postTime, String title, String permalink, int score, int postType, int voteType, int gilded, - String flair, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied, boolean archived, - boolean locked, boolean saved, boolean isCrosspost) { + int nComments, String flair, boolean hidden, boolean spoiler, boolean nsfw, boolean stickied, + boolean archived, boolean locked, boolean saved, boolean isCrosspost) { this.id = id; this.fullName = fullName; this.subredditName = subredditName; @@ -124,6 +128,7 @@ class Post implements Parcelable { this.postType = postType; this.voteType = voteType; this.gilded = gilded; + this.nComments = nComments; this.flair = flair; this.hidden = hidden; this.spoiler = spoiler; @@ -158,6 +163,7 @@ class Post implements Parcelable { gilded = in.readInt(); previewWidth = in.readInt(); previewHeight = in.readInt(); + nComments = in.readInt(); hidden = in.readByte() != 0; spoiler = in.readByte() != 0; nsfw = in.readByte() != 0; @@ -309,6 +315,14 @@ class Post implements Parcelable { return previewHeight; } + public int getnComments() { + return nComments; + } + + public void setnComments(int nComments) { + this.nComments = nComments; + } + boolean isHidden() { return hidden; } @@ -394,6 +408,7 @@ class Post implements Parcelable { parcel.writeInt(gilded); parcel.writeInt(previewWidth); parcel.writeInt(previewHeight); + parcel.writeInt(nComments); parcel.writeByte((byte) (hidden ? 1 : 0)); parcel.writeByte((byte) (spoiler ? 1 : 0)); parcel.writeByte((byte) (nsfw ? 1 : 0)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java index 0d13dc2e..5a4f71ce 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSource.java @@ -1,7 +1,5 @@ package ml.docilealligator.infinityforreddit; -import android.util.Log; - import androidx.annotation.NonNull; import androidx.lifecycle.MutableLiveData; import androidx.paging.PageKeyedDataSource; @@ -64,14 +62,13 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { initialLoadStateLiveData = new MutableLiveData<>(); hasPostLiveData = new MutableLiveData<>(); this.postType = postType; - this.sortType = sortType; + this.sortType = sortType == null ? PostDataSource.SORT_TYPE_BEST : sortType; this.filter = filter; this.nsfw = nsfw; } PostDataSource(Retrofit retrofit, String accessToken, Locale locale, String subredditOrUserName, int postType, String sortType, int filter, boolean nsfw) { - Log.i("asfdasdf", "datasource: " + sortType); this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -80,7 +77,15 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { initialLoadStateLiveData = new MutableLiveData<>(); hasPostLiveData = new MutableLiveData<>(); this.postType = postType; - this.sortType = sortType; + if(sortType == null) { + if(subredditOrUserName.equals("popular") || subredditOrUserName.equals("all")) { + this.sortType = PostDataSource.SORT_TYPE_HOT; + } else { + this.sortType = PostDataSource.SORT_TYPE_BEST; + } + } else { + this.sortType = sortType; + } this.filter = filter; this.nsfw = nsfw; } @@ -95,7 +100,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { initialLoadStateLiveData = new MutableLiveData<>(); hasPostLiveData = new MutableLiveData<>(); this.postType = postType; - this.sortType = sortType; + this.sortType = sortType == null ? PostDataSource.SORT_TYPE_NEW : sortType; userWhere = where; this.filter = filter; this.nsfw = nsfw; @@ -112,7 +117,7 @@ class PostDataSource extends PageKeyedDataSource<String, Post> { initialLoadStateLiveData = new MutableLiveData<>(); hasPostLiveData = new MutableLiveData<>(); this.postType = postType; - this.sortType = sortType; + this.sortType = sortType == null ? PostDataSource.SORT_TYPE_RELEVANCE : sortType; this.filter = filter; this.nsfw = nsfw; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java index 6e2aaa89..3942bec3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostDataSourceFactory.java @@ -1,7 +1,5 @@ package ml.docilealligator.infinityforreddit; -import android.util.Log; - import androidx.annotation.NonNull; import androidx.lifecycle.MutableLiveData; import androidx.paging.DataSource; @@ -39,7 +37,6 @@ class PostDataSourceFactory extends DataSource.Factory { PostDataSourceFactory(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType, String sortType, int filter, boolean nsfw) { - Log.i("asfdasdf", "datasource factory: " + sortType); this.retrofit = retrofit; this.accessToken = accessToken; this.locale = locale; @@ -89,7 +86,6 @@ class PostDataSourceFactory extends DataSource.Factory { postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, query, postType, sortType, filter, nsfw); } else if(postType == PostDataSource.TYPE_SUBREDDIT) { - Log.i("asfdasdf", "datasource create: " + sortType); postDataSource = new PostDataSource(retrofit, accessToken, locale, subredditName, postType, sortType, filter, nsfw); } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java index fdad950c..d61e9630 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java @@ -34,7 +34,6 @@ import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.Target; import com.google.android.material.card.MaterialCardView; -import com.google.android.material.chip.Chip; import com.libRG.CustomTextView; import org.greenrobot.eventbus.EventBus; @@ -309,7 +308,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo if(nsfw) { if(!(mContext instanceof FilteredThingActivity)) { - ((DataViewHolder) holder).nsfwChip.setOnClickListener(view -> { + ((DataViewHolder) holder).nsfwTextView.setOnClickListener(view -> { Intent intent = new Intent(mContext, FilteredThingActivity.class); intent.putExtra(FilteredThingActivity.EXTRA_NAME, post.getSubredditNamePrefixed().substring(2)); intent.putExtra(FilteredThingActivity.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); @@ -318,7 +317,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo mContext.startActivity(intent); }); } - ((DataViewHolder) holder).nsfwChip.setVisibility(View.VISIBLE); + ((DataViewHolder) holder).nsfwTextView.setVisibility(View.VISIBLE); } if(spoiler || flair != null) { @@ -327,13 +326,11 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo if(spoiler) { ((DataViewHolder) holder).spoilerTextView.setVisibility(View.VISIBLE); - ((DataViewHolder) holder).spoilerTextView.setBackgroundColor(mContext.getResources().getColor(R.color.backgroundColorPrimaryDark)); } if(flair != null) { ((DataViewHolder) holder).flairTextView.setVisibility(View.VISIBLE); ((DataViewHolder) holder).flairTextView.setText(flair); - ((DataViewHolder) holder).flairTextView.setBackgroundColor(mContext.getResources().getColor(R.color.backgroundColorPrimaryDark)); } switch (voteType) { @@ -377,12 +374,12 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo } if(!(mContext instanceof FilteredThingActivity)) { - ((DataViewHolder) holder).typeChip.setOnClickListener(view -> mCallback.typeChipClicked(post.getPostType())); + ((DataViewHolder) holder).typeTextView.setOnClickListener(view -> mCallback.typeChipClicked(post.getPostType())); } switch (post.getPostType()) { case Post.IMAGE_TYPE: - ((DataViewHolder) holder).typeChip.setText(R.string.image); + ((DataViewHolder) holder).typeTextView.setText(R.string.image); final String imageUrl = post.getUrl(); ((DataViewHolder) holder).imageView.setOnClickListener(view -> { @@ -395,7 +392,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo }); break; case Post.LINK_TYPE: - ((DataViewHolder) holder).typeChip.setText(R.string.link); + ((DataViewHolder) holder).typeTextView.setText(R.string.link); ((DataViewHolder) holder).linkTextView.setVisibility(View.VISIBLE); String domain = Uri.parse(post.getUrl()).getHost(); @@ -413,7 +410,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo }); break; case Post.GIF_VIDEO_TYPE: - ((DataViewHolder) holder).typeChip.setText(R.string.gif); + ((DataViewHolder) holder).typeTextView.setText(R.string.gif); final Uri gifVideoUri = Uri.parse(post.getVideoUrl()); ((DataViewHolder) holder).imageView.setOnClickListener(view -> { @@ -426,7 +423,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo }); break; case Post.VIDEO_TYPE: - ((DataViewHolder) holder).typeChip.setText(R.string.video); + ((DataViewHolder) holder).typeTextView.setText(R.string.video); final Uri videoUri = Uri.parse(post.getVideoUrl()); ((DataViewHolder) holder).imageView.setOnClickListener(view -> { @@ -439,7 +436,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo }); break; case Post.NO_PREVIEW_LINK_TYPE: - ((DataViewHolder) holder).typeChip.setText(R.string.link); + ((DataViewHolder) holder).typeTextView.setText(R.string.link); String noPreviewLinkUrl = post.getUrl(); ((DataViewHolder) holder).linkTextView.setVisibility(View.VISIBLE); @@ -458,7 +455,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo }); break; case Post.TEXT_TYPE: - ((DataViewHolder) holder).typeChip.setText(R.string.text); + ((DataViewHolder) holder).typeTextView.setText(R.string.text); break; } @@ -516,7 +513,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo ((DataViewHolder) holder).downvoteButton.clearColorFilter(); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType())); - EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post.getId(), post.getVoteType())); + EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); } @Override @@ -528,7 +525,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo ((DataViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); ((DataViewHolder) holder).scoreTextView.setTextColor(previousScoreTextViewColor); - EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post.getId(), post.getVoteType())); + EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); } }, id, newVoteType, holder.getAdapterPosition()); }); @@ -587,7 +584,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo ((DataViewHolder) holder).upvoteButton.clearColorFilter(); ((DataViewHolder) holder).scoreTextView.setText(Integer.toString(post.getScore() + post.getVoteType())); - EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post.getId(), post.getVoteType())); + EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); } @Override @@ -599,11 +596,82 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo ((DataViewHolder) holder).downvoteButton.setColorFilter(previousDownvoteButtonColorFilter); ((DataViewHolder) holder).scoreTextView.setTextColor(previousScoreTextViewColor); - EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post.getId(), post.getVoteType())); + EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); } }, id, newVoteType, holder.getAdapterPosition()); }); + ((DataViewHolder) holder).commentButton.setOnClickListener(view -> { + if(mAccessToken == null) { + Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); + return; + } + + Intent intent = new Intent(mContext, CommentActivity.class); + intent.putExtra(CommentActivity.EXTRA_PARENT_FULLNAME_KEY, post.getFullName()); + intent.putExtra(CommentActivity.EXTRA_COMMENT_PARENT_TEXT_KEY, post.getTitle()); + intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, false); + intent.putExtra(CommentActivity.EXTRA_PARENT_DEPTH_KEY, 0); + mContext.startActivity(intent); + }); + + ((DataViewHolder) holder).commentsCountTextView.setText(Integer.toString(post.getnComments())); + + if(post.isSaved()) { + ((DataViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_24px); + } else { + ((DataViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_border_24px); + } + + ((DataViewHolder) holder).saveButton.setOnClickListener(view -> { + if(mAccessToken == null) { + Toast.makeText(mContext, R.string.login_first, Toast.LENGTH_SHORT).show(); + return; + } + + if(post.isSaved()) { + ((DataViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_border_24px); + SaveThing.unsaveThing(mOauthRetrofit, mAccessToken, post.getFullName(), + new SaveThing.SaveThingListener() { + @Override + public void success() { + post.setSaved(false); + ((DataViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_border_24px); + Toast.makeText(mContext, R.string.post_unsaved_success, Toast.LENGTH_SHORT).show(); + EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); + } + + @Override + public void failed() { + post.setSaved(true); + ((DataViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_24px); + Toast.makeText(mContext, R.string.post_unsaved_failed, Toast.LENGTH_SHORT).show(); + EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); + } + }); + } else { + ((DataViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_24px); + SaveThing.saveThing(mOauthRetrofit, mAccessToken, post.getFullName(), + new SaveThing.SaveThingListener() { + @Override + public void success() { + post.setSaved(true); + ((DataViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_24px); + Toast.makeText(mContext, R.string.post_saved_success, Toast.LENGTH_SHORT).show(); + EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); + } + + @Override + public void failed() { + post.setSaved(false); + ((DataViewHolder) holder).saveButton.setImageResource(R.drawable.ic_baseline_bookmark_border_24px); + Toast.makeText(mContext, R.string.post_saved_failed, Toast.LENGTH_SHORT).show(); + EventBus.getDefault().post(new PostUpdateEventToDetailActivity(post)); + } + }); + } + }); + ((DataViewHolder) holder).shareButton.setOnClickListener(view -> { try { Intent intent = new Intent(Intent.ACTION_SEND); @@ -703,13 +771,13 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo @BindView(R.id.stickied_post_image_view_item_post) ImageView stickiedPostImageView; @BindView(R.id.post_time_text_view_best_item_post) TextView postTimeTextView; @BindView(R.id.title_text_view_best_item_post) TextView titleTextView; - @BindView(R.id.type_text_view_item_post) Chip typeChip; + @BindView(R.id.type_text_view_item_post) CustomTextView typeTextView; @BindView(R.id.gilded_image_view_item_post) ImageView gildedImageView; @BindView(R.id.gilded_number_text_view_item_post) TextView gildedNumberTextView; @BindView(R.id.archived_image_view_item_post) ImageView archivedImageView; @BindView(R.id.locked_image_view_item_post) ImageView lockedImageView; @BindView(R.id.crosspost_image_view_item_post) ImageView crosspostImageView; - @BindView(R.id.nsfw_text_view_item_post) Chip nsfwChip; + @BindView(R.id.nsfw_text_view_item_post) CustomTextView nsfwTextView; @BindView(R.id.spoiler_flair_linear_layout_item_post) LinearLayout spoilerFlairLinearLayout; @BindView(R.id.spoiler_custom_text_view_item_post) CustomTextView spoilerTextView; @BindView(R.id.flair_custom_text_view_item_post) CustomTextView flairTextView; @@ -722,6 +790,9 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo @BindView(R.id.plus_button_item_post) ImageView upvoteButton; @BindView(R.id.score_text_view_item_post) TextView scoreTextView; @BindView(R.id.minus_button_item_post) ImageView downvoteButton; + @BindView(R.id.comment_button_item_post) ImageView commentButton; + @BindView(R.id.comments_count_item_post) TextView commentsCountTextView; + @BindView(R.id.save_button_item_post) ImageView saveButton; @BindView(R.id.share_button_item_post) ImageView shareButton; DataViewHolder(View itemView) { @@ -764,7 +835,7 @@ class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHo ((DataViewHolder) holder).crosspostImageView.setVisibility(View.GONE); ((DataViewHolder) holder).archivedImageView.setVisibility(View.GONE); ((DataViewHolder) holder).lockedImageView.setVisibility(View.GONE); - ((DataViewHolder) holder).nsfwChip.setVisibility(View.GONE); + ((DataViewHolder) holder).nsfwTextView.setVisibility(View.GONE); ((DataViewHolder) holder).spoilerFlairLinearLayout.setVisibility(View.GONE); ((DataViewHolder) holder).spoilerTextView.setVisibility(View.GONE); ((DataViewHolder) holder).flairTextView.setVisibility(View.GONE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostUpdateEventToDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostUpdateEventToDetailActivity.java index af3d504f..4646ab84 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostUpdateEventToDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostUpdateEventToDetailActivity.java @@ -1,11 +1,9 @@ package ml.docilealligator.infinityforreddit; public class PostUpdateEventToDetailActivity { - public final String postId; - public final int voteType; + public final Post post; - public PostUpdateEventToDetailActivity(String postId, int voteType) { - this.postId = postId; - this.voteType = voteType; + public PostUpdateEventToDetailActivity(Post post) { + this.post = post; } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java index e3d5d82d..05925be9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java @@ -169,11 +169,7 @@ public class SubredditSelectionActivity extends AppCompatActivity { getCurrentAccountAndBindView(); } else { mFragment = getSupportFragmentManager().getFragment(savedInstanceState, FRAGMENT_OUT_STATE); - if(mFragment == null) { - bindView(); - } else { - getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit(); - } + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_subreddit_selection_activity, mFragment).commit(); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index d6d6c0b7..accb7542 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -902,8 +902,18 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo @Subscribe public void onPostUpdateEvent(PostUpdateEventToDetailActivity event) { - if(mPost.getId().equals(event.postId)) { - mPost.setVoteType(event.voteType); + if(mPost.getId().equals(event.post.getId())) { + mPost.setVoteType(event.post.getVoteType()); + mPost.setSaved(event.post.isSaved()); + if(mMenu != null) { + if(event.post.isSaved()) { + mMenu.findItem(R.id.action_save_view_post_detail_activity).setIcon(getResources() + .getDrawable(R.drawable.ic_baseline_bookmark_24px)); + } else { + mMenu.findItem(R.id.action_save_view_post_detail_activity).setIcon(getResources() + .getDrawable(R.drawable.ic_baseline_bookmark_border_24px)); + } + } mAdapter.updatePost(mPost); } } |