fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingFeedsAsyncTa...

95 lines
3.9 KiB
Java
Raw Normal View History

2017-09-27 17:52:23 +02:00
/* Copyright 2017 Thomas Schneider
*
* This file is a part of Mastalab
*
* 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.
*
* Mastalab 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 Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.asynctasks;
import android.content.Context;
import android.os.AsyncTask;
2017-10-27 15:09:26 +02:00
import java.lang.ref.WeakReference;
2017-09-27 17:52:23 +02:00
import java.util.ArrayList;
import java.util.List;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
2018-09-22 19:13:55 +02:00
import fr.gouv.etalab.mastodon.helper.Helper;
2017-09-27 17:52:23 +02:00
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMissingFeedsInterface;
/**
* Created by Thomas on 27/09/2017.
* Retrieves missing toots since last pause
*/
public class RetrieveMissingFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
private String since_id;
private OnRetrieveMissingFeedsInterface listener;
private List<fr.gouv.etalab.mastodon.client.Entities.Status> statuses = new ArrayList<>();
2017-10-07 08:34:50 +02:00
private RetrieveFeedsAsyncTask.Type type;
2017-10-27 15:09:26 +02:00
private WeakReference<Context> contextReference;
private String remoteInstance;
2017-09-27 17:52:23 +02:00
2017-10-07 08:34:50 +02:00
public RetrieveMissingFeedsAsyncTask(Context context, String since_id, RetrieveFeedsAsyncTask.Type type, OnRetrieveMissingFeedsInterface onRetrieveMissingFeedsInterface){
2017-10-27 15:09:26 +02:00
this.contextReference = new WeakReference<>(context);
2017-09-27 17:52:23 +02:00
this.since_id = since_id;
this.listener = onRetrieveMissingFeedsInterface;
2017-10-07 08:34:50 +02:00
this.type = type;
2017-09-27 17:52:23 +02:00
}
public RetrieveMissingFeedsAsyncTask(Context context, String remoteInstance, String since_id, RetrieveFeedsAsyncTask.Type type, OnRetrieveMissingFeedsInterface onRetrieveMissingFeedsInterface){
this.contextReference = new WeakReference<>(context);
this.since_id = since_id;
this.listener = onRetrieveMissingFeedsInterface;
this.type = type;
this.remoteInstance = remoteInstance;
}
2017-09-27 17:52:23 +02:00
@Override
protected Void doInBackground(Void... params) {
2018-11-04 10:01:16 +01:00
if( this.contextReference.get() == null)
return null;
2017-10-27 15:09:26 +02:00
API api = new API(this.contextReference.get());
2017-09-27 17:52:23 +02:00
List<fr.gouv.etalab.mastodon.client.Entities.Status> tempStatus;
2017-10-07 08:34:50 +02:00
APIResponse apiResponse = null;
if( type == RetrieveFeedsAsyncTask.Type.HOME)
apiResponse = api.getHomeTimeline(since_id);
2018-10-10 11:25:54 +02:00
else if( type == RetrieveFeedsAsyncTask.Type.DIRECT)
apiResponse = api.getDirectTimelineSinceId(since_id);
else if( type == RetrieveFeedsAsyncTask.Type.CONVERSATION)
apiResponse = api.getConversationTimelineSinceId(since_id);
else if( type == RetrieveFeedsAsyncTask.Type.LOCAL)
apiResponse = api.getPublicTimelineSinceId(true, since_id);
else if( type == RetrieveFeedsAsyncTask.Type.PUBLIC)
apiResponse = api.getPublicTimelineSinceId(false, since_id);
else if (type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE)
apiResponse = api.getInstanceTimelineSinceId(remoteInstance, since_id);
if (apiResponse != null) {
2017-09-27 17:52:23 +02:00
tempStatus = apiResponse.getStatuses();
if( tempStatus != null)
2017-09-27 17:52:23 +02:00
statuses.addAll(0, tempStatus);
}
if (type == RetrieveFeedsAsyncTask.Type.HOME && statuses.size() > 0) {
2017-09-27 17:52:23 +02:00
MainActivity.lastHomeId = statuses.get(0).getId();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
listener.onRetrieveMissingFeeds(statuses);
}
}