diff --git a/android/app/build.gradle b/android/app/build.gradle index 24247dbe0..a5be6125b 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -204,6 +204,8 @@ dependencies { addUnimodulesDependencies() implementation project(':reactnativenotifications') implementation project(":reactnativekeyboardinput") + implementation project(':rn-user-defaults') + implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "com.facebook.react:react-native:+" // From node_modules implementation "com.google.firebase:firebase-messaging:18.0.0" diff --git a/android/app/src/main/java/chat/rocket/reactnative/MainApplication.java b/android/app/src/main/java/chat/rocket/reactnative/MainApplication.java index 5640f910b..a994e8116 100644 --- a/android/app/src/main/java/chat/rocket/reactnative/MainApplication.java +++ b/android/app/src/main/java/chat/rocket/reactnative/MainApplication.java @@ -14,6 +14,7 @@ import com.facebook.react.ReactPackage; import com.facebook.soloader.SoLoader; import chat.rocket.reactnative.generated.BasePackageList; +import chat.rocket.userdefaults; import org.unimodules.adapters.react.ModuleRegistryAdapter; import org.unimodules.adapters.react.ReactModuleRegistryProvider; @@ -54,6 +55,7 @@ public class MainApplication extends Application implements ReactApplication, IN packages.add(new KeyboardInputPackage(MainApplication.this)); packages.add(new RNNotificationsPackage(MainApplication.this)); packages.add(new ModuleRegistryAdapter(mModuleRegistryProvider)); + packages.add(new RNUserDefaultsPackage()); return packages; } diff --git a/android/settings.gradle b/android/settings.gradle index 060de5091..946da10bb 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -7,4 +7,6 @@ project(':reactnativenotifications').projectDir = new File(rootProject.projectDi include ':reactnativekeyboardinput' project(':reactnativekeyboardinput').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-keyboard-input/lib/android') apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings) +include ':rn-user-defaults' +project(':rn-user-defaults').projectDir = new File(rootProject.projectDir, '../node_modules/rn-user-defaults/android') include ':app' diff --git a/app/constants/native.js b/app/constants/native.js new file mode 100644 index 000000000..149bb23a5 --- /dev/null +++ b/app/constants/native.js @@ -0,0 +1,9 @@ +// This file hosts the contansts with the values used on native apps (User Defaults on iOS and Shared Preferences on Android). +import { isIOS } from '../utils/deviceInfo'; + +export const SERVERS = isIOS ? 'kServers' : 'ACCOUNTS_KEY'; +export const TOKEN = isIOS ? 'kAuthToken' : 'authToken'; +export const USER_ID = isIOS ? 'kUserId' : 'userId'; +export const SERVER_URL = isIOS ? 'kAuthServerURL' : 'serverUrl'; +export const SERVER_NAME = isIOS ? 'kServerName' : 'serverName'; +export const SERVER_ICON = isIOS ? 'kServerIconURL' : 'serverLogoUrl'; diff --git a/app/constants/sharedPreferences.js b/app/constants/sharedPreferences.js deleted file mode 100644 index 0bfa47b3b..000000000 --- a/app/constants/sharedPreferences.js +++ /dev/null @@ -1,6 +0,0 @@ -export const SHARED_PREFERENCES_ACCOUNTS_KEY = 'ACCOUNTS_KEY'; -export const ANDROID_TOKEN = 'authToken'; -export const ANDROID_USER_ID = 'userId'; -export const ANDROID_SERVER_URL = 'serverUrl'; -export const ANDROID_SERVER_NAME = 'serverName'; -export const ANDROID_SERVER_ICON = 'serverLogoUrl'; diff --git a/app/constants/userDefaults.js b/app/constants/userDefaults.js deleted file mode 100644 index e21b7b852..000000000 --- a/app/constants/userDefaults.js +++ /dev/null @@ -1,6 +0,0 @@ -export const SERVERS = 'kServers'; -export const TOKEN = 'kAuthToken'; -export const USER_ID = 'kUserId'; -export const SERVER_URL = 'kAuthServerURL'; -export const SERVER_NAME = 'kServerName'; -export const SERVER_ICON = 'kServerIconURL'; diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 3955f9665..ce6d2e78f 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -40,7 +40,7 @@ import sendMessage, { getMessage, sendMessageCall } from './methods/sendMessage' import { sendFileMessage, cancelUpload, isUploadActive } from './methods/sendFileMessage'; import { getDeviceToken } from '../notifications/push'; -import { SERVERS, SERVER_URL } from '../constants/userDefaults'; +import { SERVERS, SERVER_URL } from '../constants/native'; const TOKEN_KEY = 'reactnativemeteor_usertoken'; const SORT_PREFS_KEY = 'RC_SORT_PREFS_KEY'; diff --git a/app/sagas/init.js b/app/sagas/init.js index e333cee6d..06b2f8acc 100644 --- a/app/sagas/init.js +++ b/app/sagas/init.js @@ -14,16 +14,13 @@ import Navigation from '../lib/Navigation'; import database from '../lib/realm'; import { SERVERS, SERVER_ICON, SERVER_NAME, SERVER_URL, TOKEN, USER_ID -} from '../constants/userDefaults'; +} from '../constants/native'; import { isIOS } from '../utils/deviceInfo'; const restore = function* restore() { try { - let hasMigration; - if (isIOS) { - yield RNUserDefaults.setName('group.ios.chat.rocket'); - hasMigration = yield AsyncStorage.getItem('hasMigration'); - } + yield RNUserDefaults.setName(isIOS ? 'group.ios.chat.rocket' : 'chat.rocket.android'); + const hasMigration = yield AsyncStorage.getItem('hasMigration'); let { token, server } = yield all({ token: RNUserDefaults.get(RocketChat.TOKEN_KEY), @@ -31,7 +28,7 @@ const restore = function* restore() { }); // get native credentials - if (isIOS && !hasMigration) { + if (!hasMigration) { const { serversDB } = database.databases; const servers = yield RNUserDefaults.objectForKey(SERVERS); if (servers) { diff --git a/app/sagas/selectServer.js b/app/sagas/selectServer.js index f6793f998..ff59b90de 100644 --- a/app/sagas/selectServer.js +++ b/app/sagas/selectServer.js @@ -15,7 +15,7 @@ import RocketChat from '../lib/rocketchat'; import database from '../lib/realm'; import log from '../utils/log'; import I18n from '../i18n'; -import { SERVERS, TOKEN, SERVER_URL } from '../constants/userDefaults'; +import { SERVERS, TOKEN, SERVER_URL } from '../constants/native'; const getServerInfo = function* getServerInfo({ server, raiseError = true }) { try { diff --git a/package.json b/package.json index 2428bf30c..e670aa0e1 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,7 @@ "remove-markdown": "^0.3.0", "rn-extensions-share": "^2.3.10", "rn-fetch-blob": "^0.10.16", - "rn-user-defaults": "^1.3.5", + "rn-user-defaults": "^1.4.5", "semver": "6.3.0", "snyk": "1.210.0", "strip-ansi": "5.2.0"