diff options
author | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-01-23 23:28:01 +0000 |
---|---|---|
committer | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-01-23 23:28:01 +0000 |
commit | ff1609cd00039ae6064fbc6821ab39ec815e00ce (patch) | |
tree | 3653397fcde7e5b56d127b6c9e39f727acbe28c7 /app/src/main/java | |
parent | b9397e87ad28be35a0f242149f9567a53df2e3e5 (diff) | |
download | infinity-for-reddit-ff1609cd00039ae6064fbc6821ab39ec815e00ce.tar infinity-for-reddit-ff1609cd00039ae6064fbc6821ab39ec815e00ce.tar.gz infinity-for-reddit-ff1609cd00039ae6064fbc6821ab39ec815e00ce.tar.bz2 infinity-for-reddit-ff1609cd00039ae6064fbc6821ab39ec815e00ce.tar.lz infinity-for-reddit-ff1609cd00039ae6064fbc6821ab39ec815e00ce.tar.xz infinity-for-reddit-ff1609cd00039ae6064fbc6821ab39ec815e00ce.tar.zst infinity-for-reddit-ff1609cd00039ae6064fbc6821ab39ec815e00ce.zip |
Fix anonymous mode not loading anything.
Diffstat (limited to '')
55 files changed, 522 insertions, 535 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java index f8da226a..43003d36 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java @@ -48,33 +48,18 @@ class AccessTokenAuthenticator implements Authenticator { synchronized (this) { Account account = mRedditDataRoomDatabase.accountDao().getCurrentAccount(); if (account == null) { - //Anonymous mode - if (!mRedditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { - mRedditDataRoomDatabase.accountDao().insert(Account.getAnonymousAccount()); - } - String accessTokenFromSharedPreference = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, ""); - if (accessToken.equals(accessTokenFromSharedPreference) || accessToken.equals("null")) { - String newAccessToken = getApplicationOnlyAccessToken(); - if (!newAccessToken.equals("")) { - return response.request().newBuilder().headers(Headers.of(APIUtils.getOAuthHeader(newAccessToken))).build(); - } else { - return null; - } + return null; + } + String accessTokenFromDatabase = account.getAccessToken(); + if (accessToken.equals(accessTokenFromDatabase)) { + String newAccessToken = refreshAccessToken(account); + if (!newAccessToken.equals("")) { + return response.request().newBuilder().headers(Headers.of(APIUtils.getOAuthHeader(newAccessToken))).build(); } else { - return response.request().newBuilder().headers(Headers.of(APIUtils.getOAuthHeader(accessTokenFromSharedPreference))).build(); + return null; } } else { - String accessTokenFromDatabase = account.getAccessToken(); - if (accessToken.equals(accessTokenFromDatabase)) { - String newAccessToken = refreshAccessToken(account); - if (!newAccessToken.equals("")) { - return response.request().newBuilder().headers(Headers.of(APIUtils.getOAuthHeader(newAccessToken))).build(); - } else { - return null; - } - } else { - return response.request().newBuilder().headers(Headers.of(APIUtils.getOAuthHeader(accessTokenFromDatabase))).build(); - } + return response.request().newBuilder().headers(Headers.of(APIUtils.getOAuthHeader(accessTokenFromDatabase))).build(); } } } @@ -115,34 +100,4 @@ class AccessTokenAuthenticator implements Authenticator { return ""; } - - //For anonymous mode - private String getApplicationOnlyAccessToken() { - RedditAPI api = mRetrofit.create(RedditAPI.class); - - Map<String, String> params = new HashMap<>(); - params.put(APIUtils.GRANT_TYPE_KEY, APIUtils.GRANT_TYPE_INSTALLED_CLIENT); - params.put(APIUtils.DEVICE_ID_KEY, APIUtils.DEVICE_ID); - - Call<String> accessTokenCall = api.getAccessToken(APIUtils.getApplicationOnlyBasicAuthHeader(), params); - try { - retrofit2.Response<String> response = accessTokenCall.execute(); - if (response.isSuccessful() && response.body() != null) { - JSONObject jsonObject = new JSONObject(response.body()); - String newAccessToken = jsonObject.getString(APIUtils.ACCESS_TOKEN_KEY); - mRedditDataRoomDatabase.accountDao().updateAccessToken(Account.ANONYMOUS_ACCOUNT, newAccessToken); - mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.APPLICATION_ONLY_ACCESS_TOKEN, newAccessToken).apply(); - if (mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, Account.ANONYMOUS_ACCOUNT).equals(Account.ANONYMOUS_ACCOUNT)) { - mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, newAccessToken).apply(); - } - - return newAccessToken; - } - return ""; - } catch (IOException | JSONException e) { - e.printStackTrace(); - } - - return ""; - } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ApplicationOnlyAccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ApplicationOnlyAccessTokenAuthenticator.java deleted file mode 100644 index 970b81fd..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ApplicationOnlyAccessTokenAuthenticator.java +++ /dev/null @@ -1,97 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.content.SharedPreferences; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import ml.docilealligator.infinityforreddit.account.Account; -import ml.docilealligator.infinityforreddit.apis.RedditAPI; -import ml.docilealligator.infinityforreddit.utils.APIUtils; -import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; -import okhttp3.Authenticator; -import okhttp3.Headers; -import okhttp3.Request; -import okhttp3.Response; -import okhttp3.Route; -import retrofit2.Call; -import retrofit2.Retrofit; - -class ApplicationOnlyAccessTokenAuthenticator implements Authenticator { - private final Retrofit mRetrofit; - private final RedditDataRoomDatabase mRedditDataRoomDatabase; - private final SharedPreferences mCurrentAccountSharedPreferences; - - ApplicationOnlyAccessTokenAuthenticator(Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase, SharedPreferences currentAccountSharedPreferences) { - mRetrofit = retrofit; - mRedditDataRoomDatabase = accountRoomDatabase; - mCurrentAccountSharedPreferences = currentAccountSharedPreferences; - } - - @Nullable - @Override - public Request authenticate(Route route, @NonNull Response response) { - if (response.code() == 401) { - String accessTokenHeader = response.request().header(APIUtils.AUTHORIZATION_KEY); - if (accessTokenHeader == null) { - return null; - } - - String accessToken = accessTokenHeader.substring(APIUtils.AUTHORIZATION_BASE.length()); - synchronized (this) { - if (!mRedditDataRoomDatabase.accountDao().isAnonymousAccountInserted()) { - mRedditDataRoomDatabase.accountDao().insert(Account.getAnonymousAccount()); - } - String accessTokenFromSharedPreference = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCESS_TOKEN, ""); - if (accessToken.equals(accessTokenFromSharedPreference) || accessToken.equals("null")) { - String newAccessToken = getApplicationOnlyAccessToken(); - if (!newAccessToken.equals("")) { - return response.request().newBuilder().headers(Headers.of(APIUtils.getOAuthHeader(newAccessToken))).build(); - } else { - return null; - } - } else { - return response.request().newBuilder().headers(Headers.of(APIUtils.getOAuthHeader(accessTokenFromSharedPreference))).build(); - } - } - } - return null; - } - - //For anonymous mode - private String getApplicationOnlyAccessToken() { - RedditAPI api = mRetrofit.create(RedditAPI.class); - - Map<String, String> params = new HashMap<>(); - params.put(APIUtils.GRANT_TYPE_KEY, APIUtils.GRANT_TYPE_INSTALLED_CLIENT); - params.put(APIUtils.DEVICE_ID_KEY, APIUtils.DEVICE_ID); - - Call<String> accessTokenCall = api.getAccessToken(APIUtils.getApplicationOnlyBasicAuthHeader(), params); - try { - retrofit2.Response<String> response = accessTokenCall.execute(); - if (response.isSuccessful() && response.body() != null) { - JSONObject jsonObject = new JSONObject(response.body()); - String newAccessToken = jsonObject.getString(APIUtils.ACCESS_TOKEN_KEY); - mRedditDataRoomDatabase.accountDao().updateAccessToken(Account.ANONYMOUS_ACCOUNT, newAccessToken); - mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.APPLICATION_ONLY_ACCESS_TOKEN, newAccessToken).apply(); - if (mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, Account.ANONYMOUS_ACCOUNT).equals(Account.ANONYMOUS_ACCOUNT)) { - mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, newAccessToken).apply(); - } - - return newAccessToken; - } - return ""; - } catch (IOException | JSONException e) { - e.printStackTrace(); - } - - return ""; - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java index 96debfbb..a914bb19 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 org.json.JSONObject; import java.util.ArrayList; import java.util.concurrent.Executor; +import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.JSONUtils; @@ -28,10 +29,12 @@ public class FetchRules { } public static void fetchRules(Executor executor, Handler handler, Retrofit retrofit, @Nullable String accessToken, - String subredditName, + @NonNull String accountName, String subredditName, FetchRulesListener fetchRulesListener) { RedditAPI api = retrofit.create(RedditAPI.class); - Call<String> rulesCall = api.getRulesOauth(APIUtils.getOAuthHeader(accessToken), subredditName); + Call<String> rulesCall = accountName.equals(Account.ANONYMOUS_ACCOUNT) + ? api.getRules(subredditName) + : api.getRulesOauth(APIUtils.getOAuthHeader(accessToken), subredditName); rulesCall.enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java index ffd4dcb6..2c231682 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/NetworkModule.java @@ -12,11 +12,9 @@ import dagger.Provides; import ml.docilealligator.infinityforreddit.apis.StreamableAPI; import ml.docilealligator.infinityforreddit.network.SortTypeConverterFactory; import ml.docilealligator.infinityforreddit.utils.APIUtils; -import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import okhttp3.ConnectionPool; import okhttp3.Interceptor; import okhttp3.OkHttpClient; -import okhttp3.Request; import retrofit2.Retrofit; import retrofit2.adapter.guava.GuavaCallAdapterFactory; import retrofit2.converter.scalars.ScalarsConverterFactory; @@ -70,34 +68,6 @@ abstract class NetworkModule { } @Provides - @Named("application_only_oauth") - static Retrofit provideApplicationOnlyOAuthRetrofit(@Named("base") Retrofit retrofit, - @Named("base") OkHttpClient httpClient, - RedditDataRoomDatabase accountRoomDatabase, - @Named("current_account") SharedPreferences currentAccountSharedPreferences, - ConnectionPool connectionPool) { - return retrofit.newBuilder() - .baseUrl(APIUtils.OAUTH_API_BASE_URI) - .client(httpClient.newBuilder() - .addInterceptor(chain -> { - if (!chain.request().headers().names().contains(APIUtils.AUTHORIZATION_KEY)) { - Request newRequest = chain.request().newBuilder() - .addHeader(APIUtils.AUTHORIZATION_KEY, - APIUtils.AUTHORIZATION_BASE - + currentAccountSharedPreferences.getString(SharedPreferencesUtils.APPLICATION_ONLY_ACCESS_TOKEN, null)) - .build(); - return chain.proceed(newRequest); - } - - return chain.proceed(chain.request()); - }) - .authenticator(new ApplicationOnlyAccessTokenAuthenticator(retrofit, accountRoomDatabase, currentAccountSharedPreferences)) - .connectionPool(connectionPool) - .build()) - .build(); - } - - @Provides @Named("default") @Singleton static OkHttpClient provideOkHttpClient(@Named("base") OkHttpClient httpClient, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FetchRandomSubredditOrPostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FetchRandomSubredditOrPostActivity.java index d6b7a74c..1587d83f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FetchRandomSubredditOrPostActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FetchRandomSubredditOrPostActivity.java @@ -28,8 +28,8 @@ public class FetchRandomSubredditOrPostActivity extends BaseActivity { @BindView(R.id.relative_layout_fetch_random_subreddit_or_post_activity) RelativeLayout relativeLayout; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyOauthRetrofit; + @Named("no_oauth") + Retrofit mRetrofit; @Inject @Named("default") SharedPreferences mSharedPreferences; @@ -49,7 +49,7 @@ public class FetchRandomSubredditOrPostActivity extends BaseActivity { int option = getIntent().getIntExtra(EXTRA_RANDOM_OPTION, RandomBottomSheetFragment.RANDOM_SUBREDDIT); - FetchPost.fetchRandomPost(mExecutor, new Handler(), mApplicationOnlyOauthRetrofit, + FetchPost.fetchRandomPost(mExecutor, new Handler(), mRetrofit, option == RandomBottomSheetFragment.RANDOM_NSFW_SUBREDDIT || option == RandomBottomSheetFragment.RANDOM_NSFW_POST, new FetchPost.FetchRandomPostListener() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java index 56fa12f9..5a4c0311 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LinkResolverActivity.java @@ -62,9 +62,8 @@ public class LinkResolverActivity extends AppCompatActivity { private static final String STREAMABLE_PATTERN = "/\\w+/?"; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyRetrofit; - + @Named("no_oauth") + Retrofit mRetrofit; @Inject @Named("default") SharedPreferences mSharedPreferences; @@ -266,7 +265,7 @@ public class LinkResolverActivity extends AppCompatActivity { startActivity(intent); } else if (uri.getPath().matches(SHARELINK_SUBREDDIT_PATTERN) || uri.getPath().matches(SHARELINK_USER_PATTERN)) { - mApplicationOnlyRetrofit.callFactory().newCall(new Request.Builder().url(uri.toString()).build()).enqueue(new Callback() { + mRetrofit.callFactory().newCall(new Request.Builder().url(uri.toString()).build()).enqueue(new Callback() { @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java index 6d3913e2..4ff943fc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java @@ -141,12 +141,12 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee @BindView(R.id.images_recycler_view_post_gallery_activity) RecyclerView imagesRecyclerView; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyRetrofit; - @Inject @Named("upload_media") Retrofit mUploadMediaRetrofit; @Inject @@ -530,8 +530,8 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee } private void loadSubredditIcon() { - LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, mApplicationOnlyRetrofit, subredditName, - iconImageUrl -> { + LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, + mAccessToken, mAccountName, mOauthRetrofit, mRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java index 5005e407..56629d96 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java @@ -136,12 +136,12 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF @BindView(R.id.image_view_post_image_activity) ImageView imageView; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyRetrofit; - @Inject @Named("upload_media") Retrofit mUploadMediaRetrofit; @Inject @@ -492,8 +492,8 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF } private void loadSubredditIcon() { - LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, mApplicationOnlyRetrofit, subredditName, - iconImageUrl -> { + LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, + mAccessToken, mAccountName, mOauthRetrofit, mRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java index 9c489dd7..effadc26 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostLinkActivity.java @@ -131,9 +131,6 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyRetrofit; - @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -481,8 +478,8 @@ public class PostLinkActivity extends BaseActivity implements FlairBottomSheetFr } private void loadSubredditIcon() { - LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, mApplicationOnlyRetrofit, subredditName, - iconImageUrl -> { + LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, + mAccessToken, mAccountName, mOauthRetrofit, mRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java index 191b55e4..2f98c7de 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostPollActivity.java @@ -154,12 +154,12 @@ public class PostPollActivity extends BaseActivity implements FlairBottomSheetFr @BindView(R.id.option_6_text_input_layout_edit_text_post_poll_activity) TextInputEditText option6TextInputEditText; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyRetrofit; - @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -528,8 +528,8 @@ public class PostPollActivity extends BaseActivity implements FlairBottomSheetFr } private void loadSubredditIcon() { - LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, mApplicationOnlyRetrofit, subredditName, - iconImageUrl -> { + LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, + mAccessToken, mAccountName, mOauthRetrofit, mRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java index c45d1ced..300a34f0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostTextActivity.java @@ -139,12 +139,12 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr @BindView(R.id.markdown_bottom_bar_recycler_view_post_text_activity) RecyclerView markdownBottomBarRecyclerView; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyRetrofit; - @Inject @Named("upload_media") Retrofit mUploadMediaRetrofit; @Inject @@ -485,8 +485,8 @@ public class PostTextActivity extends BaseActivity implements FlairBottomSheetFr } private void loadSubredditIcon() { - LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, mApplicationOnlyRetrofit, subredditName, - iconImageUrl -> { + LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, + mAccessToken, mAccountName, mOauthRetrofit, mRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java index 24796c63..012b9d36 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java @@ -139,12 +139,12 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF @BindView(R.id.player_view_post_video_activity) PlayerView videoPlayerView; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyRetrofit; - @Inject @Named("upload_media") Retrofit mUploadMediaRetrofit; @Inject @@ -511,8 +511,8 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF } private void loadSubredditIcon() { - LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, mApplicationOnlyRetrofit, subredditName, - iconImageUrl -> { + LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, + mAccessToken, mAccountName, mOauthRetrofit, mRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; 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 a0c94f20..7ecc927c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java @@ -54,6 +54,9 @@ public class ReportActivity extends BaseActivity { @BindView(R.id.recycler_view_report_activity) RecyclerView recyclerView; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject @@ -119,7 +122,9 @@ public class ReportActivity extends BaseActivity { recyclerView.setAdapter(mAdapter); if (rulesReasons == null) { - FetchRules.fetchRules(mExecutor, new Handler(), mOauthRetrofit, mAccessToken, mSubredditName, new FetchRules.FetchRulesListener() { + FetchRules.fetchRules(mExecutor, new Handler(), + mAccountName.equals(Account.ANONYMOUS_ACCOUNT) ? mRetrofit : mOauthRetrofit, + mAccessToken, mAccountName, mSubredditName, new FetchRules.FetchRulesListener() { @Override public void success(ArrayList<Rule> 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 d5e89738..081af098 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RulesActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RulesActivity.java @@ -65,6 +65,9 @@ public class RulesActivity extends BaseActivity { @BindView(R.id.error_text_view_rules_activity) TextView errorTextView; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject @@ -134,8 +137,9 @@ public class RulesActivity extends BaseActivity { mAdapter = new RulesRecyclerViewAdapter(this, mCustomThemeWrapper, sliderPanel, mSubredditName); recyclerView.setAdapter(mAdapter); - FetchRules.fetchRules(mExecutor, new Handler(), mOauthRetrofit, mAccessToken, - mSubredditName, new FetchRules.FetchRulesListener() { + FetchRules.fetchRules(mExecutor, new Handler(), + mAccountName.equals(Account.ANONYMOUS_ACCOUNT) ? mRetrofit : mOauthRetrofit, + mAccessToken, mAccountName, mSubredditName, new FetchRules.FetchRulesListener() { @Override public void success(ArrayList<Rule> rules) { progressBar.setVisibility(View.GONE); @@ -183,7 +187,9 @@ public class RulesActivity extends BaseActivity { errorTextView.setOnClickListener(view -> { progressBar.setVisibility(View.VISIBLE); errorTextView.setVisibility(View.GONE); - FetchRules.fetchRules(mExecutor, new Handler(), mOauthRetrofit, mAccessToken, mSubredditName, new FetchRules.FetchRulesListener() { + FetchRules.fetchRules(mExecutor, new Handler(), + mAccountName.equals(Account.ANONYMOUS_ACCOUNT) ? mRetrofit : mOauthRetrofit, + mAccessToken, mAccountName, mSubredditName, new FetchRules.FetchRulesListener() { @Override public void success(ArrayList<Rule> rules) { progressBar.setVisibility(View.GONE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubmitCrosspostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubmitCrosspostActivity.java index 496c9c44..6c5bcea7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubmitCrosspostActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubmitCrosspostActivity.java @@ -136,12 +136,12 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom @BindView(R.id.play_button_image_view_submit_crosspost_activity) ImageView playButton; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyRetrofit; - @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -500,8 +500,8 @@ public class SubmitCrosspostActivity extends BaseActivity implements FlairBottom } private void loadSubredditIcon() { - LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, mApplicationOnlyRetrofit, subredditName, - iconImageUrl -> { + LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditName, + mAccessToken, mAccountName, mOauthRetrofit, mRetrofit, iconImageUrl -> { iconUrl = iconImageUrl; displaySubredditIcon(); loadSubredditIconSuccessful = true; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/TrendingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/TrendingActivity.java index 79983a1d..a0b568b5 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/TrendingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/TrendingActivity.java @@ -83,6 +83,9 @@ public class TrendingActivity extends BaseActivity { @BindView(R.id.fetch_trending_search_text_view_trending_activity) TextView errorTextView; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject @@ -205,8 +208,12 @@ public class TrendingActivity extends BaseActivity { trendingSearches = null; adapter.setTrendingSearches(null); Handler handler = new Handler(); - Call<String> trendingCall = mOauthRetrofit.create(RedditAPI.class) - .getTrendingSearchesOauth(APIUtils.getOAuthHeader(mAccessToken)); + Call<String> trendingCall; + if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { + trendingCall = mRetrofit.create(RedditAPI.class).getTrendingSearches(); + } else { + trendingCall = mOauthRetrofit.create(RedditAPI.class).getTrendingSearchesOauth(APIUtils.getOAuthHeader(mAccessToken)); + } trendingCall.enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java index 9bf59eac..85751042 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -127,6 +127,9 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele @BindView(R.id.close_search_panel_image_view_view_post_detail_activity) ImageView closeSearchPanelImageView; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject @@ -507,18 +510,21 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele if (postType != HistoryPostPagingSource.TYPE_READ_POSTS) { mExecutor.execute(() -> { - RedditAPI api = mOauthRetrofit.create(RedditAPI.class); + RedditAPI api = (mAccountName.equals(Account.ANONYMOUS_ACCOUNT) ? mRetrofit : mOauthRetrofit).create(RedditAPI.class); Call<String> call; String afterKey = posts.isEmpty() ? null : posts.get(posts.size() - 1).getFullName(); switch (postType) { case PostPagingSource.TYPE_SUBREDDIT: - call = api.getSubredditBestPostsOauth(subredditName, sortType, - sortTime, afterKey, APIUtils.getOAuthHeader(mAccessToken)); + if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { + call = api.getSubredditBestPosts(subredditName, sortType, sortTime, afterKey); + } else { + call = api.getSubredditBestPostsOauth(subredditName, sortType, + sortTime, afterKey, APIUtils.getOAuthHeader(mAccessToken)); + } break; case PostPagingSource.TYPE_USER: if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { - call = api.getUserPostsOauth(username, PostPagingSource.USER_WHERE_SUBMITTED, - afterKey, sortType, sortTime, APIUtils.getOAuthHeader(mAccessToken)); + call = api.getUserPosts(username, afterKey, sortType, sortTime); } else { call = api.getUserPostsOauth(username, userWhere, afterKey, sortType, sortTime, APIUtils.getOAuthHeader(mAccessToken)); @@ -526,22 +532,35 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele break; case PostPagingSource.TYPE_SEARCH: if (subredditName == null) { - call = api.searchPostsOauth(query, afterKey, sortType, - sortTime, trendingSource, APIUtils.getOAuthHeader(mAccessToken)); + if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { + call = api.searchPosts(query, afterKey, sortType, sortTime, + trendingSource); + } else { + call = api.searchPostsOauth(query, afterKey, sortType, + sortTime, trendingSource, APIUtils.getOAuthHeader(mAccessToken)); + } } else { - call = api.searchPostsInSpecificSubredditOauth(subredditName, query, - sortType, sortTime, afterKey, - APIUtils.getOAuthHeader(mAccessToken)); + if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { + call = api.searchPostsInSpecificSubreddit(subredditName, query, + sortType, sortTime, afterKey); + } else { + call = api.searchPostsInSpecificSubredditOauth(subredditName, query, + sortType, sortTime, afterKey, + APIUtils.getOAuthHeader(mAccessToken)); + } } break; case PostPagingSource.TYPE_MULTI_REDDIT: - call = api.getMultiRedditPostsOauth(multiPath, afterKey, - sortTime, APIUtils.getOAuthHeader(mAccessToken)); + if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { + call = api.getMultiRedditPosts(multiPath, afterKey, sortTime); + } else { + call = api.getMultiRedditPostsOauth(multiPath, afterKey, + sortTime, APIUtils.getOAuthHeader(mAccessToken)); + } break; case PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE: //case PostPagingSource.TYPE_ANONYMOUS_MULTIREDDIT - call = api.getSubredditBestPostsOauth(subredditName, sortType, sortTime, afterKey, - APIUtils.getOAuthHeader(mAccessToken)); + call = api.getSubredditBestPosts(subredditName, sortType, sortTime, afterKey); break; default: call = api.getBestPosts(sortType, sortTime, afterKey, @@ -609,7 +628,7 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele } }); } else { - mExecutor.execute((Runnable) () -> { + mExecutor.execute(() -> { long lastItem = 0; if (!posts.isEmpty()) { lastItem = mRedditDataRoomDatabase.readPostDao().getReadPost(posts.get(posts.size() - 1).getId()).getTime(); @@ -623,8 +642,12 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele ids.deleteCharAt(ids.length() - 1); } - Call<String> historyPosts = mOauthRetrofit.create(RedditAPI.class).getInfoOauth(ids.toString(), - APIUtils.getOAuthHeader(mAccessToken)); + Call<String> historyPosts; + if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { + historyPosts = mOauthRetrofit.create(RedditAPI.class).getInfoOauth(ids.toString(), APIUtils.getOAuthHeader(mAccessToken)); + } else { + historyPosts = mRetrofit.create(RedditAPI.class).getInfo(ids.toString()); + } try { Response<String> response = historyPosts.execute(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java index 354e589d..3b338d08 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java @@ -74,12 +74,12 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit @BindView(R.id.edit_text_wrapper_linear_layout_view_private_messages_activity) LinearLayout mEditTextLinearLayout; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyOauthRetrofit; - @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -249,7 +249,7 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit mProvideUserAvatarCallbacks.add(provideUserAvatarCallback); if (!isLoadingUserAvatar) { LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, - username, mApplicationOnlyOauthRetrofit, iconImageUrl -> { + username, mRetrofit, iconImageUrl -> { isLoadingUserAvatar = false; mUserAvatar = iconImageUrl == null ? "" : iconImageUrl; for (ProvideUserAvatarCallback provideUserAvatarCallbackInArrayList : mProvideUserAvatarCallbacks) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index 6633b7f0..3f8c21cc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -174,12 +174,12 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp @BindView(R.id.description_text_view_view_subreddit_detail_activity) TextView descriptionTextView; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyOauthRetrofit; - @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -582,7 +582,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp private void fetchSubredditData() { if (!mFetchSubredditInfoSuccess) { - FetchSubredditData.fetchSubredditData(mOauthRetrofit, subredditName, mAccessToken, new FetchSubredditData.FetchSubredditDataListener() { + FetchSubredditData.fetchSubredditData(mOauthRetrofit, mRetrofit, subredditName, mAccessToken, new FetchSubredditData.FetchSubredditDataListener() { @Override public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { mNCurrentOnlineSubscribers = nCurrentOnlineSubscribers; @@ -959,7 +959,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp subscriptionReady = false; if (getResources().getString(R.string.subscribe).contentEquals(subscribeSubredditChip.getText())) { SubredditSubscription.anonymousSubscribeToSubreddit(mExecutor, new Handler(), - mApplicationOnlyOauthRetrofit, mRedditDataRoomDatabase, subredditName, + mRetrofit, mRedditDataRoomDatabase, subredditName, new SubredditSubscription.SubredditSubscriptionListener() { @Override public void onSubredditSubscriptionSuccess() { @@ -1000,7 +1000,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp subscriptionReady = false; if (getResources().getString(R.string.subscribe).contentEquals(subscribeSubredditChip.getText())) { SubredditSubscription.subscribeToSubreddit(mExecutor, new Handler(), mOauthRetrofit, - mAccessToken, subredditName, mAccountName, mRedditDataRoomDatabase, + mRetrofit, mAccessToken, subredditName, mAccountName, mRedditDataRoomDatabase, new SubredditSubscription.SubredditSubscriptionListener() { @Override public void onSubredditSubscriptionSuccess() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index 70959ace..e57881f1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -168,12 +168,12 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele @BindView(R.id.description_text_view_view_user_detail_activity) TextView descriptionTextView; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyOauthRetrofit; - @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -442,7 +442,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele subscriptionReady = false; if (resources.getString(R.string.follow).contentEquals(subscribeUserChip.getText())) { if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { - UserFollowing.anonymousFollowUser(mExecutor, new Handler(), mApplicationOnlyOauthRetrofit, + UserFollowing.anonymousFollowUser(mExecutor, new Handler(), mRetrofit, username, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { @Override public void onUserFollowingSuccess() { @@ -459,7 +459,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele } }); } else { - UserFollowing.followUser(mOauthRetrofit, mApplicationOnlyOauthRetrofit, mAccessToken, + UserFollowing.followUser(mOauthRetrofit, mRetrofit, mAccessToken, username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { @Override public void onUserFollowingSuccess() { @@ -494,7 +494,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele } }); } else { - UserFollowing.unfollowUser(mOauthRetrofit, mApplicationOnlyOauthRetrofit, mAccessToken, + UserFollowing.unfollowUser(mOauthRetrofit, mRetrofit, mAccessToken, username, mAccountName, mRedditDataRoomDatabase, new UserFollowing.UserFollowingListener() { @Override public void onUserFollowingSuccess() { @@ -1064,7 +1064,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele private void fetchUserInfo() { if (!mFetchUserInfoSuccess) { - FetchUserData.fetchUserData(mApplicationOnlyOauthRetrofit, username, new FetchUserData.FetchUserDataListener() { + FetchUserData.fetchUserData(mRetrofit, username, new FetchUserData.FetchUserDataListener() { @Override public void onFetchUserDataSuccess(UserData userData, int inboxCount) { new ViewUserDetailActivity.InsertUserDataAsyncTask(mRedditDataRoomDatabase.userDao(), userData, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java index 38ca2f2f..37d3cda9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewVideoActivity.java @@ -92,6 +92,7 @@ import ml.docilealligator.infinityforreddit.FetchStreamableVideo; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.StreamableVideo; +import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.apis.StreamableAPI; import ml.docilealligator.infinityforreddit.apis.VReddIt; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PlaybackSpeedBottomSheetFragment; @@ -197,19 +198,19 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe private boolean useBottomAppBar; @Inject - @Named("application_only_oauth") - Retrofit applicationOnlyOauthRetrofit; + @Named("no_oauth") + Retrofit mRetrofit; @Inject @Named("redgifs") - Retrofit redgifsRetrofit; + Retrofit mRedgifsRetrofit; @Inject @Named("vReddIt") - Retrofit vReddItRetrofit; + Retrofit mVReddItRetrofit; @Inject - Provider<StreamableAPI> streamableApiProvider; + Provider<StreamableAPI> mStreamableApiProvider; @Inject @Named("default") @@ -711,7 +712,7 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe private void loadRedgifsVideo(String redgifsId, Bundle savedInstanceState) { progressBar.setVisibility(View.VISIBLE); - FetchRedgifsVideoLinks.fetchRedgifsVideoLinks(mExecutor, new Handler(), redgifsRetrofit, + FetchRedgifsVideoLinks.fetchRedgifsVideoLinks(mExecutor, new Handler(), mRedgifsRetrofit, mCurrentAccountSharedPreferences, redgifsId, new FetchRedgifsVideoLinks.FetchRedgifsVideoLinksListener() { @Override public void success(String webm, String mp4) { @@ -735,7 +736,7 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe private void loadVReddItVideo(Bundle savedInstanceState) { progressBar.setVisibility(View.VISIBLE); - vReddItRetrofit.create(VReddIt.class).getRedirectUrl(getIntent().getStringExtra(EXTRA_V_REDD_IT_URL)).enqueue(new Callback<>() { + mVReddItRetrofit.create(VReddIt.class).getRedirectUrl(getIntent().getStringExtra(EXTRA_V_REDD_IT_URL)).enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { Uri redirectUri = Uri.parse(response.raw().request().url().toString()); @@ -744,7 +745,7 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe List<String> segments = redirectUri.getPathSegments(); int commentsIndex = segments.lastIndexOf("comments"); String postId = segments.get(commentsIndex + 1); - FetchPost.fetchPost(mExecutor, new Handler(), applicationOnlyOauthRetrofit, postId, null, + FetchPost.fetchPost(mExecutor, new Handler(), mRetrofit, postId, null, Account.ANONYMOUS_ACCOUNT, new FetchPost.FetchPostListener() { @Override public void fetchPostSuccess(Post post) { @@ -814,7 +815,7 @@ public class ViewVideoActivity extends AppCompatActivity implements CustomFontRe private void loadStreamableVideo(String shortCode, Bundle savedInstanceState) { progressBar.setVisibility(View.VISIBLE); - FetchStreamableVideo.fetchStreamableVideo(mExecutor, new Handler(), streamableApiProvider, shortCode, + FetchStreamableVideo.fetchStreamableVideo(mExecutor, new Handler(), mStreamableApiProvider, shortCode, new FetchStreamableVideo.FetchStreamableVideoListener() { @Override public void success(StreamableVideo streamableVideo) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java index 7507f21c..bd8c804d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/WikiActivity.java @@ -92,8 +92,8 @@ public class WikiActivity extends BaseActivity { TextView mFetchWikiInfoTextView; @Inject - @Named("application_only_oauth") - Retrofit applicationOnlyOauthRetrofit; + @Named("no_oauth") + Retrofit mRetrofit; @Inject @Named("default") SharedPreferences mSharedPreferences; @@ -256,7 +256,7 @@ public class WikiActivity extends BaseActivity { Glide.with(this).clear(mFetchWikiInfoImageView); mFetchWikiInfoLinearLayout.setVisibility(View.GONE); - applicationOnlyOauthRetrofit.create(RedditAPI.class).getWikiPage(mSubredditName, getIntent().getStringExtra(EXTRA_WIKI_PATH)).enqueue(new Callback<>() { + mRetrofit.create(RedditAPI.class).getWikiPage(mSubredditName, getIntent().getStringExtra(EXTRA_WIKI_PATH)).enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java index 53bb5d57..8695d14b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java @@ -97,6 +97,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi private final BaseActivity mActivity; private final ViewPostDetailFragment mFragment; private final Executor mExecutor; + private final Retrofit mRetrofit; private final Retrofit mOauthRetrofit; private final EmoteCloseBracketInlineProcessor mEmoteCloseBracketInlineProcessor; private final EmotePlugin mEmotePlugin; @@ -164,7 +165,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi public CommentsRecyclerViewAdapter(BaseActivity activity, ViewPostDetailFragment fragment, CustomThemeWrapper customThemeWrapper, - Executor executor, Retrofit oauthRetrofit, + Executor executor, Retrofit retrofit, Retrofit oauthRetrofit, @Nullable String accessToken, @NonNull String accountName, Post post, Locale locale, String singleCommentId, boolean isSingleCommentThreadMode, @@ -174,6 +175,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi mActivity = activity; mFragment = fragment; mExecutor = executor; + mRetrofit = mOauthRetrofit = oauthRetrofit; mAccessToken = accessToken; mAccountName = accountName; @@ -666,8 +668,9 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerVi mVisibleComments.get(commentPosition).setLoadMoreChildrenFailed(false); ((LoadMoreChildCommentsViewHolder) holder).placeholderTextView.setText(R.string.loading); + Retrofit retrofit = mAccountName.equals(Account.ANONYMOUS_ACCOUNT) ? mRetrofit : mOauthRetrofit; SortType.Type sortType = mCommentRecyclerViewAdapterCallback.getSortType(); - FetchComment.fetchMoreComment(mExecutor, new Handler(), mOauthRetrofit, mAccessToken, + FetchComment.fetchMoreComment(mExecutor, new Handler(), retrofit, mAccessToken, mAccountName, parentComment.getMoreChildrenIds(), mExpandChildren, mPost.getFullName(), sortType, new FetchComment.FetchMoreCommentListener() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java index 150ce763..bb036181 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostDetailRecyclerViewAdapter.java @@ -137,8 +137,8 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler private final BaseActivity mActivity; private final ViewPostDetailFragment mFragment; private final Executor mExecutor; + private final Retrofit mRetrofit; private final Retrofit mOauthRetrofit; - private final Retrofit mApplicationOnlyRetrofit; private final Retrofit mRedgifsRetrofit; private final Provider<StreamableAPI> mStreamableApiProvider; private final RedditDataRoomDatabase mRedditDataRoomDatabase; @@ -221,7 +221,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler public PostDetailRecyclerViewAdapter(@NonNull BaseActivity activity, ViewPostDetailFragment fragment, Executor executor, CustomThemeWrapper customThemeWrapper, - Retrofit oauthRetrofit, Retrofit applicationOnlyRetrofit, + Retrofit oauthRetrofit, Retrofit retrofit, Retrofit redgifsRetrofit, Provider<StreamableAPI> streamableApiProvider, RedditDataRoomDatabase redditDataRoomDatabase, RequestManager glide, boolean separatePostAndComments, @Nullable String accessToken, @@ -235,8 +235,8 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler mActivity = activity; mFragment = fragment; mExecutor = executor; + mRetrofit = retrofit; mOauthRetrofit = oauthRetrofit; - mApplicationOnlyRetrofit = applicationOnlyRetrofit; mRedgifsRetrofit = redgifsRetrofit; mStreamableApiProvider = streamableApiProvider; mRedditDataRoomDatabase = redditDataRoomDatabase; @@ -523,7 +523,7 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler if (mPost.getAuthorIconUrl() == null) { String authorName = mPost.isAuthorDeleted() ? mPost.getSubredditNamePrefixed().substring(2) : mPost.getAuthor(); LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, - authorName, mApplicationOnlyRetrofit, iconImageUrl -> { + authorName, mRetrofit, iconImageUrl -> { if (mActivity != null && getItemCount() > 0) { if (iconImageUrl == null || iconImageUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) @@ -556,7 +556,8 @@ public class PostDetailRecyclerViewAdapter extends RecyclerView.Adapter<Recycler } else { if (mPost.getSubredditIconUrl() == null) { LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), - mRedditDataRoomDatabase, mApplicationOnlyRetrofit, mPost.getSubredditNamePrefixed().substring(2), + mRedditDataRoomDatabase, mPost.getSubredditNamePrefixed().substring(2), + mAccessToken, mAccountName, mOauthRetrofit, mRetrofit, iconImageUrl -> { if (iconImageUrl == null || iconImageUrl.equals("")) { mGlide.load(R.drawable.subreddit_default_icon) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java index 39cafb9d..0c134ba9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditListingRecyclerViewAdapter.java @@ -59,8 +59,8 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred private final RequestManager glide; private final BaseActivity activity; private final Executor executor; + private final Retrofit retrofit; private final Retrofit oauthRetrofit; - private final Retrofit applicationOnlyOauthRetrofit; private final String accessToken; private final String accountName; private final RedditDataRoomDatabase redditDataRoomDatabase; @@ -75,8 +75,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred private NetworkState networkState; private final Callback callback; - public SubredditListingRecyclerViewAdapter(BaseActivity activity, Executor executor, Retrofit oauthRetrofit, - Retrofit applicationOnlyOauthRetrofit, + public SubredditListingRecyclerViewAdapter(BaseActivity activity, Executor executor, Retrofit oauthRetrofit, Retrofit retrofit, CustomThemeWrapper customThemeWrapper, @Nullable String accessToken, @NonNull String accountName, RedditDataRoomDatabase redditDataRoomDatabase, @@ -85,7 +84,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred this.activity = activity; this.executor = executor; this.oauthRetrofit = oauthRetrofit; - this.applicationOnlyOauthRetrofit = applicationOnlyOauthRetrofit; + this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.redditDataRoomDatabase = redditDataRoomDatabase; @@ -161,7 +160,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred ((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> { if (!accountName.equals(Account.ANONYMOUS_ACCOUNT)) { SubredditSubscription.subscribeToSubreddit(executor, new Handler(), - oauthRetrofit, accessToken, subredditData.getName(), + oauthRetrofit, retrofit, accessToken, subredditData.getName(), accountName, redditDataRoomDatabase, new SubredditSubscription.SubredditSubscriptionListener() { @Override @@ -177,7 +176,7 @@ public class SubredditListingRecyclerViewAdapter extends PagedListAdapter<Subred }); } else { SubredditSubscription.anonymousSubscribeToSubreddit(executor, new Handler(), - applicationOnlyOauthRetrofit, redditDataRoomDatabase, + retrofit, redditDataRoomDatabase, subredditData.getName(), new SubredditSubscription.SubredditSubscriptionListener() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserListingRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserListingRecyclerViewAdapter.java index c29ddd80..4c9ef8f7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserListingRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UserListingRecyclerViewAdapter.java @@ -59,7 +59,7 @@ public class UserListingRecyclerViewAdapter extends PagedListAdapter<UserData, R private final BaseActivity activity; private final Executor executor; private final Retrofit oauthRetrofit; - private final Retrofit applicationOnlyOauthRetrofit; + private final Retrofit retrofit; private final String accessToken; private final String accountName; private final RedditDataRoomDatabase redditDataRoomDatabase; @@ -74,7 +74,7 @@ public class UserListingRecyclerViewAdapter extends PagedListAdapter<UserData, R private NetworkState networkState; private final Callback callback; - public UserListingRecyclerViewAdapter(BaseActivity activity, Executor executor, Retrofit oauthRetrofit, Retrofit applicationOnlyOauthRetrofit, + public UserListingRecyclerViewAdapter(BaseActivity activity, Executor executor, Retrofit oauthRetrofit, Retrofit retrofit, CustomThemeWrapper customThemeWrapper, @Nullable String accessToken, @NonNull String accountName, RedditDataRoomDatabase redditDataRoomDatabase, boolean isMultiSelection, Callback callback) { @@ -82,7 +82,7 @@ public class UserListingRecyclerViewAdapter extends PagedListAdapter<UserData, R this.activity = activity; this.executor = executor; this.oauthRetrofit = oauthRetrofit; - this.applicationOnlyOauthRetrofit = applicationOnlyOauthRetrofit; + this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.redditDataRoomDatabase = redditDataRoomDatabase; @@ -150,7 +150,7 @@ public class UserListingRecyclerViewAdapter extends PagedListAdapter<UserData, R public void isNotSubscribed() { ((DataViewHolder) holder).subscribeButton.setVisibility(View.VISIBLE); ((DataViewHolder) holder).subscribeButton.setOnClickListener(view -> { - UserFollowing.followUser(oauthRetrofit, applicationOnlyOauthRetrofit, + UserFollowing.followUser(oauthRetrofit, retrofit, accessToken, userData.getName(), accountName, redditDataRoomDatabase, new UserFollowing.UserFollowingListener() { @Override 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 32948ff7..9db3669e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java @@ -30,6 +30,9 @@ public interface RedditAPI { Call<String> getAccessToken(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); @GET("r/{subredditName}/about.json?raw_json=1") + Call<String> getSubredditData(@Path("subredditName") String subredditName); + + @GET("r/{subredditName}/about.json?raw_json=1") Call<String> getSubredditDataOauth(@Path("subredditName") String subredditName, @HeaderMap Map<String, String> headers); @GET("subreddits/mine/subscriber?raw_json=1") @@ -45,10 +48,20 @@ public interface RedditAPI { @GET("comments/{id}.json?raw_json=1") Call<String> getPostOauth(@Path("id") String id, @HeaderMap Map<String, String> headers); + @GET("comments/{id}.json?raw_json=1") + Call<String> getPost(@Path("id") String id); + + @GET("user/{username}/about.json?raw_json=1") + Call<String> getUserData(@Path("username") String username); + @GET("user/{username}/about.json?raw_json=1&limit=100") Call<String> getUserDataOauth(@HeaderMap Map<String, String> headers, @Path("username") String username); @GET("user/{username}/comments.json?raw_json=1&limit=100") + Call<String> getUserComments(@Path("username") String username, @Query("after") String after, + @Query("sort") SortType.Type sortType, @Query("t") SortType.Time sortTime); + + @GET("user/{username}/comments.json?raw_json=1&limit=100") Call<String> getUserCommentsOauth(@HeaderMap Map<String, String> headers, @Path("username") String username, @Query("after") String after, @Query("sort") SortType.Type sortType, @Query("t") SortType.Time sortTime); @@ -63,6 +76,9 @@ public interface RedditAPI { Call<String> subredditSubscription(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); @GET("/api/info.json?raw_json=1") + Call<String> getInfo(@Query("id") String id); + + @GET("/api/info.json?raw_json=1") Call<String> getInfoOauth(@Query("id") String id, @HeaderMap Map<String, String> headers); @GET("subreddits/search.json?raw_json=1") @@ -94,6 +110,9 @@ public interface RedditAPI { Call<String> getFlairs(@HeaderMap Map<String, String> headers, @Path("subredditName") String subredditName); @GET("/r/{subredditName}/about/rules.json?raw_json=1") + Call<String> getRules(@Path("subredditName") String subredditName); + + @GET("/r/{subredditName}/about/rules.json?raw_json=1") Call<String> getRulesOauth(@HeaderMap Map<String, String> headers, @Path("subredditName") String subredditName); @GET("/comments/{id}/placeholder/{singleCommentId}.json?raw_json=1") @@ -105,6 +124,13 @@ public interface RedditAPI { Call<String> getPostAndCommentsByIdOauth(@Path("id") String id, @Query("sort") SortType.Type sortType, @HeaderMap Map<String, String> headers); + @GET("/comments/{id}/placeholder/{singleCommentId}.json?raw_json=1") + Call<String> getPostAndCommentsSingleThreadById(@Path("id") String id, @Path("singleCommentId") String singleCommentId, + @Query("sort") SortType.Type sortType, @Query("context") String contextNumber); + + @GET("/comments/{id}.json?raw_json=1") + Call<String> getPostAndCommentsById(@Path("id") String id, @Query("sort") SortType.Type sortType); + @Multipart @POST(".") Call<String> uploadMediaToAWS(@PartMap() Map<String, RequestBody> params, @Part() MultipartBody.Part file); @@ -206,10 +232,10 @@ public interface RedditAPI { Call<String> awardThing(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> params); @GET("/r/random/comments.json?limit=1&raw_json=1") - Call<String> getRandomPostOauth(); + Call<String> getRandomPost(); @GET("/r/randnsfw/new.json?sort=new&t=all&limit=1&raw_json=1") - Call<String> getRandomNSFWPostOauth(); + Call<String> getRandomNSFWPost(); @POST("/api/read_all_messages") Call<String> readAllMessages(@HeaderMap Map<String, String> headers); @@ -235,6 +261,9 @@ public interface RedditAPI { Call<String> submitPollPost(@HeaderMap Map<String, String> headers, @Body String body); @GET("/api/trending_searches_v1.json?withAds=0&raw_json=1&gilding_detail=1") + Call<String> getTrendingSearches(); + + @GET("/api/trending_searches_v1.json?withAds=0&raw_json=1&gilding_detail=1") Call<String> getTrendingSearchesOauth(@HeaderMap Map<String, String> headers); @GET("/r/{subredditName}/wiki/{wikiPage}.json?raw_json=1") @@ -242,30 +271,52 @@ public interface RedditAPI { @GET("{sortType}?raw_json=1&limit=100") ListenableFuture<Response<String>> getBestPostsListenableFuture(@Path("sortType") SortType.Type sortType, @Query("t") SortType.Time sortTime, - @Query("after") String lastItem, @HeaderMap Map<String, String> headers); + @Query("after") String lastItem, @HeaderMap Map<String, String> headers); @GET("r/{subredditName}/{sortType}.json?raw_json=1&limit=100&always_show_media=1") ListenableFuture<Response<String>> getSubredditBestPostsOauthListenableFuture(@Path("subredditName") String subredditName, @Path("sortType") SortType.Type sortType, @Query("t") SortType.Time sortTime, @Query("after") String lastItem, @HeaderMap Map<String, String> headers); + @GET("r/{subredditName}/{sortType}.json?raw_json=1&limit=100&always_show_media=1") + ListenableFuture<Response<String>> getSubredditBestPostsListenableFuture(@Path("subredditName") String subredditName, @Path("sortType") SortType.Type sortType, + @Query("t") SortType.Time sortTime, @Query("after") String lastItem); + @GET("user/{username}/{where}.json?&type=links&raw_json=1&limit=100") ListenableFuture<Response<String>> getUserPostsOauthListenableFuture(@Path("username") String username, @Path("where") String where, @Query("after") String lastItem, @Query("sort") SortType.Type sortType, @Query("t") SortType.Time sortTime, @HeaderMap Map<String, String> headers); + @GET("user/{username}/submitted.json?raw_json=1&limit=100") + ListenableFuture<Response<String>> getUserPostsListenableFuture(@Path("username") String username, @Query("after") String lastItem, + @Query("sort") SortType.Type sortType, @Query("t") SortType.Time sortTime); + @GET("search.json?include_over_18=1&raw_json=1&limit=100&type=link") ListenableFuture<Response<String>> searchPostsOauthListenableFuture(@Query("q") String query, @Query("after") String after, @Query("sort") SortType.Type sort, @Query("t") SortType.Time sortTime, @Query("source") String source, @HeaderMap Map<String, String> headers); + @GET("search.json?include_over_18=1&raw_json=1&limit=100&type=link") + ListenableFuture<Response<String>> searchPostsListenableFuture(@Query("q") String query, @Query("after") String after, + @Query("sort") SortType.Type sort, @Query("t") SortType.Time sortTime, + @Query("source") String source); + @GET("r/{subredditName}/search.json?include_over_18=1&raw_json=1&limit=100&type=link&restrict_sr=true") ListenableFuture<Response<String>> searchPostsInSpecificSubredditOauthListenableFuture(@Path("subredditName") String subredditName, @Query("q") String query, @Query("sort") SortType.Type sort, @Query("t") SortType.Time sortTime, @Query("after") String after, @HeaderMap Map<String, String> headers); + @GET("r/{subredditName}/search.json?include_over_18=1&raw_json=1&limit=100&type=link&restrict_sr=true") + ListenableFuture<Response<String>> searchPostsInSpecificSubredditListenableFuture(@Path("subredditName") String subredditName, + @Query("q") String query, @Query("sort") SortType.Type sort, + @Query("t") SortType.Time sortTime, @Query("after") String after); + + @GET("{multipath}?raw_json=1&limit=100") + ListenableFuture<Response<String>> getMultiRedditPostsListenableFuture(@Path(value = "multipath", encoded = true) String multiPath, + @Query("after") String after, @Query("t") SortType.Time sortTime); + @GET("{multipath}.json?raw_json=1&limit=100") ListenableFuture<Response<String>> getMultiRedditPostsOauthListenableFuture(@Path(value = "multipath", encoded = true) String multiPath, @Query("after") String after, @Query("t") SortType.Time sortTime, @@ -273,30 +324,52 @@ public interface RedditAPI { @GET("{sortType}?raw_json=1&limit=100") Call<String> getBestPosts(@Path("sortType") SortType.Type sortType, @Query("t") SortType.Time sortTime, - @Query("after") String lastItem, @HeaderMap Map<String, String> headers); + @Query("after") String lastItem, @HeaderMap Map<String, String> headers); @GET("r/{subredditName}/{sortType}.json?raw_json=1&limit=100&always_show_media=1") Call<String> getSubredditBestPostsOauth(@Path("subredditName") String subredditName, @Path("sortType") SortType.Type sortType, @Query("t") SortType.Time sortTime, @Query("after") String lastItem, @HeaderMap Map<String, String> headers); + @GET("r/{subredditName}/{sortType}.json?raw_json=1&limit=100&always_show_media=1") + Call<String> getSubredditBestPosts(@Path("subredditName") String subredditName, @Path("sortType") SortType.Type sortType, + @Query("t") SortType.Time sortTime, @Query("after") String lastItem); + @GET("user/{username}/{where}.json?&type=links&raw_json=1&limit=100") Call<String> getUserPostsOauth(@Path("username") String username, @Path("where") String where, @Query("after") String lastItem, @Query("sort") SortType.Type sortType, @Query("t") SortType.Time sortTime, @HeaderMap Map<String, String> headers); + @GET("user/{username}/submitted.json?raw_json=1&limit=100") + Call<String> getUserPosts(@Path("username") String username, @Query("after") String lastItem, + @Query("sort") SortType.Type sortType, @Query("t") SortType.Time sortTime); + @GET("search.json?include_over_18=1&raw_json=1&limit=100&type=link") Call<String> searchPostsOauth(@Query("q") String query, @Query("after") String after, @Query("sort") SortType.Type sort, @Query("t") SortType.Time sortTime, @Query("source") String source, @HeaderMap Map<String, String> headers); + @GET("search.json?include_over_18=1&raw_json=1&limit=100&type=link") + Call<String> searchPosts(@Query("q") String query, @Query("after") String after, + @Query("sort") SortType.Type sort, @Query("t") SortType.Time sortTime, + @Query("source") String source); + @GET("r/{subredditName}/search.json?include_over_18=1&raw_json=1&limit=100&type=link&restrict_sr=true") Call<String> searchPostsInSpecificSubredditOauth(@Path("subredditName") String subredditName, @Query("q") String query, @Query("sort") SortType.Type sort, @Query("t") SortType.Time sortTime, @Query("after") String after, @HeaderMap Map<String, String> headers); + @GET("r/{subredditName}/search.json?include_over_18=1&raw_json=1&limit=100&type=link&restrict_sr=true") + Call<String> searchPostsInSpecificSubreddit(@Path("subredditName") String subredditName, + @Query("q") String query, @Query("sort") SortType.Type sort, + @Query("t") SortType.Time sortTime, @Query("after") String after); + + @GET("{multipath}?raw_json=1&limit=100") + Call<String> getMultiRedditPosts(@Path(value = "multipath", encoded = true) String multiPath, + @Query("after") String after, @Query("t") SortType.Time sortTime); + @GET("{multipath}.json?raw_json=1&limit=100") Call<String> getMultiRedditPostsOauth(@Path(value = "multipath", encoded = true) String multiPath, @Query("after") String after, @Query("t") SortType.Time sortTime, @@ -311,9 +384,9 @@ public interface RedditAPI { @Multipart @POST("r/{subredditName}/api/upload_sr_img") Call<String> uploadSrImg(@HeaderMap Map<String, String> headers, - @Path("subredditName") String subredditName, - @PartMap Map<String, RequestBody> params, - @Part MultipartBody.Part file); + @Path("subredditName") String subredditName, + @PartMap Map<String, RequestBody> params, + @Part MultipartBody.Part file); @GET("r/{subredditName}/about/edit?raw_json=1") Call<String> getSubredditSetting(@HeaderMap Map<String, String> headers, @Path("subredditName") String subredditName); @@ -324,5 +397,9 @@ public interface RedditAPI { @FormUrlEncoded @POST("/api/morechildren.json?raw_json=1&api_type=json") + Call<String> moreChildren(@Field("link_id") String linkId, @Field("children") String children, @Field("sort") SortType.Type sort); + + @FormUrlEncoded + @POST("/api/morechildren.json?raw_json=1&api_type=json") Call<String> moreChildrenOauth(@Field("link_id") String linkId, @Field("children") String children, @Field("sort") SortType.Type sort, @HeaderMap Map<String, String> headers); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadSubredditIcon.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadSubredditIcon.java index 8e29cdde..bfdb8098 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadSubredditIcon.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadSubredditIcon.java @@ -2,11 +2,12 @@ package ml.docilealligator.infinityforreddit.asynctasks; import android.os.Handler; +import androidx.annotation.NonNull; + import java.util.ArrayList; import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.subreddit.FetchSubredditData; import ml.docilealligator.infinityforreddit.subreddit.SubredditDao; import ml.docilealligator.infinityforreddit.subreddit.SubredditData; @@ -15,7 +16,8 @@ import retrofit2.Retrofit; public class LoadSubredditIcon { public static void loadSubredditIcon(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase, - Retrofit applicationOnlyOauthRetrofit, String subredditName, + String subredditName, String accessToken, @NonNull String accountName, + Retrofit oauthRetrofit, Retrofit retrofit, LoadSubredditIconAsyncTaskListener loadSubredditIconAsyncTaskListener) { executor.execute(() -> { SubredditDao subredditDao = redditDataRoomDatabase.subredditDao(); @@ -24,22 +26,21 @@ public class LoadSubredditIcon { String iconImageUrl = subredditDao.getSubredditData(subredditName).getIconUrl(); handler.post(() -> loadSubredditIconAsyncTaskListener.loadIconSuccess(iconImageUrl)); } else { - handler.post(() -> FetchSubredditData.fetchSubredditData(applicationOnlyOauthRetrofit, - subredditName, new FetchSubredditData.FetchSubredditDataListener() { - @Override - public void onFetchSubredditDataSuccess(SubredditData subredditData1, int nCurrentOnlineSubscribers) { - ArrayList<SubredditData> singleSubredditDataList = new ArrayList<>(); - singleSubredditDataList.add(subredditData1); - InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, Account.ANONYMOUS_ACCOUNT, - null, null, singleSubredditDataList, - () -> loadSubredditIconAsyncTaskListener.loadIconSuccess(subredditData1.getIconUrl())); - } - - @Override - public void onFetchSubredditDataFail(boolean isQuarantined) { - loadSubredditIconAsyncTaskListener.loadIconSuccess(null); - } - })); + handler.post(() -> FetchSubredditData.fetchSubredditData(oauthRetrofit, retrofit, subredditName, accessToken, new FetchSubredditData.FetchSubredditDataListener() { + @Override + public void onFetchSubredditDataSuccess(SubredditData subredditData1, int nCurrentOnlineSubscribers) { + ArrayList<SubredditData> singleSubredditDataList = new ArrayList<>(); + singleSubredditDataList.add(subredditData1); + InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, accountName, + null, null, singleSubredditDataList, + () -> loadSubredditIconAsyncTaskListener.loadIconSuccess(subredditData1.getIconUrl())); + } + + @Override + public void onFetchSubredditDataFail(boolean isQuarantined) { + loadSubredditIconAsyncTaskListener.loadIconSuccess(null); + } + })); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadUserData.java index c96bb821..2406f8f7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/LoadUserData.java @@ -13,14 +13,14 @@ import retrofit2.Retrofit; public class LoadUserData { public static void loadUserData(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase, String userName, - Retrofit applicationOnlyOauthRetrofit, LoadUserDataAsyncTaskListener loadUserDataAsyncTaskListener) { + Retrofit retrofit, LoadUserDataAsyncTaskListener loadUserDataAsyncTaskListener) { executor.execute(() -> { UserDao userDao = redditDataRoomDatabase.userDao(); if (userDao.getUserData(userName) != null) { String iconImageUrl = userDao.getUserData(userName).getIconUrl(); handler.post(() -> loadUserDataAsyncTaskListener.loadUserDataSuccess(iconImageUrl)); } else { - handler.post(() -> FetchUserData.fetchUserData(applicationOnlyOauthRetrofit, userName, new FetchUserData.FetchUserDataListener() { + handler.post(() -> FetchUserData.fetchUserData(retrofit, userName, new FetchUserData.FetchUserDataListener() { @Override public void onFetchUserDataSuccess(UserData userData, int inboxCount) { InsertUserData.insertUserData(executor, handler, redditDataRoomDatabase, userData, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java index 7a858064..bc15245d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java @@ -21,12 +21,9 @@ public class SwitchToAnonymousMode { accountDao.markAllAccountsNonCurrent(); String redgifsAccessToken = currentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, ""); - String applicationOnlyAccessToken = currentAccountSharedPreferences.getString(SharedPreferencesUtils.APPLICATION_ONLY_ACCESS_TOKEN, null); currentAccountSharedPreferences.edit().clear().apply(); currentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, redgifsAccessToken).apply(); - currentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.APPLICATION_ONLY_ACCESS_TOKEN, applicationOnlyAccessToken).apply(); - currentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, applicationOnlyAccessToken).apply(); handler.post(switchToAnonymousAccountAsyncTaskListener::logoutSuccess); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentDataSource.java index 81daa344..abd5e266 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentDataSource.java @@ -15,6 +15,7 @@ import java.util.ArrayList; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.utils.APIUtils; @@ -29,7 +30,7 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> { private final Retrofit retrofit; @Nullable private final String accessToken; - @Nullable + @NonNull private final String accountName; private final String username; private final SortType sortType; @@ -83,10 +84,15 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> { null, sortType.getType(), sortType.getTime(), APIUtils.getOAuthHeader(accessToken)); } else { - commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), username, - null, sortType.getType(), sortType.getTime()); + if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) { + commentsCall = api.getUserComments(username, null, sortType.getType(), + sortType.getTime()); + } else { + commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), username, + null, sortType.getType(), sortType.getTime()); + } } - commentsCall.enqueue(new Callback<String>() { + commentsCall.enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { @@ -142,10 +148,15 @@ public class CommentDataSource extends PageKeyedDataSource<String, Comment> { commentsCall = api.getUserSavedCommentsOauth(username, PostPagingSource.USER_WHERE_SAVED, params.key, sortType.getType(), sortType.getTime(), APIUtils.getOAuthHeader(accessToken)); } else { - commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), - username, params.key, sortType.getType(), sortType.getTime()); + if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) { + commentsCall = api.getUserComments(username, params.key, sortType.getType(), + sortType.getTime()); + } else { + commentsCall = api.getUserCommentsOauth(APIUtils.getOAuthHeader(accessToken), + username, params.key, sortType.getType(), sortType.getTime()); + } } - commentsCall.enqueue(new Callback<String>() { + commentsCall.enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (response.isSuccessful()) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentDataSourceFactory.java index 5af151a0..5ae58264 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentDataSourceFactory.java @@ -5,14 +5,11 @@ import androidx.annotation.Nullable; import androidx.lifecycle.MutableLiveData; import androidx.paging.DataSource; -import java.util.Locale; - import ml.docilealligator.infinityforreddit.SortType; import retrofit2.Retrofit; class CommentDataSourceFactory extends DataSource.Factory { private final Retrofit retrofit; - private final Locale locale; private final String accessToken; private final String accountName; private final String username; @@ -22,11 +19,10 @@ class CommentDataSourceFactory extends DataSource.Factory { private CommentDataSource commentDataSource; private final MutableLiveData<CommentDataSource> commentDataSourceLiveData; - CommentDataSourceFactory(Retrofit retrofit, Locale locale, @Nullable String accessToken, @NonNull String accountName, + CommentDataSourceFactory(Retrofit retrofit, @Nullable String accessToken, @NonNull String accountName, String username, SortType sortType, boolean areSavedComments) { this.retrofit = retrofit; - this.locale = locale; this.accessToken = accessToken; this.accountName = accountName; this.username = username; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentViewModel.java index 17574d68..df291cc6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentViewModel.java @@ -10,8 +10,6 @@ import androidx.lifecycle.ViewModelProvider; import androidx.paging.LivePagedListBuilder; import androidx.paging.PagedList; -import java.util.Locale; - import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.SortType; import retrofit2.Retrofit; @@ -24,9 +22,9 @@ public class CommentViewModel extends ViewModel { private final LiveData<PagedList<Comment>> comments; private final MutableLiveData<SortType> sortTypeLiveData; - public CommentViewModel(Retrofit retrofit, Locale locale, @Nullable String accessToken, @NonNull String accountName, + public CommentViewModel(Retrofit retrofit, @Nullable String accessToken, @NonNull String accountName, String username, SortType sortType, boolean areSavedComments) { - commentDataSourceFactory = new CommentDataSourceFactory(retrofit, locale, accessToken, accountName, + commentDataSourceFactory = new CommentDataSourceFactory(retrofit, accessToken, accountName, username, sortType, areSavedComments); initialLoadingState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(), @@ -82,17 +80,15 @@ public class CommentViewModel extends ViewModel { public static class Factory extends ViewModelProvider.NewInstanceFactory { private final Retrofit retrofit; - private final Locale locale; private final String accessToken; private final String accountName; private final String username; private final SortType sortType; private final boolean areSavedComments; - public Factory(Retrofit retrofit, Locale locale, @Nullable String accessToken, @NonNull String accountName, String username, + public Factory(Retrofit retrofit, @Nullable String accessToken, @NonNull String accountName, String username, SortType sortType, boolean areSavedComments) { this.retrofit = retrofit; - this.locale = locale; this.accessToken = accessToken; this.accountName = accountName; this.username = username; @@ -103,7 +99,7 @@ public class CommentViewModel extends ViewModel { @NonNull @Override public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { - return (T) new CommentViewModel(retrofit, locale, accessToken, accountName, username, + return (T) new CommentViewModel(retrofit, accessToken, accountName, username, sortType, areSavedComments); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java index 558f1b90..0c6fce12 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/comment/FetchComment.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.SortType; +import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.commentfilter.CommentFilter; import ml.docilealligator.infinityforreddit.utils.APIUtils; @@ -19,17 +20,25 @@ import retrofit2.Retrofit; public class FetchComment { public static void fetchComments(Executor executor, Handler handler, Retrofit retrofit, - @Nullable String accessToken, String article, + @Nullable String accessToken, @NonNull String accountName, String article, String commentId, SortType.Type sortType, String contextNumber, boolean expandChildren, CommentFilter commentFilter, FetchCommentListener fetchCommentListener) { RedditAPI api = retrofit.create(RedditAPI.class); Call<String> comments; - if (commentId == null) { - comments = api.getPostAndCommentsByIdOauth(article, sortType, APIUtils.getOAuthHeader(accessToken)); + if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) { + if (commentId == null) { + comments = api.getPostAndCommentsById(article, sortType); + } else { + comments = api.getPostAndCommentsSingleThreadById(article, commentId, sortType, contextNumber); + } } else { - comments = api.getPostAndCommentsSingleThreadByIdOauth(article, commentId, sortType, contextNumber, - APIUtils.getOAuthHeader(accessToken)); + if (commentId == null) { + comments = api.getPostAndCommentsByIdOauth(article, sortType, APIUtils.getOAuthHeader(accessToken)); + } else { + comments = api.getPostAndCommentsSingleThreadByIdOauth(article, commentId, sortType, contextNumber, + APIUtils.getOAuthHeader(accessToken)); + } } comments.enqueue(new Callback<>() { @@ -65,7 +74,7 @@ public class FetchComment { } public static void fetchMoreComment(Executor executor, Handler handler, Retrofit retrofit, - @Nullable String accessToken, String accountName, + @Nullable String accessToken, @NonNull String accountName, ArrayList<String> allChildren, boolean expandChildren, String postFullName, SortType.Type sortType, @@ -81,8 +90,13 @@ public class FetchComment { } RedditAPI api = retrofit.create(RedditAPI.class); - Call<String> moreComments = api.moreChildrenOauth(postFullName, childrenIds, - sortType, APIUtils.getOAuthHeader(accessToken)); + Call<String> moreComments; + if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) { + moreComments = api.moreChildren(postFullName, childrenIds, sortType); + } else { + moreComments = api.moreChildrenOauth(postFullName, childrenIds, + sortType, APIUtils.getOAuthHeader(accessToken)); + } moreComments.enqueue(new Callback<>() { @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java index e0bfd244..4c3a2f85 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/CommentsListingFragment.java @@ -85,6 +85,9 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni TextView mFetchCommentInfoTextView; CommentViewModel mCommentViewModel; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject @@ -301,9 +304,14 @@ public class CommentsListingFragment extends Fragment implements FragmentCommuni CommentViewModel.Factory factory; - factory = new CommentViewModel.Factory(mOauthRetrofit, - resources.getConfiguration().locale, mAccessToken, mAccountName, username, sortType, - getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS)); + if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { + factory = new CommentViewModel.Factory(mRetrofit, null, mAccountName, username, sortType, + getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS)); + } else { + factory = new CommentViewModel.Factory(mOauthRetrofit, + mAccessToken, mAccountName, username, sortType, + getArguments().getBoolean(EXTRA_ARE_SAVED_COMMENTS)); + } mCommentViewModel = new ViewModelProvider(this, factory).get(CommentViewModel.class); mCommentViewModel.getComments().observe(getViewLifecycleOwner(), comments -> mAdapter.submitList(comments)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java index 988c9b8f..df26b5d6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java @@ -70,6 +70,7 @@ import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.adapters.HistoryPostRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.adapters.Paging3LoadingStateAdapter; @@ -157,12 +158,12 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato TextView mFetchPostInfoTextView; HistoryPostViewModel mHistoryPostViewModel; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyRetrofit; - @Inject @Named("redgifs") Retrofit mRedgifsRetrofit; @Inject @@ -651,11 +652,11 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato private void initializeAndBindPostViewModel(String accessToken) { if (postType == HistoryPostPagingSource.TYPE_READ_POSTS) { mHistoryPostViewModel = new ViewModelProvider(HistoryPostFragment.this, new HistoryPostViewModel.Factory(mExecutor, - mOauthRetrofit, mRedditDataRoomDatabase, accessToken, + accountName.equals(Account.ANONYMOUS_ACCOUNT) ? mRetrofit : mOauthRetrofit, mRedditDataRoomDatabase, accessToken, accountName, mSharedPreferences, HistoryPostPagingSource.TYPE_READ_POSTS, postFilter)).get(HistoryPostViewModel.class); } else { mHistoryPostViewModel = new ViewModelProvider(HistoryPostFragment.this, new HistoryPostViewModel.Factory(mExecutor, - mOauthRetrofit, mRedditDataRoomDatabase, accessToken, + accountName.equals(Account.ANONYMOUS_ACCOUNT) ? mRetrofit : mOauthRetrofit, mRedditDataRoomDatabase, accessToken, accountName, mSharedPreferences, HistoryPostPagingSource.TYPE_READ_POSTS, postFilter)).get(HistoryPostViewModel.class); } @@ -928,14 +929,14 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato } else { if (isSubreddit) { LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, - mApplicationOnlyRetrofit, subredditOrUserName, + subredditOrUserName, accessToken, accountName, mOauthRetrofit, mRetrofit, iconImageUrl -> { subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); }); } else { LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditOrUserName, - mApplicationOnlyRetrofit, iconImageUrl -> { + mRetrofit, iconImageUrl -> { subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index 034f5a90..3d9ac681 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -180,12 +180,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { TextView mFetchPostInfoTextView; PostViewModel mPostViewModel; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyRetrofit; - @Inject @Named("redgifs") Retrofit mRedgifsRetrofit; @Inject @@ -1204,22 +1204,22 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private void initializeAndBindPostViewModel(String accessToken) { if (postType == PostPagingSource.TYPE_SEARCH) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, - mOauthRetrofit, accessToken, accountName, mSharedPreferences, + accountName.equals(Account.ANONYMOUS_ACCOUNT) ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, mPostHistorySharedPreferences, subredditName, query, trendingSource, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostPagingSource.TYPE_SUBREDDIT) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, - mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, + accountName.equals(Account.ANONYMOUS_ACCOUNT) ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, mPostHistorySharedPreferences, subredditName, postType, sortType, postFilter, readPosts)) .get(PostViewModel.class); } else if (postType == PostPagingSource.TYPE_MULTI_REDDIT) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, - mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, + accountName.equals(Account.ANONYMOUS_ACCOUNT) ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, mPostHistorySharedPreferences, multiRedditPath, postType, sortType, postFilter, readPosts)) .get(PostViewModel.class); } else if (postType == PostPagingSource.TYPE_USER) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, - mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, + accountName.equals(Account.ANONYMOUS_ACCOUNT) ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, mPostHistorySharedPreferences, username, postType, sortType, postFilter, where, readPosts)) .get(PostViewModel.class); } else { @@ -1236,28 +1236,28 @@ public class PostFragment extends Fragment implements FragmentCommunicator { //For anonymous user if (postType == PostPagingSource.TYPE_SEARCH) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, - mOauthRetrofit, accessToken, accountName, mSharedPreferences, + mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, null, subredditName, query, trendingSource, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostPagingSource.TYPE_SUBREDDIT) { mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mExecutor, - mOauthRetrofit, accessToken, accountName, mSharedPreferences, + mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, null, subredditName, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostPagingSource.TYPE_MULTI_REDDIT) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, - mOauthRetrofit, accessToken, accountName, mSharedPreferences, + mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, null, multiRedditPath, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostPagingSource.TYPE_USER) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, - mOauthRetrofit, accessToken, accountName, mSharedPreferences, + mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, null, username, postType, sortType, postFilter, where, readPosts)).get(PostViewModel.class); } else { //Anonymous Front Page mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, - mOauthRetrofit, accessToken, mSharedPreferences, concatenatedSubredditNames, postType, sortType, postFilter)) + mRetrofit, mSharedPreferences, concatenatedSubredditNames, postType, sortType, postFilter)) .get(PostViewModel.class); } @@ -1745,14 +1745,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } else { if (isSubreddit) { LoadSubredditIcon.loadSubredditIcon(mExecutor, new Handler(), mRedditDataRoomDatabase, - mApplicationOnlyRetrofit, subredditOrUserName, + subredditOrUserName, accessToken, accountName, mOauthRetrofit, mRetrofit, iconImageUrl -> { subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); }); } else { LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, subredditOrUserName, - mApplicationOnlyRetrofit, iconImageUrl -> { + mRetrofit, iconImageUrl -> { subredditOrUserIcons.put(subredditOrUserName, iconImageUrl); loadIconListener.loadIconSuccess(subredditOrUserName, iconImageUrl); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java index c56f7521..a8298a92 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SidebarFragment.java @@ -71,6 +71,9 @@ public class SidebarFragment extends Fragment { @BindView(R.id.markdown_recycler_view_sidebar_fragment) RecyclerView recyclerView; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject @@ -243,7 +246,7 @@ public class SidebarFragment extends Fragment { public void fetchSubredditData() { swipeRefreshLayout.setRefreshing(true); - FetchSubredditData.fetchSubredditData(mOauthRetrofit, subredditName, mAccessToken, new FetchSubredditData.FetchSubredditDataListener() { + FetchSubredditData.fetchSubredditData(mOauthRetrofit, mRetrofit, subredditName, mAccessToken, new FetchSubredditData.FetchSubredditDataListener() { @Override public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { swipeRefreshLayout.setRefreshing(false); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubredditListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubredditListingFragment.java index dad3a4b4..0cf8b4cb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubredditListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubredditListingFragment.java @@ -77,12 +77,12 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun TextView mFetchSubredditListingInfoTextView; SubredditListingViewModel mSubredditListingViewModel; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyOauthRetrofit; - @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -143,7 +143,7 @@ public class SubredditListingFragment extends Fragment implements FragmentCommun sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase())); boolean nsfw = !mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_NSFW_FOREVER, false) && mNsfwAndSpoilerSharedPreferences.getBoolean((accountName.equals(Account.ANONYMOUS_ACCOUNT) ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); - mAdapter = new SubredditListingRecyclerViewAdapter(mActivity, mExecutor, mOauthRetrofit, mApplicationOnlyOauthRetrofit, + mAdapter = new SubredditListingRecyclerViewAdapter(mActivity, mExecutor, mOauthRetrofit, mRetrofit, mCustomThemeWrapper, accessToken, accountName, mRedditDataRoomDatabase, getArguments().getBoolean(EXTRA_IS_MULTI_SELECTION, false), new SubredditListingRecyclerViewAdapter.Callback() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/UserListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/UserListingFragment.java index 44c472e1..e0122c63 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/UserListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/UserListingFragment.java @@ -77,12 +77,12 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato TextView mFetchUserListingInfoTextView; UserListingViewModel mUserListingViewModel; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyOauthRetrofit; - @Inject RedditDataRoomDatabase mRedditDataRoomDatabase; @Inject @Named("default") @@ -143,7 +143,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato sortType = new SortType(SortType.Type.valueOf(sort.toUpperCase())); boolean nsfw = !mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_NSFW_FOREVER, false) && mNsfwAndSpoilerSharedPreferences.getBoolean((accountName.equals(Account.ANONYMOUS_ACCOUNT) ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); - mAdapter = new UserListingRecyclerViewAdapter(mActivity, mExecutor, mOauthRetrofit, mApplicationOnlyOauthRetrofit, + mAdapter = new UserListingRecyclerViewAdapter(mActivity, mExecutor, mOauthRetrofit, mRetrofit, mCustomThemeWrapper, accessToken, accountName, mRedditDataRoomDatabase, getArguments().getBoolean(EXTRA_IS_MULTI_SELECTION, false), new UserListingRecyclerViewAdapter.Callback() { @@ -179,7 +179,7 @@ public class UserListingFragment extends Fragment implements FragmentCommunicato }); } - UserListingViewModel.Factory factory = new UserListingViewModel.Factory(mApplicationOnlyOauthRetrofit, mQuery, + UserListingViewModel.Factory factory = new UserListingViewModel.Factory(mRetrofit, mQuery, sortType, nsfw); mUserListingViewModel = new ViewModelProvider(this, factory).get(UserListingViewModel.class); mUserListingViewModel.getUsers().observe(getViewLifecycleOwner(), UserData -> mAdapter.submitList(UserData)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index 4a712ee0..c7dff78d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -151,12 +151,12 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic @Named("reveddit") Retrofit revedditRetrofit; @Inject + @Named("no_oauth") + Retrofit mRetrofit; + @Inject @Named("oauth") Retrofit mOauthRetrofit; @Inject - @Named("application_only_oauth") - Retrofit mApplicationOnlyRetrofit; - @Inject @Named("redgifs") Retrofit mRedgifsRetrofit; @Inject @@ -594,13 +594,13 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic setupMenu(); mPostAdapter = new PostDetailRecyclerViewAdapter(activity, - this, mExecutor, mCustomThemeWrapper, mOauthRetrofit, mApplicationOnlyRetrofit, + this, mExecutor, mCustomThemeWrapper, mOauthRetrofit, mRetrofit, mRedgifsRetrofit, mStreamableApiProvider, mRedditDataRoomDatabase, mGlide, mSeparatePostAndComments, mAccessToken, mAccountName, mPost, mLocale, mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostDetailsSharedPreferences, mExoCreator, post -> EventBus.getDefault().post(new PostUpdateEventToPostList(mPost, postListPosition))); mCommentsAdapter = new CommentsRecyclerViewAdapter(activity, - this, mCustomThemeWrapper, mExecutor, mOauthRetrofit, + this, mCustomThemeWrapper, mExecutor, mRetrofit, mOauthRetrofit, mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId, isSingleCommentThreadMode, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, new CommentsRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @@ -904,7 +904,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic loadIconListener.loadIconSuccess(authorName, activity.authorIcons.get(authorName)); } else { LoadUserData.loadUserData(mExecutor, new Handler(), mRedditDataRoomDatabase, authorName, - mApplicationOnlyRetrofit, iconImageUrl -> { + mRetrofit, iconImageUrl -> { activity.authorIcons.put(authorName, iconImageUrl); loadIconListener.loadIconSuccess(authorName, iconImageUrl); }); @@ -1251,12 +1251,22 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mGlide.clear(mFetchPostInfoImageView); Call<String> postAndComments; - if (isSingleCommentThreadMode && mSingleCommentId != null) { - postAndComments = mOauthRetrofit.create(RedditAPI.class).getPostAndCommentsSingleThreadByIdOauth(subredditId, - mSingleCommentId, sortType, mContextNumber, APIUtils.getOAuthHeader(mAccessToken)); + if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { + if (isSingleCommentThreadMode && mSingleCommentId != null) { + postAndComments = mRetrofit.create(RedditAPI.class).getPostAndCommentsSingleThreadById( + subredditId, mSingleCommentId, sortType, mContextNumber); + } else { + postAndComments = mRetrofit.create(RedditAPI.class).getPostAndCommentsById(subredditId, + sortType); + } } else { - postAndComments = mOauthRetrofit.create(RedditAPI.class).getPostAndCommentsByIdOauth(subredditId, - sortType, APIUtils.getOAuthHeader(mAccessToken)); + if (isSingleCommentThreadMode && mSingleCommentId != null) { + postAndComments = mOauthRetrofit.create(RedditAPI.class).getPostAndCommentsSingleThreadByIdOauth(subredditId, + mSingleCommentId, sortType, mContextNumber, APIUtils.getOAuthHeader(mAccessToken)); + } else { + postAndComments = mOauthRetrofit.create(RedditAPI.class).getPostAndCommentsByIdOauth(subredditId, + sortType, APIUtils.getOAuthHeader(mAccessToken)); + } } postAndComments.enqueue(new Callback<>() { @Override @@ -1277,7 +1287,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mPostAdapter = new PostDetailRecyclerViewAdapter(activity, ViewPostDetailFragment.this, mExecutor, mCustomThemeWrapper, - mOauthRetrofit, mApplicationOnlyRetrofit, mRedgifsRetrofit, + mOauthRetrofit, mRetrofit, mRedgifsRetrofit, mStreamableApiProvider, mRedditDataRoomDatabase, mGlide, mSeparatePostAndComments, mAccessToken, mAccountName, mPost, mLocale, mSharedPreferences, mCurrentAccountSharedPreferences, mNsfwAndSpoilerSharedPreferences, @@ -1286,7 +1296,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic mCommentsAdapter = new CommentsRecyclerViewAdapter(activity, ViewPostDetailFragment.this, mCustomThemeWrapper, mExecutor, - mOauthRetrofit, mAccessToken, mAccountName, mPost, mLocale, + mRetrofit, mOauthRetrofit, mAccessToken, mAccountName, mPost, mLocale, mSingleCommentId, isSingleCommentThreadMode, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, new CommentsRecyclerViewAdapter.CommentRecyclerViewAdapterCallback() { @@ -1424,7 +1434,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic e.printStackTrace(); } } - FetchSubredditData.fetchSubredditData(mOauthRetrofit, mPost.getSubredditName(), mAccessToken, + FetchSubredditData.fetchSubredditData(mOauthRetrofit, mRetrofit, mPost.getSubredditName(), mAccessToken, new FetchSubredditData.FetchSubredditDataListener() { @Override public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { @@ -1468,7 +1478,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic commentId = mSingleCommentId; } - FetchComment.fetchComments(mExecutor, new Handler(), mOauthRetrofit, mAccessToken, mPost.getId(), commentId, sortType, + Retrofit retrofit = mAccountName.equals(Account.ANONYMOUS_ACCOUNT) ? mRetrofit : mOauthRetrofit; + FetchComment.fetchComments(mExecutor, new Handler(), retrofit, mAccessToken, mAccountName, mPost.getId(), commentId, sortType, mContextNumber, mExpandChildren, mCommentFilter, new FetchComment.FetchCommentListener() { @Override public void onFetchCommentSuccess(ArrayList<Comment> expandedComments, @@ -1554,7 +1565,8 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic isLoadingMoreChildren = true; - FetchComment.fetchMoreComment(mExecutor, new Handler(), mOauthRetrofit, mAccessToken, mAccountName, + Retrofit retrofit = mAccountName.equals(Account.ANONYMOUS_ACCOUNT) ? mRetrofit : mOauthRetrofit; + FetchComment.fetchMoreComment(mExecutor, new Handler(), retrofit, mAccessToken, mAccountName, children, mExpandChildren, mPost.getFullName(), sortType, new FetchComment.FetchMoreCommentListener() { @Override public void onFetchMoreCommentSuccess(ArrayList<Comment> topLevelComments, @@ -1588,7 +1600,13 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic } if (fetchPost) { - FetchPost.fetchPost(mExecutor, new Handler(), mOauthRetrofit, mPost.getId(), mAccessToken, + Retrofit retrofit; + if (mAccountName.equals(Account.ANONYMOUS_ACCOUNT)) { + retrofit = mRetrofit; + } else { + retrofit = mOauthRetrofit; + } + FetchPost.fetchPost(mExecutor, new Handler(), retrofit, mPost.getId(), mAccessToken, mAccountName, new FetchPost.FetchPostListener() { @Override public void fetchPostSuccess(Post post) { 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 a1579d86..57895e17 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/FetchPost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/FetchPost.java @@ -5,9 +5,9 @@ import android.os.Handler; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import java.util.HashMap; import java.util.concurrent.Executor; +import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.utils.APIUtils; import retrofit2.Call; @@ -16,11 +16,15 @@ import retrofit2.Response; import retrofit2.Retrofit; public class FetchPost { - public static void fetchPost(Executor executor, Handler handler, Retrofit oauthRetrofit, String id, - @Nullable String accessToken, + public static void fetchPost(Executor executor, Handler handler, Retrofit retrofit, String id, @Nullable String accessToken, + @NonNull String accountName, FetchPostListener fetchPostListener) { - Call<String> postCall = oauthRetrofit.create(RedditAPI.class).getPostOauth(id, - accessToken == null ? new HashMap<>() : APIUtils.getOAuthHeader(accessToken)); + Call<String> postCall; + if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) { + postCall = retrofit.create(RedditAPI.class).getPost(id); + } else { + postCall = retrofit.create(RedditAPI.class).getPostOauth(id, APIUtils.getOAuthHeader(accessToken)); + } postCall.enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { @@ -48,13 +52,13 @@ public class FetchPost { }); } - public static void fetchRandomPost(Executor executor, Handler handler, Retrofit applicationOnlyOauthRetrofit, - boolean isNSFW, FetchRandomPostListener fetchRandomPostListener) { + public static void fetchRandomPost(Executor executor, Handler handler, Retrofit retrofit, boolean isNSFW, + FetchRandomPostListener fetchRandomPostListener) { Call<String> call; if (isNSFW) { - call = applicationOnlyOauthRetrofit.create(RedditAPI.class).getRandomNSFWPostOauth(); + call = retrofit.create(RedditAPI.class).getRandomNSFWPost(); } else { - call = applicationOnlyOauthRetrofit.create(RedditAPI.class).getRandomPostOauth(); + call = retrofit.create(RedditAPI.class).getRandomPost(); } call.enqueue(new Callback<>() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/HistoryPostPagingSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/HistoryPostPagingSource.java index 37f461e0..360496db 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/HistoryPostPagingSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/HistoryPostPagingSource.java @@ -17,6 +17,7 @@ import java.util.List; import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.readpost.ReadPost; @@ -80,7 +81,12 @@ public class HistoryPostPagingSource extends ListenableFuturePagingSource<String ids.deleteCharAt(ids.length() - 1); } - Call<String> historyPosts = retrofit.create(RedditAPI.class).getInfoOauth(ids.toString(), APIUtils.getOAuthHeader(accessToken)); + Call<String> historyPosts; + if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) { + historyPosts = retrofit.create(RedditAPI.class).getInfo(ids.toString()); + } else { + historyPosts = retrofit.create(RedditAPI.class).getInfoOauth(ids.toString(), APIUtils.getOAuthHeader(accessToken)); + } try { Response<String> response = historyPosts.execute(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java index 70752e67..cc2519cf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java @@ -233,8 +233,13 @@ public class PostPagingSource extends ListenableFuturePagingSource<String, Post> } private ListenableFuture<LoadResult<String, Post>> loadSubredditPosts(@NonNull LoadParams<String> loadParams, RedditAPI api) { - ListenableFuture<Response<String>> subredditPost = api.getSubredditBestPostsOauthListenableFuture(subredditOrUserName, sortType.getType(), - sortType.getTime(), loadParams.getKey(), APIUtils.getOAuthHeader(accessToken)); + ListenableFuture<Response<String>> subredditPost; + if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) { + subredditPost = api.getSubredditBestPostsListenableFuture(subredditOrUserName, sortType.getType(), sortType.getTime(), loadParams.getKey()); + } else { + subredditPost = api.getSubredditBestPostsOauthListenableFuture(subredditOrUserName, sortType.getType(), + sortType.getTime(), loadParams.getKey(), APIUtils.getOAuthHeader(accessToken)); + } ListenableFuture<LoadResult<String, Post>> pageFuture = Futures.transform(subredditPost, this::transformData, executor); @@ -249,11 +254,11 @@ public class PostPagingSource extends ListenableFuturePagingSource<String, Post> private ListenableFuture<LoadResult<String, Post>> loadUserPosts(@NonNull LoadParams<String> loadParams, RedditAPI api) { ListenableFuture<Response<String>> userPosts; if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) { - userPosts = api.getUserPostsOauthListenableFuture(subredditOrUserName, USER_WHERE_SUBMITTED, - loadParams.getKey(), sortType.getType(), sortType.getTime(), APIUtils.getOAuthHeader(accessToken)); + userPosts = api.getUserPostsListenableFuture(subredditOrUserName, loadParams.getKey(), sortType.getType(), + sortType.getTime()); } else { - userPosts = api.getUserPostsOauthListenableFuture(subredditOrUserName, userWhere, loadParams.getKey(), - sortType.getType(), sortType.getTime(), APIUtils.getOAuthHeader(accessToken)); + userPosts = api.getUserPostsOauthListenableFuture(subredditOrUserName, userWhere, loadParams.getKey(), sortType.getType(), + sortType.getTime(), APIUtils.getOAuthHeader(accessToken)); } ListenableFuture<LoadResult<String, Post>> pageFuture = Futures.transform(userPosts, this::transformData, executor); @@ -269,12 +274,22 @@ public class PostPagingSource extends ListenableFuturePagingSource<String, Post> private ListenableFuture<LoadResult<String, Post>> loadSearchPosts(@NonNull LoadParams<String> loadParams, RedditAPI api) { ListenableFuture<Response<String>> searchPosts; if (subredditOrUserName == null) { - searchPosts = api.searchPostsOauthListenableFuture(query, loadParams.getKey(), sortType.getType(), - sortType.getTime(), trendingSource, APIUtils.getOAuthHeader(accessToken)); + if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) { + searchPosts = api.searchPostsListenableFuture(query, loadParams.getKey(), sortType.getType(), sortType.getTime(), + trendingSource); + } else { + searchPosts = api.searchPostsOauthListenableFuture(query, loadParams.getKey(), sortType.getType(), + sortType.getTime(), trendingSource, APIUtils.getOAuthHeader(accessToken)); + } } else { - searchPosts = api.searchPostsInSpecificSubredditOauthListenableFuture(subredditOrUserName, query, - sortType.getType(), sortType.getTime(), loadParams.getKey(), - APIUtils.getOAuthHeader(accessToken)); + if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) { + searchPosts = api.searchPostsInSpecificSubredditListenableFuture(subredditOrUserName, query, + sortType.getType(), sortType.getTime(), loadParams.getKey()); + } else { + searchPosts = api.searchPostsInSpecificSubredditOauthListenableFuture(subredditOrUserName, query, + sortType.getType(), sortType.getTime(), loadParams.getKey(), + APIUtils.getOAuthHeader(accessToken)); + } } ListenableFuture<LoadResult<String, Post>> pageFuture = Futures.transform(searchPosts, this::transformData, executor); @@ -288,8 +303,13 @@ public class PostPagingSource extends ListenableFuturePagingSource<String, Post> } private ListenableFuture<LoadResult<String, Post>> loadMultiRedditPosts(@NonNull LoadParams<String> loadParams, RedditAPI api) { - ListenableFuture<Response<String>> multiRedditPosts = api.getMultiRedditPostsOauthListenableFuture(multiRedditPath, loadParams.getKey(), - sortType.getTime(), APIUtils.getOAuthHeader(accessToken)); + ListenableFuture<Response<String>> multiRedditPosts; + if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) { + multiRedditPosts = api.getMultiRedditPostsListenableFuture(multiRedditPath, loadParams.getKey(), sortType.getTime()); + } else { + multiRedditPosts = api.getMultiRedditPostsOauthListenableFuture(multiRedditPath, loadParams.getKey(), + sortType.getTime(), APIUtils.getOAuthHeader(accessToken)); + } ListenableFuture<LoadResult<String, Post>> pageFuture = Futures.transform(multiRedditPosts, this::transformData, executor); @@ -302,9 +322,7 @@ public class PostPagingSource extends ListenableFuturePagingSource<String, Post> } private ListenableFuture<LoadResult<String, Post>> loadAnonymousHomePosts(@NonNull LoadParams<String> loadParams, RedditAPI api) { - ListenableFuture<Response<String>> anonymousHomePosts; - anonymousHomePosts = api.getSubredditBestPostsOauthListenableFuture(subredditOrUserName, - sortType.getType(), sortType.getTime(), loadParams.getKey(), APIUtils.getOAuthHeader(accessToken)); + ListenableFuture<Response<String>> anonymousHomePosts = api.getSubredditBestPostsListenableFuture(subredditOrUserName, sortType.getType(), sortType.getTime(), loadParams.getKey()); ListenableFuture<LoadResult<String, Post>> pageFuture = Futures.transform(anonymousHomePosts, this::transformData, executor); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java index dfdaaa62..65f8314c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java @@ -270,7 +270,7 @@ public class PostViewModel extends ViewModel { public static class Factory extends ViewModelProvider.NewInstanceFactory { private final Executor executor; private final Retrofit retrofit; - private final String accessToken; + private String accessToken; private String accountName; private final SharedPreferences sharedPreferences; private SharedPreferences postFeedScrolledPositionSharedPreferences; @@ -360,11 +360,10 @@ public class PostViewModel extends ViewModel { } //Anonymous Front Page - public Factory(Executor executor, Retrofit retrofit, String accessToken, SharedPreferences sharedPreferences, + public Factory(Executor executor, Retrofit retrofit, SharedPreferences sharedPreferences, String concatenatedSubredditNames, int postType, SortType sortType, PostFilter postFilter) { this.executor = executor; this.retrofit = retrofit; - this.accessToken = accessToken; this.sharedPreferences = sharedPreferences; this.name = concatenatedSubredditNames; this.postType = postType; @@ -388,7 +387,7 @@ public class PostViewModel extends ViewModel { postFeedScrolledPositionSharedPreferences, postHistorySharedPreferences, name, postType, sortType, postFilter, readPostList); } else if (postType == PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE) { - return (T) new PostViewModel(executor, retrofit, accessToken, null, sharedPreferences, + return (T) new PostViewModel(executor, retrofit, null, null, sharedPreferences, null, null, name, postType, sortType, postFilter, null); } else { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java index 25a3bd82..a430cb14 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/AdvancedPreferenceFragment.java @@ -51,6 +51,9 @@ public class AdvancedPreferenceFragment extends CustomFontPreferenceFragmentComp @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject @Named("sort_type") SharedPreferences mSortTypeSharedPreferences; @Inject @@ -242,9 +245,13 @@ public class AdvancedPreferenceFragment extends CustomFontPreferenceFragmentComp postLayoutEditor.remove(SharedPreferencesUtils.POST_LAYOUT_ALL_POST_LEGACY); postLayoutEditor.remove(SharedPreferencesUtils.POST_LAYOUT_POPULAR_POST_LEGACY); + SharedPreferences.Editor currentAccountEditor = mCurrentAccountSharedPreferences.edit(); + currentAccountEditor.remove(SharedPreferencesUtils.APPLICATION_ONLY_ACCESS_TOKEN_LEGACY); + editor.apply(); sortTypeEditor.apply(); postLayoutEditor.apply(); + currentAccountEditor.apply(); Toast.makeText(activity, R.string.delete_all_legacy_settings_success, Toast.LENGTH_SHORT).show(); }) .setNegativeButton(R.string.no, null) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java index a6480ead..e19e5492 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/FetchSubredditData.java @@ -1,9 +1,10 @@ package ml.docilealligator.infinityforreddit.subreddit; +import android.text.TextUtils; + import androidx.annotation.NonNull; import java.util.ArrayList; -import java.util.HashMap; import java.util.Map; import ml.docilealligator.infinityforreddit.SortType; @@ -15,41 +16,16 @@ import retrofit2.Response; import retrofit2.Retrofit; public class FetchSubredditData { - public static void fetchSubredditData(Retrofit oauthRetrofit, String subredditName, String accessToken, - final FetchSubredditDataListener fetchSubredditDataListener) { - RedditAPI oauthApi = oauthRetrofit.create(RedditAPI.class); - Call<String> subredditData = oauthApi.getSubredditDataOauth(subredditName, APIUtils.getOAuthHeader(accessToken)); - subredditData.enqueue(new Callback<>() { - @Override - public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { - if (response.isSuccessful()) { - ParseSubredditData.parseSubredditData(response.body(), new ParseSubredditData.ParseSubredditDataListener() { - @Override - public void onParseSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { - fetchSubredditDataListener.onFetchSubredditDataSuccess(subredditData, nCurrentOnlineSubscribers); - } - - @Override - public void onParseSubredditDataFail() { - fetchSubredditDataListener.onFetchSubredditDataFail(false); - } - }); - } else { - fetchSubredditDataListener.onFetchSubredditDataFail(response.code() == 403); - } - } - - @Override - public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { - fetchSubredditDataListener.onFetchSubredditDataFail(false); - } - }); - } + public static void fetchSubredditData(Retrofit oauthRetrofit, Retrofit retrofit, String subredditName, String accessToken, final FetchSubredditDataListener fetchSubredditDataListener) { + RedditAPI api = retrofit.create(RedditAPI.class); - public static void fetchSubredditData(Retrofit applicationOnlyOauthRetrofit, String subredditName, - final FetchSubredditDataListener fetchSubredditDataListener) { - Call<String> subredditData = applicationOnlyOauthRetrofit.create(RedditAPI.class) - .getSubredditDataOauth(subredditName, new HashMap<>()); + Call<String> subredditData; + if (oauthRetrofit == null || TextUtils.isEmpty(accessToken)) { + subredditData = api.getSubredditData(subredditName); + } else { + RedditAPI oauthApi = oauthRetrofit.create(RedditAPI.class); + subredditData = oauthApi.getSubredditDataOauth(subredditName, APIUtils.getOAuthHeader(accessToken)); + } subredditData.enqueue(new Callback<>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java index 31c327b1..e8073caf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/subreddit/SubredditSubscription.java @@ -3,7 +3,6 @@ package ml.docilealligator.infinityforreddit.subreddit; import android.os.Handler; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import java.util.HashMap; import java.util.Map; @@ -20,18 +19,18 @@ import retrofit2.Retrofit; public class SubredditSubscription { public static void subscribeToSubreddit(Executor executor, Handler handler, Retrofit oauthRetrofit, - @Nullable String accessToken, String subredditName, - @NonNull String accountName, RedditDataRoomDatabase redditDataRoomDatabase, + Retrofit retrofit, String accessToken, String subredditName, + String accountName, RedditDataRoomDatabase redditDataRoomDatabase, SubredditSubscriptionListener subredditSubscriptionListener) { - subredditSubscription(executor, handler, oauthRetrofit, accessToken, subredditName, + subredditSubscription(executor, handler, oauthRetrofit, retrofit, accessToken, subredditName, accountName, "sub", redditDataRoomDatabase, subredditSubscriptionListener); } - public static void anonymousSubscribeToSubreddit(Executor executor, Handler handler, Retrofit applicationOnlyOauthRetrofit, + public static void anonymousSubscribeToSubreddit(Executor executor, Handler handler, Retrofit retrofit, RedditDataRoomDatabase redditDataRoomDatabase, String subredditName, SubredditSubscriptionListener subredditSubscriptionListener) { - FetchSubredditData.fetchSubredditData(applicationOnlyOauthRetrofit, subredditName, new FetchSubredditData.FetchSubredditDataListener() { + FetchSubredditData.fetchSubredditData(null, retrofit, subredditName, "", new FetchSubredditData.FetchSubredditDataListener() { @Override public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { insertSubscription(executor, handler, redditDataRoomDatabase, @@ -46,10 +45,10 @@ public class SubredditSubscription { } public static void unsubscribeToSubreddit(Executor executor, Handler handler, Retrofit oauthRetrofit, - @Nullable String accessToken, String subredditName, @NonNull String accountName, + String accessToken, String subredditName, String accountName, RedditDataRoomDatabase redditDataRoomDatabase, SubredditSubscriptionListener subredditSubscriptionListener) { - subredditSubscription(executor, handler, oauthRetrofit, accessToken, subredditName, + subredditSubscription(executor, handler, oauthRetrofit, null, accessToken, subredditName, accountName, "unsub", redditDataRoomDatabase, subredditSubscriptionListener); } @@ -61,8 +60,8 @@ public class SubredditSubscription { } private static void subredditSubscription(Executor executor, Handler handler, Retrofit oauthRetrofit, - @Nullable String accessToken, String subredditName, - @NonNull String accountName, String action, + Retrofit retrofit, String accessToken, String subredditName, + String accountName, String action, RedditDataRoomDatabase redditDataRoomDatabase, SubredditSubscriptionListener subredditSubscriptionListener) { RedditAPI api = oauthRetrofit.create(RedditAPI.class); @@ -72,12 +71,12 @@ public class SubredditSubscription { params.put(APIUtils.SR_NAME_KEY, subredditName); Call<String> subredditSubscriptionCall = api.subredditSubscription(APIUtils.getOAuthHeader(accessToken), params); - subredditSubscriptionCall.enqueue(new Callback<>() { + subredditSubscriptionCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { if (action.equals("sub")) { - FetchSubredditData.fetchSubredditData(oauthRetrofit, subredditName, accessToken, new FetchSubredditData.FetchSubredditDataListener() { + FetchSubredditData.fetchSubredditData(oauthRetrofit, retrofit, subredditName, accessToken, new FetchSubredditData.FetchSubredditDataListener() { @Override public void onFetchSubredditDataSuccess(SubredditData subredditData, int nCurrentOnlineSubscribers) { insertSubscription(executor, handler, redditDataRoomDatabase, @@ -113,7 +112,7 @@ public class SubredditSubscription { private static void insertSubscription(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase, - SubredditData subredditData, @NonNull String accountName, + SubredditData subredditData, String accountName, SubredditSubscriptionListener subredditSubscriptionListener) { executor.execute(() -> { SubscribedSubredditData subscribedSubredditData = new SubscribedSubredditData(subredditData.getId(), subredditData.getName(), @@ -130,7 +129,7 @@ public class SubredditSubscription { private static void removeSubscription(Executor executor, Handler handler, RedditDataRoomDatabase redditDataRoomDatabase, - String subredditName, @NonNull String accountName, + String subredditName, String accountName, SubredditSubscriptionListener subredditSubscriptionListener) { executor.execute(() -> { if (accountName.equals(Account.ANONYMOUS_ACCOUNT)) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java index 7fab00f6..b2a69826 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/FetchUserData.java @@ -1,10 +1,8 @@ package ml.docilealligator.infinityforreddit.user; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import java.util.ArrayList; -import java.util.HashMap; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SortType; @@ -15,18 +13,17 @@ import retrofit2.Callback; import retrofit2.Retrofit; public class FetchUserData { - public static void fetchUserData(Retrofit applicationOnlyOauthRetrofit, String userName, - FetchUserDataListener fetchUserDataListener) { - fetchUserData(null, applicationOnlyOauthRetrofit, null, userName, fetchUserDataListener); + public static void fetchUserData(Retrofit retrofit, String userName, FetchUserDataListener fetchUserDataListener) { + fetchUserData(null, retrofit, null, userName, fetchUserDataListener); } - public static void fetchUserData(@Nullable RedditDataRoomDatabase redditDataRoomDatabase, Retrofit retrofit, + public static void fetchUserData(RedditDataRoomDatabase redditDataRoomDatabase, Retrofit retrofit, String accessToken, String userName, FetchUserDataListener fetchUserDataListener) { RedditAPI api = retrofit.create(RedditAPI.class); Call<String> userInfo; - if (accessToken == null) { - userInfo = api.getUserDataOauth(new HashMap<>(), userName); + if (redditDataRoomDatabase == null) { + userInfo = api.getUserData(userName); } else { userInfo = api.getUserDataOauth(APIUtils.getOAuthHeader(accessToken), userName); } @@ -57,9 +54,9 @@ public class FetchUserData { }); } - public static void fetchUserListingData(Retrofit applicationOnlyOauthRetrofit, String query, String after, SortType.Type sortType, boolean nsfw, + public static void fetchUserListingData(Retrofit retrofit, String query, String after, SortType.Type sortType, boolean nsfw, FetchUserListingDataListener fetchUserListingDataListener) { - RedditAPI api = applicationOnlyOauthRetrofit.create(RedditAPI.class); + RedditAPI api = retrofit.create(RedditAPI.class); Call<String> userInfo = api.searchUsers(query, after, sortType, nsfw ? 1 : 0); userInfo.enqueue(new Callback<>() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java index faac787d..260b0222 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserFollowing.java @@ -21,18 +21,18 @@ import retrofit2.Callback; import retrofit2.Retrofit; public class UserFollowing { - public static void followUser(Retrofit oauthRetrofit, Retrofit applicationOnlyOauthRetrofit, + public static void followUser(Retrofit oauthRetrofit, Retrofit retrofit, @Nullable String accessToken, String username, @NonNull String accountName, RedditDataRoomDatabase redditDataRoomDatabase, UserFollowingListener userFollowingListener) { - userFollowing(oauthRetrofit, applicationOnlyOauthRetrofit, accessToken, username, accountName, "sub", + userFollowing(oauthRetrofit, retrofit, accessToken, username, accountName, "sub", redditDataRoomDatabase.subscribedUserDao(), userFollowingListener); } - public static void anonymousFollowUser(Executor executor, Handler handler, Retrofit applicationOnlyOauthRetrofit, - String username, RedditDataRoomDatabase redditDataRoomDatabase, + public static void anonymousFollowUser(Executor executor, Handler handler, Retrofit retrofit, String username, + RedditDataRoomDatabase redditDataRoomDatabase, UserFollowingListener userFollowingListener) { - FetchUserData.fetchUserData(applicationOnlyOauthRetrofit, username, new FetchUserData.FetchUserDataListener() { + FetchUserData.fetchUserData(retrofit, username, new FetchUserData.FetchUserDataListener() { @Override public void onFetchUserDataSuccess(UserData userData, int inboxCount) { executor.execute(() -> { @@ -53,17 +53,17 @@ public class UserFollowing { }); } - public static void unfollowUser(Retrofit oauthRetrofit, Retrofit applicationOnlyOauthRetrofit, + public static void unfollowUser(Retrofit oauthRetrofit, Retrofit retrofit, @Nullable String accessToken, String username, @NonNull String accountName, RedditDataRoomDatabase redditDataRoomDatabase, UserFollowingListener userFollowingListener) { - userFollowing(oauthRetrofit, applicationOnlyOauthRetrofit, accessToken, username, accountName, "unsub", + userFollowing(oauthRetrofit, retrofit, accessToken, username, accountName, "unsub", redditDataRoomDatabase.subscribedUserDao(), userFollowingListener); } public static void anonymousUnfollowUser(Executor executor, Handler handler, String username, - RedditDataRoomDatabase redditDataRoomDatabase, - UserFollowingListener userFollowingListener) { + RedditDataRoomDatabase redditDataRoomDatabase, + UserFollowingListener userFollowingListener) { executor.execute(() -> { redditDataRoomDatabase.subscribedUserDao().deleteSubscribedUser(username, Account.ANONYMOUS_ACCOUNT); @@ -71,7 +71,7 @@ public class UserFollowing { }); } - private static void userFollowing(Retrofit oauthRetrofit, Retrofit applicationOnlyOauthRetrofit, @Nullable String accessToken, + private static void userFollowing(Retrofit oauthRetrofit, Retrofit retrofit, @Nullable String accessToken, String username, @NonNull String accountName, String action, SubscribedUserDao subscribedUserDao, UserFollowingListener userFollowingListener) { RedditAPI api = oauthRetrofit.create(RedditAPI.class); @@ -81,12 +81,12 @@ public class UserFollowing { params.put(APIUtils.SR_NAME_KEY, "u_" + username); Call<String> subredditSubscriptionCall = api.subredditSubscription(APIUtils.getOAuthHeader(accessToken), params); - subredditSubscriptionCall.enqueue(new Callback<>() { + subredditSubscriptionCall.enqueue(new Callback<String>() { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { if (action.equals("sub")) { - FetchUserData.fetchUserData(applicationOnlyOauthRetrofit, username, new FetchUserData.FetchUserDataListener() { + FetchUserData.fetchUserData(retrofit, username, new FetchUserData.FetchUserDataListener() { @Override public void onFetchUserDataSuccess(UserData userData, int inboxCount) { new UpdateSubscriptionAsyncTask(subscribedUserDao, userData, accountName, true).execute(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserListingDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserListingDataSource.java index 825cd0d4..c3d922a7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserListingDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserListingDataSource.java @@ -12,7 +12,7 @@ import retrofit2.Retrofit; public class UserListingDataSource extends PageKeyedDataSource<String, UserData> { - private final Retrofit applicationOnlyOauthRetrofit; + private final Retrofit retrofit; private final String query; private final SortType sortType; private final boolean nsfw; @@ -24,8 +24,8 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData> private PageKeyedDataSource.LoadParams<String> params; private PageKeyedDataSource.LoadCallback<String, UserData> callback; - UserListingDataSource(Retrofit applicationOnlyOauthRetrofit, String query, SortType sortType, boolean nsfw) { - this.applicationOnlyOauthRetrofit = applicationOnlyOauthRetrofit; + UserListingDataSource(Retrofit retrofit, String query, SortType sortType, boolean nsfw) { + this.retrofit = retrofit; this.query = query; this.sortType = sortType; this.nsfw = nsfw; @@ -50,7 +50,7 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData> public void loadInitial(@NonNull PageKeyedDataSource.LoadInitialParams<String> params, @NonNull PageKeyedDataSource.LoadInitialCallback<String, UserData> callback) { initialLoadStateLiveData.postValue(NetworkState.LOADING); - FetchUserData.fetchUserListingData(applicationOnlyOauthRetrofit, query, null, sortType.getType(), nsfw, + FetchUserData.fetchUserListingData(retrofit, query, null, sortType.getType(), nsfw, new FetchUserData.FetchUserListingDataListener() { @Override public void onFetchUserListingDataSuccess(ArrayList<UserData> UserData, String after) { @@ -81,7 +81,7 @@ public class UserListingDataSource extends PageKeyedDataSource<String, UserData> return; } - FetchUserData.fetchUserListingData(applicationOnlyOauthRetrofit, query, params.key, sortType.getType(), nsfw, + FetchUserData.fetchUserListingData(retrofit, query, params.key, sortType.getType(), nsfw, new FetchUserData.FetchUserListingDataListener() { @Override public void onFetchUserListingDataSuccess(ArrayList<UserData> UserData, String after) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserListingDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserListingDataSourceFactory.java index 5bd3a51b..9d52d087 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserListingDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserListingDataSourceFactory.java @@ -8,7 +8,7 @@ import ml.docilealligator.infinityforreddit.SortType; import retrofit2.Retrofit; public class UserListingDataSourceFactory extends DataSource.Factory { - private final Retrofit applicationOnlyOauthRetrofit; + private final Retrofit retrofit; private final String query; private SortType sortType; private final boolean nsfw; @@ -16,8 +16,8 @@ public class UserListingDataSourceFactory extends DataSource.Factory { private UserListingDataSource userListingDataSource; private final MutableLiveData<UserListingDataSource> userListingDataSourceMutableLiveData; - UserListingDataSourceFactory(Retrofit applicationOnlyOauthRetrofit, String query, SortType sortType, boolean nsfw) { - this.applicationOnlyOauthRetrofit = applicationOnlyOauthRetrofit; + UserListingDataSourceFactory(Retrofit retrofit, String query, SortType sortType, boolean nsfw) { + this.retrofit = retrofit; this.query = query; this.sortType = sortType; this.nsfw = nsfw; @@ -27,7 +27,7 @@ public class UserListingDataSourceFactory extends DataSource.Factory { @NonNull @Override public DataSource create() { - userListingDataSource = new UserListingDataSource(applicationOnlyOauthRetrofit, query, sortType, nsfw); + userListingDataSource = new UserListingDataSource(retrofit, query, sortType, nsfw); userListingDataSourceMutableLiveData.postValue(userListingDataSource); return userListingDataSource; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserListingViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserListingViewModel.java index 29c3c0ff..daffdf1e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserListingViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/user/UserListingViewModel.java @@ -21,8 +21,8 @@ public class UserListingViewModel extends ViewModel { private final LiveData<PagedList<UserData>> users; private final MutableLiveData<SortType> sortTypeLiveData; - public UserListingViewModel(Retrofit appliationOnlyOauthRetrofit, String query, SortType sortType, boolean nsfw) { - userListingDataSourceFactory = new UserListingDataSourceFactory(appliationOnlyOauthRetrofit, query, sortType, nsfw); + public UserListingViewModel(Retrofit retrofit, String query, SortType sortType, boolean nsfw) { + userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType, nsfw); initialLoadingState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(), UserListingDataSource::getInitialLoadStateLiveData); @@ -74,13 +74,13 @@ public class UserListingViewModel extends ViewModel { } public static class Factory extends ViewModelProvider.NewInstanceFactory { - private final Retrofit applicationOnlyOauthRetrofit; + private final Retrofit retrofit; private final String query; private final SortType sortType; private final boolean nsfw; - public Factory(Retrofit applicationOnlyOauthRetrofit, String query, SortType sortType, boolean nsfw) { - this.applicationOnlyOauthRetrofit = applicationOnlyOauthRetrofit; + public Factory(Retrofit retrofit, String query, SortType sortType, boolean nsfw) { + this.retrofit = retrofit; this.query = query; this.sortType = sortType; this.nsfw = nsfw; @@ -89,7 +89,7 @@ public class UserListingViewModel extends ViewModel { @NonNull @Override public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { - return (T) new UserListingViewModel(applicationOnlyOauthRetrofit, query, sortType, nsfw); + return (T) new UserListingViewModel(retrofit, query, sortType, nsfw); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/APIUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/APIUtils.java index 5953285f..87ec827e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/APIUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/APIUtils.java @@ -27,7 +27,6 @@ public class APIUtils { public static final String CLIENT_ID_KEY = "client_id"; public static final String CLIENT_SECRET_KEY = "client_secret"; public static final String CLIENT_ID = "NOe2iKrPPzwscA"; - public static final String APPLICATION_ONLY_CLIENT_ID = "PLEASE_USE_YOUR_CLIENT_ID"; public static final String IMGUR_CLIENT_ID = "Client-ID cc671794e0ab397"; public static final String REDGIFS_CLIENT_ID = "1828d0bcc93-15ac-bde6-0005-d2ecbe8daab3"; public static final String REDGIFS_CLIENT_SECRET = "TJBlw7jRXW65NAGgFBtgZHu97WlzRXHYybK81sZ9dLM="; @@ -51,10 +50,7 @@ public class APIUtils { public static final String GRANT_TYPE_KEY = "grant_type"; public static final String GRANT_TYPE_REFRESH_TOKEN = "refresh_token"; public static final String GRANT_TYPE_CLIENT_CREDENTIALS = "client_credentials"; - public static final String GRANT_TYPE_INSTALLED_CLIENT = "https://oauth.reddit.com/grants/installed_client"; public static final String REFRESH_TOKEN_KEY = "refresh_token"; - public static final String DEVICE_ID_KEY = "device_id"; - public static final String DEVICE_ID = "DO_NOT_TRACK_THIS_DEVICE"; public static final String DIR_KEY = "dir"; public static final String ID_KEY = "id"; @@ -109,9 +105,6 @@ public class APIUtils { public static final String NAME_KEY = "name"; - public static final String GILD_TYPE = "gild_type"; - public static final String IS_ANONYMOUS = "is_anonymous"; - public static final String ORIGIN_KEY = "Origin"; public static final String REVEDDIT_ORIGIN = "https://www.reveddit.com"; public static final String REFERER_KEY = "Referer"; @@ -125,14 +118,6 @@ public class APIUtils { return params; } - public static Map<String, String> getApplicationOnlyBasicAuthHeader() { - Map<String, String> params = new HashMap<>(); - String credentials = String.format("%s:%s", APIUtils.APPLICATION_ONLY_CLIENT_ID, ""); - String auth = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); - params.put(APIUtils.AUTHORIZATION_KEY, auth); - return params; - } - public static Map<String, String> getOAuthHeader(String accessToken) { Map<String, String> params = new HashMap<>(); params.put(APIUtils.AUTHORIZATION_KEY, APIUtils.AUTHORIZATION_BASE + accessToken); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java index 15564ce7..4343eaad 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -338,7 +338,6 @@ public class SharedPreferencesUtils { public static final String CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.current_account"; public static final String ACCOUNT_NAME = "account_name"; public static final String ACCESS_TOKEN = "access_token"; - public static final String APPLICATION_ONLY_ACCESS_TOKEN = "app_only_access_token"; public static final String ACCOUNT_IMAGE_URL = "account_image_url"; public static final String REDGIFS_ACCESS_TOKEN = "redgifs_access_token"; @@ -396,4 +395,7 @@ public class SharedPreferencesUtils { public static final String DO_NOT_SHOW_REDDIT_API_INFO_AGAIN_LEGACY = "do_not_show_reddit_api_info_again"; public static final String HIDE_THE_NUMBER_OF_AWARDS_LEGACY = "hide_the_number_of_awards"; public static final String HIDE_COMMENT_AWARDS_LEGACY = "hide_comment_awards"; + + //Current account + public static final String APPLICATION_ONLY_ACCESS_TOKEN_LEGACY = "app_only_access_token"; } |