Random post and subreddit is available.

This commit is contained in:
Alex Ning 2020-10-04 16:55:19 +08:00
parent 4f19bd83aa
commit 2b6f04a283
18 changed files with 456 additions and 38 deletions

View File

@ -38,7 +38,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.browser:browser:1.2.0' implementation 'androidx.browser:browser:1.2.0'
implementation 'androidx.cardview:cardview:1.0.0' implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-rc1' implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
// Lifecycle components // Lifecycle components
def lifecycleVersion = '2.2.0' def lifecycleVersion = '2.2.0'
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleVersion" implementation "androidx.lifecycle:lifecycle-extensions:$lifecycleVersion"
@ -52,7 +52,7 @@ dependencies {
implementation "androidx.room:room-runtime:$roomVersion" implementation "androidx.room:room-runtime:$roomVersion"
annotationProcessor "androidx.room:room-compiler:$roomVersion" annotationProcessor "androidx.room:room-compiler:$roomVersion"
implementation 'androidx.work:work-runtime:2.4.0' implementation 'androidx.work:work-runtime:2.4.0'
implementation 'com.google.android.material:material:1.3.0-alpha02' implementation 'com.google.android.material:material:1.3.0-alpha03'
implementation 'com.google.android.exoplayer:exoplayer-core:2.10.4' implementation 'com.google.android.exoplayer:exoplayer-core:2.10.4'
implementation 'com.google.android.exoplayer:exoplayer-hls:2.10.4' implementation 'com.google.android.exoplayer:exoplayer-hls:2.10.4'
@ -66,7 +66,7 @@ dependencies {
implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.9.0' implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'
implementation 'jp.wasabeef:glide-transformations:4.1.0' implementation 'jp.wasabeef:glide-transformations:4.1.0'
implementation 'com.google.dagger:dagger:2.28.1' implementation 'com.google.dagger:dagger:2.28.3'
annotationProcessor 'com.google.dagger:dagger-compiler:2.28.1' annotationProcessor 'com.google.dagger:dagger-compiler:2.28.1'
implementation 'com.jakewharton:butterknife:10.2.1' implementation 'com.jakewharton:butterknife:10.2.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1' annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
@ -90,12 +90,13 @@ dependencies {
annotationProcessor 'com.evernote:android-state-processor:1.4.1' annotationProcessor 'com.evernote:android-state-processor:1.4.1'
implementation 'com.nex3z:flow-layout:1.3.0' implementation 'com.nex3z:flow-layout:1.3.0'
implementation 'com.r0adkll:slidableactivity:2.1.0' implementation 'com.r0adkll:slidableactivity:2.1.0'
implementation 'com.atlassian.commonmark:commonmark:0.13.1' implementation 'com.atlassian.commonmark:commonmark:0.14.0'
implementation 'com.google.code.gson:gson:2.8.6' implementation 'com.google.code.gson:gson:2.8.6'
implementation 'me.zhanghai.android.fastscroll:library:1.1.2' implementation 'me.zhanghai.android.fastscroll:library:1.1.2'
implementation "com.thefuntasty.hauler:core:3.1.0" implementation "com.thefuntasty.hauler:core:3.1.0"
implementation 'com.github.Piasy:BigImageViewer:1.6.5' implementation 'com.github.Piasy:BigImageViewer:1.6.5'
implementation 'androidx.biometric:biometric:1.1.0-alpha02' implementation 'androidx.biometric:biometric:1.1.0-beta01'
implementation 'com.airbnb.android:lottie:3.4.4'
def toroVersion = '3.7.0.2010003' def toroVersion = '3.7.0.2010003'
implementation "im.ene.toro3:toro:$toroVersion" implementation "im.ene.toro3:toro:$toroVersion"

View File

@ -32,7 +32,11 @@
android:theme="@style/AppTheme" android:theme="@style/AppTheme"
android:usesCleartextTraffic="true" android:usesCleartextTraffic="true"
tools:replace="android:label"> tools:replace="android:label">
<activity android:name=".Activity.GiveAwardActivity" <activity android:name=".Activity.FetchRandomSubredditOrPostActivity"
android:parentActivityName=".Activity.MainActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Activity.GiveAwardActivity"
android:label="@string/give_award_activity_label" android:label="@string/give_award_activity_label"
android:parentActivityName=".Activity.MainActivity" android:parentActivityName=".Activity.MainActivity"
android:theme="@style/AppTheme.Slidable" /> android:theme="@style/AppTheme.Slidable" />

View File

@ -334,6 +334,6 @@ public interface RedditAPI {
@GET("/r/random/comments.json?limit=1&raw_json=1") @GET("/r/random/comments.json?limit=1&raw_json=1")
Call<String> getRandomPost(); 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(); Call<String> getRandomNSFWPost();
} }

View File

@ -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());
}
}

View File

@ -73,6 +73,7 @@ import ml.docilealligator.infinityforreddit.AsyncTask.SwitchToAnonymousAccountAs
import ml.docilealligator.infinityforreddit.BottomSheetFragment.FABMoreOptionsBottomSheetFragment; import ml.docilealligator.infinityforreddit.BottomSheetFragment.FABMoreOptionsBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostLayoutBottomSheetFragment; import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostLayoutBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostTypeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.RandomBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTimeBottomSheetFragment; import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTimeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTypeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; 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, public class MainActivity extends BaseActivity implements SortTypeSelectionCallback,
PostTypeBottomSheetFragment.PostTypeSelectionCallback, PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, PostTypeBottomSheetFragment.PostTypeSelectionCallback, PostLayoutBottomSheetFragment.PostLayoutSelectionCallback,
ActivityToolbarInterface, FABMoreOptionsBottomSheetFragment.FABOptionSelectionCallback { ActivityToolbarInterface, FABMoreOptionsBottomSheetFragment.FABOptionSelectionCallback, RandomBottomSheetFragment.RandomOptionSelectionCallback {
static final String EXTRA_POST_TYPE = "EPT"; static final String EXTRA_POST_TYPE = "EPT";
static final String EXTRA_MESSSAGE_FULLNAME = "ENF"; static final String EXTRA_MESSSAGE_FULLNAME = "ENF";
@ -164,6 +165,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
SubscribedSubredditViewModel subscribedSubredditViewModel; SubscribedSubredditViewModel subscribedSubredditViewModel;
AccountViewModel accountViewModel; AccountViewModel accountViewModel;
@Inject @Inject
@Named("no_oauth")
Retrofit mRetrofit;
@Inject
@Named("oauth") @Named("oauth")
Retrofit mOauthRetrofit; Retrofit mOauthRetrofit;
@Inject @Inject
@ -1218,12 +1222,23 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
break; break;
} }
case FABMoreOptionsBottomSheetFragment.FAB_RANDOM: { 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; 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 { private class SectionsPagerAdapter extends FragmentStateAdapter {
int tabCount; int tabCount;
boolean showFavoriteSubscribedSubreddits; boolean showFavoriteSubscribedSubreddits;

View File

@ -779,7 +779,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
mSwipeRefreshLayout.setRefreshing(false); mSwipeRefreshLayout.setRefreshing(false);
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePost(response.body(), mLocale, new ParsePost.ParsePostListener() { ParsePost.parsePost(response.body(), new ParsePost.ParsePostListener() {
@Override @Override
public void onParsePostSuccess(Post post) { public void onParsePostSuccess(Post post) {
mPost = post; mPost = post;

View File

@ -67,6 +67,7 @@ import ml.docilealligator.infinityforreddit.AsyncTask.SwitchAccountAsyncTask;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.FABMoreOptionsBottomSheetFragment; import ml.docilealligator.infinityforreddit.BottomSheetFragment.FABMoreOptionsBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostLayoutBottomSheetFragment; import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostLayoutBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.BottomSheetFragment.PostTypeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.RandomBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTimeBottomSheetFragment; import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTimeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTypeBottomSheetFragment; import ml.docilealligator.infinityforreddit.BottomSheetFragment.SortTypeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
@ -94,7 +95,8 @@ import retrofit2.Retrofit;
public class ViewSubredditDetailActivity extends BaseActivity implements SortTypeSelectionCallback, public class ViewSubredditDetailActivity extends BaseActivity implements SortTypeSelectionCallback,
PostTypeBottomSheetFragment.PostTypeSelectionCallback, PostLayoutBottomSheetFragment.PostLayoutSelectionCallback, 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_SUBREDDIT_NAME_KEY = "ESN";
public static final String EXTRA_MESSAGE_FULLNAME = "ENF"; public static final String EXTRA_MESSAGE_FULLNAME = "ENF";
@ -427,7 +429,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
if (subredditData.isNSFW()) { if (subredditData.isNSFW()) {
if (nsfwWarningBuilder == null 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) nsfwWarningBuilder = new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
.setTitle(R.string.warning) .setTitle(R.string.warning)
.setMessage(R.string.this_is_a_nsfw_subreddit) .setMessage(R.string.this_is_a_nsfw_subreddit)
@ -1124,8 +1126,23 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
.show(); .show();
break; 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 { private class SectionsPagerAdapter extends FragmentStateAdapter {

View File

@ -13,6 +13,7 @@ import ml.docilealligator.infinityforreddit.Activity.CustomizeThemeActivity;
import ml.docilealligator.infinityforreddit.Activity.EditCommentActivity; import ml.docilealligator.infinityforreddit.Activity.EditCommentActivity;
import ml.docilealligator.infinityforreddit.Activity.EditMultiRedditActivity; import ml.docilealligator.infinityforreddit.Activity.EditMultiRedditActivity;
import ml.docilealligator.infinityforreddit.Activity.EditPostActivity; import ml.docilealligator.infinityforreddit.Activity.EditPostActivity;
import ml.docilealligator.infinityforreddit.Activity.FetchRandomSubredditOrPostActivity;
import ml.docilealligator.infinityforreddit.Activity.FilteredThingActivity; import ml.docilealligator.infinityforreddit.Activity.FilteredThingActivity;
import ml.docilealligator.infinityforreddit.Activity.GiveAwardActivity; import ml.docilealligator.infinityforreddit.Activity.GiveAwardActivity;
import ml.docilealligator.infinityforreddit.Activity.InboxActivity; import ml.docilealligator.infinityforreddit.Activity.InboxActivity;
@ -218,4 +219,6 @@ public interface AppComponent {
void inject(GiveAwardActivity giveAwardActivity); void inject(GiveAwardActivity giveAwardActivity);
void inject(TranslationFragment translationFragment); void inject(TranslationFragment translationFragment);
void inject(FetchRandomSubredditOrPostActivity fetchRandomSubredditOrPostActivity);
} }

View File

@ -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);
}
}

View File

@ -166,9 +166,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun
if (hasSubreddit) { if (hasSubreddit) {
mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE); mFetchSubredditListingInfoLinearLayout.setVisibility(View.GONE);
} else { } else {
mFetchSubredditListingInfoLinearLayout.setOnClickListener(view -> { mFetchSubredditListingInfoLinearLayout.setOnClickListener(null);
//Do nothing
});
showErrorView(R.string.no_subreddits); showErrorView(R.string.no_subreddits);
} }
}); });

View File

@ -1,5 +1,7 @@
package ml.docilealligator.infinityforreddit.Post; package ml.docilealligator.infinityforreddit.Post;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import java.util.Locale; import java.util.Locale;
@ -23,7 +25,7 @@ public class FetchPost {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { ParsePost.parsePost(response.body(), new ParsePost.ParsePostListener() {
@Override @Override
public void onParsePostSuccess(Post post) { public void onParsePostSuccess(Post post) {
fetchPostListener.fetchPostSuccess(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 { public interface FetchPostListener {
void fetchPostSuccess(Post post); void fetchPostSuccess(Post post);
void fetchPostFailed(); void fetchPostFailed();
} }
public interface FetchRandomPostListener {
void fetchRandomPostSuccess(String postId, String subredditName);
void fetchRandomPostFailed();
}
} }

View File

@ -10,7 +10,6 @@ import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Locale;
import ml.docilealligator.infinityforreddit.Fragment.PostFragment; import ml.docilealligator.infinityforreddit.Fragment.PostFragment;
import ml.docilealligator.infinityforreddit.Utils.JSONUtils; import ml.docilealligator.infinityforreddit.Utils.JSONUtils;
@ -21,13 +20,13 @@ import ml.docilealligator.infinityforreddit.Utils.Utils;
*/ */
public class ParsePost { 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) { 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) { public static void parsePost(String response, ParsePostListener parsePostListener) {
new ParsePostDataAsyncTask(response, locale, true, parsePostListener).execute(); new ParsePostDataAsyncTask(response, true, parsePostListener).execute();
} }
private static Post parseBasicData(JSONObject data) throws JSONException { private static Post parseBasicData(JSONObject data) throws JSONException {
@ -456,9 +455,13 @@ public class ParsePost {
void onParsePostFail(); void onParsePostFail();
} }
public interface ParseRandomPostListener {
void onParseRandomPostSuccess(String postId, String subredditName);
void onParseRandomPostFailed();
}
private static class ParsePostDataAsyncTask extends AsyncTask<Void, Void, Void> { private static class ParsePostDataAsyncTask extends AsyncTask<Void, Void, Void> {
private JSONArray allData; private JSONArray allData;
private Locale locale;
private int nPosts; private int nPosts;
private int filter; private int filter;
private boolean nsfw; private boolean nsfw;
@ -469,14 +472,13 @@ public class ParsePost {
private String lastItem; private String lastItem;
private boolean parseFailed; 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) { ParsePostsListingListener parsePostsListingListener) {
this.parsePostsListingListener = parsePostsListingListener; this.parsePostsListingListener = parsePostsListingListener;
try { try {
JSONObject jsonResponse = new JSONObject(response); JSONObject jsonResponse = new JSONObject(response);
allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY); lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
this.locale = locale;
this.nPosts = nPosts; this.nPosts = nPosts;
this.filter = filter; this.filter = filter;
this.nsfw = nsfw; this.nsfw = nsfw;
@ -488,12 +490,11 @@ public class ParsePost {
} }
} }
ParsePostDataAsyncTask(String response, Locale locale, boolean nsfw, ParsePostDataAsyncTask(String response, boolean nsfw,
ParsePostListener parsePostListener) { ParsePostListener parsePostListener) {
this.parsePostListener = parsePostListener; this.parsePostListener = parsePostListener;
try { try {
allData = new JSONArray(response).getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY); allData = new JSONArray(response).getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
this.locale = locale;
this.nsfw = nsfw; this.nsfw = nsfw;
parseFailed = false; parseFailed = false;
} catch (JSONException e) { } 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);
}
}
}
} }

View File

@ -247,7 +247,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -310,7 +310,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -370,7 +370,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -444,7 +444,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -505,7 +505,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -576,7 +576,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -659,7 +659,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -750,7 +750,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -810,7 +810,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {
@ -881,7 +881,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePosts(response.body(), locale, -1, filter, nsfw, ParsePost.parsePosts(response.body(), -1, filter, nsfw,
new ParsePost.ParsePostsListingListener() { new ParsePost.ParsePostsListingListener() {
@Override @Override
public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) {

View File

@ -332,7 +332,7 @@ public class SubmitPost {
@Override @Override
public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
ParsePost.parsePost(response.body(), locale, new ParsePost.ParsePostListener() { ParsePost.parsePost(response.body(), new ParsePost.ParsePostListener() {
@Override @Override
public void onParsePostSuccess(Post post) { public void onParsePostSuccess(Post post) {
submitPostListener.submitSuccessful(post); submitPostListener.submitSuccessful(post);

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relative_layout_fetch_random_subreddit_or_post_activity"
tools:context=".Activity.FetchRandomSubredditOrPostActivity">
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/lottie_animation_view_fetch_random_subreddit_or_post_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="@raw/random_subreddit_or_post" />
</RelativeLayout>

View File

@ -0,0 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".BottomSheetFragment.RandomBottomSheetFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/random_subreddit_text_view_random_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingStart="32dp"
android:paddingEnd="32dp"
android:text="@string/random_subreddit"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground" />
<TextView
android:id="@+id/random_post_text_view_random_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingStart="32dp"
android:paddingEnd="32dp"
android:text="@string/random_post"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground" />
<TextView
android:id="@+id/random_nsfw_subreddit_text_view_random_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingStart="32dp"
android:paddingEnd="32dp"
android:text="@string/random_nsfw_subreddit"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground" />
<TextView
android:id="@+id/random_nsfw_post_text_view_random_bottom_sheet_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingStart="32dp"
android:paddingEnd="32dp"
android:text="@string/random_nsfw_post"
android:textColor="?attr/primaryTextColor"
android:textSize="?attr/font_default"
android:fontFamily="?attr/font_family"
android:clickable="true"
android:focusable="true"
android:background="?attr/selectableItemBackground" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>

File diff suppressed because one or more lines are too long

View File

@ -928,5 +928,6 @@
<string name="random_nsfw_subreddit">Random NSFW Subreddit</string> <string name="random_nsfw_subreddit">Random NSFW Subreddit</string>
<string name="random_post">Random Post</string> <string name="random_post">Random Post</string>
<string name="random_nsfw_post">Random NSFW post</string> <string name="random_nsfw_post">Random NSFW post</string>
<string name="fetch_random_thing_failed">Try again later</string>
</resources> </resources>