From dd98dd879dc5ecb3e513718c990bf9b3e4e66c71 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 4 Apr 2021 12:04:06 +0800 Subject: Anonymous subscriptions page is available. --- .../infinityforreddit/AppComponent.java | 3 + .../infinityforreddit/FavoriteThing.java | 78 +++++++++------- .../activities/AnonymousSubscriptionsActivity.java | 102 +++++++++++++++++++++ .../infinityforreddit/activities/MainActivity.java | 6 +- .../NavigationDrawerRecyclerViewAdapter.java | 12 ++- .../SubscribedSubredditsRecyclerViewAdapter.java | 54 +++++------ .../SubscribedSubredditsListingFragment.java | 4 +- 7 files changed, 192 insertions(+), 67 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/activities/AnonymousSubscriptionsActivity.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 8ac039ea..b0506a82 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -5,6 +5,7 @@ import javax.inject.Singleton; import dagger.Component; import ml.docilealligator.infinityforreddit.activities.AccountPostsActivity; import ml.docilealligator.infinityforreddit.activities.AccountSavedThingActivity; +import ml.docilealligator.infinityforreddit.activities.AnonymousSubscriptionsActivity; import ml.docilealligator.infinityforreddit.activities.CommentActivity; import ml.docilealligator.infinityforreddit.activities.CreateMultiRedditActivity; import ml.docilealligator.infinityforreddit.activities.CustomThemeListingActivity; @@ -260,4 +261,6 @@ public interface AppComponent { void inject(WebViewActivity webViewActivity); void inject(CrashReportsFragment crashReportsFragment); + + void inject(AnonymousSubscriptionsActivity anonymousSubscriptionsActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java index 8a041e3b..29aace70 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FavoriteThing.java @@ -23,50 +23,60 @@ public class FavoriteThing { RedditDataRoomDatabase redditDataRoomDatabase, String accessToken, SubscribedSubredditData subscribedSubredditData, FavoriteThingListener favoriteThingListener) { - Map params = new HashMap<>(); - params.put(APIUtils.SR_NAME_KEY, subscribedSubredditData.getName()); - params.put(APIUtils.MAKE_FAVORITE_KEY, "true"); - oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, subscribedSubredditData, - favoriteThingListener::success); - } else { - favoriteThingListener.failed(); + if (accessToken == null) { + InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, subscribedSubredditData, + favoriteThingListener::success); + } else { + Map params = new HashMap<>(); + params.put(APIUtils.SR_NAME_KEY, subscribedSubredditData.getName()); + params.put(APIUtils.MAKE_FAVORITE_KEY, "true"); + oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, subscribedSubredditData, + favoriteThingListener::success); + } else { + favoriteThingListener.failed(); + } } - } - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - favoriteThingListener.failed(); - } - }); + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + favoriteThingListener.failed(); + } + }); + } } public static void unfavoriteSubreddit(Executor executor, Handler handler, Retrofit oauthRetrofit, RedditDataRoomDatabase redditDataRoomDatabase, String accessToken, SubscribedSubredditData subscribedSubredditData, FavoriteThingListener favoriteThingListener) { - Map params = new HashMap<>(); - params.put(APIUtils.SR_NAME_KEY, subscribedSubredditData.getName()); - params.put(APIUtils.MAKE_FAVORITE_KEY, "false"); - oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, - subscribedSubredditData, favoriteThingListener::success); - } else { - favoriteThingListener.failed(); + if (accessToken == null) { + InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, + subscribedSubredditData, favoriteThingListener::success); + } else { + Map params = new HashMap<>(); + params.put(APIUtils.SR_NAME_KEY, subscribedSubredditData.getName()); + params.put(APIUtils.MAKE_FAVORITE_KEY, "false"); + oauthRetrofit.create(RedditAPI.class).favoriteThing(APIUtils.getOAuthHeader(accessToken), params).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + InsertSubscribedThings.insertSubscribedThings(executor, handler, redditDataRoomDatabase, + subscribedSubredditData, favoriteThingListener::success); + } else { + favoriteThingListener.failed(); + } } - } - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - favoriteThingListener.failed(); - } - }); + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + favoriteThingListener.failed(); + } + }); + } } public static void favoriteUser(Executor executor, Handler handler, Retrofit oauthRetrofit, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AnonymousSubscriptionsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AnonymousSubscriptionsActivity.java new file mode 100644 index 00000000..2c40b1e8 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AnonymousSubscriptionsActivity.java @@ -0,0 +1,102 @@ +package ml.docilealligator.infinityforreddit.activities; + +import android.content.SharedPreferences; +import android.os.Build; +import android.os.Bundle; +import android.view.View; +import android.view.Window; +import android.view.WindowManager; + +import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; + +import com.google.android.material.appbar.AppBarLayout; +import com.r0adkll.slidr.Slidr; + +import javax.inject.Inject; +import javax.inject.Named; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.fragments.SubscribedSubredditsListingFragment; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; + +public class AnonymousSubscriptionsActivity extends BaseActivity { + + @BindView(R.id.coordinator_layout_anonymous_subscriptions_activity) + CoordinatorLayout coordinatorLayout; + @BindView(R.id.appbar_layout_anonymous_subscriptions_activity) + AppBarLayout appBarLayout; + @BindView(R.id.toolbar_anonymous_subscriptions_activity) + Toolbar toolbar; + @Inject + @Named("default") + SharedPreferences mSharedPreferences; + @Inject + CustomThemeWrapper mCustomThemeWrapper; + + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + + setImmersiveModeNotApplicable(); + + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_anonymous_subscriptions); + + ButterKnife.bind(this); + + applyCustomTheme(); + + if (mSharedPreferences.getBoolean(SharedPreferencesUtils.SWIPE_RIGHT_TO_GO_BACK, true)) { + Slidr.attach(this); + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + Window window = getWindow(); + + if (isChangeStatusBarIconColor()) { + addOnOffsetChangedListener(appBarLayout); + } + + if (isImmersiveInterface()) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + coordinatorLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | + View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | + View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION); + } else { + window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); + } + adjustToolbar(toolbar); + } + } + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + SubscribedSubredditsListingFragment mFragment = new SubscribedSubredditsListingFragment(); + Bundle bundle = new Bundle(); + bundle.putString(SubscribedSubredditsListingFragment.EXTRA_ACCOUNT_NAME, "-"); + mFragment.setArguments(bundle); + getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_anonymous_subscriptions_activity, mFragment).commit(); + } + + @Override + protected SharedPreferences getDefaultSharedPreferences() { + return mSharedPreferences; + } + + @Override + protected CustomThemeWrapper getCustomThemeWrapper() { + return mCustomThemeWrapper; + } + + @Override + protected void applyCustomTheme() { + coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); + applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index 0ea37503..2b2aa74d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -733,7 +733,11 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb intent = new Intent(MainActivity.this, ViewUserDetailActivity.class); intent.putExtra(ViewUserDetailActivity.EXTRA_USER_NAME_KEY, mAccountName); } else if (stringId == R.string.subscriptions) { - intent = new Intent(MainActivity.this, SubscribedThingListingActivity.class); + if (mAccessToken != null) { + intent = new Intent(MainActivity.this, SubscribedThingListingActivity.class); + } else { + intent = new Intent(MainActivity.this, AnonymousSubscriptionsActivity.class); + } } else if (stringId == R.string.multi_reddit) { intent = new Intent(MainActivity.this, SubscribedThingListingActivity.class); intent.putExtra(SubscribedThingListingActivity.EXTRA_SHOW_MULTIREDDITS, true); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java index d89c0160..2b53c4ca 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java @@ -483,6 +483,10 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter itemClickListener.onClick(null, null, false)); return; - } else if (itemClickListener != null && !hasClearSelectionRow && viewHolder.getAdapterPosition() == 0) { + } else if (itemClickListener != null && !hasClearSelectionRow && viewHolder.getBindingAdapterPosition() == 0) { ((SubredditViewHolder) viewHolder).favoriteImageView.setVisibility(View.GONE); name = username; iconUrl = userIconUrl; viewHolder.itemView.setOnClickListener(view -> itemClickListener.onClick(name, iconUrl, true)); - } else if (hasClearSelectionRow && viewHolder.getAdapterPosition() == 1) { + } else if (hasClearSelectionRow && viewHolder.getBindingAdapterPosition() == 1) { ((SubredditViewHolder) viewHolder).favoriteImageView.setVisibility(View.GONE); name = username; iconUrl = userIconUrl; @@ -182,24 +182,24 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte mFavoriteSubscribedSubredditData.size() + 2 : 0; } - name = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).getName(); - iconUrl = mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).getIconUrl(); - if(mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).isFavorite()) { + name = mSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset).getName(); + iconUrl = mSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset).getIconUrl(); + if(mSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset).isFavorite()) { ((SubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); } else { ((SubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); } ((SubredditViewHolder) viewHolder).favoriteImageView.setOnClickListener(view -> { - if(mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).isFavorite()) { + if(mSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset).isFavorite()) { ((SubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); - mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).setFavorite(false); + mSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset).setFavorite(false); FavoriteThing.unfavoriteSubreddit(mExecutor, new Handler(), mOauthRetrofit, mRedditDataRoomDatabase, accessToken, - mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset), + mSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset), new FavoriteThing.FavoriteThingListener() { @Override public void success() { - int position = viewHolder.getAdapterPosition() - offset; + int position = viewHolder.getBindingAdapterPosition() - offset; if(position >= 0 && mSubscribedSubredditData.size() > position) { mSubscribedSubredditData.get(position).setFavorite(false); } @@ -209,7 +209,7 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte @Override public void failed() { Toast.makeText(mContext, R.string.thing_unfavorite_failed, Toast.LENGTH_SHORT).show(); - int position = viewHolder.getAdapterPosition() - offset; + int position = viewHolder.getBindingAdapterPosition() - offset; if(position >= 0 && mSubscribedSubredditData.size() > position) { mSubscribedSubredditData.get(position).setFavorite(true); } @@ -218,14 +218,14 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte }); } else { ((SubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); - mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).setFavorite(true); + mSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset).setFavorite(true); FavoriteThing.favoriteSubreddit(mExecutor, new Handler(), mOauthRetrofit, mRedditDataRoomDatabase, accessToken, - mSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset), + mSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset), new FavoriteThing.FavoriteThingListener() { @Override public void success() { - int position = viewHolder.getAdapterPosition() - offset; + int position = viewHolder.getBindingAdapterPosition() - offset; if(position >= 0 && mSubscribedSubredditData.size() > position) { mSubscribedSubredditData.get(position).setFavorite(true); } @@ -235,7 +235,7 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte @Override public void failed() { Toast.makeText(mContext, R.string.thing_favorite_failed, Toast.LENGTH_SHORT).show(); - int position = viewHolder.getAdapterPosition() - offset; + int position = viewHolder.getBindingAdapterPosition() - offset; if(position >= 0 && mSubscribedSubredditData.size() > position) { mSubscribedSubredditData.get(position).setFavorite(false); } @@ -281,24 +281,24 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte } else { offset = 1; } - String name = mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).getName(); - String iconUrl = mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).getIconUrl(); - if(mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).isFavorite()) { + String name = mFavoriteSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset).getName(); + String iconUrl = mFavoriteSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset).getIconUrl(); + if(mFavoriteSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset).isFavorite()) { ((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); } else { ((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); } ((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setOnClickListener(view -> { - if(mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).isFavorite()) { + if(mFavoriteSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset).isFavorite()) { ((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_border_24dp); - mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).setFavorite(false); + mFavoriteSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset).setFavorite(false); FavoriteThing.unfavoriteSubreddit(mExecutor, new Handler(), mOauthRetrofit, mRedditDataRoomDatabase, accessToken, - mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset), + mFavoriteSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset), new FavoriteThing.FavoriteThingListener() { @Override public void success() { - int position = viewHolder.getAdapterPosition() - 1; + int position = viewHolder.getBindingAdapterPosition() - 1; if(position >= 0 && mFavoriteSubscribedSubredditData.size() > position) { mFavoriteSubscribedSubredditData.get(position).setFavorite(false); } @@ -308,7 +308,7 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte @Override public void failed() { Toast.makeText(mContext, R.string.thing_unfavorite_failed, Toast.LENGTH_SHORT).show(); - int position = viewHolder.getAdapterPosition() - 1; + int position = viewHolder.getBindingAdapterPosition() - 1; if(position >= 0 && mFavoriteSubscribedSubredditData.size() > position) { mFavoriteSubscribedSubredditData.get(position).setFavorite(true); } @@ -317,13 +317,13 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte }); } else { ((FavoriteSubredditViewHolder) viewHolder).favoriteImageView.setImageResource(R.drawable.ic_favorite_24dp); - mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset).setFavorite(true); + mFavoriteSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset).setFavorite(true); FavoriteThing.favoriteSubreddit(mExecutor, new Handler(), mOauthRetrofit, mRedditDataRoomDatabase, accessToken, - mFavoriteSubscribedSubredditData.get(viewHolder.getAdapterPosition() - offset), + mFavoriteSubscribedSubredditData.get(viewHolder.getBindingAdapterPosition() - offset), new FavoriteThing.FavoriteThingListener() { @Override public void success() { - int position = viewHolder.getAdapterPosition() - 1; + int position = viewHolder.getBindingAdapterPosition() - 1; if(position >= 0 && mFavoriteSubscribedSubredditData.size() > position) { mFavoriteSubscribedSubredditData.get(position).setFavorite(true); } @@ -333,7 +333,7 @@ public class SubscribedSubredditsRecyclerViewAdapter extends RecyclerView.Adapte @Override public void failed() { Toast.makeText(mContext, R.string.thing_favorite_failed, Toast.LENGTH_SHORT).show(); - int position = viewHolder.getAdapterPosition() - 1; + int position = viewHolder.getBindingAdapterPosition() - 1; if(position >= 0 && mFavoriteSubscribedSubredditData.size() > position) { mFavoriteSubscribedSubredditData.get(position).setFavorite(false); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubscribedSubredditsListingFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubscribedSubredditsListingFragment.java index 5275cb87..7bb69a76 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubscribedSubredditsListingFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/SubscribedSubredditsListingFragment.java @@ -146,7 +146,9 @@ public class SubscribedSubredditsListingFragment extends Fragment implements Fra mGlide.clear(mImageView); } - adapter.addUser(accountName, getArguments().getString(EXTRA_ACCOUNT_PROFILE_IMAGE_URL)); + if (accessToken != null) { + adapter.addUser(accountName, getArguments().getString(EXTRA_ACCOUNT_PROFILE_IMAGE_URL)); + } adapter.setSubscribedSubreddits(subscribedSubredditData); }); -- cgit v1.2.3