Use retrofit methods for status actions
This commit is contained in:
parent
750c1c80a0
commit
8035fba22c
1 changed files with 39 additions and 63 deletions
|
@ -42,6 +42,9 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
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
|
/* 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
|
* 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
|
* 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 accessToken;
|
||||||
protected String loggedInAccountId;
|
protected String loggedInAccountId;
|
||||||
protected String loggedInUsername;
|
protected String loggedInUsername;
|
||||||
|
private MastodonAPI api;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
|
@ -66,6 +70,7 @@ public class SFragment extends Fragment {
|
||||||
accessToken = preferences.getString("accessToken", null);
|
accessToken = preferences.getString("accessToken", null);
|
||||||
loggedInAccountId = preferences.getString("loggedInAccountId", null);
|
loggedInAccountId = preferences.getString("loggedInAccountId", null);
|
||||||
loggedInUsername = preferences.getString("loggedInAccountUsername", null);
|
loggedInUsername = preferences.getString("loggedInAccountUsername", null);
|
||||||
|
api = ((BaseActivity) getActivity()).mastodonAPI;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -74,43 +79,6 @@ public class SFragment extends Fragment {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void sendRequest(
|
|
||||||
int method, String endpoint, JSONObject parameters,
|
|
||||||
@Nullable Response.Listener<JSONObject> 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<JSONObject>() {
|
|
||||||
@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<String, String> getHeaders() throws AuthFailureError {
|
|
||||||
Map<String, String> 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) {
|
protected void reply(Status status) {
|
||||||
String inReplyToId = status.getActionableId();
|
String inReplyToId = status.getActionableId();
|
||||||
Status.Mention[] mentions = status.mentions;
|
Status.Mention[] mentions = status.mentions;
|
||||||
|
@ -130,53 +98,61 @@ public class SFragment extends Fragment {
|
||||||
protected void reblog(final Status status, final boolean reblog,
|
protected void reblog(final Status status, final boolean reblog,
|
||||||
final RecyclerView.Adapter adapter, final int position) {
|
final RecyclerView.Adapter adapter, final int position) {
|
||||||
String id = status.getActionableId();
|
String id = status.getActionableId();
|
||||||
String endpoint;
|
|
||||||
if (reblog) {
|
Callback<Status> cb = new Callback<Status>() {
|
||||||
endpoint = String.format(getString(R.string.endpoint_reblog), id);
|
|
||||||
} else {
|
|
||||||
endpoint = String.format(getString(R.string.endpoint_unreblog), id);
|
|
||||||
}
|
|
||||||
sendRequest(Request.Method.POST, endpoint, null,
|
|
||||||
new Response.Listener<JSONObject>() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(JSONObject response) {
|
public void onResponse(Call<Status> call, retrofit2.Response<Status> response) {
|
||||||
status.reblogged = reblog;
|
status.reblogged = reblog;
|
||||||
adapter.notifyItemChanged(position);
|
adapter.notifyItemChanged(position);
|
||||||
}
|
}
|
||||||
}, null);
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call<Status> call, Throwable t) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (reblog) {
|
||||||
|
api.reblogStatus(id).enqueue(cb);
|
||||||
|
} else {
|
||||||
|
api.unreblogStatus(id).enqueue(cb);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void favourite(final Status status, final boolean favourite,
|
protected void favourite(final Status status, final boolean favourite,
|
||||||
final RecyclerView.Adapter adapter, final int position) {
|
final RecyclerView.Adapter adapter, final int position) {
|
||||||
String id = status.getActionableId();
|
String id = status.getActionableId();
|
||||||
String endpoint;
|
|
||||||
if (favourite) {
|
Callback<Status> cb = new Callback<Status>() {
|
||||||
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<JSONObject>() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(JSONObject response) {
|
public void onResponse(Call<Status> call, retrofit2.Response<Status> response) {
|
||||||
status.favourited = favourite;
|
status.favourited = favourite;
|
||||||
adapter.notifyItemChanged(position);
|
adapter.notifyItemChanged(position);
|
||||||
}
|
}
|
||||||
}, null);
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Call<Status> call, Throwable t) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (favourite) {
|
||||||
|
api.favouriteStatus(id).enqueue(cb);
|
||||||
|
} else {
|
||||||
|
api.unfavouriteStatus(id).enqueue(cb);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void follow(String id) {
|
protected void follow(String id) {
|
||||||
String endpoint = String.format(getString(R.string.endpoint_follow), id);
|
api.followAccount(id).enqueue(null);
|
||||||
postRequest(endpoint);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void block(String id) {
|
private void block(String id) {
|
||||||
String endpoint = String.format(getString(R.string.endpoint_block), id);
|
api.blockAccount(id).enqueue(null);
|
||||||
postRequest(endpoint);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void delete(String id) {
|
private void delete(String id) {
|
||||||
String endpoint = String.format(getString(R.string.endpoint_delete), id);
|
api.deleteStatus(id).enqueue(null);
|
||||||
sendRequest(Request.Method.DELETE, endpoint, null, null, null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void more(Status status, View view, final AdapterItemRemover adapter,
|
protected void more(Status status, View view, final AdapterItemRemover adapter,
|
||||||
|
|
Loading…
Reference in a new issue