fedilab-Android-App/app/src/main/java/app/fedilab/android/asynctasks/ManagePollAsyncTask.java

78 lines
2.6 KiB
Java
Raw Normal View History

2019-03-24 17:21:13 +01:00
/* Copyright 2019 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2019-03-24 17:21:13 +01:00
*
* 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.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2019-03-24 17:21:13 +01:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2019-03-24 17:21:13 +01:00
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.asynctasks;
2019-03-24 17:21:13 +01:00
import android.content.Context;
2021-01-19 17:43:51 +01:00
import android.os.Handler;
import android.os.Looper;
2019-09-06 17:55:14 +02:00
2019-03-24 17:21:13 +01:00
import java.lang.ref.WeakReference;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.API;
import app.fedilab.android.client.Entities.Poll;
import app.fedilab.android.interfaces.OnPollInterface;
2019-03-24 17:21:13 +01:00
/**
* Created by Thomas on 23/03/2019.
* Manage Poll
*/
2021-01-19 17:43:51 +01:00
public class ManagePollAsyncTask {
2019-03-24 17:21:13 +01:00
2021-01-19 17:43:51 +01:00
private final OnPollInterface listener;
private final app.fedilab.android.client.Entities.Status status;
private final int[] choices;
private final WeakReference<Context> contextReference;
private final type_s type;
2019-03-24 17:21:13 +01:00
private Poll poll;
2019-09-06 17:55:14 +02:00
public ManagePollAsyncTask(Context context, type_s type, app.fedilab.android.client.Entities.Status status, int[] choices, OnPollInterface onPollInterface) {
2019-03-24 17:21:13 +01:00
this.contextReference = new WeakReference<>(context);
this.listener = onPollInterface;
this.status = status;
this.choices = choices;
this.type = type;
2021-01-19 17:43:51 +01:00
doInBackground();
2019-03-24 17:21:13 +01:00
}
2021-01-19 17:43:51 +01:00
protected void doInBackground() {
2019-04-18 18:01:44 +02:00
2021-01-19 17:43:51 +01:00
new Thread(() -> {
Poll _poll;
if (status.getReblog() != null)
_poll = status.getReblog().getPoll();
else
_poll = status.getPoll();
2019-03-24 17:21:13 +01:00
2021-01-19 17:43:51 +01:00
if (_poll.getId() != null) {
if (type == type_s.SUBMIT) {
poll = new API(contextReference.get()).submiteVote(_poll.getId(), choices);
} else if (type == type_s.REFRESH) {
poll = new API(contextReference.get()).getPoll(status);
}
}
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> listener.onPoll(status, poll);
mainHandler.post(myRunnable);
}).start();
2019-03-24 17:21:13 +01:00
}
2019-11-15 16:32:25 +01:00
public enum type_s {
SUBMIT,
REFRESH
}
2019-03-24 17:21:13 +01:00
}