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

feat: add amazon oncreate function to expo plugin #2862

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions plugin/build/withIAP.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ConfigPlugin } from 'expo/config-plugins';
type PaymentProvider = 'Amazon AppStore' | 'both' | 'Play Store';
export declare const modifyAppBuildGradle: (buildGradle: string, paymentProvider: PaymentProvider) => string;
export declare const modifyProjectBuildGradle: (buildGradle: string) => string;
export declare const modifyMainActivity: (mainActivity: string, paymentProvider: PaymentProvider) => string;
interface Props {
paymentProvider?: PaymentProvider;
}
Expand Down
29 changes: 28 additions & 1 deletion plugin/build/withIAP.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.modifyProjectBuildGradle = exports.modifyAppBuildGradle = void 0;
exports.modifyMainActivity = exports.modifyProjectBuildGradle = exports.modifyAppBuildGradle = void 0;
const config_plugins_1 = require("expo/config-plugins");
const config_plugins_2 = require("expo/config-plugins");
const pkg = require('../../package.json');
Expand Down Expand Up @@ -31,6 +31,13 @@ const addToBuildGradle = (newLine, anchor, offset, buildGradle) => {
lines.splice(lineIndex + offset, 0, newLine);
return lines.join('\n');
};
const addToMainActivity = (newLine, anchor, offset, mainActivity) => {
const lines = mainActivity.split('\n');
const lineIndex = lines.findIndex((line) => line.match(anchor));
// add after given line
lines.splice(lineIndex + offset, 0, newLine);
return lines.join('\n');
};
const modifyAppBuildGradle = (buildGradle, paymentProvider) => {
if (paymentProvider === 'both') {
if (buildGradle.includes(`flavorDimensions "appstore"`)) {
Expand All @@ -53,6 +60,21 @@ const modifyProjectBuildGradle = (buildGradle) => {
return addToBuildGradle(supportLibVersion, 'ext', 1, buildGradle);
};
exports.modifyProjectBuildGradle = modifyProjectBuildGradle;
const modifyMainActivity = (mainActivity, paymentProvider) => {
// These lines only need to be added if Amazon Store is a target
if (paymentProvider === 'Play Store')
return mainActivity;
const importLine = 'import com.dooboolab.rniap.RNIapActivityListener';
const listener = 'RNIapActivityListener.registerActivity(this)';
if (!mainActivity.includes(importLine)) {
mainActivity = addToMainActivity(importLine, 'import expo.modules.ReactActivityDelegateWrapper', 1, mainActivity);
}
if (!mainActivity.includes(listener)) {
mainActivity = addToMainActivity(listener, 'super.onCreate', 1, mainActivity);
}
return mainActivity;
};
exports.modifyMainActivity = modifyMainActivity;
const withIAPAndroid = (config, { paymentProvider }) => {
// eslint-disable-next-line @typescript-eslint/no-shadow
config = (0, config_plugins_1.withAppBuildGradle)(config, (config) => {
Expand All @@ -64,6 +86,11 @@ const withIAPAndroid = (config, { paymentProvider }) => {
config.modResults.contents = (0, exports.modifyProjectBuildGradle)(config.modResults.contents);
return config;
});
// eslint-disable-next-line @typescript-eslint/no-shadow
config = (0, config_plugins_1.withMainActivity)(config, (config) => {
config.modResults.contents = (0, exports.modifyMainActivity)(config.modResults.contents, paymentProvider);
return config;
});
return config;
};
const withIAP = (config, props) => {
Expand Down
52 changes: 52 additions & 0 deletions plugin/src/withIAP.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
WarningAggregator,
withAppBuildGradle,
withMainActivity,
withProjectBuildGradle,
} from 'expo/config-plugins';
import {ConfigPlugin, createRunOncePlugin} from 'expo/config-plugins';
Expand Down Expand Up @@ -46,6 +47,19 @@ const addToBuildGradle = (
return lines.join('\n');
};

const addToMainActivity = (
newLine: string,
anchor: RegExp | string,
offset: number,
mainActivity: string,
) => {
const lines = mainActivity.split('\n');
const lineIndex = lines.findIndex((line) => line.match(anchor));
// add after given line
lines.splice(lineIndex + offset, 0, newLine);
return lines.join('\n');
};

export const modifyAppBuildGradle = (
buildGradle: string,
paymentProvider: PaymentProvider,
Expand Down Expand Up @@ -82,6 +96,34 @@ export const modifyProjectBuildGradle = (buildGradle: string) => {
return addToBuildGradle(supportLibVersion, 'ext', 1, buildGradle);
};

export const modifyMainActivity = (
mainActivity: string,
paymentProvider: PaymentProvider,
) => {
// These lines only need to be added if Amazon Store is a target
if (paymentProvider === 'Play Store') return mainActivity;

const importLine = 'import com.dooboolab.rniap.RNIapActivityListener';
const listener = 'RNIapActivityListener.registerActivity(this)';
if (!mainActivity.includes(importLine)) {
mainActivity = addToMainActivity(
importLine,
'import expo.modules.ReactActivityDelegateWrapper',
1,
mainActivity,
);
}
if (!mainActivity.includes(listener)) {
mainActivity = addToMainActivity(
listener,
'super.onCreate',
1,
mainActivity,
);
}
return mainActivity;
};

const withIAPAndroid: ConfigPlugin<{paymentProvider: PaymentProvider}> = (
config,
{paymentProvider},
Expand All @@ -102,6 +144,16 @@ const withIAPAndroid: ConfigPlugin<{paymentProvider: PaymentProvider}> = (
);
return config;
});

// eslint-disable-next-line @typescript-eslint/no-shadow
config = withMainActivity(config, (config) => {
config.modResults.contents = modifyMainActivity(
config.modResults.contents,
paymentProvider,
);
return config;
});

return config;
};

Expand Down