From 2bf561c31e5173f55019de865e77ebb52151854e Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 8 Jul 2021 19:55:32 +0800 Subject: [PATCH] Support viewing single RPAN broadcast in-app. --- .../activities/LinkResolverActivity.java | 5 + .../activities/RPANActivity.java | 163 +++++++++++------- .../infinityforreddit/apis/Strapi.java | 5 +- app/src/main/res/values/strings.xml | 1 + 4 files changed, 110 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java index 7f834f48..2a26ae10 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java @@ -48,6 +48,7 @@ public class LinkResolverActivity extends AppCompatActivity { private static final String IMGUR_GALLERY_PATTERN = "/gallery/\\w+/?"; private static final String IMGUR_ALBUM_PATTERN = "/(album|a)/\\w+/?"; private static final String IMGUR_IMAGE_PATTERN = "/\\w+/?"; + private static final String RPAN_BROADCAST_PATTERN = "/rpan/r/[\\w-]+/\\w+/?\\w+/?"; @Inject @Named("default") @@ -186,6 +187,10 @@ public class LinkResolverActivity extends AppCompatActivity { intent.putExtra(ViewSubredditDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); intent.putExtra(ViewSubredditDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); startActivity(intent); + } else if (path.matches(RPAN_BROADCAST_PATTERN)) { + Intent intent = new Intent(this, RPANActivity.class); + intent.putExtra(RPANActivity.EXTRA_RPAN_BROADCAST_FULLNAME_OR_ID, path.substring(path.lastIndexOf('/') + 1)); + startActivity(intent); } else if (authority.equals("redd.it") && path.matches(REDD_IT_POST_PATTERN)) { Intent intent = new Intent(this, ViewPostDetailActivity.class); intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, path.substring(1)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RPANActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RPANActivity.java index 8f681676..54b3bcaf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RPANActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RPANActivity.java @@ -51,7 +51,6 @@ import ml.docilealligator.infinityforreddit.font.ContentFontFamily; import ml.docilealligator.infinityforreddit.font.FontFamily; import ml.docilealligator.infinityforreddit.font.TitleFontFamily; import ml.docilealligator.infinityforreddit.fragments.ViewRPANBroadcastFragment; -import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import retrofit2.Call; @@ -61,6 +60,8 @@ import retrofit2.Retrofit; public class RPANActivity extends AppCompatActivity { + public static final String EXTRA_RPAN_BROADCAST_FULLNAME_OR_ID = "ERBFOI"; + @BindView(R.id.coordinator_layout_rpan_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.view_pager_2_rpan_activity) @@ -129,25 +130,59 @@ public class RPANActivity extends AppCompatActivity { } private void loadRPANVideos() { - strapiRetrofit.create(Strapi.class).getAllBroadcasts(APIUtils.getOAuthHeader(mAccessToken)).enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - progressBar.setVisibility(View.GONE); - if (response.isSuccessful()) { - parseRPANBroadcasts(response.body()); - } else { + String rpanBroadcastFullNameOrId = getIntent().getStringExtra(EXTRA_RPAN_BROADCAST_FULLNAME_OR_ID); + if (rpanBroadcastFullNameOrId == null) { + strapiRetrofit.create(Strapi.class).getAllBroadcasts().enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + progressBar.setVisibility(View.GONE); + if (response.isSuccessful()) { + parseRPANBroadcasts(response.body()); + } else { + Toast.makeText(RPANActivity.this, + R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + progressBar.setVisibility(View.GONE); Toast.makeText(RPANActivity.this, R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); } - } + }); + } else { + strapiRetrofit.create(Strapi.class).getRPANBroadcast(rpanBroadcastFullNameOrId).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + progressBar.setVisibility(View.GONE); + if (response.isSuccessful()) { + Handler handler = new Handler(); + mExecutor.execute(() -> { + try { + rpanBroadcasts = new ArrayList<>(); + rpanBroadcasts.add(parseSingleRPANBroadcast(new JSONObject(response.body()).getJSONObject(JSONUtils.DATA_KEY))); + handler.post(() -> initializeViewPager()); + } catch (JSONException e) { + e.printStackTrace(); + handler.post(() -> Toast.makeText(RPANActivity.this, + R.string.parse_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show()); + } + }); + } else { + Toast.makeText(RPANActivity.this, + R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); + } + } - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - progressBar.setVisibility(View.GONE); - Toast.makeText(RPANActivity.this, - R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); - } - }); + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + progressBar.setVisibility(View.GONE); + Toast.makeText(RPANActivity.this, + R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); + } + }); + } } private void parseRPANBroadcasts(String response) { @@ -160,50 +195,12 @@ public class RPANActivity extends AppCompatActivity { JSONArray dataArray = responseObject.getJSONArray(JSONUtils.DATA_KEY); for (int i = 0; i < dataArray.length(); i++) { - JSONObject singleData = dataArray.getJSONObject(i); - JSONObject rpanPostObject = singleData.getJSONObject(JSONUtils.POST_KEY); - RPANBroadcast.RPANPost rpanPost = new RPANBroadcast.RPANPost( - rpanPostObject.getString(JSONUtils.ID_KEY), - rpanPostObject.getString(JSONUtils.TITLE_KEY), - rpanPostObject.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.NAME_KEY), - rpanPostObject.getJSONObject(JSONUtils.SUBREDDIT_KEY).getJSONObject(JSONUtils.STYLES_KEY).getString(JSONUtils.ICON_KEY), - rpanPostObject.getJSONObject(JSONUtils.AUTHOR_INFO_KEY).getString(JSONUtils.NAME_KEY), - rpanPostObject.getInt(JSONUtils.SCORE_KEY), - rpanPostObject.getString(JSONUtils.VOTE_STATE_KEY), - rpanPostObject.getDouble(JSONUtils.UPVOTE_RATIO_CAMEL_CASE_KEY), - rpanPostObject.getString(JSONUtils.PERMALINK_KEY), - rpanPostObject.getJSONObject(JSONUtils.OUTBOUND_LINK_KEY).getString(JSONUtils.URL_KEY), - rpanPostObject.getBoolean(JSONUtils.IS_NSFW_KEY), - rpanPostObject.getBoolean(JSONUtils.IS_LOCKED_KEY), - rpanPostObject.getBoolean(JSONUtils.IS_ARCHIVED_KEY), - rpanPostObject.getBoolean(JSONUtils.IS_SPOILER), - rpanPostObject.getString(JSONUtils.SUGGESTED_COMMENT_SORT_CAMEL_CASE_KEY), - rpanPostObject.getString(JSONUtils.LIVE_COMMENTS_WEBSOCKET_KEY) - ); - - JSONObject rpanStreamObject = singleData.getJSONObject(JSONUtils.STREAM_KEY); - RPANBroadcast.RPANStream rpanStream = new RPANBroadcast.RPANStream( - rpanStreamObject.getString(JSONUtils.STREAM_ID_KEY), - rpanStreamObject.getString(JSONUtils.HLS_URL_KEY), - rpanStreamObject.getString(JSONUtils.THUMBNAIL_KEY), - rpanStreamObject.getInt(JSONUtils.WIDTH_KEY), - rpanStreamObject.getInt(JSONUtils.HEIGHT_KEY), - rpanStreamObject.getLong(JSONUtils.PUBLISH_AT_KEY), - rpanStreamObject.getString(JSONUtils.STATE_KEY) - ); - - rpanBroadcasts.add(new RPANBroadcast( - singleData.getInt(JSONUtils.UPVOTES_KEY), - singleData.getInt(JSONUtils.DOWNVOTES_KEY), - singleData.getInt(JSONUtils.UNIQUE_WATCHERS_KEY), - singleData.getInt(JSONUtils.CONTINUOUS_WATCHERS_KEY), - singleData.getInt(JSONUtils.TOTAL_CONTINUOUS_WATCHERS_KEY), - singleData.getBoolean(JSONUtils.CHAT_DISABLED_KEY), - singleData.getDouble(JSONUtils.BROADCAST_TIME_KEY), - singleData.getDouble(JSONUtils.ESTIMATED_REMAINING_TIME_KEY), - rpanPost, - rpanStream - )); + try { + JSONObject singleData = dataArray.getJSONObject(i); + rpanBroadcasts.add(parseSingleRPANBroadcast(singleData)); + } catch (JSONException e) { + e.printStackTrace(); + } } handler.post(() -> { @@ -220,6 +217,52 @@ public class RPANActivity extends AppCompatActivity { }); } + private RPANBroadcast parseSingleRPANBroadcast(JSONObject singleData) throws JSONException { + JSONObject rpanPostObject = singleData.getJSONObject(JSONUtils.POST_KEY); + RPANBroadcast.RPANPost rpanPost = new RPANBroadcast.RPANPost( + rpanPostObject.getString(JSONUtils.ID_KEY), + rpanPostObject.getString(JSONUtils.TITLE_KEY), + rpanPostObject.getJSONObject(JSONUtils.SUBREDDIT_KEY).getString(JSONUtils.NAME_KEY), + rpanPostObject.getJSONObject(JSONUtils.SUBREDDIT_KEY).getJSONObject(JSONUtils.STYLES_KEY).getString(JSONUtils.ICON_KEY), + rpanPostObject.getJSONObject(JSONUtils.AUTHOR_INFO_KEY).getString(JSONUtils.NAME_KEY), + rpanPostObject.getInt(JSONUtils.SCORE_KEY), + rpanPostObject.getString(JSONUtils.VOTE_STATE_KEY), + rpanPostObject.getDouble(JSONUtils.UPVOTE_RATIO_CAMEL_CASE_KEY), + rpanPostObject.getString(JSONUtils.PERMALINK_KEY), + rpanPostObject.getJSONObject(JSONUtils.OUTBOUND_LINK_KEY).getString(JSONUtils.URL_KEY), + rpanPostObject.getBoolean(JSONUtils.IS_NSFW_KEY), + rpanPostObject.getBoolean(JSONUtils.IS_LOCKED_KEY), + rpanPostObject.getBoolean(JSONUtils.IS_ARCHIVED_KEY), + rpanPostObject.getBoolean(JSONUtils.IS_SPOILER), + rpanPostObject.getString(JSONUtils.SUGGESTED_COMMENT_SORT_CAMEL_CASE_KEY), + rpanPostObject.getString(JSONUtils.LIVE_COMMENTS_WEBSOCKET_KEY) + ); + + JSONObject rpanStreamObject = singleData.getJSONObject(JSONUtils.STREAM_KEY); + RPANBroadcast.RPANStream rpanStream = new RPANBroadcast.RPANStream( + rpanStreamObject.getString(JSONUtils.STREAM_ID_KEY), + rpanStreamObject.getString(JSONUtils.HLS_URL_KEY), + rpanStreamObject.getString(JSONUtils.THUMBNAIL_KEY), + rpanStreamObject.getInt(JSONUtils.WIDTH_KEY), + rpanStreamObject.getInt(JSONUtils.HEIGHT_KEY), + rpanStreamObject.getLong(JSONUtils.PUBLISH_AT_KEY), + rpanStreamObject.getString(JSONUtils.STATE_KEY) + ); + + return new RPANBroadcast( + singleData.getInt(JSONUtils.UPVOTES_KEY), + singleData.getInt(JSONUtils.DOWNVOTES_KEY), + singleData.getInt(JSONUtils.UNIQUE_WATCHERS_KEY), + singleData.getInt(JSONUtils.CONTINUOUS_WATCHERS_KEY), + singleData.getInt(JSONUtils.TOTAL_CONTINUOUS_WATCHERS_KEY), + singleData.getBoolean(JSONUtils.CHAT_DISABLED_KEY), + singleData.getDouble(JSONUtils.BROADCAST_TIME_KEY), + singleData.getDouble(JSONUtils.ESTIMATED_REMAINING_TIME_KEY), + rpanPost, + rpanStream + ); + } + private void initializeViewPager() { sectionsPagerAdapter = new SectionsPagerAdapter(this); viewPager2.setAdapter(sectionsPagerAdapter); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/Strapi.java b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/Strapi.java index e4bfe573..e68b8850 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/Strapi.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/Strapi.java @@ -1,15 +1,12 @@ package ml.docilealligator.infinityforreddit.apis; -import java.util.Map; - import retrofit2.Call; import retrofit2.http.GET; -import retrofit2.http.HeaderMap; import retrofit2.http.Path; public interface Strapi { @GET("/broadcasts") - Call getAllBroadcasts(@HeaderMap Map headers); + Call getAllBroadcasts(); @GET("/videos/{rpan_id_or_fullname}") Call getRPANBroadcast(@Path("rpan_id_or_fullname") String rpanIdOrFullname); diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c095dfc0..aad47bf1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1131,5 +1131,6 @@ Cannot load RPAN broadcasts Cannot parse RPAN broadcasts + Cannot parse RPAN broadcast