Giter Club home page Giter Club logo

react-native-orientation-controller's Introduction

react-native-orientation-controller

A react-native library for obtaining and controlling current device or application orientation

This library is a fork of this library by Ken Wheeler. We thank him.

###Getting Started

  • Run npm install --save react-native-orientation-controller

Install with RNPM

  • npm install -g rnpm
  • rnpm link react-native-orientation-controller

Or Manual Installation

Android

Add to android/settings.gradle:

include ':react-native-orientation-controller'
project(':react-native-orientation-controller').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-orientation-controller/android')

Add the compile project line to android/app/build.gradle (inside dependencies):

dependencies {
    // ... other content ... 
    compile project(':react-native-orientation-controller')
}

Inside MainActivity.java (normally somewhere here android/app/src/main/java/com/<your-app-name>/MainActivity.java) add import com.inprogress.ReactOrientationController.ReactOrientationController; and new ReactOrientationController(this) like in the example below

import com.inprogress.ReactOrientationController.ReactOrientationController; // <<< ADD THIS

/* ... other content ... */

    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
            new MainReactPackage(),
            new ReactOrientationController(this) // <<< AND THIS
            /* ... other packages can be here ... */
        );
    }

Usage

Import the library:

var Orientation = require('react-native-orientation-controller');

####rotate(orientation)

This method will change the current orientation of the device of 90° for parameter=1, 180° for parameter=2, 270° for parameter=3 :

componentDidMount(){
  Orientation.rotate(parameter);
}

####getOrientation(callback)

This method will return the current orientation of the device, the current orientation of the application, the device model and the screen size in the form of a callback:

componentDidMount(){
  Orientation.getOrientation(
    (deviceOrientation, applicationOrientation, device, size) => {
      console.log(deviceOrientation, applicationOrientation, device, size);
    }
  );
}

####addListener(callback)

This method will add a listener that will call the callback anytime the orientation changes:

_setOrientation(deviceOrientation, applicationOrientation, device, size) {
   console.log(deviceOrientation, applicationOrientation, device, size);
},
componentDidMount(){
  this.orientationListener = Orientation.addListener(this._setOrientation);
}

####removeListener(callback)

This method removes the listener you added in componentDidMount:

componentWillUnmount() {
  this.orientationListener.remove(); 
}

react-native-orientation-controller's People

Contributors

cyrbon avatar julien-inprogress avatar mokto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-native-orientation-controller's Issues

Install with android

@jujumoz

When installing react-native-orientation-controller with rnpm, I got this when building app :

/Users/theo/Projects/tactility/mobile/android/app/src/main/java/com/tactility/MainActivity.java:49: error: constructor ReactOrientationController in class ReactOrientationController cannot be applied to given types;
            new ReactOrientationController(),
            ^
  required: Activity
  found: no arguments
  reason: actual and formal argument lists differ in length

Android version is not working properly?

Hi,
Thanks for your fork!
I encountered an issue with the Android version, the iOS one works perfectly according to your recommendations.
Here is what I am doing :

componentDidMount(){
  Orientation.getDeviceOrientation(
    (orientation, device, size) => {
      console.log(orientation); // undefined
      console.log(device); // undefined
      console.log(size); // undefined

      console.log(arguments); // Sounds weird but get {}
    }
  );
}

However, orientation, device and size are all undefined.

I checked your Android ReactOrientationControllerModule, and I did not find where you send the device and size :

params.putString("orientation", Orientation);
thisContext
   .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
   .emit("applicationOrientationDidChange", params);

So, firstly, I think you did not handle the device name and size in the Android version, right?

Secondly, if you change the index.js to:

addApplicationListener: function(callback) {
    return DeviceEventEmitter.addListener(
      'applicationOrientationDidChange', function () {
        callback.apply(this, arguments) // [0] is now removed
      }
    );
},

You get the following :

componentDidMount(){
  Orientation.getDeviceOrientation(
    (orientation, device, size) => {
      console.log(orientation); // { orientation: 'LANDSCAPE RIGHT' }
      console.log(device); // undefined
      console.log(size); // undefined

      console.log(arguments); // display: { '0': { orientation: 'LANDSCAPE RIGHT' } }
    }
  );
}

What do you think about this malfunction? What about the Device and Size?
Device used : Wiko PULP 4G Android 5.1.1 (API 22)

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.