From 58661f54a484093596bd8a1a098e48bea0f619ac Mon Sep 17 00:00:00 2001 From: PhotonQyv Date: Wed, 13 Sep 2017 15:55:15 +0100 Subject: [PATCH] Closer, but still can't get a pinned toot to unpin. --- .../asynctasks/RetrieveFeedsAsyncTask.java | 6 ++- .../fr/gouv/etalab/mastodon/client/API.java | 33 +++++++------ .../mastodon/client/Entities/Status.java | 13 +++++ .../mastodon/drawers/StatusListAdapter.java | 47 +++++++++++++++++-- 4 files changed, 78 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveFeedsAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveFeedsAsyncTask.java index 99d93fd67..81bbf5f52 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveFeedsAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveFeedsAsyncTask.java @@ -50,7 +50,8 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { FAVOURITES, ONESTATUS, CONTEXT, - TAG + TAG, + PINS } public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface){ @@ -109,6 +110,9 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { case TAG: apiResponse = api.getPublicTimelineTag(tag, false, max_id); break; + case PINS: + apiResponse = api.getPinnedStatuses(targetedID); // Might need max_id later? + break; case HASHTAG: break; } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index 8b7e06636..6c6cae49b 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -68,6 +68,7 @@ public class API { private Attachment attachment; private List accounts; private List statuses; + private List pins; private List notifications; private int tootPerPage, accountPerPage, notificationPerPage; private int actionCode; @@ -309,26 +310,28 @@ public class API { */ public APIResponse getPinnedStatuses(String accountId) { + pins = new ArrayList<>(); + RequestParams params = new RequestParams(); params.put("pinned", Boolean.toString(true)); get(String.format("/accounts/%s/statuses", accountId), params, new JsonHttpResponseHandler() { - @Override - public void onSuccess(int statusCode, Header[] headers, JSONObject response) { - Status status = parseStatuses(context, response); - statuses.add(status); - } - @Override - public void onSuccess(int statusCode, Header[] headers, JSONArray response) { - statuses = parseStatuses(response); - } - @Override - public void onFailure(int statusCode, Header[] headers, Throwable error, JSONObject response){ - setError(statusCode, error); - } - }); - apiResponse.setStatuses(statuses); + @Override + public void onSuccess(int statusCode, Header[] headers, JSONObject response) { + Status status = parseStatuses(context, response); + pins.add(status); + } + @Override + public void onSuccess(int statusCode, Header[] headers, JSONArray response) { + pins = parseStatuses(response); + } + @Override + public void onFailure(int statusCode, Header[] headers, Throwable error, JSONObject response){ + setError(statusCode, error); + } + }); + apiResponse.setStatuses(pins); return apiResponse; } 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 1901c5ef7..1538bb999 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 @@ -42,6 +42,7 @@ public class Status implements Parcelable { private int favourites_count; private boolean reblogged; private boolean favourited; + private boolean pinned; private boolean sensitive; private String spoiler_text; private String visibility; @@ -51,6 +52,7 @@ public class Status implements Parcelable { private List replies; private List mentions; private List tags; + private List pins; private Application application; private String language; private boolean isTranslated = false; @@ -81,6 +83,9 @@ public class Status implements Parcelable { isTranslated = in.readByte() != 0; isTranslationShown = in.readByte() != 0; isNew = in.readByte() != 0; + + pinned = false; + pins = null; } public Status(){} @@ -201,6 +206,14 @@ public class Status implements Parcelable { this.favourited = favourited; } + public void setPinned(boolean pinned) { this.pinned = pinned; } + + public boolean isPinned() { return pinned; } + + public List getPins() { return pins; } + + public void setPins(List pins) { this.pins = pins; } + public boolean isSensitive() { return sensitive; } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java index 42024e01a..56ca9e263 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java @@ -78,12 +78,14 @@ import fr.gouv.etalab.mastodon.activities.TootActivity; import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask; import fr.gouv.etalab.mastodon.client.API; +import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.Entities.Attachment; import fr.gouv.etalab.mastodon.client.Entities.Error; import fr.gouv.etalab.mastodon.client.Entities.Status; import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface; +import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface; import fr.gouv.etalab.mastodon.interfaces.OnTranslatedInterface; import fr.gouv.etalab.mastodon.translation.GoogleTranslateQuery; import fr.gouv.etalab.mastodon.translation.YandexQuery; @@ -97,7 +99,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor; * Created by Thomas on 24/04/2017. * Adapter for Status */ -public class StatusListAdapter extends BaseAdapter implements OnPostActionInterface, OnTranslatedInterface { +public class StatusListAdapter extends BaseAdapter implements OnPostActionInterface, OnTranslatedInterface, OnRetrieveFeedsInterface { private Context context; private List statuses; @@ -116,6 +118,8 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf private HashMap urlConversion; private HashMap tagConversion; + private List pins; + public StatusListAdapter(Context context, RetrieveFeedsAsyncTask.Type type, String targetedId, boolean isOnWifi, int behaviorWithAttachments, int translator, List statuses){ this.context = context; this.statuses = statuses; @@ -126,6 +130,8 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf this.type = type; this.targetedId = targetedId; this.translator = translator; + + pins = new ArrayList<>(); } @@ -220,6 +226,11 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + + new RetrieveFeedsAsyncTask(context, RetrieveFeedsAsyncTask.Type.PINS, userId,null, false, + StatusListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + //Display a preview for accounts that have replied *if enabled and only for home timeline* if( type == RetrieveFeedsAsyncTask.Type.HOME ) { boolean showPreview = sharedpreferences.getBoolean(Helper.SET_PREVIEW_REPLIES, false); @@ -725,10 +736,10 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf holder.status_spoiler_button.setTextColor(ContextCompat.getColor(context, R.color.white)); } - String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); final boolean isOwner = status.getAccount().getId().equals(userId); if (isOwner) { + imgPinToot = ContextCompat.getDrawable(context, R.drawable.ic_action_pin); imgPinToot.setBounds(0,0,(int) (20 * iconSizePercent/100 * scale + 0.5f),(int) (20 * iconSizePercent/100 * scale + 0.5f)); @@ -893,9 +904,22 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf * @param status Status */ private void pinAction(Status status) { - // Checks for if status is already pinned & owned (Though maybe we can do this prior to getting here? - new PostActionAsyncTask(context, API.StatusAction.PIN, status.getId(), StatusListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + + for (Status pin : pins) { + if (status.getId().equals(pin.getId())) + status.setPinned(true); + } + + if (status.isPinned()) { + new PostActionAsyncTask(context, API.StatusAction.UNPIN, status.getId(), StatusListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + status.setPinned(false); + } else { + new PostActionAsyncTask(context, API.StatusAction.PIN, status.getId(), StatusListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + status.setPinned(true); + } + statusListAdapter.notifyDataSetChanged(); + } private void loadAttachments(final Status status, ViewHolder holder){ @@ -981,6 +1005,19 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf holder.status_show_more.setVisibility(View.GONE); } + @Override + public void onRetrieveFeeds(APIResponse apiResponse, boolean refreshData) { + if( apiResponse.getError() != null){ + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); + boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true); + if( show_error_messages) + Toast.makeText(context, apiResponse.getError().getError(),Toast.LENGTH_LONG).show(); + return; + } + + pins = apiResponse.getStatuses(); + } + @Override public void onPostAction(int statusCode, API.StatusAction statusAction, String targetedId, Error error) { @@ -988,7 +1025,7 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); boolean show_error_messages = sharedpreferences.getBoolean(Helper.SET_SHOW_ERROR_MESSAGES, true); if( show_error_messages) - Toast.makeText(context, error.getError(),Toast.LENGTH_LONG).show(); + Toast.makeText(context, "Here: " + error.getError(),Toast.LENGTH_LONG).show(); return; } Helper.manageMessageStatusCode(context, statusCode, statusAction);