Viewing imgur albums and images is now available.

This commit is contained in:
Alex Ning 2020-06-04 10:30:49 +08:00
parent 4927f82786
commit d9de2938b4
2 changed files with 117 additions and 2 deletions

View File

@ -10,9 +10,9 @@ public interface ImgurAPI {
@GET("gallery/{id}")
Call<String> getGalleryImages(@Header(APIUtils.AUTHORIZATION_KEY) String clientId, @Path("id") String id);
@GET("/album/{id}")
@GET("album/{id}")
Call<String> getAlbumImages(@Header(APIUtils.AUTHORIZATION_KEY) String clientId, @Path("id") String id);
@GET("/image/{id}")
@GET("image/{id}")
Call<String> getImage(@Header(APIUtils.AUTHORIZATION_KEY) String clientId, @Path("id") String id);
}

View File

@ -160,8 +160,73 @@ public class ViewImgurMediaActivity extends AppCompatActivity {
});
break;
case IMGUR_TYPE_ALBUM:
imgurRetrofit.create(ImgurAPI.class).getAlbumImages(APIUtils.IMGUR_CLIENT_ID, imgurId)
.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
new ParseImgurImagesAsyncTask(response.body(), new ParseImgurImagesAsyncTask.ParseImgurImagesAsyncTaskListener() {
@Override
public void success(ArrayList<ImgurMedia> images) {
ViewImgurMediaActivity.this.images = images;
progressBar.setVisibility(View.GONE);
errorLinearLayout.setVisibility(View.GONE);
setupViewPager();
}
@Override
public void failed() {
progressBar.setVisibility(View.GONE);
errorLinearLayout.setVisibility(View.VISIBLE);
}
}).execute();
} else {
progressBar.setVisibility(View.GONE);
errorLinearLayout.setVisibility(View.VISIBLE);
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
progressBar.setVisibility(View.GONE);
errorLinearLayout.setVisibility(View.VISIBLE);
}
});
break;
case IMGUR_TYPE_IMAGE:
imgurRetrofit.create(ImgurAPI.class).getImage(APIUtils.IMGUR_CLIENT_ID, imgurId)
.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
new ParseImgurImageAsyncTask(response.body(), new ParseImgurImageAsyncTask.ParseImgurImageAsyncTaskListener() {
@Override
public void success(ImgurMedia image) {
ViewImgurMediaActivity.this.images = new ArrayList<>();
ViewImgurMediaActivity.this.images.add(image);
progressBar.setVisibility(View.GONE);
errorLinearLayout.setVisibility(View.GONE);
setupViewPager();
}
@Override
public void failed() {
progressBar.setVisibility(View.GONE);
errorLinearLayout.setVisibility(View.VISIBLE);
}
}).execute();
} else {
progressBar.setVisibility(View.GONE);
errorLinearLayout.setVisibility(View.VISIBLE);
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
progressBar.setVisibility(View.GONE);
errorLinearLayout.setVisibility(View.VISIBLE);
}
});
break;
}
}
@ -301,4 +366,54 @@ public class ViewImgurMediaActivity extends AppCompatActivity {
}
}
}
private static class ParseImgurImageAsyncTask extends AsyncTask<Void, Void, Void> {
private String response;
private ImgurMedia image;
private boolean parseFailed = false;
private ParseImgurImageAsyncTaskListener parseImgurImageAsyncTaskListener;
interface ParseImgurImageAsyncTaskListener {
void success(ImgurMedia image);
void failed();
}
ParseImgurImageAsyncTask(String response, ParseImgurImageAsyncTaskListener parseImgurImageAsyncTaskListener) {
this.response = response;
this.parseImgurImageAsyncTaskListener = parseImgurImageAsyncTaskListener;
}
@Override
protected Void doInBackground(Void... voids) {
try {
JSONObject image = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY);
String type = image.getString(JSONUtils.TYPE_KEY);
if (type.contains("gif")) {
this.image = new ImgurMedia(image.getString(JSONUtils.ID_KEY),
image.getString(JSONUtils.TITLE_KEY), image.getString(JSONUtils.DESCRIPTION_KEY),
"video/mp4", image.getString(JSONUtils.MP4_KEY));
} else {
this.image = new ImgurMedia(image.getString(JSONUtils.ID_KEY),
image.getString(JSONUtils.TITLE_KEY), image.getString(JSONUtils.DESCRIPTION_KEY),
type, image.getString(JSONUtils.LINK_KEY));
}
} catch (JSONException e) {
e.printStackTrace();
parseFailed = true;
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (parseFailed) {
parseImgurImageAsyncTaskListener.failed();
} else {
parseImgurImageAsyncTaskListener.success(image);
}
}
}
}