Fix comment issue when posting

This commit is contained in:
Thomas 2020-10-14 16:06:17 +02:00
parent 2f773b2f0f
commit 2d33d7f970
6 changed files with 118 additions and 80 deletions

View File

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

View File

@ -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<String> postComment(@Header("Authorization") String credentials, @Path("id") String id, @Field("text") String text);
Call<CommentData.CommentPosted> postComment(@Header("Authorization") String credentials, @Path("id") String id, @Field("text") String text);
@FormUrlEncoded
@POST("videos/{id}/comments/{commentId}")
Call<String> postReply(@Header("Authorization") String credentials, @Path("id") String id, @Path("commentId") String commentId, @Field("text") String text);
Call<CommentData.CommentPosted> postReply(@Header("Authorization") String credentials, @Path("id") String id, @Path("commentId") String commentId, @Field("text") String text);
@DELETE("videos/{id}/comments/{commentId}")
Call<String> deleteComment(@Header("Authorization") String credentials, @Path("id") String id, @Path("commentId") String commentId);

View File

@ -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<String> 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<String> stringCall = peertubeService.postComment(getToken(), videoId, text);
Response<String> response = stringCall.execute();
if (response.isSuccessful()) {
apiResponse.setActionReturn(response.body());
Call<CommentData.CommentPosted> commentPostedCall = peertubeService.postComment(getToken(), videoId, text);
Response<CommentData.CommentPosted> response = commentPostedCall.execute();
if (response.isSuccessful() && response.body() != null) {
List<CommentData.Comment> comments = new ArrayList<>();
comments.add(response.body().getComment());
apiResponse.setComments(comments);
} else {
setError(apiResponse, response.code(), response.errorBody());
}
} else if (type == ActionType.REPLY) {
Call<String> stringCall = peertubeService.postReply(getToken(), videoId, toCommentId, text);
Response<String> response = stringCall.execute();
if (response.isSuccessful()) {
apiResponse.setActionReturn(response.body());
Call<CommentData.CommentPosted> commentPostedCall = peertubeService.postReply(getToken(), videoId, toCommentId, text);
Response<CommentData.CommentPosted> response = commentPostedCall.execute();
if (response.isSuccessful() && response.body() != null) {
List<CommentData.Comment> comments = new ArrayList<>();
comments.add(response.body().getComment());
apiResponse.setComments(comments);
} else {
setError(apiResponse, response.code(), response.errorBody());
}

View File

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

View File

@ -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<RecyclerView.ViewHo
public AllCommentRemoved allCommentRemoved;
private Context context;
private List<Comment> comments;
private CommentListAdapter commentListAdapter;
private final List<Comment> comments;
private final CommentListAdapter commentListAdapter;
boolean isVideoOwner;
public CommentListAdapter(List<Comment> comments, boolean isVideoOwner) {
@ -131,67 +131,64 @@ public class CommentListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHo
popup.getMenu().findItem(R.id.action_delete).setVisible(false);
}
popup.setOnMenuItemClickListener(item -> {
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;
});

View File

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