Library implements a general approach for using PaintCode generated files with React Native on both iOS and Android platforms.
-
Prepare PaintCode files
Generate Swift export for iOS, and Java file for Android.
Convert generated Java file to Kotlin file.
Do not forget to use name "PaintCode" for exported classes.
-
Place exported PaintCode files into projects root folders.
Do not forget to "Configure" project after you added PaintCode.kt file.
-
Install the library from NPM, by following instructions on latest package: https://github.com/ReferenceArmada/react-native-tref-paint-code/packages/
-
Install dependencies for iOS project
$ cd ios
$ pod install
- On native Android project, open MainApplication.java file and add following to the file:
private static void initializePaintCode(Context context) {
try {
Class<?> aClass = Class.forName("be.reference.rnpaintcode.PackageUtil");
Field packageName = aClass.getDeclaredField("packageName");
packageName.setAccessible(true);
packageName.set(aClass, context.getPackageName());
} catch (Exception e) {
e.printStackTrace();
}
}
Add following line to your onCreate() method on MainApplication.java file.
@Override
public void onCreate() {
...
initializePaintCode(this);
...
}
- Ready to go.
- Add required imports
import {TrefPaintCode,
TrefPaintCodeType,
TrefPaintCodeResizingBehaviour,
TrefPaintCodeHelper}
from '@referencearmada/react-native-tref-paint-code';
- Use TrefPaintCode component in views.
There are two required parameters to draw into TrefPaintCode component.
- method: Method name that you want to invoke from native PaintCode generated file
For example: drawView_someButton
- params: Parameter object for that specific method that you wanted to call
Each parameter object should have two properties which are type and value. Usable types are defined in TrefPaintCodeType JS file.
{
resizing:
{
type: TrefPaintCodeType.RESIZING_BEHAVIOUR,
value: TrefPaintCodeResizingBehaviour.ASPECT_FIT
},
radius:
{
type: TrefPaintCodeType.FLOAT,
value: 8
},
title: {
type: TrefPaintCodeType.STRING,
value: "Do something"
}
}
- Use colors in your styles.
const styles = StyleSheet.create({
someStyle: {
backgroundColor: TrefPaintCodeHelper.Colors.light_background_color
}
});
<TrefPaintCode
style={styles.someButton}
method="drawView_someButton"
params={
{
resizing:
{
type: TrefPaintCodeType.RESIZING_BEHAVIOUR,
value: TrefPaintCodeResizingBehaviour.ASPECT_FIT
},
radius:
{
type: TrefPaintCodeType.FLOAT,
value: 8
},
title: {
type: TrefPaintCodeType.STRING,
value: "Do something"
}
}
} />
const styles = StyleSheet.create({
someButton: {
width: 200,
height: 100,
backgroundColor: TrefPaintCodeHelper.Colors.light_background_color
}
});
For both platforms:
- Native module called TrefPaintCodeHelper used for reaching colors defined in PaintCode files
- Native UI component modules called TrefPaintCode used for actual drawing of the graphics
On Android:
- kotlin-reflect library to reach, read set and invoke class members and methods on runtime
- An initializer method to initialize library from the native application
- Kotlin language
On iOS:
- Objective-C runtime to reach, read, set and invoke class members (both Swift and Objective-C) on runtime. Used NSInvocation class.
- Objective-C and Swift together
Using reflection on both platforms, when a TrefPaintCode component is used:
- Method name and method parameters passed to native modules from JS via component
- Native modules takes method and parameters and invokes the method from provided PaintCode files via reflection
- Native UI component module draws invoked method contents to the screen
- Code is not very DRY, refactor is needed
- Work with Java classes instead of Kotlin (converting is sometimes problematic)
- Test more!