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 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= 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 { + 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 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 subscribedSubreddits) { this.subscribedSubreddits = (ArrayList) subscribedSubreddits; - notifyDataSetChanged(); + if (isInMainPage) { + notifyDataSetChanged(); + } } public void changeAccountsDataset(List accounts) { this.accounts = (ArrayList) 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 { diff --git a/app/src/main/res/drawable-night/ic_nsfw_off_24dp.xml b/app/src/main/res/drawable-night/ic_nsfw_off_24dp.xml new file mode 100644 index 00000000..9f331000 --- /dev/null +++ b/app/src/main/res/drawable-night/ic_nsfw_off_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable-night/ic_nsfw_on_24dp.xml b/app/src/main/res/drawable-night/ic_nsfw_on_24dp.xml new file mode 100644 index 00000000..ccd0b005 --- /dev/null +++ b/app/src/main/res/drawable-night/ic_nsfw_on_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable-night/ic_outline_access_time_24px.xml b/app/src/main/res/drawable-night/ic_outline_access_time_24dp.xml similarity index 100% rename from app/src/main/res/drawable-night/ic_outline_access_time_24px.xml rename to app/src/main/res/drawable-night/ic_outline_access_time_24dp.xml diff --git a/app/src/main/res/drawable/ic_nsfw_off_24dp.xml b/app/src/main/res/drawable/ic_nsfw_off_24dp.xml new file mode 100644 index 00000000..5f69aef8 --- /dev/null +++ b/app/src/main/res/drawable/ic_nsfw_off_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_nsfw_on_24dp.xml b/app/src/main/res/drawable/ic_nsfw_on_24dp.xml new file mode 100644 index 00000000..f03560c8 --- /dev/null +++ b/app/src/main/res/drawable/ic_nsfw_on_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_outline_access_time_24px.xml b/app/src/main/res/drawable/ic_outline_access_time_24dp.xml similarity index 100% rename from app/src/main/res/drawable/ic_outline_access_time_24px.xml rename to app/src/main/res/drawable/ic_outline_access_time_24dp.xml diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7224c2d2..17975d8a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -444,4 +444,7 @@ Delete successfully Delete failed Are you sure? + + Enable NSFW + Disable NSFW diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index 89d0a032..550525ef 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -53,13 +53,14 @@ android:entries="@array/settings_lazy_mode_interval" app:entryValues="@array/settings_lazy_mode_interval_values" app:key="lazy_mode_interval" - app:icon="@drawable/ic_outline_access_time_24px" + app:icon="@drawable/ic_outline_access_time_24dp" app:title="@string/settings_lazy_mode_interval_title" app:useSimpleSummaryProvider="true" />