diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingFeedsAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingFeedsAsyncTask.java index 8ed288b4b..283ed56ce 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingFeedsAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingFeedsAsyncTask.java @@ -20,10 +20,12 @@ import android.os.AsyncTask; import java.lang.ref.WeakReference; 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; -import fr.gouv.etalab.mastodon.helper.Helper; +import fr.gouv.etalab.mastodon.client.Entities.Account; +import fr.gouv.etalab.mastodon.client.Entities.Conversation; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMissingFeedsInterface; @@ -62,7 +64,7 @@ public class RetrieveMissingFeedsAsyncTask extends AsyncTask { if( this.contextReference.get() == null) return null; API api = new API(this.contextReference.get()); - List tempStatus; + List tempStatus = null; APIResponse apiResponse = null; if( type == RetrieveFeedsAsyncTask.Type.HOME) apiResponse = api.getHomeTimeline(since_id); @@ -77,7 +79,24 @@ public class RetrieveMissingFeedsAsyncTask extends AsyncTask { else if (type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE) apiResponse = api.getInstanceTimelineSinceId(remoteInstance, since_id); if (apiResponse != null) { - tempStatus = apiResponse.getStatuses(); + if( type != RetrieveFeedsAsyncTask.Type.CONVERSATION) + tempStatus = apiResponse.getStatuses(); + else{ + List conversations = apiResponse.getConversations(); + tempStatus = new ArrayList<>(); + if( conversations != null && conversations.size() > 0){ + for(Conversation conversation: conversations){ + fr.gouv.etalab.mastodon.client.Entities.Status status = conversation.getLast_status(); + List ppConversation = new ArrayList<>(); + for (Account account : conversation.getAccounts()) + ppConversation.add(account.getAvatar()); + status.setConversationProfilePicture(ppConversation); + status.setConversationId(conversation.getId()); + tempStatus.add(status); + } + } + } + if( tempStatus != null) statuses.addAll(0, tempStatus); } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java index 9a55394f0..f49ddf6b3 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Status.java @@ -118,6 +118,7 @@ public class Status implements Parcelable{ private SpannableString contentSpan, displayNameSpan, contentSpanCW, contentSpanTranslated; private RetrieveFeedsAsyncTask.Type type; private int itemViewType; + private String conversationId; public Status(){} private List conversationProfilePicture; @@ -128,6 +129,7 @@ public class Status implements Parcelable{ uri = in.readString(); url = in.readString(); in_reply_to_id = in.readString(); + conversationId = in.readString(); in_reply_to_account_id = in.readString(); reblog = in.readParcelable(Status.class.getClassLoader()); card = in.readParcelable(Card.class.getClassLoader()); @@ -165,6 +167,7 @@ public class Status implements Parcelable{ dest.writeString(uri); dest.writeString(url); dest.writeString(in_reply_to_id); + dest.writeString(conversationId); dest.writeString(in_reply_to_account_id); dest.writeParcelable(reblog, flags); dest.writeParcelable(card, flags); @@ -1138,4 +1141,12 @@ public class Status implements Parcelable{ public void setItemViewType(int itemViewType) { this.itemViewType = itemViewType; } + + public String getConversationId() { + return conversationId; + } + + public void setConversationId(String conversationId) { + this.conversationId = conversationId; + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java index df74bfcd7..d3ad4655f 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java @@ -36,6 +36,7 @@ import android.widget.RelativeLayout; import android.widget.Toast; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import fr.gouv.etalab.mastodon.R; @@ -230,7 +231,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn if( type == RetrieveFeedsAsyncTask.Type.HOME) MainActivity.countNewStatus = 0; isSwipped = true; - retrieveMissingToots(null); + retrieveMissingToots(statuses.get(0).getId()); } } }); @@ -394,15 +395,15 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn if( type == RetrieveFeedsAsyncTask.Type.CONVERSATION ){ List conversations = apiResponse.getConversations(); List statusesConversations = new ArrayList<>(); + if( conversations != null) for( Conversation conversation: conversations) { Status status = conversation.getLast_status(); - if( status != null) { - List ppConversation = new ArrayList<>(); - for (Account account : conversation.getAccounts()) - ppConversation.add(account.getAvatar()); - status.setConversationProfilePicture(ppConversation); - statusesConversations.add(status); - } + status.setConversationId(conversation.getId()); + List ppConversation = new ArrayList<>(); + for (Account account : conversation.getAccounts()) + ppConversation.add(account.getAvatar()); + status.setConversationProfilePicture(ppConversation); + statusesConversations.add(status); } apiResponse.setStatuses(statusesConversations); } @@ -586,6 +587,11 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn statusListAdapter.updateMuted(mutedAccount); if( statuses != null && statuses.size() > 0) retrieveMissingToots(statuses.get(0).getId()); + }else if (type == RetrieveFeedsAsyncTask.Type.TAG){ + if( getUserVisibleHint() ){ + if( statuses != null && statuses.size() > 0) + retrieveMissingToots(statuses.get(0).getId()); + } } } @@ -707,6 +713,21 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn isSwipped = false; if( statuses != null && statuses.size() > 0) { int inserted = 0; + if(type == RetrieveFeedsAsyncTask.Type.CONVERSATION){ //Remove conversation already displayed if new messages + int position = 0; + if( this.statuses != null) { + for (Iterator it = this.statuses.iterator(); it.hasNext(); ) { + Status status = it.next(); + for (Status status1 : statuses) { + if (status.getConversationId() != null && status.getConversationId().equals(status1.getConversationId())) { + statusListAdapter.notifyItemRemoved(position); + it.remove(); + } + } + position++; + } + } + } for (int i = statuses.size() - 1; i >= 0; i--) { if( this.statuses != null) { if (this.statuses.size() == 0 ||