aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2020-02-20 04:27:51 +0000
committerAlex Ning <chineseperson5@gmail.com>2020-02-20 04:27:51 +0000
commit59465390a9e5ec06bfd8d3b46fbcaeedf560f563 (patch)
treec351ccfef0a4839487b3ee1c151b2588bf6232ad /app/src/main/java/ml
parent97993709e2a2c286583df1cba445a351a7b9d676 (diff)
downloadinfinity-for-reddit-59465390a9e5ec06bfd8d3b46fbcaeedf560f563.tar
infinity-for-reddit-59465390a9e5ec06bfd8d3b46fbcaeedf560f563.tar.gz
infinity-for-reddit-59465390a9e5ec06bfd8d3b46fbcaeedf560f563.tar.bz2
infinity-for-reddit-59465390a9e5ec06bfd8d3b46fbcaeedf560f563.tar.lz
infinity-for-reddit-59465390a9e5ec06bfd8d3b46fbcaeedf560f563.tar.xz
infinity-for-reddit-59465390a9e5ec06bfd8d3b46fbcaeedf560f563.tar.zst
infinity-for-reddit-59465390a9e5ec06bfd8d3b46fbcaeedf560f563.zip
Add a nsfw toggle in the nav drawer.
Diffstat (limited to 'app/src/main/java/ml')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java18
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java61
2 files changed, 71 insertions, 8 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java
index a551d995..2fd3c70c 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/MainActivity.java
@@ -459,8 +459,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
fab.setVisibility(View.VISIBLE);
}
+ boolean nsfwEnabled = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false);
adapter = new NavigationDrawerRecyclerViewAdapter(this, mAccountName, mProfileImageUrl,
- mBannerImageUrl, mKarma, new NavigationDrawerRecyclerViewAdapter.ItemClickListener() {
+ mBannerImageUrl, mKarma, nsfwEnabled, new NavigationDrawerRecyclerViewAdapter.ItemClickListener() {
@Override
public void onMenuClick(int stringId) {
Intent intent = null;
@@ -511,6 +512,18 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
getTheme().applyStyle(R.style.Theme_Default_NormalDark, true);
}
break;
+ case R.string.enable_nsfw:
+ if (sectionsPagerAdapter != null) {
+ mSharedPreferences.edit().putBoolean(SharedPreferencesUtils.NSFW_KEY, true).apply();
+ sectionsPagerAdapter.changeNSFW(true);
+ }
+ break;
+ case R.string.disable_nsfw:
+ if (sectionsPagerAdapter != null) {
+ mSharedPreferences.edit().putBoolean(SharedPreferencesUtils.NSFW_KEY, false).apply();
+ sectionsPagerAdapter.changeNSFW(false);
+ }
+ break;
case R.string.settings:
intent = new Intent(MainActivity.this, SettingsActivity.class);
break;
@@ -888,6 +901,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
@Subscribe
public void onChangeNSFWEvent(ChangeNSFWEvent changeNSFWEvent) {
sectionsPagerAdapter.changeNSFW(changeNSFWEvent.nsfw);
+ if (adapter != null) {
+ adapter.setNSFWEnabled(changeNSFWEvent.nsfw);
+ }
}
@Subscribe
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java
index 59b29e67..ab8dc12c 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/NavigationDrawerRecyclerViewAdapter.java
@@ -41,7 +41,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
private static final int VIEW_TYPE_DIVIDER = 3;
private static final int VIEW_TYPE_SUBSCRIBED_SUBREDDIT = 4;
private static final int VIEW_TYPE_ACCOUNT = 5;
- private static final int CURRENT_MENU_ITEMS = 15;
+ private static final int CURRENT_MENU_ITEMS = 16;
private Context context;
private Resources resources;
@@ -50,6 +50,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
private String userIconUrl;
private String userBannerUrl;
private int karma;
+ private boolean isNSFWEnabled;
private ItemClickListener itemClickListener;
private boolean isLoggedIn;
private boolean isInMainPage = true;
@@ -57,7 +58,8 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
private ArrayList<Account> accounts;
public NavigationDrawerRecyclerViewAdapter(Context context, String accountName, String userIconUrl,
- String userBannerUrl, int karma, ItemClickListener itemClickListener) {
+ String userBannerUrl, int karma, boolean isNSFWEnabled,
+ ItemClickListener itemClickListener) {
this.context = context;
resources = context.getResources();
glide = Glide.with(context);
@@ -65,6 +67,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
this.userIconUrl = userIconUrl;
this.userBannerUrl = userBannerUrl;
this.karma = karma;
+ this.isNSFWEnabled = isNSFWEnabled;
isLoggedIn = accountName != null;
this.itemClickListener = itemClickListener;
}
@@ -73,7 +76,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
public int getItemViewType(int position) {
if (isInMainPage) {
if (isLoggedIn) {
- if (position >= 15) {
+ if (position >= CURRENT_MENU_ITEMS) {
return VIEW_TYPE_SUBSCRIBED_SUBREDDIT;
} else if (position == 0) {
return VIEW_TYPE_NAV_HEADER;
@@ -205,6 +208,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
} else if (holder instanceof MenuItemViewHolder) {
int stringId = 0;
int drawableId = 0;
+ boolean setOnClickListener = true;
if (isInMainPage) {
if (isLoggedIn) {
@@ -255,6 +259,32 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
}
break;
case 14:
+ setOnClickListener = false;
+ if (isNSFWEnabled) {
+ stringId = R.string.disable_nsfw;
+ drawableId = R.drawable.ic_nsfw_off_24dp;
+ } else {
+ stringId = R.string.enable_nsfw;
+ drawableId = R.drawable.ic_nsfw_on_24dp;
+ }
+
+ ((MenuItemViewHolder) holder).itemView.setOnClickListener(view -> {
+ if (isNSFWEnabled) {
+ isNSFWEnabled = false;
+ ((MenuItemViewHolder) holder).menuTextView.setText(R.string.enable_nsfw);
+ ((MenuItemViewHolder) holder).menuTextView.setCompoundDrawablesWithIntrinsicBounds(
+ R.drawable.ic_nsfw_on_24dp, 0, 0, 0);
+ itemClickListener.onMenuClick(R.string.disable_nsfw);
+ } else {
+ isNSFWEnabled = true;
+ ((MenuItemViewHolder) holder).menuTextView.setText(R.string.disable_nsfw);
+ ((MenuItemViewHolder) holder).menuTextView.setCompoundDrawablesWithIntrinsicBounds(
+ R.drawable.ic_nsfw_off_24dp, 0, 0, 0);
+ itemClickListener.onMenuClick(R.string.enable_nsfw);
+ }
+ });
+ break;
+ case 15:
stringId = R.string.settings;
drawableId = R.drawable.ic_settings_24dp;
}
@@ -297,8 +327,10 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
((MenuItemViewHolder) holder).menuTextView.setText(stringId);
((MenuItemViewHolder) holder).menuTextView.setCompoundDrawablesWithIntrinsicBounds(
drawableId, 0, 0, 0);
- int finalStringId = stringId;
- ((MenuItemViewHolder) holder).itemView.setOnClickListener(view -> itemClickListener.onMenuClick(finalStringId));
+ if (setOnClickListener) {
+ int finalStringId = stringId;
+ ((MenuItemViewHolder) holder).itemView.setOnClickListener(view -> itemClickListener.onMenuClick(finalStringId));
+ }
}
} else if (holder instanceof SubscribedThingViewHolder) {
SubscribedSubredditData subreddit = subscribedSubreddits.get(position - CURRENT_MENU_ITEMS);
@@ -369,12 +401,27 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
public void setSubscribedSubreddits(List<SubscribedSubredditData> subscribedSubreddits) {
this.subscribedSubreddits = (ArrayList<SubscribedSubredditData>) subscribedSubreddits;
- notifyDataSetChanged();
+ if (isInMainPage) {
+ notifyDataSetChanged();
+ }
}
public void changeAccountsDataset(List<Account> accounts) {
this.accounts = (ArrayList<Account>) accounts;
- notifyDataSetChanged();
+ if (!isInMainPage) {
+ notifyDataSetChanged();
+ }
+ }
+
+ public void setNSFWEnabled(boolean isNSFWEnabled) {
+ this.isNSFWEnabled = isNSFWEnabled;
+ if (isInMainPage) {
+ if (isLoggedIn) {
+ notifyItemChanged(CURRENT_MENU_ITEMS - 2);
+ } else {
+ notifyItemChanged(2);
+ }
+ }
}
class NavHeaderViewHolder extends RecyclerView.ViewHolder {