Notify only items instead of the whole list for recyclerview

This commit is contained in:
stom79 2018-01-10 17:01:00 +01:00
parent e8f73d3ed3
commit 8485e42c92
4 changed files with 65 additions and 44 deletions

View File

@ -21,7 +21,6 @@ import java.lang.ref.WeakReference;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Notification;
import fr.gouv.etalab.mastodon.interfaces.OnPostNotificationsActionInterface;
@ -34,19 +33,19 @@ public class PostNotificationsAsyncTask extends AsyncTask<Void, Void, Void> {
private OnPostNotificationsActionInterface listener;
private APIResponse apiResponse;
private Notification notification;
private String targetedId;
private WeakReference<Context> contextReference;
public PostNotificationsAsyncTask(Context context, Notification notification, OnPostNotificationsActionInterface onPostNotificationsActionInterface){
public PostNotificationsAsyncTask(Context context, String targetedId, OnPostNotificationsActionInterface onPostNotificationsActionInterface){
this.contextReference = new WeakReference<>(context);
this.listener = onPostNotificationsActionInterface;
this.notification = notification;
this.targetedId = targetedId;
}
@Override
protected Void doInBackground(Void... params) {
if( notification != null)
apiResponse = new API(this.contextReference.get()).postNoticationAction(notification.getId());
if( targetedId != null)
apiResponse = new API(this.contextReference.get()).postNoticationAction(targetedId);
else //Delete all notifications
apiResponse = new API(this.contextReference.get()).postNoticationAction(null);
return null;
@ -54,7 +53,7 @@ public class PostNotificationsAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
listener.onPostNotificationsAction(apiResponse, notification);
listener.onPostNotificationsAction(apiResponse, targetedId);
}
}

View File

@ -665,14 +665,14 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On
return true;
case R.id.action_mention:
status.setTakingScreenShot(true);
notificationsListAdapter.notifyDataSetChanged();
notifyNotificationChanged(notification);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Bitmap bitmap = Helper.convertTootIntoBitmap(context, holder.getView());
status.setTakingScreenShot(false);
notificationsListAdapter.notifyDataSetChanged();
notifyNotificationChanged(notification);
Intent intent = new Intent(context, TootActivity.class);
Bundle b = new Bundle();
String fname = "tootmention_" + status.getId() +".jpg";
@ -806,7 +806,7 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On
if (seletedItems.size() > 0)
new PostNotificationsAsyncTask(context, null, NotificationsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
new PostNotificationsAsyncTask(context, notification, NotificationsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
new PostNotificationsAsyncTask(context, notification.getId(), NotificationsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
dialog.dismiss();
}
}).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@ -840,52 +840,67 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On
}
if( targetedId != null ) {
if (statusAction == API.StatusAction.REBLOG) {
int position = 0;
for (Notification notification : notifications) {
if (notification.getStatus() != null && notification.getStatus().getId().equals(targetedId)) {
notification.getStatus().setReblogs_count(notification.getStatus().getReblogs_count() + 1);
notificationsListAdapter.notifyItemChanged(position);
break;
}
}
notificationsListAdapter.notifyDataSetChanged();
} else if (statusAction == API.StatusAction.UNREBLOG) {
int position = 0;
for (Notification notification : notifications) {
if (notification.getStatus() != null && notification.getStatus().getId().equals(targetedId)) {
if (notification.getStatus().getReblogs_count() - 1 >= 0)
notification.getStatus().setReblogs_count(notification.getStatus().getReblogs_count() - 1);
notificationsListAdapter.notifyItemChanged(position);
break;
}
position++;
}
notificationsListAdapter.notifyDataSetChanged();
} else if (statusAction == API.StatusAction.FAVOURITE) {
int position = 0;
for (Notification notification : notifications) {
if (notification.getStatus() != null && notification.getStatus().getId().equals(targetedId)) {
notification.getStatus().setFavourites_count(notification.getStatus().getFavourites_count() + 1);
notificationsListAdapter.notifyItemChanged(position);
break;
}
position++;
}
notificationsListAdapter.notifyDataSetChanged();
} else if (statusAction == API.StatusAction.UNFAVOURITE) {
int position = 0;
for (Notification notification : notifications) {
if (notification.getStatus() != null && notification.getStatus().getId().equals(targetedId)) {
if (notification.getStatus().getFavourites_count() - 1 >= 0)
notification.getStatus().setFavourites_count(notification.getStatus().getFavourites_count() - 1);
notificationsListAdapter.notifyItemChanged(position);
break;
}
position++;
}
notificationsListAdapter.notifyDataSetChanged();
}
}
}
@Override
public void onPostNotificationsAction(APIResponse apiResponse, Notification notification) {
public void onPostNotificationsAction(APIResponse apiResponse, String targetedId) {
if(apiResponse.getError() != null){
Toast.makeText(context, R.string.toast_error,Toast.LENGTH_LONG).show();
return;
}
if( notification != null){
notifications.remove(notification);
notificationsListAdapter.notifyDataSetChanged();
if( targetedId != null){
int position = 0;
for (Notification notif : notifications) {
if (notif.getId().equals(targetedId)) {
notifications.remove(notif);
notificationsListAdapter.notifyItemRemoved(position);
break;
}
position++;
}
Toast.makeText(context,R.string.delete_notification,Toast.LENGTH_LONG).show();
}else{
int size = notifications.size();

View File

@ -1308,7 +1308,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
return true;
case R.id.action_mention:
status.setTakingScreenShot(true);
statusListAdapter.notifyDataSetChanged();
notifyStatusChanged(status);
// Get a handler that can be used to post to the main thread
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@ -1316,7 +1316,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
public void run() {
Bitmap bitmap = Helper.convertTootIntoBitmap(context, holder.getView());
status.setTakingScreenShot(false);
statusListAdapter.notifyDataSetChanged();
notifyStatusChanged(status);
Intent intent = new Intent(context, TootActivity.class);
Bundle b = new Bundle();
String fname = "tootmention_" + status.getId() +".jpg";
@ -1557,66 +1557,74 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
statuses.removeAll(statusesToRemove);
statusListAdapter.notifyDataSetChanged();
}else if( statusAction == API.StatusAction.UNSTATUS ){
int position = 0;
for(Status status: statuses){
if( status.getId().equals(targetedId))
statusesToRemove.add(status);
}
statuses.removeAll(statusesToRemove);
statusListAdapter.notifyDataSetChanged();
}
else if ( statusAction == API.StatusAction.PIN || statusAction == API.StatusAction.UNPIN ) {
Status toCheck = null;
for (Status checkPin: statuses) {
if (checkPin.getId().equals(targetedId)) {
toCheck = checkPin;
if( status.getId().equals(targetedId)) {
statuses.remove(status);
statusListAdapter.notifyItemRemoved(position);
break;
}
position++;
}
if (statusAction == API.StatusAction.PIN) {
if (toCheck != null)
toCheck.setPinned(true);
}
else if ( statusAction == API.StatusAction.PIN || statusAction == API.StatusAction.UNPIN ) {
int position = 0;
for (Status status: statuses) {
if (status.getId().equals(targetedId)) {
if (statusAction == API.StatusAction.PIN)
status.setPinned(true);
else
status.setPinned(false);
statusListAdapter.notifyItemChanged(position);
break;
}
position++;
}
else {
if (toCheck != null)
toCheck.setPinned(false);
}
statusListAdapter.notifyDataSetChanged();
}
if( statusAction == API.StatusAction.REBLOG){
int position = 0;
for(Status status: statuses){
if( status.getId().equals(targetedId)) {
status.setReblogs_count(status.getReblogs_count() + 1);
statusListAdapter.notifyItemChanged(position);
break;
}
position++;
}
statusListAdapter.notifyDataSetChanged();
}else if( statusAction == API.StatusAction.UNREBLOG){
int position = 0;
for(Status status: statuses){
if( status.getId().equals(targetedId)) {
if( status.getReblogs_count() - 1 >= 0)
status.setReblogs_count(status.getReblogs_count() - 1);
statusListAdapter.notifyItemChanged(position);
break;
}
position++;
}
statusListAdapter.notifyDataSetChanged();
}else if( statusAction == API.StatusAction.FAVOURITE){
int position = 0;
for(Status status: statuses){
if( status.getId().equals(targetedId)) {
status.setFavourites_count(status.getFavourites_count() + 1);
statusListAdapter.notifyItemChanged(position);
break;
}
position++;
}
statusListAdapter.notifyDataSetChanged();
}else if( statusAction == API.StatusAction.UNFAVOURITE){
int position = 0;
for(Status status: statuses){
if( status.getId().equals(targetedId)) {
if( status.getFavourites_count() - 1 >= 0)
status.setFavourites_count(status.getFavourites_count() - 1);
statusListAdapter.notifyItemChanged(position);
break;
}
position++;
}
statusListAdapter.notifyDataSetChanged();
}
}

View File

@ -16,12 +16,11 @@ package fr.gouv.etalab.mastodon.interfaces;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Notification;
/**
* Created by Thomas on 29/07/2017.
* Interface when deleting a notification
*/
public interface OnPostNotificationsActionInterface {
void onPostNotificationsAction(APIResponse apiResponse, Notification notification);
void onPostNotificationsAction(APIResponse apiResponse, String targetedId);
}