Use retrofit methods for profile actions

This commit is contained in:
Eugen Rochko 2017-03-09 01:01:45 +01:00
parent 8035fba22c
commit f938dff9ed
2 changed files with 43 additions and 106 deletions

View File

@ -39,22 +39,13 @@ import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.keylesspalace.tusky.entity.Account; import com.keylesspalace.tusky.entity.Account;
import com.keylesspalace.tusky.entity.Relationship;
import com.pkmmte.view.CircularImageView; import com.pkmmte.view.CircularImageView;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import org.json.JSONArray; import java.util.ArrayList;
import org.json.JSONException; import java.util.List;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
@ -247,40 +238,20 @@ public class AccountActivity extends BaseActivity {
} }
private void obtainRelationships() { private void obtainRelationships() {
String endpoint = getString(R.string.endpoint_relationships); List<String> ids = new ArrayList<>(1);
String url = String.format("https://%s%s?id=%s", domain, endpoint, accountId); ids.add(accountId);
JsonArrayRequest request = new JsonArrayRequest(url, mastodonAPI.relationships(ids).enqueue(new Callback<List<Relationship>>() {
new Response.Listener<JSONArray>() {
@Override @Override
public void onResponse(JSONArray response) { public void onResponse(Call<List<Relationship>> call, retrofit2.Response<List<Relationship>> response) {
boolean following; Relationship relationship = response.body().get(0);
boolean blocking; onObtainRelationshipsSuccess(relationship.following, relationship.blocking);
try {
JSONObject object = response.getJSONObject(0);
following = object.getBoolean("following");
blocking = object.getBoolean("blocking");
} catch (JSONException e) {
onObtainRelationshipsFailure(e);
return;
} }
onObtainRelationshipsSuccess(following, blocking);
}
},
new Response.ErrorListener() {
@Override @Override
public void onErrorResponse(VolleyError error) { public void onFailure(Call<List<Relationship>> call, Throwable t) {
onObtainRelationshipsFailure(error); onObtainRelationshipsFailure((Exception) t);
} }
}) { });
@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(this).addToRequestQueue(request);
} }
private void onObtainRelationshipsSuccess(boolean following, boolean blocking) { private void onObtainRelationshipsSuccess(boolean following, boolean blocking) {
@ -347,50 +318,26 @@ public class AccountActivity extends BaseActivity {
return super.onPrepareOptionsMenu(menu); return super.onPrepareOptionsMenu(menu);
} }
private void postRequest(String endpoint, Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener) {
String url = "https://" + domain + endpoint;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null, listener,
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(this).addToRequestQueue(request);
}
private void follow(final String id) { private void follow(final String id) {
int endpointId; Callback<Relationship> cb = new Callback<Relationship>() {
if (following) {
endpointId = R.string.endpoint_unfollow;
} else {
endpointId = R.string.endpoint_follow;
}
postRequest(String.format(getString(endpointId), id),
new Response.Listener<JSONObject>() {
@Override @Override
public void onResponse(JSONObject response) { public void onResponse(Call<Relationship> call, retrofit2.Response<Relationship> response) {
boolean followingValue; following = response.body().following;
try { // TODO: display message/indicator when "requested" is true (i.e. when the follow is awaiting approval)
followingValue = response.getBoolean("following");
} catch (JSONException e) {
onFollowFailure(id);
return;
}
following = followingValue;
updateButtons(); updateButtons();
} }
},
new Response.ErrorListener() {
@Override @Override
public void onErrorResponse(VolleyError error) { public void onFailure(Call<Relationship> call, Throwable t) {
onFollowFailure(id); onFollowFailure(id);
} }
}); };
if (following) {
mastodonAPI.unfollowAccount(id).enqueue(cb);
} else {
mastodonAPI.followAccount(id).enqueue(cb);
}
} }
private void onFollowFailure(final String id) { private void onFollowFailure(final String id) {
@ -412,33 +359,23 @@ public class AccountActivity extends BaseActivity {
} }
private void block(final String id) { private void block(final String id) {
int endpointId; Callback<Relationship> cb = new Callback<Relationship>() {
if (blocking) {
endpointId = R.string.endpoint_unblock;
} else {
endpointId = R.string.endpoint_block;
}
postRequest(String.format(getString(endpointId), id),
new Response.Listener<JSONObject>() {
@Override @Override
public void onResponse(JSONObject response) { public void onResponse(Call<Relationship> call, retrofit2.Response<Relationship> response) {
boolean blockingValue; blocking = response.body().blocking;
try {
blockingValue = response.getBoolean("blocking");
} catch (JSONException e) {
onBlockFailure(id);
return;
}
blocking = blockingValue;
updateButtons(); updateButtons();
} }
},
new Response.ErrorListener() {
@Override @Override
public void onErrorResponse(VolleyError error) { public void onFailure(Call<Relationship> call, Throwable t) {
onBlockFailure(id); onBlockFailure(id);
} }
}); };
if (blocking) {
mastodonAPI.unblockAccount(id).enqueue(cb);
} else {
mastodonAPI.blockAccount(id).enqueue(cb);
}
} }
private void onBlockFailure(final String id) { private void onBlockFailure(final String id) {

View File

@ -133,7 +133,7 @@ public interface MastodonAPI {
Call<Relationship> unmuteAccount(@Path("id") String accountId); Call<Relationship> unmuteAccount(@Path("id") String accountId);
@GET("api/v1/accounts/relationships") @GET("api/v1/accounts/relationships")
Call<List<Relationship>> relationships(@Query("id[]") List<Integer> accountIds); Call<List<Relationship>> relationships(@Query("id[]") List<String> accountIds);
@GET("api/v1/blocks") @GET("api/v1/blocks")
Call<List<Account>> blocks( Call<List<Account>> blocks(