diff --git a/src/config/locales/en-US.json b/src/config/locales/en-US.json index ded641bd6..6656bf34f 100644 --- a/src/config/locales/en-US.json +++ b/src/config/locales/en-US.json @@ -63,7 +63,13 @@ "dark_theme": "Dark Theme", "push_notification": "Push Notification", "pincode": "PIN code", - "reset": "Reset" + "reset": "Reset", + "nsfw_content": "NSFW Content", + "nsfw": { + "always_show": "Always show", + "always_hide": "Always hide", + "always_warn": "Always warn" + } }, "voters": { "voters_info": "Voters Info", diff --git a/src/constants/options/nsfw.js b/src/constants/options/nsfw.js new file mode 100644 index 000000000..7f2d275f3 --- /dev/null +++ b/src/constants/options/nsfw.js @@ -0,0 +1,5 @@ +export default [ + 'settings.nsfw.always_show', + 'settings.nsfw.always_warn', + 'settings.nsfw.always_hide', +]; diff --git a/src/realm/realm.js b/src/realm/realm.js index ed60baa5c..93a67d04d 100644 --- a/src/realm/realm.js +++ b/src/realm/realm.js @@ -51,6 +51,7 @@ const settingsSchema = { notification: { type: 'bool', default: true }, server: { type: 'string', default: null }, upvotePercent: { type: 'string', default: null }, + nsfw: { type: 'string', default: null }, }, }; @@ -74,7 +75,7 @@ const authSchema = { const realm = new Realm({ path: 'esteem.realm', schema: [userSchema, authSchema, draftSchema, settingsSchema, applicationSchema, scAccounts], - schemaVersion: 0, + schemaVersion: 1, migration, }); @@ -102,6 +103,7 @@ if (Array.from(settings).length <= 0) { notification: true, server: '', upvotePercent: '1', + nsfw: '0', }); }); } @@ -365,6 +367,29 @@ export const getUpvotePercent = () => new Promise((resolve, reject) => { } }); +export const getNsfw = () => new Promise((resolve, reject) => { + try { + if (settings[0]) { + resolve(settings[0].nsfw); + } else { + resolve(false); + } + } catch (error) { + reject(error); + } +}); + +export const setNsfw = nsfw => new Promise((resolve, reject) => { + try { + realm.write(() => { + settings[0].nsfw = nsfw; + resolve(true); + }); + } catch (error) { + reject(error); + } +}); + export const getTheme = () => new Promise((resolve, reject) => { try { if (settings[0]) { diff --git a/src/redux/actions/applicationActions.js b/src/redux/actions/applicationActions.js index 49ffbd4d4..b539dce52 100644 --- a/src/redux/actions/applicationActions.js +++ b/src/redux/actions/applicationActions.js @@ -15,6 +15,7 @@ import { SET_CURRENCY, SET_LANGUAGE, SET_UPVOTE_PERCENT, + SET_NSFW, } from '../constants/constants'; export const login = payload => ({ @@ -77,6 +78,11 @@ export const setConnectivityStatus = payload => ({ type: IS_CONNECTED, }); +export const setNsfw = payload => ({ + payload, + type: SET_NSFW, +}); + /** * MW */ diff --git a/src/redux/constants/constants.js b/src/redux/constants/constants.js index c692cd191..17015c5ba 100644 --- a/src/redux/constants/constants.js +++ b/src/redux/constants/constants.js @@ -21,6 +21,7 @@ export const SET_API = 'SET_API'; export const SET_CURRENCY = 'SET_CURRENCY'; export const SET_LANGUAGE = 'SET_LANGUAGE'; export const SET_UPVOTE_PERCENT = 'SET_UPVOTE_PERCENT'; +export const SET_NSFW = 'SET_NSFW'; // Accounts export const ADD_OTHER_ACCOUNT = 'ADD_OTHER_ACCOUNT'; diff --git a/src/redux/reducers/applicationReducer.js b/src/redux/reducers/applicationReducer.js index 98bc74d5b..bb60404cd 100644 --- a/src/redux/reducers/applicationReducer.js +++ b/src/redux/reducers/applicationReducer.js @@ -13,6 +13,7 @@ import { SET_CURRENCY, SET_LANGUAGE, SET_UPVOTE_PERCENT, + SET_NSFW, } from '../constants/constants'; const initialState = { @@ -33,6 +34,7 @@ const initialState = { language: 'en-US', loading: false, // It is lock to all screen and shows loading animation. upvotePercent: 1, + nsfw: 'Always show', }; export default function (state = initialState, action) { @@ -101,6 +103,10 @@ export default function (state = initialState, action) { return Object.assign({}, state, { upvotePercent: action.payload, }); + case SET_NSFW: + return Object.assign({}, state, { + nsfw: action.payload, + }); default: return state; } diff --git a/src/screens/application/container/applicationContainer.js b/src/screens/application/container/applicationContainer.js index 71f9242a5..685694186 100644 --- a/src/screens/application/container/applicationContainer.js +++ b/src/screens/application/container/applicationContainer.js @@ -65,6 +65,7 @@ import { setCurrency, setLanguage, setUpvotePercent, + setNsfw, } from '../../../redux/actions/applicationActions'; // Container @@ -250,6 +251,7 @@ class ApplicationContainer extends Component { dispatch(isNotificationOpen(response.notification)); Push.setEnabled(response.notification); } + if (response.nsfw !== '') dispatch(setNsfw(response.nsfw)); dispatch(setCurrency(response.currency !== '' ? response.currency : 'usd')); diff --git a/src/screens/settings/container/settingsContainer.js b/src/screens/settings/container/settingsContainer.js index ed7a955fc..4996886a5 100644 --- a/src/screens/settings/container/settingsContainer.js +++ b/src/screens/settings/container/settingsContainer.js @@ -13,6 +13,7 @@ import { setServer, setNotificationIsOpen, getExistUser, + setNsfw as setNsfw2DB, } from '../../../realm/realm'; // Services and Actions @@ -23,6 +24,7 @@ import { setApi, isDarkTheme, openPinCodeModal, + setNsfw, } from '../../../redux/actions/applicationActions'; import { toastNotification } from '../../../redux/actions/uiAction'; import { setPushToken, getNodes } from '../../../providers/esteem/esteem'; @@ -79,6 +81,11 @@ class SettingsContainer extends Component { this._changeApi(action); break; + case 'nsfw': + dispatch(setNsfw(action)); + setNsfw2DB(action); + break; + default: break; } @@ -112,7 +119,6 @@ class SettingsContainer extends Component { dispatch(toastNotification('Server not available')); isError = true; - this.setState({ apiCheck: false }); return; } } @@ -232,6 +238,7 @@ const mapStateToProps = state => ({ isNotificationSettingsOpen: state.application.isNotificationOpen, isLoggedIn: state.application.isLoggedIn, username: state.account.currentAccount && state.account.currentAccount.name, + nsfw: state.application.nsfw, }); export default connect(mapStateToProps)(SettingsContainer); diff --git a/src/screens/settings/screen/settingsScreen.js b/src/screens/settings/screen/settingsScreen.js index cc9672508..4bda6243b 100644 --- a/src/screens/settings/screen/settingsScreen.js +++ b/src/screens/settings/screen/settingsScreen.js @@ -8,6 +8,7 @@ import { groomingServerName } from '../../../utils/settings'; // Constants import LANGUAGE, { VALUE as LANGUAGE_VALUE } from '../../../constants/options/language'; import CURRENCY, { VALUE as CURRENCY_VALUE } from '../../../constants/options/currency'; +import NSFW from '../../../constants/options/nsfw'; // Components import { BasicHeader } from '../../../components/basicHeader'; @@ -41,6 +42,7 @@ class SettingsScreen extends PureComponent { selectedCurrency, selectedLanguage, serverList, + nsfw, } = this.props; return ( @@ -83,6 +85,18 @@ class SettingsScreen extends PureComponent { defaultText={groomingServerName(selectedApi)} handleOnChange={handleOnChange} /> + intl.formatMessage({ + id: item, + }))} + selectedOptionIndex={parseInt(nsfw, 10)} + handleOnChange={handleOnChange} + />