Skip to content

Commit

Permalink
Support persisted settings in Android + iOS (#34964)
Browse files Browse the repository at this point in the history
Summary:
* Add a DevToolsSettingsManager, which has android and iOS variants, which uses a new TM (Android) or takes advantage of the Settings TM (iOS) to get/set console patch settings
* This is backed by either the existing Settings module (iOS) or a new Java TM, which uses the SharedPreferences AP

## Testing

Manual testing

## Changelog

[General] [Added] - Add DevToolsSettingsManager

Pull Request resolved: #34964

Test Plan: * Extensive manual testing

Reviewed By: NickGerleman

Differential Revision: D40333083

Pulled By: rbalicki2

fbshipit-source-id: f3816e3bd7dea3086f6f2269c3a099af14aebb3b
  • Loading branch information
rbalicki2 authored and facebook-github-bot committed Oct 26, 2022
1 parent adaa4fe commit 0fac981
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Libraries/Core/setUpReactDevTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ if (__DEV__) {
});

const ReactNativeStyleAttributes = require('../Components/View/ReactNativeStyleAttributes');
const devToolsSettingsManager = require('../DevToolsSettings/DevToolsSettingsManager');

reactDevTools.connectToDevTools({
isAppActive,
Expand All @@ -70,6 +71,7 @@ if (__DEV__) {
ReactNativeStyleAttributes,
),
websocket: ws,
devToolsSettingsManager,
});
}
};
Expand Down
13 changes: 13 additions & 0 deletions Libraries/DevToolsSettings/DevToolsSettingsManager.android.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/

import NativeDevToolsSettingsManager from './NativeDevToolsSettingsManager';

module.exports = NativeDevToolsSettingsManager;
33 changes: 33 additions & 0 deletions Libraries/DevToolsSettings/DevToolsSettingsManager.ios.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/

import type {Spec} from './NativeDevToolsSettingsManager';

import Settings from '../Settings/Settings';

const CONSOLE_PATCH_SETTINGS_KEY = 'ReactDevTools::ConsolePatchSettings';

const DevToolsSettingsManager = {
setConsolePatchSettings: (newConsolePatchSettings: string) => {
Settings.set({
[CONSOLE_PATCH_SETTINGS_KEY]: newConsolePatchSettings,
});
},
getConsolePatchSettings: () => {
const value = Settings.get(CONSOLE_PATCH_SETTINGS_KEY);
if (typeof value === 'string') {
// $FlowFixMe[unclear-type]
return ((value: any): string);
}
return null;
},
};

module.exports = (DevToolsSettingsManager: Spec);
22 changes: 22 additions & 0 deletions Libraries/DevToolsSettings/NativeDevToolsSettingsManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/

import type {TurboModule} from '../TurboModule/RCTExport';

import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry';

export interface Spec extends TurboModule {
+setConsolePatchSettings: (newConsolePatchSettings: string) => void;
+getConsolePatchSettings: () => ?string;
}

export default (TurboModuleRegistry.get<Spec>(
'DevToolsSettingsManager',
): ?Spec);
1 change: 1 addition & 0 deletions React/CoreModules/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ rn_apple_library(
"//xplat/js/react-native-github:FBReactNativeSpecApple",
"//xplat/js/react-native-github:RCTLinkingApple",
"//xplat/js/react-native-github:RCTPushNotificationApple",
"//xplat/js/react-native-github:RCTSettingsApple",
"//xplat/js/react-native-github:ReactInternalApple",
],
)
1 change: 1 addition & 0 deletions ReactAndroid/src/main/java/com/facebook/react/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ rn_android_library(
react_native_target("java/com/facebook/react/modules/appearance:appearance"),
react_native_target("java/com/facebook/react/modules/bundleloader:bundleloader"),
react_native_target("java/com/facebook/react/modules/debug:debug"),
react_native_target("java/com/facebook/react/modules/devtoolssettings:devtoolssettings"),
react_native_target("java/com/facebook/react/modules/fabric:fabric"),
react_native_target("java/com/facebook/react/modules/debug:interfaces"),
react_native_target("java/com/facebook/react/modules/deviceinfo:deviceinfo"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_root_target", "react_native_target", "rn_android_library")

rn_android_library(
name = "devtoolssettings",
srcs = glob(["**/*.java"]),
autoglob = False,
labels = [
"pfh:ReactNative_CommonInfrastructurePlaceholder",
"supermodule:xplat/default/public.react_native.infra",
],
language = "JAVA",
provided_deps = [
react_native_dep("third-party/android/androidx:annotation"),
],
visibility = [
"PUBLIC",
],
deps = [
react_native_dep("libraries/fbcore/src/main/java/com/facebook/common/logging:logging"),
react_native_dep("third-party/java/infer-annotations:infer-annotations"),
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/common:common"),
react_native_target("java/com/facebook/react/module/annotations:annotations"),
react_native_target("java/com/facebook/react/modules/core:core"),
],
exported_deps = [react_native_root_target(":FBReactNativeSpec")],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.modules.devtoolssettings;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import androidx.annotation.Nullable;
import com.facebook.fbreact.specs.NativeDevToolsSettingsManagerSpec;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.module.annotations.ReactModule;

@ReactModule(name = DevToolsSettingsManagerModule.NAME)
public class DevToolsSettingsManagerModule extends NativeDevToolsSettingsManagerSpec {
public static final String NAME = "DevToolsSettingsManager";

private static final String SHARED_PREFERENCES_PREFIX = "ReactNative__DevToolsSettings";
private static final String KEY_CONSOLE_PATCH_SETTINGS = "ConsolePatchSettings";

private final SharedPreferences mSharedPreferences;

public DevToolsSettingsManagerModule(ReactApplicationContext reactContext) {
super(reactContext);
mSharedPreferences =
reactContext.getSharedPreferences(SHARED_PREFERENCES_PREFIX, Context.MODE_PRIVATE);
}

@Override
public String getName() {
return NAME;
}

@Override
public @Nullable String getConsolePatchSettings() {
return mSharedPreferences.getString(KEY_CONSOLE_PATCH_SETTINGS, null);
}

@Override
public void setConsolePatchSettings(String newSettings) {
Editor editor = mSharedPreferences.edit();
editor.putString(KEY_CONSOLE_PATCH_SETTINGS, newSettings);
editor.apply();
}
}
1 change: 1 addition & 0 deletions ReactAndroid/src/main/java/com/facebook/react/shell/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ rn_android_library(
react_native_target("java/com/facebook/react/modules/clipboard:clipboard"),
react_native_target("java/com/facebook/react/modules/core:core"),
react_native_target("java/com/facebook/react/modules/debug:debug"),
react_native_target("java/com/facebook/react/modules/devtoolssettings:devtoolssettings"),
react_native_target("java/com/facebook/react/modules/dialog:dialog"),
react_native_target("java/com/facebook/react/modules/fresco:fresco"),
react_native_target("java/com/facebook/react/modules/i18nmanager:i18nmanager"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.facebook.react.modules.blob.FileReaderModule;
import com.facebook.react.modules.camera.ImageStoreManager;
import com.facebook.react.modules.clipboard.ClipboardModule;
import com.facebook.react.modules.devtoolssettings.DevToolsSettingsManagerModule;
import com.facebook.react.modules.dialog.DialogModule;
import com.facebook.react.modules.fresco.FrescoModule;
import com.facebook.react.modules.i18nmanager.I18nManagerModule;
Expand Down Expand Up @@ -145,6 +146,8 @@ public MainReactPackage(MainPackageConfig config) {
return new VibrationModule(context);
case WebSocketModule.NAME:
return new WebSocketModule(context);
case DevToolsSettingsManagerModule.NAME:
return new DevToolsSettingsManagerModule(context);
default:
return null;
}
Expand Down Expand Up @@ -185,7 +188,8 @@ public ReactModuleInfoProvider getReactModuleInfoProvider() {
Class.forName("com.facebook.react.shell.MainReactPackage$$ReactModuleInfoProvider");
return (ReactModuleInfoProvider) reactModuleInfoProviderClass.newInstance();
} catch (ClassNotFoundException e) {
// In OSS case, the annotation processor does not run. We fall back on creating this byhand
// In the OSS case, the annotation processor does not run. We fall back to creating this by
// hand
Class<? extends NativeModule>[] moduleList =
new Class[] {
AccessibilityInfoModule.class,
Expand All @@ -204,6 +208,7 @@ public ReactModuleInfoProvider getReactModuleInfoProvider() {
NativeAnimatedModule.class,
NetworkingModule.class,
PermissionsModule.class,
DevToolsSettingsManagerModule.class,
ShareModule.class,
StatusBarModule.class,
SoundManagerModule.class,
Expand Down
1 change: 1 addition & 0 deletions ReactAndroid/src/test/java/com/facebook/react/modules/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ rn_robolectric_test(
react_native_target("java/com/facebook/react/modules/core:core"),
react_native_target("java/com/facebook/react/modules/debug:debug"),
react_native_target("java/com/facebook/react/modules/deviceinfo:deviceinfo"),
react_native_target("java/com/facebook/react/modules/devtoolssettings:devtoolssettings"),
react_native_target("java/com/facebook/react/modules/dialog:dialog"),
react_native_target("java/com/facebook/react/modules/network:network"),
react_native_target("java/com/facebook/react/modules/share:share"),
Expand Down

0 comments on commit 0fac981

Please sign in to comment.