npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-predict-io

v5.0.0

Published

A battery-optimized SDK for React Native to get real-time updates with context information when a user starts or ends a journey.

Downloads

21

Readme

Getting started

Prerequisites

  • React Native 0.49.5
  • Xcode 9.3 / Swift 4.1
  • Android Studio 3.0+

Installation

npm install react-native-predict-io --save

iOS

1. Install Cocoapods
2. Add predict.io SDK dependency
  • In ios/Podfile, add the following line above # Add new pods below this line:

    eval(File.read('../node_modules/react-native-predict-io/ios/Podfile.template'))
  • Run pod install in the ios directory to install

3. Configure your app's permissions for location
4. Fix iOS Project Settings (after react-native link)

React Native doesn't correctly install the native predict.io SDK so we need to fix some minor issues with the iOS project.

  1. Open your .xcworkspace from the ios directory of your app, e.g. Example.xcworkspace
  2. Inside the Libraries group in the project explorer, select and remove RNPredictIO
5. Run your app normally with react-native run-ios

Android

##### 1. Open up android/app/src/main/java/[...]/MainActivity.java

  • Add import io.predict.android.sdk.react.RNPredictIOPackage; to the imports at the top of the file
  • Add new RNPredictIOPackage() to the list returned by the getPackages() method

##### 2. Append the following lines to android/settings.gradle:

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

##### 3. Insert the following lines inside the dependencies block in android/app/build.gradle

compile project(':react-native-predict-io')
4. Configure your app's permissions for location
  • In android/app/src/main/AndroidManifest.xml add the following permissions tags inside <application>:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
5. Add Kotlin support to android/app/build.gradle
  • Add the followiing to the top of the file:

    buildscript {
        ext.kotlin_version = '1.2.40'
        repositories {
            jcenter()
        }
      
        dependencies {
            classpath 'com.android.tools.build:gradle:2.2.2'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    apply plugin: 'kotlin-android'
6. Initialize the predict.io SDK
  • Inside your MainApplication.java file inside the android directory of your app:

    • Find the onCreate method and add the following line to it:

      PredictIo.init(this)

Usage

import PredictIO from 'react-native-predict-io';

import {
    NativeEventEmitter,
    DeviceEventEmitter,
}

const PREDICT_IO_API_KEY = "<YOUR API KEY>";

async function startPredictIOSDK() {
  if (Platform.OS === "android") {
    const PermissionsAndroid = require('PermissionsAndroid');
    const status = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
    );

    console.log("Android Location Permission => " + status)
  }
  else if (Platform.OS === "ios") {
    const status = await navigator.geolocation.requestAuthorization()
    console.log("iOS Location Permission => " + status)
  }

  PredictIO.start(PREDICT_IO_API_KEY, "highPower", (status) => {
    console.log("PredictIO Error => " + status);

    switch (status) {
        case "success":
            console.log("SUCCESS => PredictIO SDK started successfully!");
        break;

        case "invalidKey":
            console.error("ERROR => PredictIO SDK API key is not valid!");
        break;

        case "killSwitch":
            console.error("ERROR => PredictIO SDK kill switch has been activated!");
        break;

        case "wifiDisabled":
            console.warn("WARNING => Wifi is disabled, negatively impacting location quality.");
        break;

        case "locationPermission":
            console.error("ERROR => Location permission has not been granted (Android)");
        break;

        case "locationPermissionNotDetermined":
            console.error("ERROR => Location permission: not yet determined (iOS)");
        break;

        case "locationPermissionWhenInUse":
            console.error("ERROR => Location permission: when in use (iOS)");
        break;

        case "locationPermissionRestricted":
            console.error("ERROR => Location permission: restricted (iOS)");
        break;

        case "locationPermissionDenied":
            console.error("ERROR => Location permission: denied (iOS)");
        break;
    }
  });
}

startPredictIOSDK();

// Listen for events

function handlePredictIOEvent(event) {
    	alert(`${event.type}\n${event.timestamp}\n${event.coordinate.latitude},${event.coordinate.longitude}`);
}

// Android Subscription
DeviceEventEmitter.addListener('PredictIOTripEvent', handlePredictIOEvent);

// iOS Subscription
const predictIOEventEmitter = new NativeEventEmitter(PredictIO);
const predictIOEventSubscription = predictIOEventEmitter.addListener('PredictIOTripEvent', handlePredictIOEvent);

Known Issues

  • On Android, the SDK may not yet generate events correctly
  • On iOS, reloading the React Native app without recompiling may result in a crash