From 5b26d57fdb16361e3ab1c5dbbfbc08b3295f7804 Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Thu, 27 Jul 2023 13:08:01 +0200 Subject: [PATCH] Save posts/comment with save post button The save post/comment buttons are now functional. Signed-off-by: Balazs Toldi --- .../toldi/infinityforlemmy/SaveComment.java | 53 ++++++++++++++++++ .../eu/toldi/infinityforlemmy/SavePost.java | 55 ++++++++++++++++++ .../eu/toldi/infinityforlemmy/SaveThing.java | 56 ++----------------- .../activities/ViewPostDetailActivity.java | 6 +- .../CommentsListingRecyclerViewAdapter.java | 6 +- .../adapters/CommentsRecyclerViewAdapter.java | 6 +- .../HistoryPostRecyclerViewAdapter.java | 12 ++-- .../PostDetailRecyclerViewAdapter.java | 7 ++- .../adapters/PostRecyclerViewAdapter.java | 12 ++-- .../toldi/infinityforlemmy/apis/LemmyAPI.java | 11 ++++ .../infinityforlemmy/dto/SaveCommentDTO.java | 26 +++++++++ .../infinityforlemmy/dto/SavePostDTO.java | 27 +++++++++ .../fragments/ViewPostDetailFragment.java | 6 +- 13 files changed, 211 insertions(+), 72 deletions(-) create mode 100644 app/src/main/java/eu/toldi/infinityforlemmy/SaveComment.java create mode 100644 app/src/main/java/eu/toldi/infinityforlemmy/SavePost.java create mode 100644 app/src/main/java/eu/toldi/infinityforlemmy/dto/SaveCommentDTO.java create mode 100644 app/src/main/java/eu/toldi/infinityforlemmy/dto/SavePostDTO.java diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/SaveComment.java b/app/src/main/java/eu/toldi/infinityforlemmy/SaveComment.java new file mode 100644 index 00000000..cff7264a --- /dev/null +++ b/app/src/main/java/eu/toldi/infinityforlemmy/SaveComment.java @@ -0,0 +1,53 @@ +package eu.toldi.infinityforlemmy; + +import androidx.annotation.NonNull; + +import eu.toldi.infinityforlemmy.apis.LemmyAPI; +import eu.toldi.infinityforlemmy.dto.SaveCommentDTO; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class SaveComment implements SaveThing { + @Override + public void saveThing(Retrofit retrofit, String accessToken, int id, SaveThingListener saveThingListener) { + + retrofit.create(LemmyAPI.class).commentSave(new SaveCommentDTO(id, true, accessToken)).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(); + } + }); + + } + + @Override + public void unsaveThing(Retrofit retrofit, String accessToken, int id, SaveThingListener saveThingListener) { + + retrofit.create(LemmyAPI.class).commentSave(new SaveCommentDTO(id, false, accessToken)).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/eu/toldi/infinityforlemmy/SavePost.java b/app/src/main/java/eu/toldi/infinityforlemmy/SavePost.java new file mode 100644 index 00000000..27aa3ad8 --- /dev/null +++ b/app/src/main/java/eu/toldi/infinityforlemmy/SavePost.java @@ -0,0 +1,55 @@ +package eu.toldi.infinityforlemmy; + +import androidx.annotation.NonNull; + +import eu.toldi.infinityforlemmy.apis.LemmyAPI; +import eu.toldi.infinityforlemmy.dto.SavePostDTO; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +public class SavePost implements SaveThing { + + @Override + public void saveThing(Retrofit retrofit, String accessToken, int id, SaveThingListener saveThingListener) { + + + retrofit.create(LemmyAPI.class).postSave(new SavePostDTO(id, true, accessToken)).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(); + } + }); + + } + + @Override + public void unsaveThing(Retrofit retrofit, String accessToken, int id, SaveThingListener saveThingListener) { + + retrofit.create(LemmyAPI.class).postSave(new SavePostDTO(id, false, accessToken)).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/eu/toldi/infinityforlemmy/SaveThing.java b/app/src/main/java/eu/toldi/infinityforlemmy/SaveThing.java index 2e346623..530b3cee 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/SaveThing.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/SaveThing.java @@ -1,59 +1,13 @@ package eu.toldi.infinityforlemmy; -import androidx.annotation.NonNull; - -import java.util.HashMap; -import java.util.Map; - -import eu.toldi.infinityforlemmy.apis.RedditAPI; -import eu.toldi.infinityforlemmy.utils.APIUtils; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; import retrofit2.Retrofit; -public class SaveThing { - public static void saveThing(Retrofit oauthRetrofit, String accessToken, String fullname, - SaveThingListener saveThingListener) { - Map params = new HashMap<>(); - params.put(APIUtils.ID_KEY, fullname); - oauthRetrofit.create(RedditAPI.class).save(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - saveThingListener.success(); - } else { - saveThingListener.failed(); - } - } +public interface SaveThing { + void saveThing(Retrofit oauthRetrofit, String accessToken, int id, + SaveThingListener saveThingListener); - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - saveThingListener.failed(); - } - }); - } - - public static void unsaveThing(Retrofit oauthRetrofit, String accessToken, String fullname, - SaveThingListener saveThingListener) { - Map params = new HashMap<>(); - params.put(APIUtils.ID_KEY, fullname); - oauthRetrofit.create(RedditAPI.class).unsave(APIUtils.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(); - } - }); - } + void unsaveThing(Retrofit oauthRetrofit, String accessToken, int id, + SaveThingListener saveThingListener); public interface SaveThingListener { void success(); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java index b65c5c5b..2353cf01 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/ViewPostDetailActivity.java @@ -63,6 +63,7 @@ import eu.toldi.infinityforlemmy.LoadingMorePostsStatus; import eu.toldi.infinityforlemmy.R; import eu.toldi.infinityforlemmy.RedditDataRoomDatabase; import eu.toldi.infinityforlemmy.RetrofitHolder; +import eu.toldi.infinityforlemmy.SaveComment; import eu.toldi.infinityforlemmy.SaveThing; import eu.toldi.infinityforlemmy.SortType; import eu.toldi.infinityforlemmy.SortTypeSelectionCallback; @@ -443,9 +444,10 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele } public void saveComment(@NonNull Comment comment, int position) { + SaveComment saveComment = new SaveComment(); if (comment.isSaved()) { comment.setSaved(false); - SaveThing.unsaveThing(mOauthRetrofit, mAccessToken, comment.getFullName(), new SaveThing.SaveThingListener() { + saveComment.unsaveThing(mOauthRetrofit, mAccessToken, comment.getId(), new SaveThing.SaveThingListener() { @Override public void success() { ViewPostDetailFragment fragment = sectionsPagerAdapter.getCurrentFragment(); @@ -466,7 +468,7 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele }); } else { comment.setSaved(true); - SaveThing.saveThing(mOauthRetrofit, mAccessToken, comment.getFullName(), new SaveThing.SaveThingListener() { + saveComment.saveThing(mOauthRetrofit, mAccessToken, comment.getId(), new SaveThing.SaveThingListener() { @Override public void success() { ViewPostDetailFragment fragment = sectionsPagerAdapter.getCurrentFragment(); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsListingRecyclerViewAdapter.java b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsListingRecyclerViewAdapter.java index 8c75ee40..7a5cdffc 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsListingRecyclerViewAdapter.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/adapters/CommentsListingRecyclerViewAdapter.java @@ -32,6 +32,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import eu.toldi.infinityforlemmy.NetworkState; import eu.toldi.infinityforlemmy.R; +import eu.toldi.infinityforlemmy.SaveComment; import eu.toldi.infinityforlemmy.SaveThing; import eu.toldi.infinityforlemmy.VoteThing; import eu.toldi.infinityforlemmy.activities.BaseActivity; @@ -646,9 +647,10 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter communityFollow(@Body FollowCommunityDTO params); + @Headers("Content-Type: application/json") + @PUT("api/v3/post/save") + Call postSave(@Body SavePostDTO params); + + @Headers("Content-Type: application/json") + @PUT("api/v3/comment/save") + Call commentSave(@Body SaveCommentDTO params); + @GET("api/v3/comment/list") Call getComments( @Query("type_") String type, diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/dto/SaveCommentDTO.java b/app/src/main/java/eu/toldi/infinityforlemmy/dto/SaveCommentDTO.java new file mode 100644 index 00000000..b913e510 --- /dev/null +++ b/app/src/main/java/eu/toldi/infinityforlemmy/dto/SaveCommentDTO.java @@ -0,0 +1,26 @@ +package eu.toldi.infinityforlemmy.dto; + +public class SaveCommentDTO { + + private int comment_id; + private boolean save; + private String auth; + + public SaveCommentDTO(int comment_id, boolean save, String auth) { + this.comment_id = comment_id; + this.save = save; + this.auth = auth; + } + + public int getComment_id() { + return comment_id; + } + + public boolean isSave() { + return save; + } + + public String getAuth() { + return auth; + } +} diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/dto/SavePostDTO.java b/app/src/main/java/eu/toldi/infinityforlemmy/dto/SavePostDTO.java new file mode 100644 index 00000000..077feb5f --- /dev/null +++ b/app/src/main/java/eu/toldi/infinityforlemmy/dto/SavePostDTO.java @@ -0,0 +1,27 @@ +package eu.toldi.infinityforlemmy.dto; + +public class SavePostDTO { + + + private int post_id; + private boolean save; + private String auth; + + public SavePostDTO(int post_id, boolean save, String auth) { + this.post_id = post_id; + this.save = save; + this.auth = auth; + } + + public int getPost_id() { + return post_id; + } + + public boolean isSave() { + return save; + } + + public String getAuth() { + return auth; + } +} diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java index 2ba582a8..47731b25 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/fragments/ViewPostDetailFragment.java @@ -73,6 +73,7 @@ import eu.toldi.infinityforlemmy.Infinity; import eu.toldi.infinityforlemmy.R; import eu.toldi.infinityforlemmy.RedditDataRoomDatabase; import eu.toldi.infinityforlemmy.RetrofitHolder; +import eu.toldi.infinityforlemmy.SavePost; import eu.toldi.infinityforlemmy.SaveThing; import eu.toldi.infinityforlemmy.SortType; import eu.toldi.infinityforlemmy.activities.CommentActivity; @@ -948,9 +949,10 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic return true; } else if (itemId == R.id.action_save_view_post_detail_fragment) { if (mPost != null && mAccessToken != null) { + SavePost savePost = new SavePost(); if (mPost.isSaved()) { item.setIcon(mUnsavedIcon); - SaveThing.unsaveThing(mOauthRetrofit, mAccessToken, mPost.getFullName(), + savePost.unsaveThing(mOauthRetrofit, mAccessToken, mPost.getId(), new SaveThing.SaveThingListener() { @Override public void success() { @@ -974,7 +976,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic }); } else { item.setIcon(mSavedIcon); - SaveThing.saveThing(mOauthRetrofit, mAccessToken, mPost.getFullName(), + savePost.saveThing(mOauthRetrofit, mAccessToken, mPost.getId(), new SaveThing.SaveThingListener() { @Override public void success() {