Closer, but still can't get a pinned toot to unpin.

This commit is contained in:
PhotonQyv 2017-09-13 15:55:15 +01:00
parent 50fb1a1515
commit 58661f54a4
4 changed files with 78 additions and 21 deletions

View File

@ -50,7 +50,8 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
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<Void, Void, Void> {
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;
}

View File

@ -68,6 +68,7 @@ public class API {
private Attachment attachment;
private List<Account> accounts;
private List<Status> statuses;
private List<Status> pins;
private List<Notification> 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;
}

View File

@ -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<Status> replies;
private List<Mention> mentions;
private List<Tag> tags;
private List<Status> 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<Status> getPins() { return pins; }
public void setPins(List<Status> pins) { this.pins = pins; }
public boolean isSensitive() {
return sensitive;
}

View File

@ -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<Status> statuses;
@ -116,6 +118,8 @@ public class StatusListAdapter extends BaseAdapter implements OnPostActionInterf
private HashMap<String, String> urlConversion;
private HashMap<String, String> tagConversion;
private List<Status> pins;
public StatusListAdapter(Context context, RetrieveFeedsAsyncTask.Type type, String targetedId, boolean isOnWifi, int behaviorWithAttachments, int translator, List<Status> 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);