Skip to content

Commit 89ad09f

Browse files
author
Burak Akkas
committed
Initial commit
0 parents  commit 89ad09f

31 files changed

+1870
-0
lines changed

.gitignore

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Java class files
2+
*.class
3+
4+
# Generated files
5+
bin/
6+
gen/
7+
out/
8+
9+
# Gradle files
10+
.gradle/
11+
build/
12+
13+
# Local configuration file (sdk path, etc)
14+
local.properties
15+
16+
# Proguard folder generated by Eclipse
17+
proguard/
18+
19+
# Log Files
20+
*.log
21+
22+
# Android Studio Navigation editor temp files
23+
.navigation/
24+
25+
# Android Studio captures folder
26+
captures/
27+
28+
# External native build folder generated in Android Studio 2.2 and later
29+
.externalNativeBuild
30+
.cxx/
31+
32+
# Version control
33+
vcs.xml
34+
35+
# lint
36+
lint/intermediates/
37+
lint/generated/
38+
lint/outputs/
39+
lint/tmp/
40+
41+
# User-specific stuff
42+
.idea/**/workspace.xml
43+
.idea/**/tasks.xml
44+
.idea/**/usage.statistics.xml
45+
.idea/**/dictionaries
46+
.idea/**/shelf
47+
48+
# Generated files
49+
.idea/**/contentModel.xml
50+
51+
# Sensitive or high-churn files
52+
.idea/**/dataSources/
53+
.idea/**/dataSources.ids
54+
.idea/**/dataSources.local.xml
55+
.idea/**/sqlDataSources.xml
56+
.idea/**/dynamic.xml
57+
.idea/**/uiDesigner.xml
58+
.idea/**/dbnavigator.xml
59+
60+
# Gradle
61+
.idea/**/gradle.xml
62+
.idea/**/libraries
63+
64+
# Gradle and Maven with auto-import
65+
# When using Gradle or Maven with auto-import, you should exclude module files,
66+
# since they will be recreated, and may cause churn. Uncomment if using
67+
# auto-import.
68+
# .idea/artifacts
69+
# .idea/compiler.xml
70+
# .idea/jarRepositories.xml
71+
# .idea/modules.xml
72+
# .idea/*.iml
73+
# .idea/modules
74+
# *.iml
75+
# *.ipr
76+
77+
# CMake
78+
cmake-build-*/
79+
80+
# Mongo Explorer plugin
81+
.idea/**/mongoSettings.xml
82+
83+
# File-based project format
84+
*.iws
85+
86+
# IntelliJ
87+
out/
88+
89+
# mpeltonen/sbt-idea plugin
90+
.idea_modules/
91+
92+
# JIRA plugin
93+
atlassian-ide-plugin.xml
94+
95+
# Cursive Clojure plugin
96+
.idea/replstate.xml
97+
98+
# Crashlytics plugin (for Android Studio and IntelliJ)
99+
com_crashlytics_export_strings.xml
100+
crashlytics.properties
101+
crashlytics-build.properties
102+
fabric.properties
103+
104+
# Editor-based Rest Client
105+
.idea/httpRequests
106+
107+
# Android studio 3.1+ serialized cache file
108+
.idea/caches/build_file_checksums.ser
109+
110+
## User settings
111+
xcuserdata/
112+
113+
## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9)
114+
*.xcscmblueprint
115+
*.xccheckout
116+
117+
## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4)
118+
build/
119+
DerivedData/
120+
*.moved-aside
121+
*.pbxuser
122+
!default.pbxuser
123+
*.mode1v3
124+
!default.mode1v3
125+
*.mode2v3
126+
!default.mode2v3
127+
*.perspectivev3
128+
!default.perspectivev3
129+
130+
## Gcc Patch
131+
/*.gcno

README.md

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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!

android/android.iml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module external.linked.project.id=":" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
3+
<component name="FacetManager">
4+
<facet type="android-gradle" name="Android-Gradle">
5+
<configuration>
6+
<option name="GRADLE_PROJECT_PATH" value=":" />
7+
<option name="LAST_SUCCESSFUL_SYNC_AGP_VERSION" />
8+
<option name="LAST_KNOWN_AGP_VERSION" />
9+
</configuration>
10+
</facet>
11+
</component>
12+
<component name="NewModuleRootManager" inherit-compiler-output="true">
13+
<exclude-output />
14+
<content url="file://$MODULE_DIR$" />
15+
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
16+
<orderEntry type="sourceFolder" forTests="false" />
17+
</component>
18+
</module>

0 commit comments

Comments
 (0)