Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
640cdea
chore: updating mmkv library
AlexAlexandre Jan 20, 2022
531b35b
feat: updating android ejson to use the getSecureKey new version
AlexAlexandre Jan 20, 2022
8c02eae
feat: updating IOS files to use the getSecureKey new version
AlexAlexandre Jan 24, 2022
4998269
feat: changing all mmkv methods to use sync calls
AlexAlexandre Jan 25, 2022
664ddae
feat: changing mmkv methods from Screen lock
AlexAlexandre Jan 25, 2022
d7702c5
feat: changing all mmkv methods from login, ssl certificate and chang…
AlexAlexandre Jan 25, 2022
3c95008
feat: changing all mmkv methods from login, ssl certificate and chang…
AlexAlexandre Jan 25, 2022
5469b11
feat: changing all mmkv methods from logout
AlexAlexandre Jan 25, 2022
490172b
feat: changing all mmkv methods from e2e
AlexAlexandre Jan 25, 2022
037da6a
fix: small fix at encryption and server drop down
AlexAlexandre Jan 25, 2022
18c4a96
feat: changing all mmkv methods from set theme
AlexAlexandre Jan 25, 2022
5c63e86
feat: changing all mmkv methods from openLink
AlexAlexandre Jan 25, 2022
7e3ca25
fix: setting up mmkv to works property on Android
AlexAlexandre Jan 26, 2022
967ba20
fix: fix an error to set the theme when open the app
AlexAlexandre Jan 26, 2022
f97307a
Merge branch 'develop' into feature/update-mmkv-storage
AlexAlexandre Jan 26, 2022
1600f44
refactor: change the react-native branch (temporary)
AlexAlexandre Jan 26, 2022
d5e452d
refactor: removing all `Async` from mmkv functions name
AlexAlexandre Jan 26, 2022
bc01f7f
Merge branch 'develop' into feature/update-mmkv-storage
AlexAlexandre Jan 27, 2022
ebf92ba
refactor: removing await from unnecessary functions, removing console…
AlexAlexandre Jan 27, 2022
13ee3cd
refactor: removing unnecessary undefined from methods
AlexAlexandre Jan 28, 2022
38a9d39
feat: creating a custom hook for mmkv
AlexAlexandre Jan 28, 2022
2e6c78c
refactor: changing the fetchPasscode to use the useUserPreferences hook
AlexAlexandre Jan 28, 2022
9b2e2ff
refactor: changing setTheme from app/index
AlexAlexandre Jan 28, 2022
ba72a7a
refactor: small fix on setTheme
AlexAlexandre Jan 28, 2022
287ecad
Merge branch 'develop' into feature/update-mmkv-storage
AlexAlexandre Feb 1, 2022
ac5ab54
Merge branch 'develop' into feature/update-mmkv-storage
AlexAlexandre Feb 2, 2022
caab947
chore: update mmkv to 0.6.11
AlexAlexandre Feb 2, 2022
1c3c18d
chore: update mmkv to 0.6.11
AlexAlexandre Feb 2, 2022
bc17b9f
chore: minor tweak
AlexAlexandre Feb 6, 2022
b750b1a
chore: update mmkv to 0.6.12
AlexAlexandre Feb 6, 2022
cd0e42b
chore: mock NativeModules
AlexAlexandre Feb 8, 2022
548ca38
chore: fix test mmkv
AlexAlexandre Feb 8, 2022
38ec7fd
chore: removing custom MMKV JSI module, since is no more necessary af…
AlexAlexandre Feb 8, 2022
cf37d77
Merge branch 'develop' into feature/update-mmkv-storage
AlexAlexandre Feb 10, 2022
aca76e8
feat: removing some async calls from mmkv after update from develop
AlexAlexandre Feb 10, 2022
602ac35
feat: creating a function to get the initialTheme
AlexAlexandre Feb 11, 2022
fb2795c
feat: removing unnecessary try/catch
AlexAlexandre Feb 11, 2022
f539278
fix: fixing the blink white when open the app
AlexAlexandre Feb 11, 2022
6e3ae6b
feat: changing useMMKVStorage to create from mmkv lib
AlexAlexandre Feb 11, 2022
20fbfd2
Merge branch 'develop' into feature/update-mmkv-storage
AlexAlexandre Feb 11, 2022
d5e7d35
test: creating a mock for mmkv create function
AlexAlexandre Feb 11, 2022
b52ac4c
Merge branch 'develop' into feature/update-mmkv-storage
AlexAlexandre Feb 26, 2022
eba55e8
Merge branch 'develop' into feature/update-mmkv-storage
AlexAlexandre Mar 3, 2022
862ae9a
Merge branch 'develop' into feature/update-mmkv-storage
AlexAlexandre Mar 3, 2022
a035bee
Merge branch 'develop' into feature/update-mmkv-storage
AlexAlexandre Mar 8, 2022
74a9632
chore: fix errors on tablet
AlexAlexandre Mar 8, 2022
30ed00e
Merge branch 'develop' into feature/update-mmkv-storage
AlexAlexandre Mar 9, 2022
7889b0b
minor tweak
AlexAlexandre Mar 9, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions __tests__/Storyshots.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ jest.mock('react-native-file-viewer', () => ({
jest.mock('../app/lib/database', () => jest.fn(() => null));
global.Date.now = jest.fn(() => new Date('2019-10-10').getTime());

jest.mock('react-native-mmkv-storage', () => {
return {
Loader: jest.fn().mockImplementation(() => {
return {
setProcessingMode: jest.fn().mockImplementation(() => {
return {
withEncryption: jest.fn().mockImplementation(() => {
return {
initialize: jest.fn()
};
})
};
})
};
}),
create: jest.fn(),
MODES: { MULTI_PROCESS: '' }
};
});

const converter = new Stories2SnapsConverter();

initStoryshots({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.reactnativecommunity.viewpager.RNCViewPagerPackage;
import com.facebook.react.bridge.JSIModulePackage;
import com.swmansion.reanimated.ReanimatedJSIModulePackage;

import org.unimodules.adapters.react.ModuleRegistryAdapter;
import org.unimodules.adapters.react.ReactModuleRegistryProvider;

Expand Down Expand Up @@ -54,7 +53,7 @@ protected String getJSMainModuleName() {

@Override
protected JSIModulePackage getJSIModulePackage() {
return new ReanimatedJSIModulePackage(); // <- add
return new ReanimatedJSIModulePackage();
}

@Override
Expand Down
12 changes: 2 additions & 10 deletions android/app/src/play/java/chat/rocket/reactnative/Ejson.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,8 @@ public Ejson() {
String alias = Utils.toHex("com.MMKV.default");

// Retrieve container password
secureKeystore.getSecureKey(alias, new RNCallback() {
@Override
public void invoke(Object... args) {
String error = (String) args[0];
if (error == null) {
String password = (String) args[1];
mmkv = MMKV.mmkvWithID("default", MMKV.SINGLE_PROCESS_MODE, password);
}
}
});
String password = secureKeystore.getSecureKey(alias);
mmkv = MMKV.mmkvWithID("default", MMKV.SINGLE_PROCESS_MODE, password);
}

public String getAvatarUri() {
Expand Down
10 changes: 2 additions & 8 deletions app/containers/Passcode/PasscodeEnter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { TYPE } from './constants';
import { ATTEMPTS_KEY, LOCKED_OUT_TIMER_KEY, MAX_ATTEMPTS, PASSCODE_KEY } from '../../constants/localAuthentication';
import { biometryAuth, resetAttempts } from '../../utils/localAuthentication';
import { getDiff, getLockedUntil } from './utils';
import UserPreferences from '../../lib/userPreferences';
import { useUserPreferences } from '../../lib/userPreferences';
import I18n from '../../i18n';

interface IPasscodePasscodeEnter {
Expand All @@ -23,16 +23,11 @@ const PasscodeEnter = ({ theme, hasBiometry, finishProcess }: IPasscodePasscodeE
const ref = useRef(null);
let attempts: any = 0;
let lockedUntil: any = false;
const [passcode, setPasscode] = useState(null);
const [passcode] = useUserPreferences(PASSCODE_KEY);
const [status, setStatus] = useState(null);
const { getItem: getAttempts, setItem: setAttempts } = useAsyncStorage(ATTEMPTS_KEY);
const { setItem: setLockedUntil } = useAsyncStorage(LOCKED_OUT_TIMER_KEY);

const fetchPasscode = async () => {
const p: any = await UserPreferences.getStringAsync(PASSCODE_KEY);
setPasscode(p);
};

const biometry = async () => {
if (hasBiometry && status === TYPE.ENTER) {
const result = await biometryAuth();
Expand All @@ -56,7 +51,6 @@ const PasscodeEnter = ({ theme, hasBiometry, finishProcess }: IPasscodePasscodeE
} else {
setStatus(TYPE.ENTER);
}
await fetchPasscode();
biometry();
};

Expand Down
21 changes: 10 additions & 11 deletions app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { KeyCommandsEmitter } from 'react-native-keycommands';
import RNScreens from 'react-native-screens';
import { SafeAreaProvider, initialWindowMetrics } from 'react-native-safe-area-context';

import { defaultTheme, newThemeState, subscribeTheme, unsubscribeTheme } from './utils/theme';
import UserPreferences from './lib/userPreferences';
import { getTheme, initialTheme, newThemeState, subscribeTheme, unsubscribeTheme } from './utils/theme';
import EventEmitter from './utils/events';
import { appInit, appInitLocalSettings, setMasterDetail as setMasterDetailAction } from './actions/app';
import { deepLinkingOpen } from './actions/deepLinking';
Expand All @@ -17,9 +16,9 @@ import store from './lib/createStore';
import { toggleAnalyticsEventsReport, toggleCrashErrorsReport } from './utils/log';
import { ThemeContext } from './theme';
import { DimensionsContext } from './dimensions';
import RocketChat, { THEME_PREFERENCES_KEY } from './lib/rocketchat';
import RocketChat from './lib/rocketchat';
import { MIN_WIDTH_MASTER_DETAIL_LAYOUT } from './constants/tablet';
import { isTablet, supportSystemTheme } from './utils/deviceInfo';
import { isTablet } from './utils/deviceInfo';
import { KEY_COMMAND } from './commands';
import AppContainer from './AppContainer';
import TwoFactor from './containers/TwoFactor';
Expand All @@ -33,6 +32,7 @@ import { isFDroidBuild } from './constants/environment';
import { IThemePreference } from './definitions/ITheme';
import { ICommand } from './definitions/ICommand';
import { initStore } from './lib/auxStore';
import { themes } from './constants/colors';

RNScreens.enableScreens();
initStore(store);
Expand Down Expand Up @@ -88,12 +88,10 @@ export default class Root extends React.Component<{}, IState> {
this.initCrashReport();
}
const { width, height, scale, fontScale } = Dimensions.get('window');
const theme = initialTheme();
this.state = {
theme: defaultTheme(),
themePreferences: {
currentTheme: supportSystemTheme() ? 'automatic' : 'light',
darkLevel: 'black'
},
theme: getTheme(theme),
themePreferences: theme,
width,
height,
scale,
Expand Down Expand Up @@ -128,7 +126,6 @@ export default class Root extends React.Component<{}, IState> {
}

init = async () => {
UserPreferences.getMapAsync(THEME_PREFERENCES_KEY).then((theme: any) => this.setTheme(theme));
store.dispatch(appInitLocalSettings());

// Open app from push notification
Expand Down Expand Up @@ -209,7 +206,9 @@ export default class Root extends React.Component<{}, IState> {
render() {
const { themePreferences, theme, width, height, scale, fontScale } = this.state;
return (
<SafeAreaProvider initialMetrics={initialWindowMetrics}>
<SafeAreaProvider
initialMetrics={initialWindowMetrics}
style={{ backgroundColor: themes[this.state.theme].backgroundColor }}>
<AppearanceProvider>
<Provider store={store}>
<ThemeContext.Provider
Expand Down
10 changes: 5 additions & 5 deletions app/lib/encryption/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ class Encryption {
// Persist keys on UserPreferences
persistKeys = async (server: string, publicKey: string, privateKey: string) => {
this.privateKey = await SimpleCrypto.RSA.importKey(EJSON.parse(privateKey));
await UserPreferences.setStringAsync(`${server}-${E2E_PUBLIC_KEY}`, EJSON.stringify(publicKey));
await UserPreferences.setStringAsync(`${server}-${E2E_PRIVATE_KEY}`, privateKey);
UserPreferences.setString(`${server}-${E2E_PUBLIC_KEY}`, EJSON.stringify(publicKey));
UserPreferences.setString(`${server}-${E2E_PRIVATE_KEY}`, privateKey);
};

// Could not obtain public-private keypair from server.
Expand Down Expand Up @@ -182,9 +182,9 @@ class Encryption {
};

// Create a random password to local created keys
createRandomPassword = async (server: string) => {
createRandomPassword = (server: string) => {
const password = randomPassword();
await UserPreferences.setStringAsync(`${server}-${E2E_RANDOM_PASSWORD_KEY}`, password);
UserPreferences.setString(`${server}-${E2E_RANDOM_PASSWORD_KEY}`, password);
return password;
};

Expand All @@ -194,7 +194,7 @@ class Encryption {

// Encode the private key
const encodedPrivateKey = await this.encodePrivateKey(EJSON.stringify(privateKey), password, this.userId as string);
const publicKey = await UserPreferences.getStringAsync(`${server}-${E2E_PUBLIC_KEY}`);
const publicKey = UserPreferences.getString(`${server}-${E2E_PUBLIC_KEY}`);

// Send the new keys to the server
await RocketChat.e2eSetUserPublicAndPrivateKeys(EJSON.stringify(publicKey), encodedPrivateKey);
Expand Down
30 changes: 15 additions & 15 deletions app/lib/methods/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ import UserPreferences from '../userPreferences';
import { ICertificate, IRocketChat } from '../../definitions';
import sdk from '../rocketchat/services/sdk';

async function removeServerKeys({ server, userId }: { server: string; userId?: string | null }) {
await UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${server}`);
function removeServerKeys({ server, userId }: { server: string; userId?: string | null }) {
UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${server}`);
if (userId) {
await UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${userId}`);
UserPreferences.removeItem(`${RocketChat.TOKEN_KEY}-${userId}`);
}
await UserPreferences.removeItem(`${BASIC_AUTH_KEY}-${server}`);
await UserPreferences.removeItem(`${server}-${E2E_PUBLIC_KEY}`);
await UserPreferences.removeItem(`${server}-${E2E_PRIVATE_KEY}`);
await UserPreferences.removeItem(`${server}-${E2E_RANDOM_PASSWORD_KEY}`);
UserPreferences.removeItem(`${BASIC_AUTH_KEY}-${server}`);
UserPreferences.removeItem(`${server}-${E2E_PUBLIC_KEY}`);
UserPreferences.removeItem(`${server}-${E2E_PRIVATE_KEY}`);
UserPreferences.removeItem(`${server}-${E2E_RANDOM_PASSWORD_KEY}`);
}

async function removeSharedCredentials({ server }: { server: string }) {
// clear certificate for server - SSL Pinning
try {
const certificate = (await UserPreferences.getMapAsync(extractHostname(server))) as ICertificate | null;
const certificate = UserPreferences.getMap(extractHostname(server)) as ICertificate | null;
if (certificate?.path) {
await UserPreferences.removeItem(extractHostname(server));
UserPreferences.removeItem(extractHostname(server));
await FileSystem.deleteAsync(certificate.path);
}
} catch (e) {
Expand All @@ -42,7 +42,7 @@ async function removeServerData({ server }: { server: string }) {
try {
const batch: Model[] = [];
const serversDB = database.servers;
const userId = await UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${server}`);
const userId = UserPreferences.getString(`${RocketChat.TOKEN_KEY}-${server}`);

const usersCollection = serversDB.get('users');
if (userId) {
Expand All @@ -55,14 +55,14 @@ async function removeServerData({ server }: { server: string }) {

await serversDB.write(() => serversDB.batch(...batch));
await removeSharedCredentials({ server });
await removeServerKeys({ server, userId });
removeServerKeys({ server, userId });
} catch (e) {
log(e);
}
}

async function removeCurrentServer() {
await UserPreferences.removeItem(RocketChat.CURRENT_SERVER);
function removeCurrentServer() {
UserPreferences.removeItem(RocketChat.CURRENT_SERVER);
}

async function removeServerDatabase({ server }: { server: string }) {
Expand All @@ -76,9 +76,9 @@ async function removeServerDatabase({ server }: { server: string }) {

export async function removeServer({ server }: { server: string }): Promise<void> {
try {
const userId = await UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${server}`);
const userId = UserPreferences.getString(`${RocketChat.TOKEN_KEY}-${server}`);
if (userId) {
const resume = await UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${userId}`);
const resume = UserPreferences.getString(`${RocketChat.TOKEN_KEY}-${userId}`);

const sdk = new RocketchatClient({ host: server, protocol: 'ddp', useSsl: useSsl(server) });
await sdk.login({ resume });
Expand Down
17 changes: 8 additions & 9 deletions app/lib/rocketchat/rocketchat.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ const RocketChat = {
database.setShareDB(server);

try {
const certificate = await UserPreferences.getStringAsync(`${RocketChat.CERTIFICATE_KEY}-${server}`);
await SSLPinning.setCertificate(certificate, server);
const certificate = UserPreferences.getString(`${RocketChat.CERTIFICATE_KEY}-${server}`);
SSLPinning.setCertificate(certificate, server);
} catch {
// Do nothing
}
Expand Down Expand Up @@ -156,7 +156,7 @@ const RocketChat = {
reduxStore.dispatch(shareSetSettings(this.parseSettings(parsed)));

// set User info
const userId = await UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${server}`);
const userId = UserPreferences.getString(`${RocketChat.TOKEN_KEY}-${server}`);
const userCollections = serversDB.get('users');
let user = null;
if (userId) {
Expand Down Expand Up @@ -457,14 +457,13 @@ const RocketChat = {
}
return JSON.parse(allowAnalyticsEvents);
},
async getSortPreferences() {
const prefs = await UserPreferences.getMapAsync(SORT_PREFS_KEY);
return prefs;
getSortPreferences() {
return UserPreferences.getMap(SORT_PREFS_KEY);
},
async saveSortPreference(param) {
let prefs = await RocketChat.getSortPreferences();
saveSortPreference(param) {
let prefs = RocketChat.getSortPreferences();
prefs = { ...prefs, ...param };
return UserPreferences.setMapAsync(SORT_PREFS_KEY, prefs);
return UserPreferences.setMap(SORT_PREFS_KEY, prefs);
},
getLoginServices,
determineAuthType,
Expand Down
33 changes: 16 additions & 17 deletions app/lib/userPreferences.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,56 @@
import MMKVStorage from 'react-native-mmkv-storage';
import MMKVStorage, { create } from 'react-native-mmkv-storage';

const MMKV = new MMKVStorage.Loader()
// MODES.MULTI_PROCESS = ACCESSIBLE BY APP GROUP (iOS)
.setProcessingMode(MMKVStorage.MODES.MULTI_PROCESS)
.withEncryption()
.initialize();

export const useUserPreferences = create(MMKV);

class UserPreferences {
private mmkv: MMKVStorage.API;
constructor() {
this.mmkv = MMKV;
}

async getStringAsync(key: string) {
getString(key: string): string | null {
try {
const value = await this.mmkv.getStringAsync(key);
return value;
return this.mmkv.getString(key) || null;
} catch {
return null;
}
}

setStringAsync(key: string, value: string) {
return this.mmkv.setStringAsync(key, value);
setString(key: string, value: string): boolean | undefined {
return this.mmkv.setString(key, value);
}

async getBoolAsync(key: string) {
getBool(key: string): boolean | null {
try {
const value = await this.mmkv.getBoolAsync(key);
return value;
return this.mmkv.getBool(key) || null;
} catch {
return null;
}
}

setBoolAsync(key: string, value: boolean) {
return this.mmkv.setBoolAsync(key, value);
setBool(key: string, value: boolean): boolean | undefined {
return this.mmkv.setBool(key, value);
}

async getMapAsync(key: string) {
getMap(key: string): object | null {
try {
const value = await this.mmkv.getMapAsync(key);
return value;
return this.mmkv.getMap(key) || null;
} catch {
return null;
}
}

setMapAsync(key: string, value: object) {
return this.mmkv.setMapAsync(key, value);
setMap(key: string, value: object): boolean | undefined {
return this.mmkv.setMap(key, value);
}

removeItem(key: string) {
removeItem(key: string): boolean | undefined {
return this.mmkv.removeItem(key);
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/sagas/deepLinking.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ const handleOpen = function* handleOpen({ params }) {
}

const [server, user] = yield all([
UserPreferences.getStringAsync(RocketChat.CURRENT_SERVER),
UserPreferences.getStringAsync(`${RocketChat.TOKEN_KEY}-${host}`)
UserPreferences.getString(RocketChat.CURRENT_SERVER),
UserPreferences.getString(`${RocketChat.TOKEN_KEY}-${host}`)
]);

// TODO: needs better test
Expand Down
Loading