Add a nsfw toggle in the nav drawer.

This commit is contained in:
Alex Ning 2020-02-20 12:27:51 +08:00
parent 97993709e2
commit 59465390a9
11 changed files with 113 additions and 10 deletions

View File

@ -459,8 +459,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
fab.setVisibility(View.VISIBLE); fab.setVisibility(View.VISIBLE);
} }
boolean nsfwEnabled = mSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false);
adapter = new NavigationDrawerRecyclerViewAdapter(this, mAccountName, mProfileImageUrl, adapter = new NavigationDrawerRecyclerViewAdapter(this, mAccountName, mProfileImageUrl,
mBannerImageUrl, mKarma, new NavigationDrawerRecyclerViewAdapter.ItemClickListener() { mBannerImageUrl, mKarma, nsfwEnabled, new NavigationDrawerRecyclerViewAdapter.ItemClickListener() {
@Override @Override
public void onMenuClick(int stringId) { public void onMenuClick(int stringId) {
Intent intent = null; Intent intent = null;
@ -511,6 +512,18 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
getTheme().applyStyle(R.style.Theme_Default_NormalDark, true); getTheme().applyStyle(R.style.Theme_Default_NormalDark, true);
} }
break; 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: case R.string.settings:
intent = new Intent(MainActivity.this, SettingsActivity.class); intent = new Intent(MainActivity.this, SettingsActivity.class);
break; break;
@ -888,6 +901,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
@Subscribe @Subscribe
public void onChangeNSFWEvent(ChangeNSFWEvent changeNSFWEvent) { public void onChangeNSFWEvent(ChangeNSFWEvent changeNSFWEvent) {
sectionsPagerAdapter.changeNSFW(changeNSFWEvent.nsfw); sectionsPagerAdapter.changeNSFW(changeNSFWEvent.nsfw);
if (adapter != null) {
adapter.setNSFWEnabled(changeNSFWEvent.nsfw);
}
} }
@Subscribe @Subscribe

View File

@ -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_DIVIDER = 3;
private static final int VIEW_TYPE_SUBSCRIBED_SUBREDDIT = 4; private static final int VIEW_TYPE_SUBSCRIBED_SUBREDDIT = 4;
private static final int VIEW_TYPE_ACCOUNT = 5; 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 Context context;
private Resources resources; private Resources resources;
@ -50,6 +50,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
private String userIconUrl; private String userIconUrl;
private String userBannerUrl; private String userBannerUrl;
private int karma; private int karma;
private boolean isNSFWEnabled;
private ItemClickListener itemClickListener; private ItemClickListener itemClickListener;
private boolean isLoggedIn; private boolean isLoggedIn;
private boolean isInMainPage = true; private boolean isInMainPage = true;
@ -57,7 +58,8 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
private ArrayList<Account> accounts; private ArrayList<Account> accounts;
public NavigationDrawerRecyclerViewAdapter(Context context, String accountName, String userIconUrl, 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; this.context = context;
resources = context.getResources(); resources = context.getResources();
glide = Glide.with(context); glide = Glide.with(context);
@ -65,6 +67,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
this.userIconUrl = userIconUrl; this.userIconUrl = userIconUrl;
this.userBannerUrl = userBannerUrl; this.userBannerUrl = userBannerUrl;
this.karma = karma; this.karma = karma;
this.isNSFWEnabled = isNSFWEnabled;
isLoggedIn = accountName != null; isLoggedIn = accountName != null;
this.itemClickListener = itemClickListener; this.itemClickListener = itemClickListener;
} }
@ -73,7 +76,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
public int getItemViewType(int position) { public int getItemViewType(int position) {
if (isInMainPage) { if (isInMainPage) {
if (isLoggedIn) { if (isLoggedIn) {
if (position >= 15) { if (position >= CURRENT_MENU_ITEMS) {
return VIEW_TYPE_SUBSCRIBED_SUBREDDIT; return VIEW_TYPE_SUBSCRIBED_SUBREDDIT;
} else if (position == 0) { } else if (position == 0) {
return VIEW_TYPE_NAV_HEADER; return VIEW_TYPE_NAV_HEADER;
@ -205,6 +208,7 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
} else if (holder instanceof MenuItemViewHolder) { } else if (holder instanceof MenuItemViewHolder) {
int stringId = 0; int stringId = 0;
int drawableId = 0; int drawableId = 0;
boolean setOnClickListener = true;
if (isInMainPage) { if (isInMainPage) {
if (isLoggedIn) { if (isLoggedIn) {
@ -255,6 +259,32 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
} }
break; break;
case 14: 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; stringId = R.string.settings;
drawableId = R.drawable.ic_settings_24dp; 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.setText(stringId);
((MenuItemViewHolder) holder).menuTextView.setCompoundDrawablesWithIntrinsicBounds( ((MenuItemViewHolder) holder).menuTextView.setCompoundDrawablesWithIntrinsicBounds(
drawableId, 0, 0, 0); drawableId, 0, 0, 0);
int finalStringId = stringId; if (setOnClickListener) {
((MenuItemViewHolder) holder).itemView.setOnClickListener(view -> itemClickListener.onMenuClick(finalStringId)); int finalStringId = stringId;
((MenuItemViewHolder) holder).itemView.setOnClickListener(view -> itemClickListener.onMenuClick(finalStringId));
}
} }
} else if (holder instanceof SubscribedThingViewHolder) { } else if (holder instanceof SubscribedThingViewHolder) {
SubscribedSubredditData subreddit = subscribedSubreddits.get(position - CURRENT_MENU_ITEMS); 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) { public void setSubscribedSubreddits(List<SubscribedSubredditData> subscribedSubreddits) {
this.subscribedSubreddits = (ArrayList<SubscribedSubredditData>) subscribedSubreddits; this.subscribedSubreddits = (ArrayList<SubscribedSubredditData>) subscribedSubreddits;
notifyDataSetChanged(); if (isInMainPage) {
notifyDataSetChanged();
}
} }
public void changeAccountsDataset(List<Account> accounts) { public void changeAccountsDataset(List<Account> accounts) {
this.accounts = (ArrayList<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 { class NavHeaderViewHolder extends RecyclerView.ViewHolder {

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M13,2V10H21A8,8 0,0 0,13 2M19.32,15.89C20.37,14.54 21,12.84 21,11H6.44L5.5,9H2V11H4.22C4.22,11 6.11,15.07 6.34,15.42C5.24,16 4.5,17.17 4.5,18.5A3.5,3.5 0,0 0,8 22C9.76,22 11.22,20.7 11.46,19H13.54C13.78,20.7 15.24,22 17,22A3.5,3.5 0,0 0,20.5 18.5C20.5,17.46 20.04,16.53 19.32,15.89M8,20A1.5,1.5 0,0 1,6.5 18.5A1.5,1.5 0,0 1,8 17A1.5,1.5 0,0 1,9.5 18.5A1.5,1.5 0,0 1,8 20M17,20A1.5,1.5 0,0 1,15.5 18.5A1.5,1.5 0,0 1,17 17A1.5,1.5 0,0 1,18.5 18.5A1.5,1.5 0,0 1,17 20Z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M22,10C22,5.6 18.4,2 14,2V10H22M14.2,11H22C22,12.8 21.4,14.5 20.3,15.9C21,16.5 21.4,17.4 21.5,18.3L14.2,11M20.8,22.7L22.1,21.4L2.4,1.7L1.1,3L9.1,11H7.4L6.5,9H3V11H5.2C5.2,11 7.1,15.1 7.3,15.4C6.3,15.9 5.6,16.9 5.5,18C5.2,19.9 6.6,21.7 8.5,22C10.4,22.3 12.2,20.9 12.5,19H14.6C14.7,19.4 14.8,19.8 15,20.2C15.9,21.9 18.1,22.5 19.7,21.6L20.8,22.7M10.5,18.5C10.5,19.3 9.8,20 9,20S7.5,19.3 7.5,18.5 8.2,17 9,17 10.5,17.7 10.5,18.5M18.1,20C17.2,20 16.5,19.3 16.5,18.5V18.4L18.1,20Z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M13,2V10H21A8,8 0,0 0,13 2M19.32,15.89C20.37,14.54 21,12.84 21,11H6.44L5.5,9H2V11H4.22C4.22,11 6.11,15.07 6.34,15.42C5.24,16 4.5,17.17 4.5,18.5A3.5,3.5 0,0 0,8 22C9.76,22 11.22,20.7 11.46,19H13.54C13.78,20.7 15.24,22 17,22A3.5,3.5 0,0 0,20.5 18.5C20.5,17.46 20.04,16.53 19.32,15.89M8,20A1.5,1.5 0,0 1,6.5 18.5A1.5,1.5 0,0 1,8 17A1.5,1.5 0,0 1,9.5 18.5A1.5,1.5 0,0 1,8 20M17,20A1.5,1.5 0,0 1,15.5 18.5A1.5,1.5 0,0 1,17 17A1.5,1.5 0,0 1,18.5 18.5A1.5,1.5 0,0 1,17 20Z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M22,10C22,5.6 18.4,2 14,2V10H22M14.2,11H22C22,12.8 21.4,14.5 20.3,15.9C21,16.5 21.4,17.4 21.5,18.3L14.2,11M20.8,22.7L22.1,21.4L2.4,1.7L1.1,3L9.1,11H7.4L6.5,9H3V11H5.2C5.2,11 7.1,15.1 7.3,15.4C6.3,15.9 5.6,16.9 5.5,18C5.2,19.9 6.6,21.7 8.5,22C10.4,22.3 12.2,20.9 12.5,19H14.6C14.7,19.4 14.8,19.8 15,20.2C15.9,21.9 18.1,22.5 19.7,21.6L20.8,22.7M10.5,18.5C10.5,19.3 9.8,20 9,20S7.5,19.3 7.5,18.5 8.2,17 9,17 10.5,17.7 10.5,18.5M18.1,20C17.2,20 16.5,19.3 16.5,18.5V18.4L18.1,20Z"/>
</vector>

View File

@ -444,4 +444,7 @@
<string name="delete_multi_reddit_success">Delete successfully</string> <string name="delete_multi_reddit_success">Delete successfully</string>
<string name="delete_multi_reddit_failed">Delete failed</string> <string name="delete_multi_reddit_failed">Delete failed</string>
<string name="delete_multi_reddit_dialog_message">Are you sure?</string> <string name="delete_multi_reddit_dialog_message">Are you sure?</string>
<string name="enable_nsfw">Enable NSFW</string>
<string name="disable_nsfw">Disable NSFW</string>
</resources> </resources>

View File

@ -53,13 +53,14 @@
android:entries="@array/settings_lazy_mode_interval" android:entries="@array/settings_lazy_mode_interval"
app:entryValues="@array/settings_lazy_mode_interval_values" app:entryValues="@array/settings_lazy_mode_interval_values"
app:key="lazy_mode_interval" 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:title="@string/settings_lazy_mode_interval_title"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />
<SwitchPreference <SwitchPreference
app:defaultValue="false" app:defaultValue="false"
app:key="nsfw" app:key="nsfw"
app:icon="@drawable/ic_nsfw_on_24dp"
app:title="@string/settings_enable_nsfw_title" /> app:title="@string/settings_enable_nsfw_title" />
<SwitchPreference <SwitchPreference

View File

@ -13,7 +13,7 @@
app:entries="@array/settings_notification_interval" app:entries="@array/settings_notification_interval"
app:entryValues="@array/settings_notification_interval_values" app:entryValues="@array/settings_notification_interval_values"
app:key="notificaiton_interval" app:key="notificaiton_interval"
app:icon="@drawable/ic_outline_access_time_24px" app:icon="@drawable/ic_outline_access_time_24dp"
app:title="@string/settings_notification_interval_title" app:title="@string/settings_notification_interval_title"
app:useSimpleSummaryProvider="true" app:useSimpleSummaryProvider="true"
app:isPreferenceVisible="false" /> app:isPreferenceVisible="false" />