Skip to content

Commit ba59c97

Browse files
committed
expo: add expo 50 (beta) support
1 parent 7d5711b commit ba59c97

File tree

5 files changed

+202
-21
lines changed

5 files changed

+202
-21
lines changed

BUILDING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## How to develop
22

3-
### Install dependcies
3+
### Install dependencies
44

55
Install the plugin's dependencies.
66

ios/RNBatch.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#if __has_include(<React/RCTBridgeModule.h>)
22
#import <React/RCTBridgeModule.h>
33
#else
4-
#import "RCTBridgeModule.h"
4+
#import <React/RCTBridgeModule.h>
55
#endif
66

77
#if __has_include(<React/RCTEventEmitter.h>)
88
#import <React/RCTEventEmitter.h>
99
#else
10-
#import "RCTEventEmitter.h"
10+
#import <React/RCTEventEmitter.h>
1111
#endif
1212

1313
#import <Batch/Batch.h>
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { modifyMainActivity } from '../android/withReactNativeBatchMainActivity';
22
import {
3-
mainActivityExpectedFixture,
4-
mainActivityFixture,
3+
mainJavaActivityExpectedFixture,
4+
mainJavaActivityFixture,
5+
mainKotlinActivityExpectedFixture,
6+
mainKotlinActivityFixture,
57
} from '../fixtures/mainActivity';
68

79
describe(modifyMainActivity, () => {
8-
it('should push depedencies in the ProjetGradle file', () => {
9-
const result = modifyMainActivity(mainActivityFixture);
10+
it('should push on new intent in java main activity', () => {
11+
const result = modifyMainActivity(mainJavaActivityFixture);
12+
expect(result).toEqual(mainJavaActivityExpectedFixture);
13+
});
14+
15+
it('should push on new intent in kotlin main activity', () => {
16+
const result = modifyMainActivity(mainKotlinActivityFixture);
1017

11-
expect(result).toEqual(mainActivityExpectedFixture);
18+
expect(result).toEqual(mainKotlinActivityExpectedFixture);
1219
});
1320
});

plugin/src/android/withReactNativeBatchMainActivity.ts

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ConfigPlugin, withMainActivity } from '@expo/config-plugins';
22

3-
export const modifyMainActivity = (content: string): string => {
3+
export const modifyMainJavaActivity = (content: string): string => {
44
let newContent = content;
55

66
if (!newContent.includes('import android.content.Intent;')) {
@@ -19,7 +19,7 @@ import com.batch.android.Batch;`
1919
}
2020

2121
if (!newContent.includes('onNewIntent(')) {
22-
let lastBracketIndex = newContent.lastIndexOf('}');
22+
const lastBracketIndex = newContent.lastIndexOf('}');
2323

2424
const start = newContent.substring(0, lastBracketIndex);
2525
const end = newContent.substring(lastBracketIndex);
@@ -29,10 +29,52 @@ import com.batch.android.Batch;`
2929
`\n @Override
3030
public void onNewIntent(Intent intent)
3131
{
32-
Batch.onNewIntent(this, intent);
3332
super.onNewIntent(intent);
33+
Batch.onNewIntent(this, intent);
3434
}\n\n` +
3535
end;
36+
} else {
37+
newContent = newContent.replace(
38+
'super.onNewIntent(intent);',
39+
`super.onNewIntent(intent);
40+
Batch.onNewIntent(this, intent);`
41+
);
42+
}
43+
44+
return newContent;
45+
};
46+
47+
export const modifyMainKotlinActivity = (content: string): string => {
48+
let newContent = content;
49+
50+
if (!newContent.includes('import android.content.Intent')) {
51+
newContent = content.replace(
52+
'import com.facebook.react.defaults.DefaultReactActivityDelegate',
53+
`import com.facebook.react.defaults.DefaultReactActivityDelegate
54+
import android.content.Intent
55+
import com.batch.android.Batch`
56+
);
57+
} else {
58+
newContent = content.replace(
59+
'import com.facebook.react.defaults.DefaultReactActivityDelegate',
60+
`import com.facebook.react.defaults.DefaultReactActivityDelegate
61+
import com.batch.android.Batch`
62+
);
63+
}
64+
65+
if (!newContent.includes('onNewIntent(')) {
66+
const lastBracketIndex = newContent.lastIndexOf('}');
67+
68+
const start = newContent.substring(0, lastBracketIndex);
69+
const end = newContent.substring(lastBracketIndex);
70+
71+
newContent =
72+
start +
73+
`\n override fun onNewIntent(intent: Intent?) {
74+
super.onNewIntent(intent)
75+
Batch.onNewIntent(this, intent)
76+
}\n` +
77+
end;
3678
} else {
3779
newContent = newContent.replace(
3880
'super.onNewIntent(intent);',
@@ -44,19 +86,22 @@ import com.batch.android.Batch;`
4486
return newContent;
4587
};
4688

47-
export const withReactNativeBatchMainActivity: ConfigPlugin<{} | void> = config => {
48-
const newConfig = withMainActivity(config, config => {
49-
const content = config.modResults.contents;
50-
const newContents = modifyMainActivity(content);
89+
export const modifyMainActivity = (content: string): string => {
90+
return isKotlinMainActivity(content) ? modifyMainKotlinActivity(content) : modifyMainJavaActivity(content);
91+
};
92+
93+
const isKotlinMainActivity = (content: string): boolean => {
94+
return content.includes('class MainActivity : ReactActivity()');
95+
};
5196

97+
export const withReactNativeBatchMainActivity: ConfigPlugin<object | void> = config => {
98+
return withMainActivity(config, config => {
5299
return {
53100
...config,
54101
modResults: {
55102
...config.modResults,
56-
contents: newContents,
103+
contents: modifyMainActivity(config.modResults.contents),
57104
},
58105
};
59106
});
60-
61-
return newConfig;
62107
};

plugin/src/fixtures/mainActivity.ts

Lines changed: 132 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const mainActivityFixture = `package com.poc.bam.tech;
1+
export const mainJavaActivityFixture = `package com.poc.bam.tech;
22
33
import com.facebook.react.ReactActivity;
44
@@ -15,7 +15,7 @@ public class MainActivity extends ReactActivity {
1515
}
1616
`;
1717

18-
export const mainActivityExpectedFixture = `package com.poc.bam.tech;
18+
export const mainJavaActivityExpectedFixture = `package com.poc.bam.tech;
1919
2020
import com.facebook.react.ReactActivity;
2121
import android.content.Intent;
@@ -35,9 +35,138 @@ public class MainActivity extends ReactActivity {
3535
@Override
3636
public void onNewIntent(Intent intent)
3737
{
38-
Batch.onNewIntent(this, intent);
3938
super.onNewIntent(intent);
39+
Batch.onNewIntent(this, intent);
4040
}
4141
4242
}
4343
`;
44+
45+
export const mainKotlinActivityFixture = `package com.arnaudr.expobeta50
46+
47+
import android.os.Build
48+
import android.os.Bundle
49+
50+
import com.facebook.react.ReactActivity
51+
import com.facebook.react.ReactActivityDelegate
52+
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
53+
import com.facebook.react.defaults.DefaultReactActivityDelegate
54+
55+
import expo.modules.ReactActivityDelegateWrapper
56+
57+
class MainActivity : ReactActivity() {
58+
override fun onCreate(savedInstanceState: Bundle?) {
59+
// Set the theme to AppTheme BEFORE onCreate to support
60+
// coloring the background, status bar, and navigation bar.
61+
// This is required for expo-splash-screen.
62+
setTheme(R.style.AppTheme);
63+
super.onCreate(null)
64+
}
65+
66+
/**
67+
* Returns the name of the main component registered from JavaScript. This is used to schedule
68+
* rendering of the component.
69+
*/
70+
override fun getMainComponentName(): String = "main"
71+
72+
/**
73+
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
74+
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
75+
*/
76+
override fun createReactActivityDelegate(): ReactActivityDelegate {
77+
return ReactActivityDelegateWrapper(
78+
this,
79+
BuildConfig.IS_NEW_ARCHITECTURE_ENABLED,
80+
object : DefaultReactActivityDelegate(
81+
this,
82+
mainComponentName,
83+
fabricEnabled
84+
){})
85+
}
86+
87+
/**
88+
* Align the back button behavior with Android S
89+
* where moving root activities to background instead of finishing activities.
90+
*/
91+
override fun invokeDefaultOnBackPressed() {
92+
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
93+
if (!moveTaskToBack(false)) {
94+
// For non-root activities, use the default implementation to finish them.
95+
super.invokeDefaultOnBackPressed()
96+
}
97+
return
98+
}
99+
100+
// Use the default back button implementation on Android S
101+
// because it's doing more than [Activity.moveTaskToBack] in fact.
102+
super.invokeDefaultOnBackPressed()
103+
}
104+
}`;
105+
106+
export const mainKotlinActivityExpectedFixture = `package com.arnaudr.expobeta50
107+
108+
import android.os.Build
109+
import android.os.Bundle
110+
111+
import com.facebook.react.ReactActivity
112+
import com.facebook.react.ReactActivityDelegate
113+
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
114+
import com.facebook.react.defaults.DefaultReactActivityDelegate
115+
import android.content.Intent
116+
import com.batch.android.Batch
117+
118+
import expo.modules.ReactActivityDelegateWrapper
119+
120+
class MainActivity : ReactActivity() {
121+
override fun onCreate(savedInstanceState: Bundle?) {
122+
// Set the theme to AppTheme BEFORE onCreate to support
123+
// coloring the background, status bar, and navigation bar.
124+
// This is required for expo-splash-screen.
125+
setTheme(R.style.AppTheme);
126+
super.onCreate(null)
127+
}
128+
129+
/**
130+
* Returns the name of the main component registered from JavaScript. This is used to schedule
131+
* rendering of the component.
132+
*/
133+
override fun getMainComponentName(): String = "main"
134+
135+
/**
136+
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
137+
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
138+
*/
139+
override fun createReactActivityDelegate(): ReactActivityDelegate {
140+
return ReactActivityDelegateWrapper(
141+
this,
142+
BuildConfig.IS_NEW_ARCHITECTURE_ENABLED,
143+
object : DefaultReactActivityDelegate(
144+
this,
145+
mainComponentName,
146+
fabricEnabled
147+
){})
148+
}
149+
150+
/**
151+
* Align the back button behavior with Android S
152+
* where moving root activities to background instead of finishing activities.
153+
*/
154+
override fun invokeDefaultOnBackPressed() {
155+
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
156+
if (!moveTaskToBack(false)) {
157+
// For non-root activities, use the default implementation to finish them.
158+
super.invokeDefaultOnBackPressed()
159+
}
160+
return
161+
}
162+
163+
// Use the default back button implementation on Android S
164+
// because it's doing more than [Activity.moveTaskToBack] in fact.
165+
super.invokeDefaultOnBackPressed()
166+
}
167+
168+
override fun onNewIntent(intent: Intent?) {
169+
super.onNewIntent(intent)
170+
Batch.onNewIntent(this, intent)
171+
}
172+
}`;

0 commit comments

Comments
 (0)