From 8035fba22c601f84e65d8ae843d2042b8aac7390 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 9 Mar 2017 00:46:13 +0100 Subject: [PATCH] Use retrofit methods for status actions --- .../com/keylesspalace/tusky/SFragment.java | 102 +++++++----------- 1 file changed, 39 insertions(+), 63 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/SFragment.java b/app/src/main/java/com/keylesspalace/tusky/SFragment.java index 58e23ce4..7d4feda3 100644 --- a/app/src/main/java/com/keylesspalace/tusky/SFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/SFragment.java @@ -42,6 +42,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import retrofit2.Call; +import retrofit2.Callback; + /* Note from Andrew on Jan. 22, 2017: This class is a design problem for me, so I left it with an * awkward name. TimelineFragment and NotificationFragment have significant overlap but the nature * of that is complicated by how they're coupled with Status and Notification and the corresponding @@ -55,6 +58,7 @@ public class SFragment extends Fragment { protected String accessToken; protected String loggedInAccountId; protected String loggedInUsername; + private MastodonAPI api; @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -66,6 +70,7 @@ public class SFragment extends Fragment { accessToken = preferences.getString("accessToken", null); loggedInAccountId = preferences.getString("loggedInAccountId", null); loggedInUsername = preferences.getString("loggedInAccountUsername", null); + api = ((BaseActivity) getActivity()).mastodonAPI; } @Override @@ -74,43 +79,6 @@ public class SFragment extends Fragment { super.onDestroy(); } - protected void sendRequest( - int method, String endpoint, JSONObject parameters, - @Nullable Response.Listener responseListener, - @Nullable Response.ErrorListener errorListener) { - if (responseListener == null) { - // Use a dummy listener if one wasn't specified so the request can be constructed. - responseListener = new Response.Listener() { - @Override - public void onResponse(JSONObject response) {} - }; - } - if (errorListener == null) { - errorListener = new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - Log.e(TAG, "Request Failed: " + error.getMessage()); - } - }; - } - String url = "https://" + domain + endpoint; - JsonObjectRequest request = new JsonObjectRequest( - method, url, parameters, responseListener, errorListener) { - @Override - public Map getHeaders() throws AuthFailureError { - Map headers = new HashMap<>(); - headers.put("Authorization", "Bearer " + accessToken); - return headers; - } - }; - request.setTag(TAG); - VolleySingleton.getInstance(getContext()).addToRequestQueue(request); - } - - protected void postRequest(String endpoint) { - sendRequest(Request.Method.POST, endpoint, null, null, null); - } - protected void reply(Status status) { String inReplyToId = status.getActionableId(); Status.Mention[] mentions = status.mentions; @@ -130,53 +98,61 @@ public class SFragment extends Fragment { protected void reblog(final Status status, final boolean reblog, final RecyclerView.Adapter adapter, final int position) { String id = status.getActionableId(); - String endpoint; + + Callback cb = new Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + status.reblogged = reblog; + adapter.notifyItemChanged(position); + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }; + if (reblog) { - endpoint = String.format(getString(R.string.endpoint_reblog), id); + api.reblogStatus(id).enqueue(cb); } else { - endpoint = String.format(getString(R.string.endpoint_unreblog), id); + api.unreblogStatus(id).enqueue(cb); } - sendRequest(Request.Method.POST, endpoint, null, - new Response.Listener() { - @Override - public void onResponse(JSONObject response) { - status.reblogged = reblog; - adapter.notifyItemChanged(position); - } - }, null); } protected void favourite(final Status status, final boolean favourite, final RecyclerView.Adapter adapter, final int position) { String id = status.getActionableId(); - String endpoint; - if (favourite) { - endpoint = String.format(getString(R.string.endpoint_favourite), id); - } else { - endpoint = String.format(getString(R.string.endpoint_unfavourite), id); - } - sendRequest(Request.Method.POST, endpoint, null, new Response.Listener() { + + Callback cb = new Callback() { @Override - public void onResponse(JSONObject response) { + public void onResponse(Call call, retrofit2.Response response) { status.favourited = favourite; adapter.notifyItemChanged(position); } - }, null); + + @Override + public void onFailure(Call call, Throwable t) { + + } + }; + + if (favourite) { + api.favouriteStatus(id).enqueue(cb); + } else { + api.unfavouriteStatus(id).enqueue(cb); + } } protected void follow(String id) { - String endpoint = String.format(getString(R.string.endpoint_follow), id); - postRequest(endpoint); + api.followAccount(id).enqueue(null); } private void block(String id) { - String endpoint = String.format(getString(R.string.endpoint_block), id); - postRequest(endpoint); + api.blockAccount(id).enqueue(null); } private void delete(String id) { - String endpoint = String.format(getString(R.string.endpoint_delete), id); - sendRequest(Request.Method.DELETE, endpoint, null, null, null); + api.deleteStatus(id).enqueue(null); } protected void more(Status status, View view, final AdapterItemRemover adapter,