aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2020-06-04 02:30:49 +0000
committerAlex Ning <chineseperson5@gmail.com>2020-06-04 02:30:49 +0000
commitd9de2938b495b4972ba706bfc383ee2800073e45 (patch)
tree02d7ca2e30d4beb1f7a2f4dad1fe4b4fe7c845b7 /app/src/main/java/ml/docilealligator/infinityforreddit
parent4927f8278643e8deccede688a96183f139ce9691 (diff)
downloadinfinity-for-reddit-d9de2938b495b4972ba706bfc383ee2800073e45.tar
infinity-for-reddit-d9de2938b495b4972ba706bfc383ee2800073e45.tar.gz
infinity-for-reddit-d9de2938b495b4972ba706bfc383ee2800073e45.tar.bz2
infinity-for-reddit-d9de2938b495b4972ba706bfc383ee2800073e45.tar.lz
infinity-for-reddit-d9de2938b495b4972ba706bfc383ee2800073e45.tar.xz
infinity-for-reddit-d9de2938b495b4972ba706bfc383ee2800073e45.tar.zst
infinity-for-reddit-d9de2938b495b4972ba706bfc383ee2800073e45.zip
Viewing imgur albums and images is now available.
Diffstat (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/API/ImgurAPI.java4
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewImgurMediaActivity.java115
2 files changed, 117 insertions, 2 deletions
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<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);
}
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<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);
+ }
+ }
+ }
} \ No newline at end of file