Skip to content

Commit

Permalink
fix(react-native-google-cast): add support for kotlin MainActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric committed Jan 19, 2024
1 parent a5adf54 commit 1ade1e8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
2 changes: 1 addition & 1 deletion apps/react-native-google-cast/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function HomeScreen() {
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>State: {castState}</Text>
<CastButton style={{ width: 24, height: 24 }} />
<CastButton style={{ width: 24, height: 24, tintColor: 'black' }} />
<Button
title="Overlay"
onPress={() => {
Expand Down
4 changes: 4 additions & 0 deletions apps/react-native-google-cast/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"compilerOptions": {},
"extends": "expo/tsconfig.base"
}
19 changes: 10 additions & 9 deletions packages/react-native-google-cast/build/withAndroidGoogleCast.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 33 additions & 10 deletions packages/react-native-google-cast/src/withAndroidGoogleCast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,12 @@ const withMainActivityLazyLoading: ConfigPlugin = (config) => {
["com.google.android.gms.cast.framework.CastContext"],
config.modResults.language === "java",
);
if (config.modResults.language === "java") {
config.modResults.contents = addGoogleCastLazyLoadingImport(src).contents;
} else {
throw new Error(
"react-native-google-cast config plugin does not support kotlin MainActivity yet.",
);
}

config.modResults.contents = addGoogleCastLazyLoadingImport(
src,
config.modResults.language,
).contents;

return config;
});
};
Expand Down Expand Up @@ -165,15 +164,39 @@ export const withAndroidGoogleCast: ConfigPlugin<{
return config;
};

function addGoogleCastLazyLoadingImport(src: string) {
const MAIN_ACTIVITY_LANGUAGES: Record<
"java" | "kt",
{ code: string; anchor: RegExp }
> = {
java: {
code: "CastContext.getSharedInstance(this);",
anchor: /super\.onCreate\(\w+\);/,
},
kt: {
code: "CastContext.getSharedInstance(this)",
anchor: /super\.onCreate\(\w+\)/,
},
};

function addGoogleCastLazyLoadingImport(
src: string,
language: keyof typeof MAIN_ACTIVITY_LANGUAGES,
) {
const mainActivity = MAIN_ACTIVITY_LANGUAGES[language];
if (!mainActivity) {
throw new Error(
`react-native-google-cast config plugin does not support MainActivity.${language} yet`,
);
}

const newSrc = [];
newSrc.push(" CastContext.getSharedInstance(this);");
newSrc.push(` ${mainActivity.code}`);

return mergeContents({
tag: "react-native-google-cast-onCreate",
src,
newSrc: newSrc.join("\n"),
anchor: /super\.onCreate\(\w+\);/,
anchor: mainActivity.anchor,
offset: 1,
comment: "//",
});
Expand Down

0 comments on commit 1ade1e8

Please sign in to comment.