Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: android build fail with rnx kit & treeShaking enabled #380

Merged
merged 5 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
2 changes: 1 addition & 1 deletion src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export { default as KeyboardAwareScrollView } from "./KeyboardAwareScrollView";
export {
default as KeyboardToolbar,
DefaultKeyboardToolbarTheme,
KeyboardToolbarProps,
type KeyboardToolbarProps,
kirillzyusko marked this conversation as resolved.
Show resolved Hide resolved
} from "./KeyboardToolbar";
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export {
// keyboard toolbar
KeyboardToolbar,
DefaultKeyboardToolbarTheme,
KeyboardToolbarProps,
type KeyboardToolbarProps,
} from "./components";
51 changes: 26 additions & 25 deletions src/monkey-patch.android.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
// @ts-expect-error because there is no corresponding type definition
import * as NativeAndroidManager from "react-native/Libraries/Components/StatusBar/NativeStatusBarManagerAndroid";
const RCTStatusBarManagerCompat = require("./specs/NativeStatusBarManagerCompat").default;

const DefaultNativeAndroidManager = NativeAndroidManager.default;
const getConstants = NativeAndroidManager.default.getConstants;
const RCTStatusBarManagerCompat =
require("./specs/NativeStatusBarManagerCompat").default;
// Copy original default manager to keep its original state and methods
const OriginalNativeAndroidManager = {...NativeAndroidManager.default};

// Create a new object that modifies the necessary methods
// On Android < 11 RN uses legacy API which breaks EdgeToEdge mode in RN, so
// in order to use library on all available platforms we have to monkey patch
// default RN implementation and use modern `WindowInsetsControllerCompat`.
export const applyMonkeyPatch = () => {
NativeAndroidManager.default = {
getConstants,
setColor(color: number, animated: boolean): void {
RCTStatusBarManagerCompat.setColor(color, animated);
},

setTranslucent(translucent: boolean): void {
RCTStatusBarManagerCompat.setTranslucent(translucent);
},

/**
const ModifiedNativeAndroidManager = {
...NativeAndroidManager.default, // Spread original properties to keep existing functionality
setColor: (color: number, animated: boolean): void => {
RCTStatusBarManagerCompat.setColor(color, animated);
},
setTranslucent: (translucent: boolean): void => {
RCTStatusBarManagerCompat.setTranslucent(translucent);
},
/**
* - statusBarStyles can be:
* - 'default'
* - 'dark-content'
*/
setStyle(statusBarStyle?: string): void {
RCTStatusBarManagerCompat.setStyle(statusBarStyle);
},
setStyle: (statusBarStyle?: string): void => {
RCTStatusBarManagerCompat.setStyle(statusBarStyle);
},
setHidden: (hidden: boolean): void => {
RCTStatusBarManagerCompat.setHidden(hidden);
},
};

setHidden(hidden: boolean): void {
RCTStatusBarManagerCompat.setHidden(hidden);
},
};
// Define a function to apply the monkey patch
export const applyMonkeyPatch = () => {
Object.assign(NativeAndroidManager.default, ModifiedNativeAndroidManager);
};
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll check how it works today and if everything is okay, then I'll merge it 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had a little help from gpt!


// Define a function to revert changes back to the original state
export const revertMonkeyPatch = () => {
NativeAndroidManager.default = DefaultNativeAndroidManager;
Object.assign(NativeAndroidManager.default, OriginalNativeAndroidManager);
};
Loading