Save posts/comment with save post button

The save post/comment buttons are now functional.
Signed-off-by: Balazs Toldi <balazs@toldi.eu>
This commit is contained in:
Balazs Toldi 2023-07-27 13:08:01 +02:00
parent e2de8b8160
commit 5b26d57fdb
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
13 changed files with 211 additions and 72 deletions

View File

@ -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<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
saveThingListener.success();
} else {
saveThingListener.failed();
}
}
@Override
public void onFailure(@NonNull Call<String> 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<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
saveThingListener.success();
} else {
saveThingListener.failed();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
saveThingListener.failed();
}
});
}
}

View File

@ -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<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
saveThingListener.success();
} else {
saveThingListener.failed();
}
}
@Override
public void onFailure(@NonNull Call<String> 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<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
saveThingListener.success();
} else {
saveThingListener.failed();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
saveThingListener.failed();
}
});
}
}

View File

@ -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<String, String> params = new HashMap<>();
params.put(APIUtils.ID_KEY, fullname);
oauthRetrofit.create(RedditAPI.class).save(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> 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<String> call, @NonNull Throwable t) {
saveThingListener.failed();
}
});
}
public static void unsaveThing(Retrofit oauthRetrofit, String accessToken, String fullname,
SaveThingListener saveThingListener) {
Map<String, String> params = new HashMap<>();
params.put(APIUtils.ID_KEY, fullname);
oauthRetrofit.create(RedditAPI.class).unsave(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
saveThingListener.success();
} else {
saveThingListener.failed();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
saveThingListener.failed();
}
});
}
void unsaveThing(Retrofit oauthRetrofit, String accessToken, int id,
SaveThingListener saveThingListener);
public interface SaveThingListener {
void success();

View File

@ -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();

View File

@ -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<Comment
}
Comment comment = getItem(position);
if (comment != null) {
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() {
comment.setSaved(false);
@ -669,7 +671,7 @@ public class CommentsListingRecyclerViewAdapter extends PagedListAdapter<Comment
});
} 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() {
comment.setSaved(true);

View File

@ -42,6 +42,7 @@ import java.util.concurrent.Executor;
import butterknife.BindView;
import butterknife.ButterKnife;
import eu.toldi.infinityforlemmy.R;
import eu.toldi.infinityforlemmy.SaveComment;
import eu.toldi.infinityforlemmy.SaveThing;
import eu.toldi.infinityforlemmy.SortType;
import eu.toldi.infinityforlemmy.VoteThing;
@ -1545,9 +1546,10 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
Comment comment = getCurrentComment(this);
if (comment != null) {
int position = getBindingAdapterPosition();
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() {
comment.setSaved(false);
@ -1568,7 +1570,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi
});
} 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() {
comment.setSaved(true);

View File

@ -67,6 +67,7 @@ import eu.toldi.infinityforlemmy.FetchGfycatOrRedgifsVideoLinks;
import eu.toldi.infinityforlemmy.FetchStreamableVideo;
import eu.toldi.infinityforlemmy.R;
import eu.toldi.infinityforlemmy.SaveMemoryCenterInisdeDownsampleStrategy;
import eu.toldi.infinityforlemmy.SavePost;
import eu.toldi.infinityforlemmy.SaveThing;
import eu.toldi.infinityforlemmy.StreamableVideo;
import eu.toldi.infinityforlemmy.VoteThing;
@ -2476,10 +2477,10 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show();
return;
}
SavePost savePost = new SavePost();
if (post.isSaved()) {
saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp);
SaveThing.unsaveThing(mOauthRetrofit, mAccessToken, post.getFullName(),
savePost.unsaveThing(mOauthRetrofit, mAccessToken, post.getId(),
new SaveThing.SaveThingListener() {
@Override
public void success() {
@ -2503,7 +2504,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
});
} else {
saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp);
SaveThing.saveThing(mOauthRetrofit, mAccessToken, post.getFullName(),
savePost.saveThing(mOauthRetrofit, mAccessToken, post.getId(),
new SaveThing.SaveThingListener() {
@Override
public void success() {
@ -3804,9 +3805,10 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
}
Post post = getItem(position);
if (post != null) {
SavePost savePost = new SavePost();
if (post.isSaved()) {
saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp);
SaveThing.unsaveThing(mOauthRetrofit, mAccessToken, post.getFullName(),
savePost.unsaveThing(mOauthRetrofit, mAccessToken, post.getId(),
new SaveThing.SaveThingListener() {
@Override
public void success() {
@ -3830,7 +3832,7 @@ public class HistoryPostRecyclerViewAdapter extends PagingDataAdapter<Post, Recy
});
} else {
saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp);
SaveThing.saveThing(mOauthRetrofit, mAccessToken, post.getFullName(),
savePost.saveThing(mOauthRetrofit, mAccessToken, post.getId(),
new SaveThing.SaveThingListener() {
@Override
public void success() {

View File

@ -64,6 +64,7 @@ import eu.toldi.infinityforlemmy.FetchStreamableVideo;
import eu.toldi.infinityforlemmy.R;
import eu.toldi.infinityforlemmy.RedditDataRoomDatabase;
import eu.toldi.infinityforlemmy.SaveMemoryCenterInisdeDownsampleStrategy;
import eu.toldi.infinityforlemmy.SavePost;
import eu.toldi.infinityforlemmy.SaveThing;
import eu.toldi.infinityforlemmy.StreamableVideo;
import eu.toldi.infinityforlemmy.VoteThing;
@ -1350,10 +1351,10 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show();
return;
}
SavePost savePost = new SavePost();
if (mPost.isSaved()) {
mSaveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp);
SaveThing.unsaveThing(mOauthRetrofit, mAccessToken, mPost.getFullName(),
savePost.unsaveThing(mOauthRetrofit, mAccessToken, mPost.getId(),
new SaveThing.SaveThingListener() {
@Override
public void success() {
@ -1373,7 +1374,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler
});
} else {
mSaveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp);
SaveThing.saveThing(mOauthRetrofit, mAccessToken, mPost.getFullName(),
savePost.saveThing(mOauthRetrofit, mAccessToken, mPost.getId(),
new SaveThing.SaveThingListener() {
@Override
public void success() {

View File

@ -68,6 +68,7 @@ import eu.toldi.infinityforlemmy.FetchStreamableVideo;
import eu.toldi.infinityforlemmy.MarkPostAsReadInterface;
import eu.toldi.infinityforlemmy.R;
import eu.toldi.infinityforlemmy.SaveMemoryCenterInisdeDownsampleStrategy;
import eu.toldi.infinityforlemmy.SavePost;
import eu.toldi.infinityforlemmy.SaveThing;
import eu.toldi.infinityforlemmy.StreamableVideo;
import eu.toldi.infinityforlemmy.VoteThing;
@ -2587,10 +2588,10 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
Toast.makeText(mActivity, R.string.login_first, Toast.LENGTH_SHORT).show();
return;
}
SavePost savePost = new SavePost();
if (post.isSaved()) {
saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp);
SaveThing.unsaveThing(mOauthRetrofit, mAccessToken, post.getFullName(),
savePost.unsaveThing(mOauthRetrofit, mAccessToken, post.getId(),
new SaveThing.SaveThingListener() {
@Override
public void success() {
@ -2614,7 +2615,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
});
} else {
saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp);
SaveThing.saveThing(mOauthRetrofit, mAccessToken, post.getFullName(),
savePost.saveThing(mOauthRetrofit, mAccessToken, post.getId(),
new SaveThing.SaveThingListener() {
@Override
public void success() {
@ -3948,9 +3949,10 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
}
Post post = getItem(position);
if (post != null) {
SavePost postSave = new SavePost();
if (post.isSaved()) {
saveButton.setImageResource(R.drawable.ic_bookmark_border_grey_24dp);
SaveThing.unsaveThing(mOauthRetrofit, mAccessToken, post.getFullName(),
postSave.unsaveThing(mOauthRetrofit, mAccessToken, post.getId(),
new SaveThing.SaveThingListener() {
@Override
public void success() {
@ -3974,7 +3976,7 @@ public class PostRecyclerViewAdapter extends PagingDataAdapter<Post, RecyclerVie
});
} else {
saveButton.setImageResource(R.drawable.ic_bookmark_grey_24dp);
SaveThing.saveThing(mOauthRetrofit, mAccessToken, post.getFullName(),
postSave.saveThing(mOauthRetrofit, mAccessToken, post.getId(),
new SaveThing.SaveThingListener() {
@Override
public void success() {

View File

@ -7,12 +7,15 @@ import eu.toldi.infinityforlemmy.dto.CommentDTO;
import eu.toldi.infinityforlemmy.dto.CommentVoteDTO;
import eu.toldi.infinityforlemmy.dto.FollowCommunityDTO;
import eu.toldi.infinityforlemmy.dto.PostVoteDTO;
import eu.toldi.infinityforlemmy.dto.SaveCommentDTO;
import eu.toldi.infinityforlemmy.dto.SavePostDTO;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Query;
public interface LemmyAPI {
@ -71,6 +74,14 @@ public interface LemmyAPI {
@POST("api/v3/community/follow")
Call<String> communityFollow(@Body FollowCommunityDTO params);
@Headers("Content-Type: application/json")
@PUT("api/v3/post/save")
Call<String> postSave(@Body SavePostDTO params);
@Headers("Content-Type: application/json")
@PUT("api/v3/comment/save")
Call<String> commentSave(@Body SaveCommentDTO params);
@GET("api/v3/comment/list")
Call<String> getComments(
@Query("type_") String type,

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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() {