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

146 lines
5.3 KiB
Java
Raw Normal View History

2020-06-26 08:50:49 +02:00
package app.fedilab.fedilabtube.asynctasks;
2020-07-01 16:36:08 +02:00
/* 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>. */
2020-06-26 08:50:49 +02:00
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import java.lang.ref.WeakReference;
import java.util.List;
import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.PeertubeAPI;
import app.fedilab.fedilabtube.client.entities.Peertube;
import app.fedilab.fedilabtube.interfaces.OnRetrieveFeedsInterface;
2020-06-26 14:34:39 +02:00
import app.fedilab.fedilabtube.sqlite.PeertubeFavoritesDAO;
2020-06-26 08:50:49 +02:00
import app.fedilab.fedilabtube.sqlite.Sqlite;
public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
private Type action;
private APIResponse apiResponse;
private String max_id;
private OnRetrieveFeedsInterface listener;
private WeakReference<Context> contextReference;
2020-06-27 19:08:52 +02:00
private String target;
2020-06-26 08:50:49 +02:00
2020-06-26 14:34:39 +02:00
public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface) {
this.contextReference = new WeakReference<>(context);
this.action = action;
this.max_id = max_id;
this.listener = onRetrieveFeedsInterface;
2020-06-27 19:08:52 +02:00
this.target = null;
2020-06-26 14:34:39 +02:00
}
2020-06-26 08:50:49 +02:00
2020-06-27 19:08:52 +02:00
public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, String target, OnRetrieveFeedsInterface onRetrieveFeedsInterface) {
2020-06-26 08:50:49 +02:00
this.contextReference = new WeakReference<>(context);
this.action = action;
this.max_id = max_id;
this.listener = onRetrieveFeedsInterface;
2020-06-27 19:08:52 +02:00
this.target = target;
2020-06-26 08:50:49 +02:00
}
@Override
protected Void doInBackground(Void... params) {
PeertubeAPI peertubeAPI = new PeertubeAPI(this.contextReference.get());
2020-06-26 14:34:39 +02:00
2020-06-26 08:50:49 +02:00
if (action == null)
return null;
switch (action) {
case USER:
2020-06-27 19:08:52 +02:00
apiResponse = peertubeAPI.getVideos(target, max_id);
2020-06-26 08:50:49 +02:00
break;
case MYVIDEOS:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
apiResponse = peertubeAPI.getMyVideos(max_id);
break;
case PEERTUBE_HISTORY:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
apiResponse = peertubeAPI.getMyHistory(max_id);
break;
case CHANNEL:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
2020-06-27 19:08:52 +02:00
apiResponse = peertubeAPI.getVideosChannel(target, max_id);
2020-06-26 08:50:49 +02:00
break;
case CACHE_BOOKMARKS_PEERTUBE:
apiResponse = new APIResponse();
2020-06-26 14:34:39 +02:00
SQLiteDatabase db = Sqlite.getInstance(contextReference.get().getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2020-06-26 08:50:49 +02:00
List<Peertube> peertubes = new PeertubeFavoritesDAO(contextReference.get(), db).getAllPeertube();
apiResponse.setPeertubes(peertubes);
break;
case PSUBSCRIPTIONS:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
apiResponse = peertubeAPI.getSubscriptionsTL(max_id);
break;
case POVERVIEW:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
apiResponse = peertubeAPI.getOverviewTL(max_id);
break;
case PTRENDING:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
apiResponse = peertubeAPI.getTrendingTL(max_id);
break;
case PRECENTLYADDED:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
apiResponse = peertubeAPI.getRecentlyAddedTL(max_id);
break;
case PLOCAL:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
apiResponse = peertubeAPI.getLocalTL(max_id);
break;
2020-06-27 11:21:25 +02:00
case PPUBLIC:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
apiResponse = peertubeAPI.getPublicTL(max_id);
break;
case PMOSTLIKED:
peertubeAPI = new PeertubeAPI(this.contextReference.get());
apiResponse = peertubeAPI.getLikedTL(max_id);
break;
2020-06-26 08:50:49 +02:00
}
return null;
}
@Override
protected void onPostExecute(Void result) {
listener.onRetrieveFeeds(apiResponse);
}
public enum Type {
2020-06-26 18:17:17 +02:00
2020-06-26 08:50:49 +02:00
USER,
2020-06-26 18:17:17 +02:00
PPUBLIC,
2020-06-26 08:50:49 +02:00
PSUBSCRIPTIONS,
POVERVIEW,
PTRENDING,
PRECENTLYADDED,
2020-06-26 18:17:17 +02:00
PMOSTLIKED,
2020-06-26 08:50:49 +02:00
PLOCAL,
CHANNEL,
MYVIDEOS,
PEERTUBE_HISTORY,
CACHE_BOOKMARKS_PEERTUBE,
}
}