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_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));

View File

@ -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,7 +130,9 @@ public class RPANActivity extends AppCompatActivity {
}
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);
if (rpanBroadcastFullNameOrId == null) {
strapiRetrofit.create(Strapi.class).getAllBroadcasts().enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
progressBar.setVisibility(View.GONE);
@ -148,6 +151,38 @@ public class RPANActivity extends AppCompatActivity {
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
public void onFailure(@NonNull Call<String> 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,7 +195,29 @@ public class RPANActivity extends AppCompatActivity {
JSONArray dataArray = responseObject.getJSONArray(JSONUtils.DATA_KEY);
for (int i = 0; i < dataArray.length(); i++) {
try {
JSONObject singleData = dataArray.getJSONObject(i);
rpanBroadcasts.add(parseSingleRPANBroadcast(singleData));
} catch (JSONException e) {
e.printStackTrace();
}
}
handler.post(() -> {
RPANActivity.this.rpanBroadcasts = rpanBroadcasts;
RPANActivity.this.nextCursor = nextCursor;
initializeViewPager();
});
} catch (JSONException e) {
e.printStackTrace();
handler.post(() -> Toast.makeText(RPANActivity.this,
R.string.parse_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show());
}
});
}
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),
@ -192,7 +249,7 @@ public class RPANActivity extends AppCompatActivity {
rpanStreamObject.getString(JSONUtils.STATE_KEY)
);
rpanBroadcasts.add(new RPANBroadcast(
return new RPANBroadcast(
singleData.getInt(JSONUtils.UPVOTES_KEY),
singleData.getInt(JSONUtils.DOWNVOTES_KEY),
singleData.getInt(JSONUtils.UNIQUE_WATCHERS_KEY),
@ -203,21 +260,7 @@ public class RPANActivity extends AppCompatActivity {
singleData.getDouble(JSONUtils.ESTIMATED_REMAINING_TIME_KEY),
rpanPost,
rpanStream
));
}
handler.post(() -> {
RPANActivity.this.rpanBroadcasts = rpanBroadcasts;
RPANActivity.this.nextCursor = nextCursor;
initializeViewPager();
});
} catch (JSONException e) {
e.printStackTrace();
handler.post(() -> Toast.makeText(RPANActivity.this,
R.string.parse_rpan_broadcasts_failed, Toast.LENGTH_SHORT).show());
}
});
);
}
private void initializeViewPager() {

View File

@ -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<String> getAllBroadcasts(@HeaderMap Map<String ,String> headers);
Call<String> getAllBroadcasts();
@GET("/videos/{rpan_id_or_fullname}")
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="parse_rpan_broadcasts_failed">Cannot parse RPAN broadcasts</string>
<string name="parse_rpan_broadcast_failed">Cannot parse RPAN broadcast</string>
</resources>