aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2020-10-04 08:55:19 +0000
committerAlex Ning <chineseperson5@gmail.com>2020-10-04 08:55:19 +0000
commit2b6f04a283c755f5f3b60d7585a9136e1a8bd491 (patch)
tree1830cd2ff8d2ee1c20169ccebc62e91035aeb9aa /app/src/main/java
parent4f19bd83aa8d2500d825ce63ae8770325b4a44e9 (diff)
downloadinfinity-for-reddit-2b6f04a283c755f5f3b60d7585a9136e1a8bd491.tar
infinity-for-reddit-2b6f04a283c755f5f3b60d7585a9136e1a8bd491.tar.gz
infinity-for-reddit-2b6f04a283c755f5f3b60d7585a9136e1a8bd491.tar.bz2
infinity-for-reddit-2b6f04a283c755f5f3b60d7585a9136e1a8bd491.tar.lz
infinity-for-reddit-2b6f04a283c755f5f3b60d7585a9136e1a8bd491.tar.xz
infinity-for-reddit-2b6f04a283c755f5f3b60d7585a9136e1a8bd491.tar.zst
infinity-for-reddit-2b6f04a283c755f5f3b60d7585a9136e1a8bd491.zip
Random post and subreddit is available.
Diffstat (limited to 'app/src/main/java')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java2
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FetchRandomSubredditOrPostActivity.java95
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java19
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java2
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java21
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java3
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/RandomBottomSheetFragment.java88
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java4
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchPost.java46
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java69
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java20
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java2
12 files changed, 340 insertions, 31 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java
index ae2756e0..2a9a4711 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/API/RedditAPI.java
@@ -334,6 +334,6 @@ public interface RedditAPI {
@GET("/r/random/comments.json?limit=1&raw_json=1")
Call<String> getRandomPost();
- @GET("/r/randnsfw/new?sort=new&t=all&limit=1&raw_json=1")
+ @GET("/r/randnsfw/new.json?sort=new&t=all&limit=1&raw_json=1")
Call<String> getRandomNSFWPost();
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FetchRandomSubredditOrPostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FetchRandomSubredditOrPostActivity.java
new file mode 100644
index 00000000..422f326e
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/FetchRandomSubredditOrPostActivity.java
@@ -0,0 +1,95 @@
+package ml.docilealligator.infinityforreddit.Activity;
+
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.os.Bundle;
+import android.widget.RelativeLayout;
+import android.widget.Toast;
+
+import com.airbnb.lottie.LottieAnimationView;
+
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import butterknife.BindView;
+import butterknife.ButterKnife;
+import ml.docilealligator.infinityforreddit.BottomSheetFragment.RandomBottomSheetFragment;
+import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
+import ml.docilealligator.infinityforreddit.Infinity;
+import ml.docilealligator.infinityforreddit.Post.FetchPost;
+import ml.docilealligator.infinityforreddit.R;
+import retrofit2.Retrofit;
+
+public class FetchRandomSubredditOrPostActivity extends BaseActivity {
+
+ public static final String EXTRA_RANDOM_OPTION = "ERO";
+
+ @BindView(R.id.relative_layout_fetch_random_subreddit_or_post_activity)
+ RelativeLayout relativeLayout;
+ @BindView(R.id.lottie_animation_view_fetch_random_subreddit_or_post_activity)
+ LottieAnimationView lottieAnimationView;
+ @Inject
+ @Named("no_oauth")
+ Retrofit mRetrofit;
+ @Inject
+ @Named("default")
+ SharedPreferences mSharedPreferences;
+ @Inject
+ CustomThemeWrapper mCustomThemeWrapper;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ ((Infinity) getApplicationContext()).getAppComponent().inject(this);
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_fetch_random_subreddit_or_post);
+
+ ButterKnife.bind(this);
+
+ int option = getIntent().getIntExtra(EXTRA_RANDOM_OPTION, RandomBottomSheetFragment.RANDOM_SUBREDDIT);
+
+ FetchPost.fetchRandomPost(mRetrofit, option == RandomBottomSheetFragment.RANDOM_NSFW_SUBREDDIT
+ || option == RandomBottomSheetFragment.RANDOM_NSFW_POST, new FetchPost.FetchRandomPostListener() {
+ @Override
+ public void fetchRandomPostSuccess(String postId, String subredditName) {
+ switch (option) {
+ case RandomBottomSheetFragment.RANDOM_SUBREDDIT:
+ case RandomBottomSheetFragment.RANDOM_NSFW_SUBREDDIT: {
+ Intent intent = new Intent(FetchRandomSubredditOrPostActivity.this, ViewSubredditDetailActivity.class);
+ intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, subredditName);
+ startActivity(intent);
+ }
+ break;
+ case RandomBottomSheetFragment.RANDOM_POST:
+ case RandomBottomSheetFragment.RANDOM_NSFW_POST:
+ Intent intent = new Intent(FetchRandomSubredditOrPostActivity.this, ViewPostDetailActivity.class);
+ intent.putExtra(ViewPostDetailActivity.EXTRA_POST_ID, postId);
+ startActivity(intent);
+ break;
+ }
+
+ finish();
+ }
+
+ @Override
+ public void fetchRandomPostFailed() {
+ Toast.makeText(FetchRandomSubredditOrPostActivity.this, R.string.fetch_random_thing_failed, Toast.LENGTH_SHORT).show();
+ finish();
+ }
+ });
+ }
+
+ @Override
+ protected SharedPreferences getDefaultSharedPreferences() {
+ return mSharedPreferences;
+ }
+
+ @Override
+ protected CustomThemeWrapper getCustomThemeWrapper() {
+ return mCustomThemeWrapper;
+ }
+
+ @Override
+ protected void applyCustomTheme() {
+ relativeLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor());
+ }
+} \ No newline at end of file
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java
index 96851532..c96e5544 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java
@@ -73,6 +73,7 @@ import ml.docilealligator.infinityforreddit.AsyncTask.SwitchToAnonymousAccountAs
import ml.docilealligator.infinityforreddit.BottomSheetFragment.FABMoreOptionsBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostLayoutBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostTypeBottomSheetFragment;
+import ml.docilealligator.infinityforreddit.BottomSheetFragment.RandomBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTimeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTypeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
@@ -108,7 +109,7 @@ import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;
public class MainActivity extends BaseActivity implements SortTypeSelectionCallback,
PostTypeBottomSheetFragment.PostTypeSelectionCallback, PostLayoutBottomSheetFragment.PostLayoutSelectionCallback,
- ActivityToolbarInterface, FABMoreOptionsBottomSheetFragment.FABOptionSelectionCallback {
+ ActivityToolbarInterface, FABMoreOptionsBottomSheetFragment.FABOptionSelectionCallback, RandomBottomSheetFragment.RandomOptionSelectionCallback {
static final String EXTRA_POST_TYPE = "EPT";
static final String EXTRA_MESSSAGE_FULLNAME = "ENF";
@@ -164,6 +165,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
SubscribedSubredditViewModel subscribedSubredditViewModel;
AccountViewModel accountViewModel;
@Inject
+ @Named("no_oauth")
+ Retrofit mRetrofit;
+ @Inject
@Named("oauth")
Retrofit mOauthRetrofit;
@Inject
@@ -1218,12 +1222,23 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
break;
}
case FABMoreOptionsBottomSheetFragment.FAB_RANDOM: {
-
+ RandomBottomSheetFragment randomBottomSheetFragment = new RandomBottomSheetFragment();
+ Bundle bundle = new Bundle();
+ bundle.putBoolean(RandomBottomSheetFragment.EXTRA_IS_NSFW, mNsfwAndSpoilerSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.NSFW_BASE, false));
+ randomBottomSheetFragment.setArguments(bundle);
+ randomBottomSheetFragment.show(getSupportFragmentManager(), randomBottomSheetFragment.getTag());
break;
}
}
}
+ @Override
+ public void randomOptionSelected(int option) {
+ Intent intent = new Intent(this, FetchRandomSubredditOrPostActivity.class);
+ intent.putExtra(FetchRandomSubredditOrPostActivity.EXTRA_RANDOM_OPTION, option);
+ startActivity(intent);
+ }
+
private class SectionsPagerAdapter extends FragmentStateAdapter {
int tabCount;
boolean showFavoriteSubscribedSubreddits;
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java
index 8a5b2cf4..0705064e 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPostDetailActivity.java
@@ -779,7 +779,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
mSwipeRefreshLayout.setRefreshing(false);
if (response.isSuccessful()) {
- ParsePost.parsePost(response.body(), mLocale, new ParsePost.ParsePostListener() {
+ ParsePost.parsePost(response.body(), new ParsePost.ParsePostListener() {
@Override
public void onParsePostSuccess(Post post) {
mPost = post;
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java
index 3b043b9e..7db3a9cc 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewSubredditDetailActivity.java
@@ -67,6 +67,7 @@ import ml.docilealligator.infinityforreddit.AsyncTask.SwitchAccountAsyncTask;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.FABMoreOptionsBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostLayoutBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostTypeBottomSheetFragment;
+import ml.docilealligator.infinityforreddit.BottomSheetFragment.RandomBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTimeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTypeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
@@ -94,7 +95,8 @@ import retrofit2.Retrofit;
public class ViewSubredditDetailActivity extends BaseActivity implements SortTypeSelectionCallback,
PostTypeBottomSheetFragment.PostTypeSelectionCallback, PostLayoutBottomSheetFragment.PostLayoutSelectionCallback,
- ActivityToolbarInterface, FABMoreOptionsBottomSheetFragment.FABOptionSelectionCallback {
+ ActivityToolbarInterface, FABMoreOptionsBottomSheetFragment.FABOptionSelectionCallback,
+ RandomBottomSheetFragment.RandomOptionSelectionCallback {
public static final String EXTRA_SUBREDDIT_NAME_KEY = "ESN";
public static final String EXTRA_MESSAGE_FULLNAME = "ENF";
@@ -427,7 +429,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
if (subredditData.isNSFW()) {
if (nsfwWarningBuilder == null
- && mNsfwAndSpoilerSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.NSFW_BASE, false)) {
+ && !mNsfwAndSpoilerSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.NSFW_BASE, false)) {
nsfwWarningBuilder = new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.warning)
.setMessage(R.string.this_is_a_nsfw_subreddit)
@@ -1124,9 +1126,24 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
.show();
break;
}
+ case FABMoreOptionsBottomSheetFragment.FAB_RANDOM: {
+ RandomBottomSheetFragment randomBottomSheetFragment = new RandomBottomSheetFragment();
+ Bundle bundle = new Bundle();
+ bundle.putBoolean(RandomBottomSheetFragment.EXTRA_IS_NSFW, mNsfwAndSpoilerSharedPreferences.getBoolean((mAccountName == null ? "" : mAccountName) + SharedPreferencesUtils.NSFW_BASE, false));
+ randomBottomSheetFragment.setArguments(bundle);
+ randomBottomSheetFragment.show(getSupportFragmentManager(), randomBottomSheetFragment.getTag());
+ break;
+ }
}
}
+ @Override
+ public void randomOptionSelected(int option) {
+ Intent intent = new Intent(this, FetchRandomSubredditOrPostActivity.class);
+ intent.putExtra(FetchRandomSubredditOrPostActivity.EXTRA_RANDOM_OPTION, option);
+ startActivity(intent);
+ }
+
private class SectionsPagerAdapter extends FragmentStateAdapter {
SectionsPagerAdapter(FragmentManager fm, Lifecycle lifecycle) {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java
index e537b248..40a4e0c5 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java
@@ -13,6 +13,7 @@ import ml.docilealligator.infinityforreddit.Activity.CustomizeThemeActivity;
import ml.docilealligator.infinityforreddit.Activity.EditCommentActivity;
import ml.docilealligator.infinityforreddit.Activity.EditMultiRedditActivity;
import ml.docilealligator.infinityforreddit.Activity.EditPostActivity;
+import ml.docilealligator.infinityforreddit.Activity.FetchRandomSubredditOrPostActivity;
import ml.docilealligator.infinityforreddit.Activity.FilteredThingActivity;
import ml.docilealligator.infinityforreddit.Activity.GiveAwardActivity;
import ml.docilealligator.infinityforreddit.Activity.InboxActivity;
@@ -218,4 +219,6 @@ public interface AppComponent {
void inject(GiveAwardActivity giveAwardActivity);
void inject(TranslationFragment translationFragment);
+
+ void inject(FetchRandomSubredditOrPostActivity fetchRandomSubredditOrPostActivity);
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/RandomBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/RandomBottomSheetFragment.java
new file mode 100644
index 00000000..56763627
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/BottomSheetFragment/RandomBottomSheetFragment.java
@@ -0,0 +1,88 @@
+package ml.docilealligator.infinityforreddit.BottomSheetFragment;
+
+import android.content.Context;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.TextView;
+
+import androidx.annotation.NonNull;
+
+import com.deishelon.roundedbottomsheet.RoundedBottomSheetDialogFragment;
+
+import butterknife.BindView;
+import butterknife.ButterKnife;
+import ml.docilealligator.infinityforreddit.R;
+
+public class RandomBottomSheetFragment extends RoundedBottomSheetDialogFragment {
+
+ public static final String EXTRA_IS_NSFW = "EIN";
+ public static final int RANDOM_SUBREDDIT = 0;
+ public static final int RANDOM_POST = 1;
+ public static final int RANDOM_NSFW_SUBREDDIT = 2;
+ public static final int RANDOM_NSFW_POST = 3;
+
+ @BindView(R.id.random_subreddit_text_view_random_bottom_sheet_fragment)
+ TextView randomSubredditTextView;
+ @BindView(R.id.random_post_text_view_random_bottom_sheet_fragment)
+ TextView randomPostTextView;
+ @BindView(R.id.random_nsfw_subreddit_text_view_random_bottom_sheet_fragment)
+ TextView randomNSFWSubredditTextView;
+ @BindView(R.id.random_nsfw_post_text_view_random_bottom_sheet_fragment)
+ TextView randomNSFWPostTextView;
+
+ private RandomOptionSelectionCallback activity;
+
+ public RandomBottomSheetFragment() {
+ // Required empty public constructor
+ }
+
+ @Override
+ public View onCreateView(LayoutInflater inflater, ViewGroup container,
+ Bundle savedInstanceState) {
+ // Inflate the layout for this fragment
+ View rootView = inflater.inflate(R.layout.fragment_random_bottom_sheet, container, false);
+
+ ButterKnife.bind(this, rootView);
+
+ boolean isNSFW = getArguments().getBoolean(EXTRA_IS_NSFW, false);
+
+ if (!isNSFW) {
+ randomNSFWSubredditTextView.setVisibility(View.GONE);
+ randomNSFWPostTextView.setVisibility(View.GONE);
+ } else {
+ randomNSFWSubredditTextView.setOnClickListener(view -> {
+ activity.randomOptionSelected(RANDOM_NSFW_SUBREDDIT);
+ dismiss();
+ });
+
+ randomNSFWPostTextView.setOnClickListener(view -> {
+ activity.randomOptionSelected(RANDOM_NSFW_POST);
+ dismiss();
+ });
+ }
+
+ randomSubredditTextView.setOnClickListener(view -> {
+ activity.randomOptionSelected(RANDOM_SUBREDDIT);
+ dismiss();
+ });
+
+ randomPostTextView.setOnClickListener(view -> {
+ activity.randomOptionSelected(RANDOM_POST);
+ dismiss();
+ });
+
+ return rootView;
+ }
+
+ @Override
+ public void onAttach(@NonNull Context context) {
+ super.onAttach(context);
+ this.activity = (RandomOptionSelectionCallback) context;
+ }
+
+ public interface RandomOptionSelectionCallback {
+ void randomOptionSelected(int option);
+ }
+} \ No newline at end of file
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java
index 3c397a7a..61bff945 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/SubredditListingFragment.java
@@ -166,9 +166,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
if (hasSubreddit) {
mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE);
} else {
- mFetchSubredditListingInfoLinearLayout.setOnClickListener(view -> {
- //Do nothing
- });
+ mFetchSubredditListingInfoLinearLayout.setOnClickListener(null);
showErrorView(R.string.no_subreddits);
}
});
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchPost.java
index 9695419e..ccfc40eb 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchPost.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/FetchPost.java
@@ -1,5 +1,7 @@
package ml.docilealligator.infinityforreddit.Post;
+import android.util.Log;
+
import androidx.annotation.NonNull;
import java.util.Locale;
@@ -23,7 +25,7 @@ public class FetchPost {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
- ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() {
+ ParsePost.parsePost(response.body(), new ParsePost.ParsePostListener() {
@Override
public void onParsePostSuccess(Post post) {
fetchPostListener.fetchPostSuccess(post);
@@ -46,9 +48,51 @@ public class FetchPost {
});
}
+ public static void fetchRandomPost(Retrofit retrofit, boolean isNSFW, FetchRandomPostListener fetchRandomPostListener) {
+ Call<String> call;
+ if (isNSFW) {
+ call = retrofit.create(RedditAPI.class).getRandomNSFWPost();
+ } else {
+ call = retrofit.create(RedditAPI.class).getRandomPost();
+ }
+
+ call.enqueue(new Callback<String>() {
+ @Override
+ public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
+ if (response.isSuccessful()) {
+ Log.i("asdasdf", "s " + response.body());
+ new ParsePost.ParseRandomPostAsyncTask(response.body(), isNSFW, new ParsePost.ParseRandomPostListener() {
+
+ @Override
+ public void onParseRandomPostSuccess(String postId, String subredditName) {
+ fetchRandomPostListener.fetchRandomPostSuccess(postId, subredditName);
+ }
+
+ @Override
+ public void onParseRandomPostFailed() {
+ fetchRandomPostListener.fetchRandomPostFailed();
+ }
+ }).execute();
+ } else {
+ fetchRandomPostListener.fetchRandomPostFailed();
+ }
+ }
+
+ @Override
+ public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
+ fetchRandomPostListener.fetchRandomPostFailed();
+ }
+ });
+ }
+
public interface FetchPostListener {
void fetchPostSuccess(Post post);
void fetchPostFailed();
}
+
+ public interface FetchRandomPostListener {
+ void fetchRandomPostSuccess(String postId, String subredditName);
+ void fetchRandomPostFailed();
+ }
}
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 2f6b4a1a..b77449cb 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/ParsePost.java
@@ -10,7 +10,6 @@ import org.json.JSONObject;
import java.util.ArrayList;
import java.util.LinkedHashSet;
-import java.util.Locale;
import ml.docilealligator.infinityforreddit.Fragment.PostFragment;
import ml.docilealligator.infinityforreddit.Utils.JSONUtils;
@@ -21,13 +20,13 @@ import ml.docilealligator.infinityforreddit.Utils.Utils;
*/
public class ParsePost {
- public static void parsePosts(String response, Locale locale, int nPosts, int filter, boolean nsfw,
+ public static void parsePosts(String response, int nPosts, int filter, boolean nsfw,
ParsePostsListingListener parsePostsListingListener) {
- new ParsePostDataAsyncTask(response, locale, nPosts, filter, nsfw, parsePostsListingListener).execute();
+ new ParsePostDataAsyncTask(response, nPosts, filter, nsfw, parsePostsListingListener).execute();
}
- public static void parsePost(String response, Locale locale, ParsePostListener parsePostListener) {
- new ParsePostDataAsyncTask(response, locale, true, parsePostListener).execute();
+ public static void parsePost(String response, ParsePostListener parsePostListener) {
+ new ParsePostDataAsyncTask(response, true, parsePostListener).execute();
}
private static Post parseBasicData(JSONObject data) throws JSONException {
@@ -456,9 +455,13 @@ public class ParsePost {
void onParsePostFail();
}
+ public interface ParseRandomPostListener {
+ void onParseRandomPostSuccess(String postId, String subredditName);
+ void onParseRandomPostFailed();
+ }
+
private static class ParsePostDataAsyncTask extends AsyncTask<Void, Void, Void> {
private JSONArray allData;
- private Locale locale;
private int nPosts;
private int filter;
private boolean nsfw;
@@ -469,14 +472,13 @@ public class ParsePost {
private String lastItem;
private boolean parseFailed;
- ParsePostDataAsyncTask(String response, Locale locale, int nPosts, int filter, boolean nsfw,
+ ParsePostDataAsyncTask(String response, int nPosts, int filter, boolean nsfw,
ParsePostsListingListener parsePostsListingListener) {
this.parsePostsListingListener = parsePostsListingListener;
try {
JSONObject jsonResponse = new JSONObject(response);
allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
- this.locale = locale;
this.nPosts = nPosts;
this.filter = filter;
this.nsfw = nsfw;
@@ -488,12 +490,11 @@ public class ParsePost {
}
}
- ParsePostDataAsyncTask(String response, Locale locale, boolean nsfw,
+ ParsePostDataAsyncTask(String response, boolean nsfw,
ParsePostListener parsePostListener) {
this.parsePostListener = parsePostListener;
try {
allData = new JSONArray(response).getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
- this.locale = locale;
this.nsfw = nsfw;
parseFailed = false;
} catch (JSONException e) {
@@ -573,4 +574,52 @@ public class ParsePost {
}
}
}
+
+ public static class ParseRandomPostAsyncTask extends AsyncTask<Void, Void, Void> {
+
+ private String response;
+ private boolean isNSFW;
+ private ParseRandomPostListener parseRandomPostListener;
+ private String subredditName;
+ private String postId;
+ private boolean parseFailed = false;
+
+ ParseRandomPostAsyncTask(String response, boolean isNSFW, ParseRandomPostListener parseRandomPostListener) {
+ this.response = response;
+ this.isNSFW = isNSFW;
+ this.parseRandomPostListener = parseRandomPostListener;
+ }
+
+ @Override
+ protected Void doInBackground(Void... voids) {
+ try {
+ JSONArray postsArray = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
+ if (postsArray.length() == 0) {
+ parseFailed = true;
+ } else {
+ JSONObject post = postsArray.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY);
+ subredditName = post.getString(JSONUtils.SUBREDDIT_KEY);
+ if (isNSFW) {
+ postId = post.getString(JSONUtils.ID_KEY);
+ } else {
+ postId = post.getString(JSONUtils.LINK_ID_KEY).substring("t3_".length());
+ }
+ }
+ } catch (JSONException e) {
+ e.printStackTrace();
+ parseFailed = true;
+ }
+ return null;
+ }
+
+ @Override
+ protected void onPostExecute(Void aVoid) {
+ super.onPostExecute(aVoid);
+ if (parseFailed) {
+ parseRandomPostListener.onParseRandomPostFailed();
+ } else {
+ parseRandomPostListener.onParseRandomPostSuccess(postId, subredditName);
+ }
+ }
+ }
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java
index d444a0b6..b42d710e 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/PostDataSource.java
@@ -247,7 +247,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw,
+ ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@@ -310,7 +310,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw,
+ ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@@ -370,7 +370,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw,
+ ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@@ -444,7 +444,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw,
+ ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@@ -505,7 +505,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw,
+ ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@@ -576,7 +576,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw,
+ ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@@ -659,7 +659,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw,
+ ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@@ -750,7 +750,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw,
+ ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@@ -810,7 +810,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw,
+ ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@@ -881,7 +881,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
- ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw,
+ ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() {
@Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java
index c1c908f2..0abd88e9 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post/SubmitPost.java
@@ -332,7 +332,7 @@ public class SubmitPost {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) {
- ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() {
+ ParsePost.parsePost(response.body(), new ParsePost.ParsePostListener() {
@Override
public void onParsePostSuccess(Post post) {
submitPostListener.submitSuccessful(post);