Code Monkey home page Code Monkey logo

react-native-tref-paint-code's Introduction

Getting started

Library implements a general approach for using PaintCode generated files with React Native on both iOS and Android platforms.

Installation

  1. 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.

  2. Place exported PaintCode files into projects root folders.

    Do not forget to "Configure" project after you added PaintCode.kt file.

  3. Install the library from NPM, by following instructions on latest package: https://github.com/ReferenceArmada/react-native-tref-paint-code/packages/

  4. Install dependencies for iOS project

$ cd ios
$ pod install
  1. 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);
  ...
}
  1. Ready to go.

Usage

  1. Add required imports
import {TrefPaintCode,
        TrefPaintCodeType,
        TrefPaintCodeResizingBehaviour,
        TrefPaintCodeHelper}
        from '@referencearmada/react-native-tref-paint-code';
  1. 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"
        }
    }
  1. Use colors in your styles.
const styles = StyleSheet.create({
  someStyle: {
    backgroundColor: TrefPaintCodeHelper.Colors.light_background_color
  }
});

Full example

<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
  }
});

How it works?

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

Tech stack

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

Flow

Using reflection on both platforms, when a TrefPaintCode component is used:

  1. Method name and method parameters passed to native modules from JS via component
  2. Native modules takes method and parameters and invokes the method from provided PaintCode files via reflection
  3. Native UI component module draws invoked method contents to the screen

Known issues and TODOs

  • Code is not very DRY, refactor is needed
  • Work with Java classes instead of Kotlin (converting is sometimes problematic)
  • Test more!

react-native-tref-paint-code's People

Contributors

referencearmada avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.