|
| 1 | +## Getting started |
| 2 | + |
| 3 | +Library implements a general approach for using PaintCode generated files from React Native on both iOS and Android platforms. |
| 4 | + |
| 5 | +## Installation |
| 6 | +1. Prepare PaintCode files |
| 7 | + > Generate Swift export for iOS, and Java file for Android. |
| 8 | + |
| 9 | + > Convert generated Java file to Kotlin file. |
| 10 | +
|
| 11 | +2. Place exported PaintCode files into projects root folders. |
| 12 | + |
| 13 | +3. Install the library from NPM |
| 14 | + |
| 15 | +```sh |
| 16 | +$ yarn add react-native-tref-paint-code |
| 17 | +``` |
| 18 | + |
| 19 | +4. Install dependencies for iOS project |
| 20 | + |
| 21 | +```sh |
| 22 | +$ cd ios |
| 23 | +$ pod install |
| 24 | +``` |
| 25 | + |
| 26 | +> Do not forget to add **use_frameworks!** on the top of the podfile. |
| 27 | +
|
| 28 | +5. On native Android project, open **MainApplication.java** file and add following to the file: |
| 29 | + |
| 30 | +```java |
| 31 | +private static void initializePaintCode(Context context) { |
| 32 | + try { |
| 33 | + Class<?> aClass = Class.forName("be.reference.rnpaintcode.PackageUtil"); |
| 34 | + Field packageName = aClass.getDeclaredField("packageName"); |
| 35 | + packageName.setAccessible(true); |
| 36 | + packageName.set(aClass, context.getPackageName()); |
| 37 | + } catch (Exception e) { |
| 38 | + e.printStackTrace(); |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +Add following line to your **onCreate()** method on **MainApplication.java** file. |
| 44 | + |
| 45 | +```java |
| 46 | +@Override |
| 47 | +public void onCreate() { |
| 48 | + ... |
| 49 | + initializePaintCode(this); |
| 50 | + ... |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +6. Ready to go. |
| 55 | + |
| 56 | + |
| 57 | +## Usage |
| 58 | +1. Add required imports |
| 59 | + |
| 60 | +```javascript |
| 61 | +import {TrefPaintCode, |
| 62 | + TrefPaintCodeType, |
| 63 | + TrefPaintCodeResizingBehaviour, |
| 64 | + TrefPaintCodeHelper} |
| 65 | + from 'react-native-tref-paint-code'; |
| 66 | +``` |
| 67 | + |
| 68 | +2. Use **TrefPaintCode** component in views. |
| 69 | + |
| 70 | +There are two required parameters to draw into **TrefPaintCode** component. |
| 71 | + |
| 72 | +- **method**: Method name that you want to invoke from native PaintCode generated file |
| 73 | + |
| 74 | +> **For example:** drawView_someButton |
| 75 | +
|
| 76 | +- **params**: Parameter object for that specific method that you wanted to call |
| 77 | + |
| 78 | +> Each parameter object should have two properties which are **type** and **value**. Usable types are defined in TrefPaintCodeType JS file. |
| 79 | +
|
| 80 | +```javascript |
| 81 | + { |
| 82 | + resizing: |
| 83 | + { |
| 84 | + type: TrefPaintCodeType.RESIZING_BEHAVIOUR, |
| 85 | + value: TrefPaintCodeResizingBehaviour.ASPECT_FIT |
| 86 | + }, |
| 87 | + radius: |
| 88 | + { |
| 89 | + type: TrefPaintCodeType.FLOAT, |
| 90 | + value: 8 |
| 91 | + }, |
| 92 | + title: { |
| 93 | + type: TrefPaintCodeType.STRING, |
| 94 | + value: "Do something" |
| 95 | + } |
| 96 | + } |
| 97 | +``` |
| 98 | + |
| 99 | +3. Use colors in your styles. |
| 100 | + |
| 101 | +```javascript |
| 102 | +const styles = StyleSheet.create({ |
| 103 | + someStyle: { |
| 104 | + backgroundColor: TrefPaintCodeHelper.Colors.light_background_color |
| 105 | + } |
| 106 | +}); |
| 107 | +``` |
| 108 | + |
| 109 | +### Full example |
| 110 | + |
| 111 | +```javascript |
| 112 | +<TrefPaintCode |
| 113 | + style={styles.someButton} |
| 114 | + method="drawView_someButton" |
| 115 | + params={ |
| 116 | + { |
| 117 | + resizing: |
| 118 | + { |
| 119 | + type: TrefPaintCodeType.RESIZING_BEHAVIOUR, |
| 120 | + value: TrefPaintCodeResizingBehaviour.ASPECT_FIT |
| 121 | + }, |
| 122 | + radius: |
| 123 | + { |
| 124 | + type: TrefPaintCodeType.FLOAT, |
| 125 | + value: 8 |
| 126 | + }, |
| 127 | + title: { |
| 128 | + type: TrefPaintCodeType.STRING, |
| 129 | + value: "Do something" |
| 130 | + } |
| 131 | + } |
| 132 | +} /> |
| 133 | + |
| 134 | +const styles = StyleSheet.create({ |
| 135 | + someButton: { |
| 136 | + width: 200, |
| 137 | + height: 100, |
| 138 | + backgroundColor: TrefPaintCodeHelper.Colors.light_background_color |
| 139 | + } |
| 140 | +}); |
| 141 | +``` |
| 142 | + |
| 143 | +## How it works? |
| 144 | +For both platforms: |
| 145 | +- Native module called **TrefPaintCodeHelper** used for reaching colors defined in PaintCode files |
| 146 | +- Native UI component modules called **TrefPaintCode** used for actual drawing of the graphics |
| 147 | + |
| 148 | +### **Tech stack** |
| 149 | + |
| 150 | +**On Android:** |
| 151 | + |
| 152 | +- **kotlin-reflect** library to reach, read set and invoke class members and methods on runtime |
| 153 | +- An initializer method to initialize library from the native application |
| 154 | +- Kotlin language |
| 155 | + |
| 156 | +**On iOS:** |
| 157 | + |
| 158 | +- Objective-C runtime to reach, read, set and invoke class members (both Swift and Objective-C) on runtime. Used NSInvocation class. |
| 159 | +- Objective-C and Swift together |
| 160 | + |
| 161 | +### **Flow** |
| 162 | + |
| 163 | +Using reflection on both platforms, when a TrefPaintCode component is used: |
| 164 | + |
| 165 | +1. Method name and method parameters passed to native modules from JS via component |
| 166 | +2. Native modules takes method and parameters and invokes the method from provided PaintCode files via reflection |
| 167 | +3. Native UI component module draws invoked method contents to the screen |
| 168 | + |
| 169 | +## Known issues and TODOs |
| 170 | +- Code is not very DRY, refactor is needed |
| 171 | +- Work with Java classes instead of Kotlin (converting is sometimes problematic) |
| 172 | +- Test more! |
0 commit comments