From fc1b1ae58546861edd8f745fee9e807a13d8629a Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 10 Sep 2020 21:42:37 +0800 Subject: Barebone gfycat and redgifs video autoplay in PostRecyclerViewAdapter. --- .../Adapter/PostRecyclerViewAdapter.java | 49 ++++++++++++++++++++-- .../FetchGfycatOrRedgifsVideoLinks.java | 44 +++++++++++++++++++ .../infinityforreddit/Fragment/PostFragment.java | 31 +++++++++----- .../infinityforreddit/Post/ParsePost.java | 25 +++++++++-- .../infinityforreddit/Post/Post.java | 44 +++++++++++++++++++ 5 files changed, 176 insertions(+), 17 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index cde3919c..9315f153 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -81,6 +81,7 @@ import ml.docilealligator.infinityforreddit.BottomSheetFragment.ShareLinkBottomS import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.CustomView.AspectRatioGifImageView; import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToDetailActivity; +import ml.docilealligator.infinityforreddit.FetchGfycatOrRedgifsVideoLinks; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.Post.Post; import ml.docilealligator.infinityforreddit.Post.PostDataSource; @@ -124,6 +125,8 @@ public class PostRecyclerViewAdapter extends PagedListAdapter= mStartAutoplayVisibleAreaOffset; + return mediaUri != null && ToroUtil.visibleAreaOffset(this, itemView.getParent()) >= mStartAutoplayVisibleAreaOffset; } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java index 9d51cb1e..4567fe85 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchGfycatOrRedgifsVideoLinks.java @@ -15,11 +15,20 @@ import retrofit2.Response; import retrofit2.Retrofit; public class FetchGfycatOrRedgifsVideoLinks { + private FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener; + private ParseGfycatVideoLinksAsyncTask parseGfycatVideoLinksAsyncTask; + Retrofit gfycatRetrofit; + Call gfycatCall; + public interface FetchGfycatOrRedgifsVideoLinksListener { void success(String webm, String mp4); void failed(int errorCode); } + public FetchGfycatOrRedgifsVideoLinks(FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { + this.fetchGfycatOrRedgifsVideoLinksListener = fetchGfycatOrRedgifsVideoLinksListener; + } + public static void fetchGfycatOrRedgifsVideoLinks(Retrofit gfycatRetrofit, String gfycatId, FetchGfycatOrRedgifsVideoLinksListener fetchGfycatOrRedgifsVideoLinksListener) { gfycatRetrofit.create(GfycatAPI.class).getGfycatData(gfycatId).enqueue(new Callback() { @@ -39,6 +48,41 @@ public class FetchGfycatOrRedgifsVideoLinks { }); } + public void fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(Retrofit gfycatRetrofit, Retrofit redgifsRetrofit, + String gfycatId, boolean isGfycatVideo, + boolean automaticallyTryRedgifs) { + gfycatCall = (isGfycatVideo ? gfycatRetrofit : redgifsRetrofit).create(GfycatAPI.class).getGfycatData(gfycatId); + gfycatCall.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + parseGfycatVideoLinksAsyncTask = new ParseGfycatVideoLinksAsyncTask(response.body(), fetchGfycatOrRedgifsVideoLinksListener); + parseGfycatVideoLinksAsyncTask.execute(); + } else { + if (response.code() == 404 && isGfycatVideo && automaticallyTryRedgifs) { + fetchGfycatOrRedgifsVideoLinksInRecyclerViewAdapter(gfycatRetrofit, redgifsRetrofit, gfycatId, false, false); + } else { + fetchGfycatOrRedgifsVideoLinksListener.failed(response.code()); + } + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + fetchGfycatOrRedgifsVideoLinksListener.failed(-1); + } + }); + } + + public void cancel() { + if (gfycatCall != null && !gfycatCall.isCanceled()) { + gfycatCall.cancel(); + } + if (parseGfycatVideoLinksAsyncTask != null && !parseGfycatVideoLinksAsyncTask.isCancelled()) { + parseGfycatVideoLinksAsyncTask.cancel(true); + } + } + private static class ParseGfycatVideoLinksAsyncTask extends AsyncTask { private String response; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java index 7edfa0c4..f334b7fa 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/PostFragment.java @@ -138,6 +138,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Named("oauth") Retrofit mOauthRetrofit; @Inject + @Named("gfycat") + Retrofit mGfycatRetrofit; + @Inject + @Named("redgifs") + Retrofit mRedgifsRetrofit; + @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -401,8 +407,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { sortType = new SortType(SortType.Type.valueOf(sort), SortType.Time.valueOf(sortTime)); postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, defaultPostLayout); - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -456,8 +463,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { sortType = new SortType(SortType.Type.valueOf(sort)); } - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, displaySubredditName, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + windowWidth, accessToken, postType, postLayout, displaySubredditName, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -504,8 +512,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { sortType = new SortType(SortType.Type.valueOf(sort)); } - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -550,8 +559,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE + username, defaultPostLayout); - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { @@ -589,8 +599,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, defaultPostLayout); - mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mRedditDataRoomDatabase, - customThemeWrapper, locale, windowWidth, accessToken, postType, postLayout, true, + mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + windowWidth, accessToken, postType, postLayout, true, mSharedPreferences, exoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java index 477cb697..11a08868 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java @@ -384,9 +384,14 @@ public class ParsePost { try { Uri uri = Uri.parse(url); String authority = uri.getAuthority(); - if (authority != null && (authority.contains("gfycat.com") || authority.contains("redgifs.com"))) { - post.setPostType(Post.LINK_TYPE); - post.setUrl(url); + if (authority != null && (authority.contains("gfycat.com"))) { + post.setIsGfycat(true); + post.setVideoUrl(url); + post.setGfycatId(url.substring(url.lastIndexOf("/") + 1)); + } else if (authority != null && authority.contains("redgifs.com")) { + post.setIsRedgifs(true); + post.setVideoUrl(url); + post.setGfycatId(url.substring(url.lastIndexOf("/") + 1)); } } catch (IllegalArgumentException ignore) { } } else if (post.getPostType() == Post.LINK_TYPE || post.getPostType() == Post.NO_PREVIEW_LINK_TYPE) { @@ -422,6 +427,20 @@ public class ParsePost { post.setPostType(Post.GALLERY_TYPE); post.setGallery(gallery); } + } else if (post.getPostType() == Post.LINK_TYPE) { + Uri uri = Uri.parse(url); + String authority = uri.getAuthority(); + if (authority != null && (authority.contains("gfycat.com"))) { + post.setPostType(Post.VIDEO_TYPE); + post.setIsGfycat(true); + post.setVideoUrl(url); + post.setGfycatId(url.substring(url.lastIndexOf("/") + 1)); + } else if (authority != null && authority.contains("redgifs.com")) { + post.setPostType(Post.VIDEO_TYPE); + post.setIsRedgifs(true); + post.setVideoUrl(url); + post.setGfycatId(url.substring(url.lastIndexOf("/") + 1)); + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java index a1a8c9c7..82c550d5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/Post.java @@ -52,6 +52,10 @@ public class Post implements Parcelable { private String url; private String videoUrl; private String videoDownloadUrl; + private String gfycatId; + private boolean isGfycat; + private boolean isRedgifs; + private boolean loadGfyOrRedgifsVideoSuccess; private String permalink; private String flair; private String awards; @@ -202,6 +206,10 @@ public class Post implements Parcelable { url = in.readString(); videoUrl = in.readString(); videoDownloadUrl = in.readString(); + gfycatId = in.readString(); + isGfycat = in.readByte() != 0; + isRedgifs = in.readByte() != 0; + loadGfyOrRedgifsVideoSuccess = in.readByte() != 0; permalink = in.readString(); flair = in.readString(); awards = in.readString(); @@ -353,6 +361,38 @@ public class Post implements Parcelable { this.videoDownloadUrl = videoDownloadUrl; } + public String getGfycatId() { + return gfycatId; + } + + public void setGfycatId(String gfycatId) { + this.gfycatId = gfycatId; + } + + public boolean isGfycat() { + return isGfycat; + } + + public void setIsGfycat(boolean isGfycat) { + this.isGfycat = isGfycat; + } + + public boolean isRedgifs() { + return isRedgifs; + } + + public void setIsRedgifs(boolean isRedgifs) { + this.isRedgifs = isRedgifs; + } + + public boolean isLoadGfyOrRedgifsVideoSuccess() { + return loadGfyOrRedgifsVideoSuccess; + } + + public void setLoadGfyOrRedgifsVideoSuccess(boolean loadGfyOrRedgifsVideoSuccess) { + this.loadGfyOrRedgifsVideoSuccess = loadGfyOrRedgifsVideoSuccess; + } + public String getPermalink() { return permalink; } @@ -512,6 +552,10 @@ public class Post implements Parcelable { parcel.writeString(url); parcel.writeString(videoUrl); parcel.writeString(videoDownloadUrl); + parcel.writeString(gfycatId); + parcel.writeByte((byte) (isGfycat ? 1 : 0)); + parcel.writeByte((byte) (isRedgifs ? 1 : 0)); + parcel.writeByte((byte) (loadGfyOrRedgifsVideoSuccess ? 1 : 0)); parcel.writeString(permalink); parcel.writeString(flair); parcel.writeString(awards); -- cgit v1.2.3