From d4fc671605df4d7e25a84a47fcc12d3a1d7423e3 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 22 Aug 2019 17:01:25 +0800 Subject: [PATCH] Saving and unsaving post are now available. Disable commenting and replying for locked posts in last commit. --- .idea/assetWizardSettings.xml | 4 +- .../infinityforreddit/JSONUtils.java | 1 + .../infinityforreddit/MainActivity.java | 6 +- .../infinityforreddit/ParsePost.java | 29 +-- .../infinityforreddit/Post.java | 20 +- .../infinityforreddit/PostFragment.java | 1 + .../infinityforreddit/RedditAPI.java | 8 + .../infinityforreddit/SaveThing.java | 60 ++++++ .../ViewPostDetailActivity.java | 187 ++++++++++++++---- .../drawable/ic_baseline_bookmark_24px.xml | 9 + .../ic_baseline_bookmark_border_24px.xml | 9 + .../res/menu/view_post_detail_activity.xml | 27 ++- app/src/main/res/values/strings.xml | 4 + 13 files changed, 294 insertions(+), 71 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/SaveThing.java create mode 100644 app/src/main/res/drawable/ic_baseline_bookmark_24px.xml create mode 100644 app/src/main/res/drawable/ic_baseline_bookmark_border_24px.xml diff --git a/.idea/assetWizardSettings.xml b/.idea/assetWizardSettings.xml index 43d6177f..03c55734 100644 --- a/.idea/assetWizardSettings.xml +++ b/.idea/assetWizardSettings.xml @@ -35,9 +35,9 @@ - + - + diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java index f13185d1..235ba621 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java @@ -76,6 +76,7 @@ public class JSONUtils { static final String DESCRIPTION_HTML_KEY = "description_html"; static final String ARCHIVED_KEY = "archived"; static final String LOCKEC_KEY = "locked"; + static final String SAVED_KEY = "saved"; static final String TEXT_EDITABLE_KEY = "text_editable"; static final String SUBJECT_KEY = "subject"; static final String CONTEXT_KEY = "context"; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index a84811ea..300cd4c1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -317,7 +317,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe .build(); workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, - ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); + ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); } else { workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG); } @@ -343,7 +343,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe .build(); workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, - ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); + ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); } else { workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG); } @@ -373,7 +373,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe .build(); workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, - ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); + ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); } else { workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index 63a31ded..cac74e40 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -167,6 +167,7 @@ class ParsePost { boolean stickied = data.getBoolean(JSONUtils.STICKIED_KEY); boolean archived = data.getBoolean(JSONUtils.ARCHIVED_KEY); boolean locked = data.getBoolean(JSONUtils.LOCKEC_KEY); + boolean saved = data.getBoolean(JSONUtils.SAVED_KEY); String flair = null; if(!data.isNull(JSONUtils.LINK_FLAIR_TEXT_KEY)) { flair = data.getString(JSONUtils.LINK_FLAIR_TEXT_KEY); @@ -201,11 +202,13 @@ class ParsePost { data = data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0); return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, previewWidth, previewHeight, - score, voteType, gilded, flair, spoiler, nsfw, stickied, archived, locked, true, i); + score, voteType, gilded, flair, spoiler, nsfw, stickied, archived, locked, saved, + true, i); } else { return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, previewWidth, previewHeight, - score, voteType, gilded, flair, spoiler, nsfw, stickied, archived, locked, false, i); + score, voteType, gilded, flair, spoiler, nsfw, stickied, archived, locked, saved, + false, i); } } @@ -214,7 +217,7 @@ class ParsePost { String formattedPostTime, String title, String previewUrl, int previewWidth, int previewHeight, int score, int voteType, int gilded, String flair, boolean spoiler, boolean nsfw, boolean stickied, boolean archived, - boolean locked, boolean isCrosspost, int i) throws JSONException { + boolean locked, boolean saved, boolean isCrosspost, int i) throws JSONException { Post post; boolean isVideo = data.getBoolean(JSONUtils.IS_VIDEO_KEY); @@ -227,7 +230,7 @@ class ParsePost { int postType = Post.TEXT_TYPE; post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw, - stickied, archived, locked, isCrosspost); + stickied, archived, locked, saved, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_KEY)) { post.setSelfText(""); } else { @@ -239,7 +242,7 @@ class ParsePost { 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, spoiler, nsfw, stickied, archived, locked, isCrosspost); + voteType, gilded, flair, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_KEY)) { post.setSelfText(""); } else { @@ -261,7 +264,7 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, - gilded, flair, spoiler, nsfw, stickied, archived, locked, isCrosspost, true); + gilded, flair, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost, true); post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); @@ -278,7 +281,7 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, - gilded, flair, spoiler, nsfw, stickied, archived, locked, isCrosspost, false); + gilded, flair, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost, false); post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); post.setVideoUrl(videoUrl); @@ -293,7 +296,7 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, - gilded, flair, spoiler, nsfw, stickied, archived, locked, isCrosspost, true); + gilded, flair, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost, true); post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); post.setVideoUrl(videoUrl); @@ -306,7 +309,7 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, url, url, permalink, score, postType, - voteType, gilded, flair, spoiler, nsfw, stickied, archived, locked, isCrosspost); + voteType, gilded, flair, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); @@ -318,7 +321,7 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, permalink, score, postType, voteType, gilded, flair, spoiler, - nsfw, stickied, archived, locked, isCrosspost); + nsfw, stickied, archived, locked, saved, isCrosspost); post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); @@ -335,7 +338,7 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, url, permalink, score, postType, voteType, gilded, - flair, spoiler, nsfw, stickied, archived, locked, isCrosspost); + flair, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_KEY)) { post.setSelfText(""); } else { @@ -355,7 +358,7 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, url, permalink, score, postType, - voteType, gilded, flair, spoiler, nsfw, stickied, archived, locked, isCrosspost); + voteType, gilded, flair, spoiler, nsfw, stickied, archived, locked, saved, isCrosspost); post.setPreviewWidth(previewWidth); post.setPreviewHeight(previewHeight); } else { @@ -365,7 +368,7 @@ class ParsePost { post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, url, url, permalink, score, postType, voteType, - gilded, flair, spoiler, nsfw, stickied, archived, locked, isCrosspost); + gilded, flair, 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 855e85cd..99236eac 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java @@ -43,6 +43,7 @@ class Post implements Parcelable { private boolean stickied; private boolean archived; private boolean locked; + private boolean saved; private boolean isCrosspost; private boolean isDashVideo; private boolean isDownloadableGifOrVideo; @@ -50,7 +51,7 @@ 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 spoiler, boolean nsfw, boolean stickied, - boolean archived, boolean locked, boolean isCrosspost, boolean isDashVideo) { + boolean archived, boolean locked, boolean saved, boolean isCrosspost, boolean isDashVideo) { this.id = id; this.fullName = fullName; this.subredditName = subredditName; @@ -71,6 +72,7 @@ class Post implements Parcelable { this.stickied = stickied; this.archived = archived; this.locked = locked; + this.saved = saved; this.isCrosspost = isCrosspost; this.isDashVideo = isDashVideo; } @@ -78,7 +80,7 @@ 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 spoiler, boolean nsfw, boolean stickied, - boolean archived, boolean locked, boolean isCrosspost) { + boolean archived, boolean locked, boolean saved, boolean isCrosspost) { this.id = id; this.fullName = fullName; this.subredditName = subredditName; @@ -100,13 +102,14 @@ class Post implements Parcelable { this.stickied = stickied; this.archived = archived; this.locked = locked; + this.saved = saved; this.isCrosspost = isCrosspost; } 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 spoiler, boolean nsfw, boolean stickied, boolean archived, boolean locked, - boolean isCrosspost) { + boolean saved, boolean isCrosspost) { this.id = id; this.fullName = fullName; this.subredditName = subredditName; @@ -126,6 +129,7 @@ class Post implements Parcelable { this.stickied = stickied; this.archived = archived; this.locked = locked; + this.saved = saved; this.isCrosspost= isCrosspost; } @@ -158,6 +162,7 @@ class Post implements Parcelable { stickied = in.readByte() != 0; archived = in.readByte() != 0; locked = in.readByte() != 0; + saved = in.readByte() != 0; isCrosspost = in.readByte() != 0; isDashVideo = in.readByte() != 0; isDownloadableGifOrVideo = in.readByte() != 0; @@ -356,6 +361,14 @@ class Post implements Parcelable { return locked; } + boolean isSaved() { + return saved; + } + + void setSaved(boolean saved) { + this.saved = saved; + } + boolean isCrosspost() { return isCrosspost; } @@ -390,6 +403,7 @@ class Post implements Parcelable { parcel.writeByte((byte) (stickied ? 1 : 0)); parcel.writeByte((byte) (archived ? 1 : 0)); parcel.writeByte((byte) (locked ? 1 : 0)); + parcel.writeByte((byte) (saved ? 1 : 0)); parcel.writeByte((byte) (isCrosspost ? 1 : 0)); parcel.writeByte((byte) (isDashVideo ? 1 : 0)); parcel.writeByte((byte) (isDownloadableGifOrVideo ? 1 : 0)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java index bd58f032..8ffbfe3a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostFragment.java @@ -445,6 +445,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { post.setNSFW(event.post.isNSFW()); post.setSpoiler(event.post.isSpoiler()); post.setFlair(event.post.getFlair()); + post.setSaved(event.post.isSaved()); mAdapter.notifyItemChanged(event.positionInList); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index b0ce908a..e4838897 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -168,4 +168,12 @@ public interface RedditAPI { @FormUrlEncoded @POST("/api/read_message") Call readMessage(@HeaderMap Map headers, @FieldMap Map ids); + + @FormUrlEncoded + @POST("/api/save") + Call save(@HeaderMap Map headers, @FieldMap Map params); + + @FormUrlEncoded + @POST("/api/unsave") + Call unsave(@HeaderMap Map headers, @FieldMap Map params); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SaveThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SaveThing.java new file mode 100644 index 00000000..743fec29 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SaveThing.java @@ -0,0 +1,60 @@ +package ml.docilealligator.infinityforreddit; + +import androidx.annotation.NonNull; + +import java.util.HashMap; +import java.util.Map; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +class SaveThing { + interface SaveThingListener { + void success(); + void failed(); + } + + static void saveThing(Retrofit oauthRetrofit, String accessToken, String fullname, + SaveThingListener saveThingListener) { + Map params = new HashMap<>(); + params.put(RedditUtils.ID_KEY, fullname); + oauthRetrofit.create(RedditAPI.class).save(RedditUtils.getOAuthHeader(accessToken), params).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if(response.isSuccessful()) { + saveThingListener.success(); + } else { + saveThingListener.failed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + saveThingListener.failed(); + } + }); + } + + static void unsaveThing(Retrofit oauthRetrofit, String accessToken, String fullname, + SaveThingListener saveThingListener) { + Map params = new HashMap<>(); + params.put(RedditUtils.ID_KEY, fullname); + oauthRetrofit.create(RedditAPI.class).unsave(RedditUtils.getOAuthHeader(accessToken), params).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if(response.isSuccessful()) { + saveThingListener.success(); + } else { + saveThingListener.failed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + saveThingListener.failed(); + } + }); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index ee39e267..08cad105 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -6,6 +6,7 @@ import android.content.res.Configuration; import android.content.res.Resources; import android.os.Build; import android.os.Bundle; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -296,36 +297,53 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo } if(mPost == null) { - mPost = getIntent().getExtras().getParcelable(EXTRA_POST_DATA); + mPost = getIntent().getParcelableExtra(EXTRA_POST_DATA); } + Log.i("asdfasdfadfadf", "" + mPost.isSaved()); if(mPost == null) { - fetchPostAndCommentsById(getIntent().getExtras().getString(EXTRA_POST_ID)); + fetchPostAndCommentsById(getIntent().getStringExtra(EXTRA_POST_ID)); } else { - if(mMenu != null && mPost.getAuthor().equals(mAccountName)) { - if(mPost.getPostType() == Post.TEXT_TYPE) { - mMenu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true); - } - mMenu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true); - - MenuItem nsfwItem = mMenu.findItem(R.id.action_nsfw_view_post_detail_activity); - nsfwItem.setVisible(true); - if(mPost.isNSFW()) { - nsfwItem.setTitle(R.string.action_unmark_nsfw); + if(mMenu != null) { + MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); + if(mAccessToken != null) { + if(mPost.isSaved()) { + saveItem.setVisible(true); + saveItem.setIcon(R.drawable.ic_baseline_bookmark_24px); + } else { + saveItem.setVisible(true); + saveItem.setIcon(R.drawable.ic_baseline_bookmark_border_24px); + } } else { - nsfwItem.setTitle(R.string.action_mark_nsfw); + saveItem.setVisible(false); } - MenuItem spoilerItem = mMenu.findItem(R.id.action_spoiler_view_post_detail_activity); - spoilerItem.setVisible(true); - if(mPost.isSpoiler()) { - spoilerItem.setTitle(R.string.action_unmark_spoiler); - } else { - spoilerItem.setTitle(R.string.action_mark_spoiler); - } + if(mPost.getAuthor().equals(mAccountName)) { + if(mPost.getPostType() == Post.TEXT_TYPE) { + mMenu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true); + } + mMenu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true); - mMenu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true); + MenuItem nsfwItem = mMenu.findItem(R.id.action_nsfw_view_post_detail_activity); + nsfwItem.setVisible(true); + if(mPost.isNSFW()) { + nsfwItem.setTitle(R.string.action_unmark_nsfw); + } else { + nsfwItem.setTitle(R.string.action_mark_nsfw); + } + + MenuItem spoilerItem = mMenu.findItem(R.id.action_spoiler_view_post_detail_activity); + spoilerItem.setVisible(true); + if(mPost.isSpoiler()) { + spoilerItem.setTitle(R.string.action_unmark_spoiler); + } else { + spoilerItem.setTitle(R.string.action_mark_spoiler); + } + + mMenu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true); + } } + mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId, isSingleCommentThreadMode, @@ -395,6 +413,21 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo public void onParsePostSuccess(Post post) { mPost = post; + if(mMenu != null) { + MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); + if(mAccessToken != null) { + if(post.isSaved()) { + saveItem.setVisible(true); + saveItem.setIcon(R.drawable.ic_baseline_bookmark_24px); + } else { + saveItem.setVisible(true); + saveItem.setIcon(R.drawable.ic_baseline_bookmark_border_24px); + } + } else { + saveItem.setVisible(false); + } + } + if(mMenu != null && mPost.getAuthor().equals(mAccountName)) { if(mPost.getPostType() == Post.TEXT_TYPE) { mMenu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true); @@ -582,6 +615,20 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo mAdapter.updatePost(mPost); EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); isRefreshing = false; + if(mMenu != null) { + MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); + if(mAccessToken != null) { + if(post.isSaved()) { + saveItem.setVisible(true); + saveItem.setIcon(R.drawable.ic_baseline_bookmark_24px); + } else { + saveItem.setVisible(true); + saveItem.setIcon(R.drawable.ic_baseline_bookmark_border_24px); + } + } else { + saveItem.setVisible(false); + } + } } @Override @@ -744,29 +791,44 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.view_post_detail_activity, menu); mMenu = menu; - if(mPost != null && mPost.getAuthor().equals(mAccountName)) { - if(mPost.getPostType() == Post.TEXT_TYPE) { - menu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true); - } - menu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true); - - MenuItem nsfwItem = menu.findItem(R.id.action_nsfw_view_post_detail_activity); - nsfwItem.setVisible(true); - if(mPost.isNSFW()) { - nsfwItem.setTitle(R.string.action_unmark_nsfw); + if(mPost != null) { + MenuItem saveItem = mMenu.findItem(R.id.action_save_view_post_detail_activity); + if(mAccessToken != null) { + if(mPost.isSaved()) { + saveItem.setVisible(true); + saveItem.setIcon(R.drawable.ic_baseline_bookmark_24px); + } else { + saveItem.setVisible(true); + saveItem.setIcon(R.drawable.ic_baseline_bookmark_border_24px); + } } else { - nsfwItem.setTitle(R.string.action_mark_nsfw); + saveItem.setVisible(false); } - MenuItem spoilerItem = menu.findItem(R.id.action_spoiler_view_post_detail_activity); - spoilerItem.setVisible(true); - if(mPost.isSpoiler()) { - spoilerItem.setTitle(R.string.action_unmark_spoiler); - } else { - spoilerItem.setTitle(R.string.action_mark_spoiler); - } + if(mPost.getAuthor().equals(mAccountName)) { + if(mPost.getPostType() == Post.TEXT_TYPE) { + menu.findItem(R.id.action_edit_view_post_detail_activity).setVisible(true); + } + menu.findItem(R.id.action_delete_view_post_detail_activity).setVisible(true); - menu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true); + MenuItem nsfwItem = menu.findItem(R.id.action_nsfw_view_post_detail_activity); + nsfwItem.setVisible(true); + if(mPost.isNSFW()) { + nsfwItem.setTitle(R.string.action_unmark_nsfw); + } else { + nsfwItem.setTitle(R.string.action_mark_nsfw); + } + + MenuItem spoilerItem = menu.findItem(R.id.action_spoiler_view_post_detail_activity); + spoilerItem.setVisible(true); + if(mPost.isSpoiler()) { + spoilerItem.setTitle(R.string.action_unmark_spoiler); + } else { + spoilerItem.setTitle(R.string.action_mark_spoiler); + } + + menu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true); + } } return true; } @@ -800,6 +862,51 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo intent.putExtra(CommentActivity.EXTRA_IS_REPLYING_KEY, false); startActivityForResult(intent, WRITE_COMMENT_REQUEST_CODE); return true; + case R.id.action_save_view_post_detail_activity: + if(mPost != null && mAccessToken != null) { + if(mPost.isSaved()) { + item.setIcon(R.drawable.ic_baseline_bookmark_border_24px); + SaveThing.unsaveThing(mOauthRetrofit, mAccessToken, mPost.getFullName(), + new SaveThing.SaveThingListener() { + @Override + public void success() { + mPost.setSaved(false); + item.setIcon(R.drawable.ic_baseline_bookmark_border_24px); + showMessage(R.string.post_unsaved_success); + EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); + } + + @Override + public void failed() { + mPost.setSaved(true); + item.setIcon(R.drawable.ic_baseline_bookmark_24px); + showMessage(R.string.post_unsaved_failed); + EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); + } + }); + } else { + item.setIcon(R.drawable.ic_baseline_bookmark_24px); + SaveThing.saveThing(mOauthRetrofit, mAccessToken, mPost.getFullName(), + new SaveThing.SaveThingListener() { + @Override + public void success() { + mPost.setSaved(true); + item.setIcon(R.drawable.ic_baseline_bookmark_24px); + showMessage(R.string.post_saved_success); + EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); + } + + @Override + public void failed() { + mPost.setSaved(false); + item.setIcon(R.drawable.ic_baseline_bookmark_border_24px); + showMessage(R.string.post_saved_failed); + EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition)); + } + }); + } + } + break; case R.id.action_edit_view_post_detail_activity: Intent editPostItent = new Intent(this, EditPostActivity.class); editPostItent.putExtra(EditPostActivity.EXTRA_ACCESS_TOKEN, mAccessToken); diff --git a/app/src/main/res/drawable/ic_baseline_bookmark_24px.xml b/app/src/main/res/drawable/ic_baseline_bookmark_24px.xml new file mode 100644 index 00000000..a7ff3655 --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_bookmark_24px.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_baseline_bookmark_border_24px.xml b/app/src/main/res/drawable/ic_baseline_bookmark_border_24px.xml new file mode 100644 index 00000000..71382d27 --- /dev/null +++ b/app/src/main/res/drawable/ic_baseline_bookmark_border_24px.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/menu/view_post_detail_activity.xml b/app/src/main/res/menu/view_post_detail_activity.xml index f8c546f0..756f51b7 100644 --- a/app/src/main/res/menu/view_post_detail_activity.xml +++ b/app/src/main/res/menu/view_post_detail_activity.xml @@ -9,40 +9,47 @@ app:showAsAction="ifRoom" /> + + - - + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a5a170d3..37a3518f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -206,6 +206,10 @@ Press here to login Login first + Post saved + Unable to save post + Post unsaved + Unable to unsave post Delete This Post Delete This Comment Are you sure?