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

99 lines
4.3 KiB
Java

package app.fedilab.fedilabtube.asynctasks;
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
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.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;
public PostActionAsyncTask(Context context, PeertubeAPI.StatusAction apiAction, String targetedId, OnPostActionInterface onPostActionInterface) {
this.contextReference = new WeakReference<>(context);
this.listener = onPostActionInterface;
this.apiAction = apiAction;
this.targetedId = targetedId;
}
@SuppressWarnings("unused")
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, 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) {
PeertubeAPI peertubeAPI = new PeertubeAPI(contextReference.get());
if (apiAction == PeertubeAPI.StatusAction.FOLLOW || apiAction == PeertubeAPI.StatusAction.UNFOLLOW
|| apiAction == PeertubeAPI.StatusAction.MUTE || apiAction == PeertubeAPI.StatusAction.UNMUTE) {
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);
} else if (apiAction == PeertubeAPI.StatusAction.REPORT_ACCOUNT) {
statusCode = peertubeAPI.report(PeertubeAPI.reportType.ACCOUNT, targetedId, comment);
} else if (apiAction == PeertubeAPI.StatusAction.REPORT_VIDEO) {
statusCode = peertubeAPI.report(PeertubeAPI.reportType.VIDEO, targetedId, comment);
}
error = peertubeAPI.getError();
return null;
}
@Override
protected void onPostExecute(Void result) {
if (listener != null) {
listener.onPostAction(statusCode, apiAction, targetedId, error);
}
}
}