diff --git a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java index 0336de7..8c57fae 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java @@ -975,8 +975,10 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd public void manageVIewPostActions(RetrofitPeertubeAPI.ActionType statusAction, APIResponse apiResponse) { if (peertube.isCommentsEnabled() && statusAction == ADD_COMMENT) { - CommentVM commentViewModel = new ViewModelProvider(PeertubeActivity.this).get(CommentVM.class); - commentViewModel.getThread(sepiaSearch?peertubeInstance:null, videoUuid, max_id).observe(PeertubeActivity.this, this::manageVIewComment); + if( apiResponse.getComments() != null && apiResponse.getComments().size() > 0 ) { + comments.add(0, apiResponse.getComments().get(0)); + commentListAdapter.notifyItemInserted(0); + } } else if (statusAction == RetrofitPeertubeAPI.ActionType.REPORT_ACCOUNT) { Toasty.success(PeertubeActivity.this, getString(R.string.successful_report), Toasty.LENGTH_LONG).show(); } else if (statusAction == RetrofitPeertubeAPI.ActionType.REPORT_VIDEO) { diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java index bcffbe5..16689c2 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java @@ -17,7 +17,7 @@ package app.fedilab.fedilabtube.client; import java.util.List; import java.util.Map; -import app.fedilab.fedilabtube.client.data.AccountData; + import app.fedilab.fedilabtube.client.data.BlockData; import app.fedilab.fedilabtube.client.data.CaptionData; import app.fedilab.fedilabtube.client.data.ChannelData; @@ -332,11 +332,11 @@ public interface PeertubeService { @FormUrlEncoded @POST("videos/{id}/comment-threads") - Call postComment(@Header("Authorization") String credentials, @Path("id") String id, @Field("text") String text); + Call postComment(@Header("Authorization") String credentials, @Path("id") String id, @Field("text") String text); @FormUrlEncoded @POST("videos/{id}/comments/{commentId}") - Call postReply(@Header("Authorization") String credentials, @Path("id") String id, @Path("commentId") String commentId, @Field("text") String text); + Call postReply(@Header("Authorization") String credentials, @Path("id") String id, @Path("commentId") String commentId, @Field("text") String text); @DELETE("videos/{id}/comments/{commentId}") Call deleteComment(@Header("Authorization") String credentials, @Path("id") String id, @Path("commentId") String commentId); diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java index a05342f..bca0d53 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java @@ -82,12 +82,12 @@ import retrofit2.converter.gson.GsonConverterFactory; @SuppressWarnings({"unused", "RedundantSuppression"}) public class RetrofitPeertubeAPI { - private String finalUrl; - private Context _context; - private String instance; + private final String finalUrl; + private final Context _context; + private final String instance; private String token; private Set selection; - private String count; + private final String count; public RetrofitPeertubeAPI(Context context) { _context = context; @@ -1232,18 +1232,22 @@ public class RetrofitPeertubeAPI { APIResponse apiResponse = new APIResponse(); try { if (type == ActionType.ADD_COMMENT) { - Call stringCall = peertubeService.postComment(getToken(), videoId, text); - Response response = stringCall.execute(); - if (response.isSuccessful()) { - apiResponse.setActionReturn(response.body()); + Call commentPostedCall = peertubeService.postComment(getToken(), videoId, text); + Response response = commentPostedCall.execute(); + if (response.isSuccessful() && response.body() != null) { + List comments = new ArrayList<>(); + comments.add(response.body().getComment()); + apiResponse.setComments(comments); } else { setError(apiResponse, response.code(), response.errorBody()); } } else if (type == ActionType.REPLY) { - Call stringCall = peertubeService.postReply(getToken(), videoId, toCommentId, text); - Response response = stringCall.execute(); - if (response.isSuccessful()) { - apiResponse.setActionReturn(response.body()); + Call commentPostedCall = peertubeService.postReply(getToken(), videoId, toCommentId, text); + Response response = commentPostedCall.execute(); + if (response.isSuccessful() && response.body() != null) { + List comments = new ArrayList<>(); + comments.add(response.body().getComment()); + apiResponse.setComments(comments); } else { setError(apiResponse, response.code(), response.errorBody()); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/data/CommentData.java b/app/src/main/java/app/fedilab/fedilabtube/client/data/CommentData.java index 6d93dea..22273b1 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/data/CommentData.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/data/CommentData.java @@ -163,6 +163,20 @@ public class CommentData { } } + public static class CommentPosted{ + @SerializedName("comment") + private Comment comment; + + public Comment getComment() { + return comment; + } + + public void setComment(Comment comment) { + this.comment = comment; + } + } + + public static class NotificationComment { @SerializedName("id") private String id; diff --git a/app/src/main/java/app/fedilab/fedilabtube/drawer/CommentListAdapter.java b/app/src/main/java/app/fedilab/fedilabtube/drawer/CommentListAdapter.java index 431ece9..6968b3f 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/drawer/CommentListAdapter.java +++ b/app/src/main/java/app/fedilab/fedilabtube/drawer/CommentListAdapter.java @@ -16,6 +16,7 @@ package app.fedilab.fedilabtube.drawer; import android.annotation.SuppressLint; import android.app.Activity; +import android.app.AlertDialog; import android.content.Context; import android.os.Build; import android.os.Handler; @@ -49,7 +50,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import app.fedilab.fedilabtube.R; -import app.fedilab.fedilabtube.ShowAccountActivity; import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI; import app.fedilab.fedilabtube.client.data.CommentData.Comment; @@ -66,8 +66,8 @@ public class CommentListAdapter extends RecyclerView.Adapter comments; - private CommentListAdapter commentListAdapter; + private final List comments; + private final CommentListAdapter commentListAdapter; boolean isVideoOwner; public CommentListAdapter(List comments, boolean isVideoOwner) { @@ -131,67 +131,64 @@ public class CommentListAdapter extends RecyclerView.Adapter { - switch (item.getItemId()) { - case R.id.action_delete: - android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context); - builder.setTitle(R.string.delete_comment); - builder.setMessage(R.string.delete_comment_confirm); - builder.setIcon(android.R.drawable.ic_dialog_alert) - .setPositiveButton(R.string.delete, (dialog, which) -> { - new Thread(() -> { - new RetrofitPeertubeAPI(context).post(RetrofitPeertubeAPI.ActionType.PEERTUBEDELETECOMMENT, comment.getVideoId(), comment.getId()); - Handler mainHandler = new Handler(Looper.getMainLooper()); - Runnable myRunnable = () -> { - comments.remove(comment); - notifyDataSetChanged(); - if (comments.size() == 0) { - allCommentRemoved.onAllCommentRemoved(); - } - }; - mainHandler.post(myRunnable); - }).start(); + int itemId = item.getItemId(); + if (itemId == R.id.action_delete) { + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(R.string.delete_comment); + builder.setMessage(R.string.delete_comment_confirm); + builder.setIcon(android.R.drawable.ic_dialog_alert) + .setPositiveButton(R.string.delete, (dialog, which) -> { + new Thread(() -> { + new RetrofitPeertubeAPI(context).post(RetrofitPeertubeAPI.ActionType.PEERTUBEDELETECOMMENT, comment.getVideoId(), comment.getId()); + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> { + comments.remove(comment); + notifyDataSetChanged(); + if (comments.size() == 0) { + allCommentRemoved.onAllCommentRemoved(); + } + }; + mainHandler.post(myRunnable); + }).start(); - dialog.dismiss(); - }) - .setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss()) - .show(); - break; - case R.id.action_report: - reportComment(comment); - break; - case R.id.action_mute: - PostActionsVM viewModel = new ViewModelProvider((ViewModelStoreOwner) context).get(PostActionsVM.class); - viewModel.post(MUTE, comment.getAccount().getAcct(), null).observe((LifecycleOwner) context, apiResponse -> manageVIewPostActions(MUTE, apiResponse)); - comments.remove(comment); - notifyDataSetChanged(); - if (comments.size() == 0) { - allCommentRemoved.onAllCommentRemoved(); - } - break; - case R.id.action_remove_comments: - builder = new android.app.AlertDialog.Builder(context); - builder.setTitle(R.string.delete_account_comment); - builder.setMessage(R.string.delete_account_comment_confirm); - builder.setIcon(android.R.drawable.ic_dialog_alert) - .setPositiveButton(R.string.delete, (dialog, which) -> { - new Thread(() -> { - new RetrofitPeertubeAPI(context).post(RetrofitPeertubeAPI.ActionType.PEERTUBE_DELETE_ALL_COMMENT_FOR_ACCOUNT, comment.getAccount().getAcct(), "my-videos"); - Handler mainHandler = new Handler(Looper.getMainLooper()); - Runnable myRunnable = () -> { - comments.remove(comment); - notifyDataSetChanged(); - if (comments.size() == 0) { - allCommentRemoved.onAllCommentRemoved(); - } - }; - mainHandler.post(myRunnable); - }).start(); + dialog.dismiss(); + }) + .setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss()) + .show(); + } else if (itemId == R.id.action_report) { + reportComment(comment); + } else if (itemId == R.id.action_mute) { + PostActionsVM viewModel = new ViewModelProvider((ViewModelStoreOwner) context).get(PostActionsVM.class); + viewModel.post(MUTE, comment.getAccount().getAcct(), null).observe((LifecycleOwner) context, apiResponse -> manageVIewPostActions(MUTE, apiResponse)); + comments.remove(comment); + notifyDataSetChanged(); + if (comments.size() == 0) { + allCommentRemoved.onAllCommentRemoved(); + } + } else if (itemId == R.id.action_remove_comments) { + AlertDialog.Builder builder; + builder = new AlertDialog.Builder(context); + builder.setTitle(R.string.delete_account_comment); + builder.setMessage(R.string.delete_account_comment_confirm); + builder.setIcon(android.R.drawable.ic_dialog_alert) + .setPositiveButton(R.string.delete, (dialog, which) -> { + new Thread(() -> { + new RetrofitPeertubeAPI(context).post(RetrofitPeertubeAPI.ActionType.PEERTUBE_DELETE_ALL_COMMENT_FOR_ACCOUNT, comment.getAccount().getAcct(), "my-videos"); + Handler mainHandler = new Handler(Looper.getMainLooper()); + Runnable myRunnable = () -> { + comments.remove(comment); + notifyDataSetChanged(); + if (comments.size() == 0) { + allCommentRemoved.onAllCommentRemoved(); + } + }; + mainHandler.post(myRunnable); + }).start(); - dialog.dismiss(); - }) - .setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss()) - .show(); - break; + dialog.dismiss(); + }) + .setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss()) + .show(); } return true; }); diff --git a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java index 2ecb753..372c4ed 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java +++ b/app/src/main/java/app/fedilab/fedilabtube/helper/Helper.java @@ -253,6 +253,10 @@ public class Helper { } } + /** + * Get a default instance host name depending of the device locale + * @return peertube host String + */ private static String getDefaultInstance() { String lang = Locale.getDefault().getLanguage(); if (lang.contains("-")) { @@ -331,6 +335,11 @@ public class Helper { } + /** + * Convert second to String formated date + * @param pTime timestamp + * @return String formatted value + */ public static String secondsToString(int pTime) { int hour = pTime / 3600; @@ -391,6 +400,11 @@ public class Helper { } + /** + * Return rounded numbers depending of the value + * @param count long + * @return String rounded value to be displayed + */ public static String withSuffix(long count) { if (count < 1000) return "" + count; int exp = (int) (Math.log(count) / Math.log(1000)); @@ -419,7 +433,7 @@ public class Helper { } public static void loadGiF(final Context context, String url, final ImageView imageView, int round) { - loadGif(context, null, url, imageView, 10); + loadGif(context, null, url, imageView, round); } @SuppressWarnings("SameParameterValue") @@ -458,6 +472,13 @@ public class Helper { } + /** + * Initialize the webview + * @param activity Current Activity + * @param webviewId int id of the webview layout + * @param rootView View the root view + * @return CustomWebview + */ @SuppressLint("SetJavaScriptEnabled") public static CustomWebview initializeWebview(Activity activity, int webviewId, View rootView) {