TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/asynctasks/PostActionAsyncTask.java

89 lines
3.5 KiB
Java
Raw Normal View History

2020-06-26 08:50:49 +02:00
package app.fedilab.fedilabtube.asynctasks;
import android.content.Context;
import android.os.AsyncTask;
import java.lang.ref.WeakReference;
import app.fedilab.fedilabtube.client.PeertubeAPI;
import app.fedilab.fedilabtube.client.entities.Account;
import app.fedilab.fedilabtube.client.entities.Error;
import app.fedilab.fedilabtube.interfaces.OnPostActionInterface;
public class PostActionAsyncTask extends AsyncTask<Void, Void, Void> {
private OnPostActionInterface listener;
private int statusCode;
private PeertubeAPI.StatusAction apiAction;
private String targetedId, targetedComment;
private String comment;
private WeakReference<Context> contextReference;
private Error error;
private Account account;
public PostActionAsyncTask(Context context, Account account, PeertubeAPI.StatusAction apiAction, String targetedId, OnPostActionInterface onPostActionInterface) {
this.contextReference = new WeakReference<>(context);
this.listener = onPostActionInterface;
this.apiAction = apiAction;
this.targetedId = targetedId;
this.account = account;
}
public PostActionAsyncTask(Context context, String targetedId, String comment, String targetedComment, OnPostActionInterface onPostActionInterface) {
this.contextReference = new WeakReference<>(context);
this.listener = onPostActionInterface;
this.apiAction = PeertubeAPI.StatusAction.PEERTUBEREPLY;
this.targetedId = targetedId;
this.comment = comment;
this.targetedComment = targetedComment;
}
public PostActionAsyncTask(Context context, PeertubeAPI.StatusAction apiAction, String targetedId, app.fedilab.fedilabtube.client.entities.Status status, String comment, OnPostActionInterface onPostActionInterface) {
contextReference = new WeakReference<>(context);
this.listener = onPostActionInterface;
this.apiAction = apiAction;
this.targetedId = targetedId;
this.comment = comment;
}
@Override
protected Void doInBackground(Void... params) {
//Remote action
PeertubeAPI peertubeAPI;
if (account != null)
peertubeAPI = new PeertubeAPI(contextReference.get(), account.getInstance(), account.getToken());
else
peertubeAPI = new PeertubeAPI(contextReference.get());
if (apiAction == PeertubeAPI.StatusAction.FOLLOW || apiAction == PeertubeAPI.StatusAction.UNFOLLOW)
statusCode = peertubeAPI.postAction(apiAction, targetedId);
else if (apiAction == PeertubeAPI.StatusAction.RATEVIDEO)
statusCode = peertubeAPI.postRating(targetedId, comment);
else if (apiAction == PeertubeAPI.StatusAction.PEERTUBECOMMENT)
statusCode = peertubeAPI.postComment(targetedId, comment);
else if (apiAction == PeertubeAPI.StatusAction.PEERTUBEREPLY)
statusCode = peertubeAPI.postReply(targetedId, comment, targetedComment);
else if (apiAction == PeertubeAPI.StatusAction.PEERTUBEDELETECOMMENT) {
statusCode = peertubeAPI.deleteComment(targetedId, comment);
targetedId = comment;
} else if (apiAction == PeertubeAPI.StatusAction.PEERTUBEDELETEVIDEO) {
statusCode = peertubeAPI.deleteVideo(targetedId);
}
error = peertubeAPI.getError();
return null;
}
@Override
protected void onPostExecute(Void result) {
if (listener != null) {
listener.onPostAction(statusCode, apiAction, targetedId, error);
}
}
}