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

55 lines
1.8 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.APIResponse;
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.OnRetrievePeertubeNotificationsInterface;
public class RetrievePeertubeNotificationsAsyncTask extends AsyncTask<Void, Void, Void> {
private APIResponse apiResponse;
private String max_id;
private Account account;
private OnRetrievePeertubeNotificationsInterface listener;
private WeakReference<Context> contextReference;
public RetrievePeertubeNotificationsAsyncTask(Context context, Account account, String max_id, OnRetrievePeertubeNotificationsInterface onRetrievePeertubeNotificationsInterface) {
this.contextReference = new WeakReference<>(context);
this.max_id = max_id;
this.listener = onRetrievePeertubeNotificationsInterface;
this.account = account;
}
@Override
protected Void doInBackground(Void... params) {
PeertubeAPI api;
if (account == null) {
api = new PeertubeAPI(this.contextReference.get());
apiResponse = api.getNotifications(max_id);
} else {
if (this.contextReference.get() == null) {
apiResponse.setError(new Error());
return null;
}
api = new PeertubeAPI(this.contextReference.get(), account.getInstance(), account.getToken());
apiResponse = api.getNotificationsSince(max_id);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
listener.onRetrievePeertubeNotifications(apiResponse, account);
}
}