From d9de2938b495b4972ba706bfc383ee2800073e45 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 4 Jun 2020 10:30:49 +0800 Subject: Viewing imgur albums and images is now available. --- .../infinityforreddit/API/ImgurAPI.java | 4 +- .../Activity/ViewImgurMediaActivity.java | 115 +++++++++++++++++++++ 2 files changed, 117 insertions(+), 2 deletions(-) (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/API/ImgurAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/API/ImgurAPI.java index b6534ea5..66058a36 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/API/ImgurAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/API/ImgurAPI.java @@ -10,9 +10,9 @@ public interface ImgurAPI { @GET("gallery/{id}") Call getGalleryImages(@Header(APIUtils.AUTHORIZATION_KEY) String clientId, @Path("id") String id); - @GET("/album/{id}") + @GET("album/{id}") Call getAlbumImages(@Header(APIUtils.AUTHORIZATION_KEY) String clientId, @Path("id") String id); - @GET("/image/{id}") + @GET("image/{id}") Call getImage(@Header(APIUtils.AUTHORIZATION_KEY) String clientId, @Path("id") String id); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java index e04d6048..e9040328 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java @@ -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() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + new ParseImgurImagesAsyncTask(response.body(), new ParseImgurImagesAsyncTask.ParseImgurImagesAsyncTaskListener() { + @Override + public void success(ArrayList 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 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() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response 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 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 { + + 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); + } + } + } } \ No newline at end of file -- cgit v1.2.3