From 0cab9663d4292a01f6f43e5b7419459105680da0 Mon Sep 17 00:00:00 2001 From: Abi0711 <87072811+Abi0711@users.noreply.github.com> Date: Wed, 2 Nov 2022 23:18:00 +1100 Subject: Rules can now show in private subreddits (#1181) * Added oauth to fetching rules * Added getRulesOauth for users that are logged in Co-authored-by: Abi --- .../ml/docilealligator/infinityforreddit/FetchRules.java | 5 +++-- .../infinityforreddit/activities/ReportActivity.java | 2 +- .../infinityforreddit/activities/RulesActivity.java | 16 ++++++++++++++-- .../infinityforreddit/apis/RedditAPI.java | 3 +++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java index e1f9c5e7..1a7736c4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java @@ -12,6 +12,7 @@ import java.util.ArrayList; import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.apis.RedditAPI; +import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.Utils; import retrofit2.Call; @@ -25,10 +26,10 @@ public class FetchRules { void failed(); } - public static void fetchRules(Executor executor, Handler handler, Retrofit retrofit, String subredditName, + public static void fetchRules(Executor executor, Handler handler, Retrofit retrofit, String accessToken, String subredditName, FetchRulesListener fetchRulesListener) { RedditAPI api = retrofit.create(RedditAPI.class); - Call rulesCall = api.getRules(subredditName); + Call rulesCall = accessToken == null ? api.getRules(subredditName) : api.getRulesOauth(APIUtils.getOAuthHeader(accessToken), subredditName); rulesCall.enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java index 91ca0686..f3265367 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java @@ -119,7 +119,7 @@ public class ReportActivity extends BaseActivity { recyclerView.setAdapter(mAdapter); if (rulesReasons == null) { - FetchRules.fetchRules(mExecutor, new Handler(), mRetrofit, mSubredditName, new FetchRules.FetchRulesListener() { + FetchRules.fetchRules(mExecutor, new Handler(), mAccessToken == null ? mRetrofit : mOauthRetrofit, mAccessToken, mSubredditName, new FetchRules.FetchRulesListener() { @Override public void success(ArrayList rules) { mAdapter.setRules(ReportReason.convertRulesToReasons(rules)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RulesActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RulesActivity.java index 21bf5f85..a33bdde0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RulesActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RulesActivity.java @@ -11,6 +11,7 @@ import android.view.Window; import android.view.WindowManager; import android.widget.ProgressBar; import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; @@ -64,6 +65,15 @@ public class RulesActivity extends BaseActivity { @Inject @Named("no_oauth") Retrofit mRetrofit; + @Inject + @Named("oauth") + Retrofit mOauthRetrofit; + + private String mAccessToken; + @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject @Named("default") SharedPreferences mSharedPreferences; @@ -114,6 +124,7 @@ public class RulesActivity extends BaseActivity { } } } + mAccessToken = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, null); appBarLayout.setBackgroundColor(mCustomThemeWrapper.getColorPrimary()); setSupportActionBar(toolbar); @@ -124,7 +135,7 @@ public class RulesActivity extends BaseActivity { mAdapter = new RulesRecyclerViewAdapter(this, mCustomThemeWrapper, slidrInterface); recyclerView.setAdapter(mAdapter); - FetchRules.fetchRules(mExecutor, new Handler(), mRetrofit, mSubredditName, new FetchRules.FetchRulesListener() { + FetchRules.fetchRules(mExecutor, new Handler(), mAccessToken == null ? mRetrofit : mOauthRetrofit, mAccessToken, mSubredditName, new FetchRules.FetchRulesListener() { @Override public void success(ArrayList rules) { progressBar.setVisibility(View.GONE); @@ -142,6 +153,7 @@ public class RulesActivity extends BaseActivity { displayError(); } }); + } @Override @@ -172,7 +184,7 @@ public class RulesActivity extends BaseActivity { errorTextView.setOnClickListener(view -> { progressBar.setVisibility(View.VISIBLE); errorTextView.setVisibility(View.GONE); - FetchRules.fetchRules(mExecutor, new Handler(), mRetrofit, mSubredditName, new FetchRules.FetchRulesListener() { + FetchRules.fetchRules(mExecutor, new Handler(), mAccessToken == null ? mRetrofit : mOauthRetrofit, mAccessToken, mSubredditName, new FetchRules.FetchRulesListener() { @Override public void success(ArrayList rules) { progressBar.setVisibility(View.GONE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java index 2c75a17c..9cb8246b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java @@ -131,6 +131,9 @@ public interface RedditAPI { @GET("/r/{subredditName}/about/rules.json?raw_json=1") Call getRules(@Path("subredditName") String subredditName); + @GET("/r/{subredditName}/about/rules.json?raw_json=1") + Call getRulesOauth(@HeaderMap Map headers, @Path("subredditName") String subredditName); + @GET("/comments/{id}/placeholder/{singleCommentId}.json?raw_json=1") Call getPostAndCommentsSingleThreadByIdOauth(@Path("id") String id, @Path("singleCommentId") String singleCommentId, @Query("sort") String sortType, @Query("context") String contextNumber, -- cgit v1.2.3