Support viewing single RPAN broadcast in-app.

This commit is contained in:
Alex Ning 2021-07-08 19:55:32 +08:00
parent 9099b55964
commit 2bf561c31e
4 changed files with 110 additions and 64 deletions

View File

@ -48,6 +48,7 @@ public class LinkResolverActivity extends AppCompatActivity {
private static final String IMGUR_GALLERY_PATTERN = "/gallery/\\w+/?"; private static final String IMGUR_GALLERY_PATTERN = "/gallery/\\w+/?";
private static final String IMGUR_ALBUM_PATTERN = "/(album|a)/\\w+/?"; private static final String IMGUR_ALBUM_PATTERN = "/(album|a)/\\w+/?";
private static final String IMGUR_IMAGE_PATTERN = "/\\w+/?"; private static final String IMGUR_IMAGE_PATTERN = "/\\w+/?";
private static final String RPAN_BROADCAST_PATTERN = "/rpan/r/[\\w-]+/\\w+/?\\w+/?";
@Inject @Inject
@Named("default") @Named("default")
@ -186,6 +187,10 @@ public class LinkResolverActivity extends AppCompatActivity {
intent.putExtra(ViewSubredditDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname); intent.putExtra(ViewSubredditDetailActivity.EXTRA_MESSAGE_FULLNAME, messageFullname);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName); intent.putExtra(ViewSubredditDetailActivity.EXTRA_NEW_ACCOUNT_NAME, newAccountName);
startActivity(intent); 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)) { } else if (authority.equals("redd.it") && path.matches(REDD_IT_POST_PATTERN)) {
Intent intent = new Intent(this, ViewPostDetailActivity.class); Intent intent = new Intent(this, ViewPostDetailActivity.class);
intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, path.substring(1)); intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, path.substring(1));

View File

@ -51,7 +51,6 @@ import ml.docilealligator.infinityforreddit.font.ContentFontFamily;
import ml.docilealligator.infinityforreddit.font.FontFamily; import ml.docilealligator.infinityforreddit.font.FontFamily;
import ml.docilealligator.infinityforreddit.font.TitleFontFamily; import ml.docilealligator.infinityforreddit.font.TitleFontFamily;
import ml.docilealligator.infinityforreddit.fragments.ViewRPANBroadcastFragment; import ml.docilealligator.infinityforreddit.fragments.ViewRPANBroadcastFragment;
import ml.docilealligator.infinityforreddit.utils.APIUtils;
import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.JSONUtils;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
import retrofit2.Call; import retrofit2.Call;
@ -61,6 +60,8 @@ import retrofit2.Retrofit;
public class RPANActivity extends AppCompatActivity { public class RPANActivity extends AppCompatActivity {
public static final String EXTRA_RPAN_BROADCAST_FULLNAME_OR_ID = "ERBFOI";
@BindView(R.id.coordinator_layout_rpan_activity) @BindView(R.id.coordinator_layout_rpan_activity)
CoordinatorLayout coordinatorLayout; CoordinatorLayout coordinatorLayout;
@BindView(R.id.view_pager_2_rpan_activity) @BindView(R.id.view_pager_2_rpan_activity)
@ -129,25 +130,59 @@ public class RPANActivity extends AppCompatActivity {
} }
private void loadRPANVideos() { private void loadRPANVideos() {
strapiRetrofit.create(Strapi.class).getAllBroadcasts(APIUtils.getOAuthHeader(mAccessToken)).enqueue(new Callback<String>() { String rpanBroadcastFullNameOrId = getIntent().getStringExtra(EXTRA_RPAN_BROADCAST_FULLNAME_OR_ID);
@Override if (rpanBroadcastFullNameOrId == null) {
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { strapiRetrofit.create(Strapi.class).getAllBroadcasts().enqueue(new Callback<String>() {
progressBar.setVisibility(View.GONE); @Override
if (response.isSuccessful()) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
parseRPANBroadcasts(response.body()); progressBar.setVisibility(View.GONE);
} else { 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<String> call, @NonNull Throwable t) {
progressBar.setVisibility(View.GONE);
Toast.makeText(RPANActivity.this, Toast.makeText(RPANActivity.this,
R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show();
} }
} });
} else {
strapiRetrofit.create(Strapi.class).getRPANBroadcast(rpanBroadcastFullNameOrId).enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> 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 @Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
progressBar.setVisibility(View.GONE); progressBar.setVisibility(View.GONE);
Toast.makeText(RPANActivity.this, Toast.makeText(RPANActivity.this,
R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show(); R.string.load_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show();
} }
}); });
}
} }
private void parseRPANBroadcasts(String response) { private void parseRPANBroadcasts(String response) {
@ -160,50 +195,12 @@ public class RPANActivity extends AppCompatActivity {
JSONArray dataArray = responseObject.getJSONArray(JSONUtils.DATA_KEY); JSONArray dataArray = responseObject.getJSONArray(JSONUtils.DATA_KEY);
for (int i = 0; i < dataArray.length(); i++) { for (int i = 0; i < dataArray.length(); i++) {
JSONObject singleData = dataArray.getJSONObject(i); try {
JSONObject rpanPostObject = singleData.getJSONObject(JSONUtils.POST_KEY); JSONObject singleData = dataArray.getJSONObject(i);
RPANBroadcast.RPANPost rpanPost = new RPANBroadcast.RPANPost( rpanBroadcasts.add(parseSingleRPANBroadcast(singleData));
rpanPostObject.getString(JSONUtils.ID_KEY), } catch (JSONException e) {
rpanPostObject.getString(JSONUtils.TITLE_KEY), e.printStackTrace();
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
));
} }
handler.post(() -> { 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() { private void initializeViewPager() {
sectionsPagerAdapter = new SectionsPagerAdapter(this); sectionsPagerAdapter = new SectionsPagerAdapter(this);
viewPager2.setAdapter(sectionsPagerAdapter); viewPager2.setAdapter(sectionsPagerAdapter);

View File

@ -1,15 +1,12 @@
package ml.docilealligator.infinityforreddit.apis; package ml.docilealligator.infinityforreddit.apis;
import java.util.Map;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.http.GET; import retrofit2.http.GET;
import retrofit2.http.HeaderMap;
import retrofit2.http.Path; import retrofit2.http.Path;
public interface Strapi { public interface Strapi {
@GET("/broadcasts") @GET("/broadcasts")
Call<String> getAllBroadcasts(@HeaderMap Map<String ,String> headers); Call<String> getAllBroadcasts();
@GET("/videos/{rpan_id_or_fullname}") @GET("/videos/{rpan_id_or_fullname}")
Call<String> getRPANBroadcast(@Path("rpan_id_or_fullname") String rpanIdOrFullname); Call<String> getRPANBroadcast(@Path("rpan_id_or_fullname") String rpanIdOrFullname);

View File

@ -1131,5 +1131,6 @@
<string name="load_rpan_broadcasts_failed">Cannot load RPAN broadcasts</string> <string name="load_rpan_broadcasts_failed">Cannot load RPAN broadcasts</string>
<string name="parse_rpan_broadcasts_failed">Cannot parse RPAN broadcasts</string> <string name="parse_rpan_broadcasts_failed">Cannot parse RPAN broadcasts</string>
<string name="parse_rpan_broadcast_failed">Cannot parse RPAN broadcast</string>
</resources> </resources>