From 057a940a2eb73fa11bb64c29e22f858a7d27626c Mon Sep 17 00:00:00 2001 From: PhotonQyv Date: Tue, 12 Sep 2017 16:29:43 +0100 Subject: [PATCH] Added code so that user can pin a toot. Can't currently unpin one, nor see existing pinned toots. (API calls are coded). Work in Progress. --- .../fr/gouv/etalab/mastodon/client/API.java | 41 ++++++++++++++++++- .../mastodon/drawers/StatusListAdapter.java | 9 ++-- .../gouv/etalab/mastodon/helper/Helper.java | 16 +++++--- app/src/main/res/values/strings.xml | 4 ++ 4 files changed, 60 insertions(+), 10 deletions(-) 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 b16de7a9a..8b7e06636 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 @@ -93,7 +93,9 @@ public class API { AUTHORIZE, REJECT, REPORT, - REMOTE_FOLLOW + REMOTE_FOLLOW, + PIN, + UNPIN } public API(Context context) { @@ -299,6 +301,37 @@ public class API { return getStatus(accountId, true, false, max_id, null, tootPerPage); } + /** + * Retrieves pinned status(es) *synchronously* + * + * @param accountId String Id of the account + * @return APIResponse + */ + public APIResponse getPinnedStatuses(String accountId) + { + 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); + return apiResponse; + } + /** * Retrieves status for the account *synchronously* @@ -835,6 +868,12 @@ public class API { case UNMUTE: action = String.format("/accounts/%s/unmute", targetedId); break; + case PIN: + action = String.format("/statuses/%s/pin", targetedId); + break; + case UNPIN: + action = String.format("statuses/%s/unpin", targetedId); + break; case UNSTATUS: action = String.format("/statuses/%s", targetedId); break; 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 57fb935bb..87b6cad15 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 @@ -91,7 +91,6 @@ import mastodon.etalab.gouv.fr.mastodon.R; import static fr.gouv.etalab.mastodon.activities.MainActivity.currentLocale; import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor; -import static fr.gouv.etalab.mastodon.helper.Helper.shortnameToUnicode; /** @@ -465,7 +464,7 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf //Redraws top icons (boost/reply) final float scale = context.getResources().getDisplayMetrics().density; - if( !status.getIn_reply_to_account_id().equals("null") || !status.getIn_reply_to_id().equals("null") ){ + if( (status.getIn_reply_to_account_id()!= null && !status.getIn_reply_to_account_id().equals("null")) || (status.getIn_reply_to_id() != null && !status.getIn_reply_to_id().equals("null")) ){ Drawable img = ContextCompat.getDrawable(context, R.drawable.ic_reply); img.setBounds(0,0,(int) (20 * iconSizePercent/100 * scale + 0.5f),(int) (15 * iconSizePercent/100 * scale + 0.5f)); holder.status_account_displayname.setCompoundDrawables( img, null, null, null); @@ -1178,7 +1177,7 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf if( isOwner) { stringArray = context.getResources().getStringArray(R.array.more_action_owner); stringArrayConf = context.getResources().getStringArray(R.array.more_action_owner_confirm); - doAction = new API.StatusAction[]{API.StatusAction.UNSTATUS}; + doAction = new API.StatusAction[]{API.StatusAction.PIN,API.StatusAction.UNSTATUS}; }else { stringArray = context.getResources().getStringArray(R.array.more_action); @@ -1290,7 +1289,9 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf doAction[position] == API.StatusAction.UNFAVOURITE || doAction[position] == API.StatusAction.REBLOG || doAction[position] == API.StatusAction.UNREBLOG || - doAction[position] == API.StatusAction.UNSTATUS + doAction[position] == API.StatusAction.UNSTATUS || + doAction[position] == API.StatusAction.PIN || + doAction[position] == API.StatusAction.UNPIN ) targetedId = status.getId(); else diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index 7f684d329..561352662 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -525,11 +525,15 @@ public class Helper { message = context.getString(R.string.toast_favourite); }else if(statusAction == API.StatusAction.UNFAVOURITE){ message = context.getString(R.string.toast_unfavourite); + }else if(statusAction == API.StatusAction.PIN){ + message = context.getString(R.string.toast_pin); + }else if (statusAction == API.StatusAction.UNPIN){ + message = context.getString(R.string.toast_unpin); }else if(statusAction == API.StatusAction.REPORT){ message = context.getString(R.string.toast_report); }else if(statusAction == API.StatusAction.UNSTATUS){ message = context.getString(R.string.toast_unstatus); - } + } }else { message = context.getString(R.string.toast_error); } @@ -1571,7 +1575,7 @@ public class Helper { navigationView.getMenu().findItem(R.id.nav_local).setVisible(false); navigationView.getMenu().findItem(R.id.nav_global).setVisible(false); navigationView.getMenu().findItem(R.id.nav_notification).setVisible(false); - params.height = (int) Helper.convertDpToPixel(heightSearchdp, activity);; + params.height = (int) Helper.convertDpToPixel(heightSearchdp, activity); toolbar_search_container.setLayoutParams(params); tableLayout.setVisibility(View.VISIBLE); break; @@ -1589,7 +1593,7 @@ public class Helper { navigationView.getMenu().findItem(R.id.nav_local).setVisible(true); navigationView.getMenu().findItem(R.id.nav_global).setVisible(true); navigationView.getMenu().findItem(R.id.nav_notification).setVisible(true); - params.height = (int) Helper.convertDpToPixel(heightSearchdp, activity);; + params.height = (int) Helper.convertDpToPixel(heightSearchdp, activity); toolbar_search_container.setLayoutParams(params); tableLayout.setVisibility(View.VISIBLE); break; @@ -1665,7 +1669,8 @@ public class Helper { Gson gson = new Gson(); String json = sharedpreferences.getString(Helper.SET_TEMP_STATUS + userId, null); Type type = new TypeToken>() {}.getType(); - return gson.fromJson(json, type); + ArrayList statuses = gson.fromJson(json, type); + return (statuses != null)?statuses:new ArrayList(); } @@ -1713,7 +1718,8 @@ public class Helper { Gson gson = new Gson(); String json = sharedpreferences.getString(Helper.SET_TEMP_NOTIFICATIONS + userId, null); Type type = new TypeToken>() {}.getType(); - return gson.fromJson(json, type); + ArrayList notifications = gson.fromJson(json, type); + return (notifications != null)?notifications:new ArrayList(); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ede1b727d..0d14d6517 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -105,6 +105,7 @@ Share + Pin Remove Copy Share @@ -125,6 +126,7 @@ + Pin this toot? Remove this toot? null null @@ -261,6 +263,8 @@ The toot was removed from your favourites! The toot was reported! The toot was deleted! + The toot was pinned! + The toot was unpinned! Oops ! An error occurred! An error occurred! The instance did not return an authorisation code! The instance domain does not seem to be valide!