Backend for Peertube notifications
This commit is contained in:
parent
701b310a37
commit
aa7d0236f9
|
@ -0,0 +1,73 @@
|
|||
/* Copyright 2019 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;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Error;
|
||||
import fr.gouv.etalab.mastodon.client.PeertubeAPI;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrievePeertubeNotificationsInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Thomas on 23/01/2019.
|
||||
* Retrieves Peertube notifications on the instance
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -27,6 +27,7 @@ import fr.gouv.etalab.mastodon.client.Entities.HowToVideo;
|
|||
import fr.gouv.etalab.mastodon.client.Entities.Instance;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Notification;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Peertube;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.PeertubeNotification;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Relationship;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Status;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.StoredStatus;
|
||||
|
@ -46,6 +47,7 @@ public class APIResponse {
|
|||
private List<Relationship> relationships = null;
|
||||
private List<HowToVideo> howToVideos = null;
|
||||
private List<Peertube> peertubes = null;
|
||||
private List<PeertubeNotification> peertubeNotifications = null;
|
||||
private List<Filters> filters = null;
|
||||
private List<String> domains = null;
|
||||
private List<fr.gouv.etalab.mastodon.client.Entities.List> lists = null;
|
||||
|
@ -189,4 +191,12 @@ public class APIResponse {
|
|||
public void setStoredStatuses(List<StoredStatus> storedStatuses) {
|
||||
this.storedStatuses = storedStatuses;
|
||||
}
|
||||
|
||||
public List<PeertubeNotification> getPeertubeNotifications() {
|
||||
return peertubeNotifications;
|
||||
}
|
||||
|
||||
public void setPeertubeNotifications(List<PeertubeNotification> peertubeNotifications) {
|
||||
this.peertubeNotifications = peertubeNotifications;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ public class PeertubeNotification {
|
|||
|
||||
private String id;
|
||||
private boolean read;
|
||||
private Date updatedAt;
|
||||
private Date updatedAt, createdAt;
|
||||
private int type;
|
||||
private PeertubeComment peertubeComment;
|
||||
private PeertubeVideoNotification peertubeVideoNotification;
|
||||
|
@ -91,4 +91,12 @@ public class PeertubeNotification {
|
|||
public void setPeertubeVideoNotification(PeertubeVideoNotification peertubeVideoNotification) {
|
||||
this.peertubeVideoNotification = peertubeVideoNotification;
|
||||
}
|
||||
|
||||
public Date getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setCreatedAt(Date createdAt) {
|
||||
this.createdAt = createdAt;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,7 +48,12 @@ import fr.gouv.etalab.mastodon.client.Entities.Filters;
|
|||
import fr.gouv.etalab.mastodon.client.Entities.HowToVideo;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Instance;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Peertube;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.PeertubeAccountNotification;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.PeertubeActorFollow;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.PeertubeComment;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.PeertubeInformation;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.PeertubeNotification;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.PeertubeVideoNotification;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Relationship;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Results;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Status;
|
||||
|
@ -587,6 +592,67 @@ public class PeertubeAPI {
|
|||
return apiResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves Peertube notifications for the account *synchronously*
|
||||
*
|
||||
* @param max_id String id max
|
||||
* @return APIResponse
|
||||
*/
|
||||
public APIResponse getNotifications(String max_id){
|
||||
return getNotifications(max_id, null, 20);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves Peertube notifications since id for the account *synchronously*
|
||||
*
|
||||
* @param since_id String id since
|
||||
* @return APIResponse
|
||||
*/
|
||||
public APIResponse getNotificationsSince(String since_id){
|
||||
return getNotifications(null, since_id, 20);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves Peertube notifications for the account *synchronously*
|
||||
*
|
||||
* @param max_id String id max
|
||||
* @param since_id String since the id
|
||||
* @param limit int limit - max value 40
|
||||
* @return APIResponse
|
||||
*/
|
||||
@SuppressWarnings("SameParameterValue")
|
||||
private APIResponse getNotifications(String max_id, String since_id, int limit) {
|
||||
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
if (max_id != null)
|
||||
params.put("start", max_id);
|
||||
if (since_id != null)
|
||||
params.put("since_id", since_id);
|
||||
if (0 < limit || limit > 40)
|
||||
limit = 40;
|
||||
params.put("count", String.valueOf(limit));
|
||||
List<PeertubeNotification> peertubeNotifications = new ArrayList<>();
|
||||
try {
|
||||
HttpsConnection httpsConnection = new HttpsConnection(context);
|
||||
String response = httpsConnection.get(getAbsoluteUrl("/users/me/notifications"), 60, params, prefKeyOauthTokenT);
|
||||
JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
|
||||
peertubeNotifications = parsePeertubeNotifications(jsonArray);
|
||||
} catch (HttpsConnection.HttpsConnectionException e) {
|
||||
setError(e.getStatusCode(), e);
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
apiResponse.setPeertubeNotifications(peertubeNotifications);
|
||||
return apiResponse;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves videos channel for the account *synchronously*
|
||||
|
@ -1391,6 +1457,120 @@ public class PeertubeAPI {
|
|||
return howToVideos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse json response for peertube notifications
|
||||
* @param jsonArray JSONArray
|
||||
* @return List<PeertubeNotification>
|
||||
*/
|
||||
private List<PeertubeNotification> parsePeertubeNotifications(JSONArray jsonArray){
|
||||
List<PeertubeNotification> peertubeNotifications = new ArrayList<>();
|
||||
try {
|
||||
int i = 0;
|
||||
while (i < jsonArray.length() ){
|
||||
JSONObject resobj = jsonArray.getJSONObject(i);
|
||||
PeertubeNotification peertubeNotification = parsePeertubeNotifications(context, resobj);
|
||||
i++;
|
||||
peertubeNotifications.add(peertubeNotification);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
setDefaultError(e);
|
||||
}
|
||||
return peertubeNotifications;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse json response for unique how to
|
||||
* @param resobj JSONObject
|
||||
* @return Peertube
|
||||
*/
|
||||
public static PeertubeNotification parsePeertubeNotifications(Context context,JSONObject resobj){
|
||||
PeertubeNotification peertubeNotification = new PeertubeNotification();
|
||||
try {
|
||||
peertubeNotification.setId(resobj.get("id").toString());
|
||||
peertubeNotification.setType(resobj.getInt("type"));
|
||||
peertubeNotification.setUpdatedAt(Helper.mstStringToDate(context, resobj.get("updatedAt").toString()));
|
||||
peertubeNotification.setCreatedAt(Helper.mstStringToDate(context, resobj.get("createdAt").toString()));
|
||||
peertubeNotification.setRead(resobj.getBoolean("read"));
|
||||
|
||||
if( resobj.has("comment")){
|
||||
PeertubeComment peertubeComment = new PeertubeComment();
|
||||
JSONObject comment = resobj.getJSONObject("comment");
|
||||
PeertubeAccountNotification peertubeAccountNotification = new PeertubeAccountNotification();
|
||||
peertubeAccountNotification.setDisplayName(comment.get("displayName").toString());
|
||||
peertubeAccountNotification.setName(comment.get("name").toString());
|
||||
peertubeAccountNotification.setId(comment.get("id").toString());
|
||||
if( comment.has("avatar")){
|
||||
peertubeAccountNotification.setAvatar(comment.getJSONObject("avatar").get("path").toString());
|
||||
}
|
||||
peertubeComment.setPeertubeAccountNotification(peertubeAccountNotification);
|
||||
PeertubeVideoNotification peertubeVideoNotification = new PeertubeVideoNotification();
|
||||
peertubeVideoNotification.setUuid(comment.get("uuid").toString());
|
||||
peertubeVideoNotification.setName(comment.get("name").toString());
|
||||
peertubeVideoNotification.setId(comment.get("id").toString());
|
||||
peertubeComment.setPeertubeVideoNotification(peertubeVideoNotification);
|
||||
peertubeNotification.setPeertubeComment(peertubeComment);
|
||||
}
|
||||
|
||||
if( resobj.has("video")){
|
||||
PeertubeVideoNotification peertubeVideoNotification = new PeertubeVideoNotification();
|
||||
JSONObject video = resobj.getJSONObject("video");
|
||||
peertubeVideoNotification.setUuid(video.get("uuid").toString());
|
||||
peertubeVideoNotification.setName(video.get("name").toString());
|
||||
peertubeVideoNotification.setId(video.get("id").toString());
|
||||
if( video.has("channel")){
|
||||
PeertubeAccountNotification peertubeAccountNotification = new PeertubeAccountNotification();
|
||||
JSONObject channel = resobj.getJSONObject("video");
|
||||
peertubeAccountNotification.setDisplayName(channel.get("displayName").toString());
|
||||
peertubeAccountNotification.setName(channel.get("name").toString());
|
||||
peertubeAccountNotification.setId(channel.get("id").toString());
|
||||
if( channel.has("avatar")){
|
||||
peertubeAccountNotification.setAvatar(channel.getJSONObject("avatar").get("path").toString());
|
||||
}
|
||||
peertubeVideoNotification.setPeertubeAccountNotification(peertubeAccountNotification);
|
||||
}
|
||||
peertubeNotification.setPeertubeVideoNotification(peertubeVideoNotification);
|
||||
}
|
||||
|
||||
if( resobj.has("actorFollow")){
|
||||
PeertubeActorFollow peertubeActorFollow = new PeertubeActorFollow();
|
||||
JSONObject actorFollow = resobj.getJSONObject("actorFollow");
|
||||
|
||||
JSONObject follower = actorFollow.getJSONObject("follower");
|
||||
JSONObject following = actorFollow.getJSONObject("following");
|
||||
|
||||
PeertubeAccountNotification peertubeAccountNotification = new PeertubeAccountNotification();
|
||||
peertubeAccountNotification.setDisplayName(follower.get("displayName").toString());
|
||||
peertubeAccountNotification.setName(follower.get("name").toString());
|
||||
peertubeAccountNotification.setId(follower.get("id").toString());
|
||||
if( follower.has("avatar")){
|
||||
peertubeAccountNotification.setAvatar(follower.getJSONObject("avatar").get("path").toString());
|
||||
}
|
||||
peertubeActorFollow.setFollower(peertubeAccountNotification);
|
||||
|
||||
PeertubeAccountNotification peertubeAccounFollowingNotification = new PeertubeAccountNotification();
|
||||
peertubeAccounFollowingNotification.setDisplayName(following.get("displayName").toString());
|
||||
peertubeAccounFollowingNotification.setName(following.get("name").toString());
|
||||
peertubeAccounFollowingNotification.setId(following.get("id").toString());
|
||||
if( following.has("avatar")){
|
||||
peertubeAccounFollowingNotification.setAvatar(following.getJSONObject("avatar").get("path").toString());
|
||||
}
|
||||
peertubeActorFollow.setFollowing(peertubeAccounFollowingNotification);
|
||||
peertubeActorFollow.setId(actorFollow.get("id").toString());
|
||||
peertubeNotification.setPeertubeActorFollow(peertubeActorFollow);
|
||||
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return peertubeNotification;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Parse json response for several howto
|
||||
* @param jsonArray JSONArray
|
||||
|
@ -1414,6 +1594,8 @@ public class PeertubeAPI {
|
|||
return peertubes;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Parse json response for unique how to
|
||||
* @param resobj JSONObject
|
||||
|
|
|
@ -14,10 +14,7 @@ package fr.gouv.etalab.mastodon.fragments;
|
|||
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
|
@ -26,7 +23,6 @@ import android.os.Looper;
|
|||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.support.v4.widget.SwipeRefreshLayout;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
|
@ -43,24 +39,21 @@ import es.dmoral.toasty.Toasty;
|
|||
import fr.gouv.etalab.mastodon.R;
|
||||
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.RetrieveMissingNotificationsAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.RetrieveNotificationsAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.client.API;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.RetrievePeertubeNotificationsAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Notification;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Status;
|
||||
import fr.gouv.etalab.mastodon.drawers.NotificationsListAdapter;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMissingNotificationsInterface;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveNotificationsInterface;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrievePeertubeNotificationsInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Thomas on 24/01/2019.
|
||||
* Fragment to display peertube notifications
|
||||
*/
|
||||
public class DisplayPeertubeNotificationsFragment extends Fragment implements OnRetrieveNotificationsInterface, OnRetrieveMissingNotificationsInterface {
|
||||
public class DisplayPeertubeNotificationsFragment extends Fragment implements OnRetrieveMissingNotificationsInterface, OnRetrievePeertubeNotificationsInterface {
|
||||
|
||||
|
||||
|
||||
|
@ -78,8 +71,7 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements On
|
|||
private String userId, instance;
|
||||
private SharedPreferences sharedpreferences;
|
||||
LinearLayoutManager mLayoutManager;
|
||||
private BroadcastReceiver receive_action;
|
||||
private BroadcastReceiver receive_data;
|
||||
|
||||
|
||||
//Peertube notification type
|
||||
public static int NEW_VIDEO_FROM_SUBSCRIPTION = 1;
|
||||
|
@ -135,7 +127,7 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements On
|
|||
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
|
||||
if (!flag_loading) {
|
||||
flag_loading = true;
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayPeertubeNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
asyncTask = new RetrievePeertubeNotificationsAsyncTask(context, null, max_id, DisplayPeertubeNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
nextElementLoader.setVisibility(View.VISIBLE);
|
||||
}
|
||||
} else {
|
||||
|
@ -145,42 +137,6 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements On
|
|||
}
|
||||
});
|
||||
|
||||
if( MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON) {
|
||||
|
||||
if( receive_action != null)
|
||||
LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_action);
|
||||
receive_action = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Bundle b = intent.getExtras();
|
||||
assert b != null;
|
||||
Status status = b.getParcelable("status");
|
||||
API.StatusAction statusAction = (API.StatusAction) b.getSerializable("action");
|
||||
if( status != null) {
|
||||
notificationsListAdapter.notifyNotificationWithActionChanged(statusAction, status);
|
||||
}
|
||||
}
|
||||
};
|
||||
LocalBroadcastManager.getInstance(context).registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_ACTION));
|
||||
|
||||
if( receive_data != null)
|
||||
LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_data);
|
||||
receive_data = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Bundle b = intent.getExtras();
|
||||
assert b != null;
|
||||
String userIdService = b.getString("userIdService", null);
|
||||
if( userIdService != null && userIdService.equals(userId)) {
|
||||
Notification notification = b.getParcelable("data");
|
||||
refresh(notification);
|
||||
if( context instanceof MainActivity)
|
||||
((MainActivity)context).updateNotifCounter();
|
||||
}
|
||||
}
|
||||
};
|
||||
LocalBroadcastManager.getInstance(context).registerReceiver(receive_data, new IntentFilter(Helper.RECEIVE_DATA));
|
||||
}
|
||||
|
||||
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
||||
@Override
|
||||
|
@ -223,13 +179,13 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements On
|
|||
break;
|
||||
}
|
||||
if( context != null)
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayPeertubeNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
asyncTask = new RetrievePeertubeNotificationsAsyncTask(context, null, max_id, DisplayPeertubeNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
else
|
||||
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if( context != null)
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayPeertubeNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
asyncTask = new RetrievePeertubeNotificationsAsyncTask(context, null, max_id, DisplayPeertubeNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
}, 500);
|
||||
return rootView;
|
||||
|
@ -255,73 +211,10 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements On
|
|||
super.onDestroy();
|
||||
if(asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING)
|
||||
asyncTask.cancel(true);
|
||||
if( receive_action != null)
|
||||
LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_action);
|
||||
if( receive_data != null)
|
||||
LocalBroadcastManager.getInstance(context).unregisterReceiver(receive_data);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRetrieveNotifications(APIResponse apiResponse, Account account, boolean refreshData) {
|
||||
mainLoader.setVisibility(View.GONE);
|
||||
nextElementLoader.setVisibility(View.GONE);
|
||||
String lastReadNotifications = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId + instance, null);
|
||||
if( apiResponse.getError() != null){
|
||||
Toasty.error(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
|
||||
flag_loading = false;
|
||||
swipeRefreshLayout.setRefreshing(false);
|
||||
swiped = false;
|
||||
return;
|
||||
}
|
||||
|
||||
int previousPosition = notifications.size();
|
||||
max_id = apiResponse.getMax_id();
|
||||
List<Notification> notifications = apiResponse.getNotifications();
|
||||
|
||||
if( !swiped && firstLoad && (notifications == null || notifications.size() == 0))
|
||||
textviewNoAction.setVisibility(View.VISIBLE);
|
||||
else
|
||||
textviewNoAction.setVisibility(View.GONE);
|
||||
if( swiped ){
|
||||
if (previousPosition > 0) {
|
||||
for (int i = 0; i < previousPosition; i++) {
|
||||
this.notifications.remove(0);
|
||||
}
|
||||
notificationsListAdapter.notifyItemRangeRemoved(0, previousPosition);
|
||||
}
|
||||
swiped = false;
|
||||
}
|
||||
|
||||
if( notifications != null && notifications.size() > 0) {
|
||||
for(Notification tmpNotification: notifications){
|
||||
|
||||
if( lastReadNotifications != null && Long.parseLong(tmpNotification.getId()) > Long.parseLong(lastReadNotifications)) {
|
||||
MainActivity.countNewNotifications++;
|
||||
}
|
||||
try {
|
||||
((MainActivity) context).updateNotifCounter();
|
||||
}catch (Exception ignored){}
|
||||
this.notifications.add(tmpNotification);
|
||||
}
|
||||
if( firstLoad) {
|
||||
//Update the id of the last notification retrieved
|
||||
if( MainActivity.lastNotificationId == null || Long.parseLong(notifications.get(0).getId()) > Long.parseLong(MainActivity.lastNotificationId))
|
||||
MainActivity.lastNotificationId = notifications.get(0).getId();
|
||||
updateNotificationLastId(notifications.get(0).getId());
|
||||
}
|
||||
notificationsListAdapter.notifyItemRangeInserted(previousPosition, notifications.size());
|
||||
}else {
|
||||
if( firstLoad)
|
||||
textviewNoAction.setVisibility(View.VISIBLE);
|
||||
}
|
||||
if( firstLoad )
|
||||
((MainActivity)context).updateNotifCounter();
|
||||
swipeRefreshLayout.setRefreshing(false);
|
||||
firstLoad = false;
|
||||
//The initial call comes from a classic tab refresh
|
||||
flag_loading = (max_id == null );
|
||||
}
|
||||
|
||||
/**
|
||||
* Called from main activity in onResume to retrieve missing notifications
|
||||
|
@ -360,7 +253,7 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements On
|
|||
flag_loading = true;
|
||||
swiped = true;
|
||||
MainActivity.countNewNotifications = 0;
|
||||
asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, DisplayPeertubeNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
asyncTask = new RetrievePeertubeNotificationsAsyncTask(context, null, null, DisplayPeertubeNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
|
||||
|
@ -430,4 +323,65 @@ public class DisplayPeertubeNotificationsFragment extends Fragment implements On
|
|||
editor.apply();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRetrievePeertubeNotifications(APIResponse apiResponse, Account account) {
|
||||
mainLoader.setVisibility(View.GONE);
|
||||
nextElementLoader.setVisibility(View.GONE);
|
||||
String lastReadNotifications = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId + instance, null);
|
||||
if( apiResponse.getError() != null){
|
||||
Toasty.error(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show();
|
||||
flag_loading = false;
|
||||
swipeRefreshLayout.setRefreshing(false);
|
||||
swiped = false;
|
||||
return;
|
||||
}
|
||||
|
||||
int previousPosition = notifications.size();
|
||||
max_id = apiResponse.getMax_id();
|
||||
List<Notification> notifications = apiResponse.getNotifications();
|
||||
|
||||
if( !swiped && firstLoad && (notifications == null || notifications.size() == 0))
|
||||
textviewNoAction.setVisibility(View.VISIBLE);
|
||||
else
|
||||
textviewNoAction.setVisibility(View.GONE);
|
||||
if( swiped ){
|
||||
if (previousPosition > 0) {
|
||||
for (int i = 0; i < previousPosition; i++) {
|
||||
this.notifications.remove(0);
|
||||
}
|
||||
notificationsListAdapter.notifyItemRangeRemoved(0, previousPosition);
|
||||
}
|
||||
swiped = false;
|
||||
}
|
||||
|
||||
if( notifications != null && notifications.size() > 0) {
|
||||
for(Notification tmpNotification: notifications){
|
||||
|
||||
if( lastReadNotifications != null && Long.parseLong(tmpNotification.getId()) > Long.parseLong(lastReadNotifications)) {
|
||||
MainActivity.countNewNotifications++;
|
||||
}
|
||||
try {
|
||||
((MainActivity) context).updateNotifCounter();
|
||||
}catch (Exception ignored){}
|
||||
this.notifications.add(tmpNotification);
|
||||
}
|
||||
if( firstLoad) {
|
||||
//Update the id of the last notification retrieved
|
||||
if( MainActivity.lastNotificationId == null || Long.parseLong(notifications.get(0).getId()) > Long.parseLong(MainActivity.lastNotificationId))
|
||||
MainActivity.lastNotificationId = notifications.get(0).getId();
|
||||
updateNotificationLastId(notifications.get(0).getId());
|
||||
}
|
||||
notificationsListAdapter.notifyItemRangeInserted(previousPosition, notifications.size());
|
||||
}else {
|
||||
if( firstLoad)
|
||||
textviewNoAction.setVisibility(View.VISIBLE);
|
||||
}
|
||||
if( firstLoad )
|
||||
((MainActivity)context).updateNotifCounter();
|
||||
swipeRefreshLayout.setRefreshing(false);
|
||||
firstLoad = false;
|
||||
//The initial call comes from a classic tab refresh
|
||||
flag_loading = (max_id == null );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
/* Copyright 2019 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.interfaces;
|
||||
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
|
||||
/**
|
||||
* Created by Thomas on 23/01/2019.
|
||||
* Interface when notifications have been retrieved for Peertube
|
||||
*/
|
||||
public interface OnRetrievePeertubeNotificationsInterface {
|
||||
void onRetrievePeertubeNotifications(APIResponse apiResponse, Account account);
|
||||
}
|
Loading…
Reference in New Issue