Skip to content

Commit 911a282

Browse files
authored
Finalize the Kotlinization (facebook#3360)
* Finalize the Kotlinization * Remove extra imports
1 parent 3c124e0 commit 911a282

File tree

4 files changed

+432
-46
lines changed

4 files changed

+432
-46
lines changed

docs/the-new-architecture/backward-compatibility-fabric-components.md

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ my-component
287287
│ │ ├── AndroidManifest.xml
288288
│ │ └── java
289289
│ │ └── com
290-
│ │ └── MyComponent
290+
│ │ └── mycomponent
291291
│ │ ├── MyComponentView.java
292292
│ │ ├── MyComponentViewManagerImpl.java
293293
│ │ └── MyComponentViewPackage.java
@@ -306,8 +306,12 @@ my-component
306306

307307
The code that should go in the `MyComponentViewManagerImpl.java` and that can be shared between the Native Component and the Fabric Native Component is, for example:
308308

309+
<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
310+
<TabItem value="java">
311+
309312
```java title="example of MyComponentViewManager.java"
310-
package com.MyComponent;
313+
package com.mycomponent;
314+
311315
import androidx.annotation.Nullable;
312316
import com.facebook.react.uimanager.ThemedReactContext;
313317

@@ -325,10 +329,34 @@ public class MyComponentViewManagerImpl {
325329
}
326330
```
327331

332+
</TabItem>
333+
<TabItem value="kotlin">
334+
335+
```kotlin title="example of MyComponentViewManager.kt"
336+
package com.mycomponent
337+
338+
import com.facebook.react.uimanager.ThemedReactContext
339+
340+
object MyComponentViewManagerImpl {
341+
const val NAME = "MyComponent"
342+
fun createViewInstance(context: ThemedReactContext?) = MyComponentView(context)
343+
344+
fun setFoo(view: MyComponentView, param: String) {
345+
// implement the logic of the foo function using the view and the param passed.
346+
}
347+
}
348+
```
349+
350+
</TabItem>
351+
</Tabs>
352+
328353
Then, the Native Component and the Fabric Native Component can be updated using the function declared in the shared manager.
329354

330355
For example, for a Native Component:
331356

357+
<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
358+
<TabItem value="java">
359+
332360
```java title="Native Component using the ViewManagerImpl"
333361
public class MyComponentViewManager extends SimpleViewManager<MyComponentView> {
334362

@@ -359,8 +387,34 @@ public class MyComponentViewManager extends SimpleViewManager<MyComponentView> {
359387
}
360388
```
361389

390+
</TabItem>
391+
<TabItem value="kotlin">
392+
393+
```kotlin title="Native Component using the ViewManagerImpl"
394+
class MyComponentViewManager(var context: ReactApplicationContext) : SimpleViewManager<MyComponentView>() {
395+
// Use the static NAME property from the shared implementation
396+
override fun getName() = MyComponentViewManagerImpl.NAME
397+
398+
public override fun createViewInstance(context: ThemedReactContext): MyComponentView =
399+
// static createViewInstance function from the shared implementation
400+
MyComponentViewManagerImpl.createViewInstance(context)
401+
402+
@ReactProp(name = "foo")
403+
fun setFoo(view: MyComponentView, param: String) {
404+
// static custom function from the shared implementation
405+
MyComponentViewManagerImpl.setFoo(view, param)
406+
}
407+
}
408+
```
409+
410+
</TabItem>
411+
</Tabs>
412+
362413
And, for a Fabric Native Component:
363414

415+
<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
416+
<TabItem value="java">
417+
364418
```java title="Fabric Component using the ViewManagerImpl"
365419
// Use the static NAME property from the shared implementation
366420
@ReactModule(name = MyComponentViewManagerImpl.NAME)
@@ -397,11 +451,40 @@ public class MyComponentViewManager extends SimpleViewManager<MyComponentView>
397451
@ReactProp(name = "foo")
398452
public void setFoo(MyComponentView view, @Nullable String param) {
399453
// static custom function from the shared implementation
400-
MyComponentViewManagerImpl.setFoo(view, param]);
454+
MyComponentViewManagerImpl.setFoo(view, param);
401455
}
402456
}
403457
```
404458

459+
</TabItem>
460+
<TabItem value="kotlin">
461+
462+
```kotlin title="Fabric Component using the ViewManagerImpl"
463+
// Use the static NAME property from the shared implementation
464+
@ReactModule(name = MyComponentViewManagerImpl.NAME)
465+
class MyComponentViewManager(context: ReactApplicationContext) : SimpleViewManager<MyComponentView>(), MyComponentViewManagerInterface<MyComponentView> {
466+
private val delegate: ViewManagerDelegate<MyComponentView> = MyComponentViewManagerDelegate(this)
467+
468+
override fun getDelegate(): ViewManagerDelegate<MyComponentView> = delegate
469+
470+
// Use the static NAME property from the shared implementation
471+
override fun getName(): String = MyComponentViewManagerImpl.NAME
472+
473+
override fun createViewInstance(context: ThemedReactContext): MyComponentView =
474+
// static createViewInstance function from the shared implementation
475+
MyComponentViewManagerImpl.createViewInstance(context)
476+
477+
@ReactProp(name = "foo")
478+
override fun setFoo(view: MyComponentView, text: String) {
479+
// static custom function from the shared implementation
480+
MyComponentViewManagerImpl.setFoo(view, param);
481+
}
482+
}
483+
```
484+
485+
</TabItem>
486+
</Tabs>
487+
405488
For a step-by-step example on how to achieve this, have a look at [this repo](https://github.com/react-native-community/RNNewArchitectureLibraries/tree/feat/back-fabric-comp).
406489

407490
## Unify the JavaScript specs

docs/the-new-architecture/backward-compatibility-turbomodules.md

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ my-module
234234
│ │ ├── AndroidManifest.xml
235235
│ │ └── java
236236
│ │ └── com
237-
│ │ └── MyModule
237+
│ │ └── mymodule
238238
│ │ ├── MyModuleImpl.java
239239
│ │ └── MyModulePackage.java
240240
│ ├── newarch
@@ -252,8 +252,11 @@ my-module
252252

253253
The code that should go in the `MyModuleImpl.java`, and that can be shared by the Legacy Native Module and the Turbo Native Module is, for example:
254254

255-
```java title="example of MyModuleImple.java"
256-
package com.MyModule;
255+
<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
256+
<TabItem value="java">
257+
258+
```java title="example of MyModuleImpl.java"
259+
package com.mymodule;
257260

258261
import androidx.annotation.NonNull;
259262
import com.facebook.react.bridge.Promise;
@@ -271,6 +274,29 @@ public class MyModuleImpl {
271274
}
272275
```
273276

277+
</TabItem>
278+
<TabItem value="kotlin">
279+
280+
```kotlin title="example of MyModuleImpl.kt"
281+
package com.mymodule;
282+
283+
import com.facebook.react.bridge.Promise
284+
285+
class MyModuleImpl {
286+
fun foo(a: Double, b: Double, promise: Promise) {
287+
// implement the logic for foo and then invoke
288+
// promise.resolve or promise.reject.
289+
}
290+
291+
companion object {
292+
const val NAME = "MyModule"
293+
}
294+
}
295+
```
296+
297+
</TabItem>
298+
</Tabs>
299+
274300
Then, the Legacy Native Module and the Turbo Native Module can be updated with the following steps:
275301

276302
1. Create a private instance of the `MyModuleImpl` class.
@@ -279,13 +305,16 @@ Then, the Legacy Native Module and the Turbo Native Module can be updated with t
279305

280306
For example, for a Legacy Native Module:
281307

308+
<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
309+
<TabItem value="java">
310+
282311
```java title="Native Module using the Impl module"
283312
public class MyModule extends ReactContextBaseJavaModule {
284313

285314
// declare an instance of the implementation
286315
private MyModuleImpl implementation;
287316

288-
CalculatorModule(ReactApplicationContext context) {
317+
MyModule(ReactApplicationContext context) {
289318
super(context);
290319
// initialize the implementation of the module
291320
implementation = MyModuleImpl();
@@ -305,8 +334,32 @@ public class MyModule extends ReactContextBaseJavaModule {
305334
}
306335
```
307336

337+
</TabItem>
338+
<TabItem value="kotlin">
339+
340+
```kotlin title="Native Module using the Impl module"
341+
class MyModule(context: ReactApplicationContext) : ReactContextBaseJavaModule(context) {
342+
// declare an instance of the implementation and use it in all the methods
343+
private var implementation: MyModuleImpl = MyModuleImpl()
344+
345+
override fun getName(): String = MyModuleImpl.NAME
346+
347+
@ReactMethod
348+
fun foo(a: Double, b: Double, promise: Promise) {
349+
// Use the implementation instance to execute the function.
350+
implementation.foo(a, b, promise)
351+
}
352+
}
353+
```
354+
355+
</TabItem>
356+
</Tabs>
357+
308358
And, for a Turbo Native Module:
309359

360+
<Tabs groupId="android-language" defaultValue={constants.defaultAndroidLanguage} values={constants.androidLanguages}>
361+
<TabItem value="java">
362+
310363
```java title="TurboModule using the Impl module"
311364
public class MyModule extends MyModuleSpec {
312365
// declare an instance of the implementation
@@ -333,6 +386,25 @@ public class MyModule extends MyModuleSpec {
333386
}
334387
```
335388

389+
</TabItem>
390+
<TabItem value="kotlin">
391+
392+
```kotlin title="TurboModule using the Impl module"
393+
class MyModule(reactContext: ReactApplicationContext) : MyModuleSpec(reactContext) {
394+
// declare an instance of the implementation and use it in all the methods
395+
private var implementation: MyModuleImpl = MyModuleImpl()
396+
397+
override fun getName(): String = MyModuleImpl.NAME
398+
399+
override fun add(a: Double, b: Double, promise: Promise) {
400+
implementation.add(a, b, promise)
401+
}
402+
}
403+
```
404+
405+
</TabItem>
406+
</Tabs>
407+
336408
For a step-by-step example on how to achieve this, have a look at [this repo](https://github.com/react-native-community/RNNewArchitectureLibraries/tree/feat/back-turbomodule).
337409

338410
## Unify the JavaScript specs

0 commit comments

Comments
 (0)