Code Monkey home page Code Monkey logo

mroads / react-native-user-inactivity Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 5.0 37.5 MB

๐Ÿ‘จ๐Ÿฟโ€๐Ÿ’ป react-native-user-inactivity is a lightweight library to track user activity and inactivity in your React Native application that works seamlessly in android and iOS devices.

Java 16.80% JavaScript 46.72% Starlark 6.82% Ruby 12.93% Objective-C 16.72%
react-native react-native-user-inactivity react hacktoberfest hacktoberfest2021

react-native-user-inactivity's Introduction

React Native User Inactivity check

react-native-user-inactivity-check library helps in monitoring child components actions like Move, Drag, Press. If the user didnt perfrom any of the actions in child screens or components, It triggers an action which is passed to it.

Setup

Installation

$ npm install react-native-user-inactivity-check --save

or

$ yarn add react-native-user-inactivity-check

Usage

  • When the component is renderd it will start listening to the actions happening in the child Screens and Components. Main difference here is with the given timeToInactivity. This library calculates the logtime (i.e Time to trigger the action if user is inactive). It checks for every interval with logtime if the current time near logtime it triggers the action and clears the timer.

  • If it register's any action it will again calculate the logtime from that insatnt and reset the timer.

  • handleAppState prop is a boolean value which will tell the librabry to consider the appState. If this value is set to "true", And if the app is moved to background then the timer is cleared and no logtime is considered. When the app comes to foreground then app again calculates the logtime and starts the timer. By default it runs the timer in background.

  • Main objectives of this package is to handle the ideal state of the app. So that we can perform an action if the app is Ideal for a particular given time.

  • The suggested approach is if you want to monitor ideal state of complete app. Then load the App as a child in UserInactivityCheck. If it's only for a particular flow then make a "Navigator" and load it as a child.

Props

Function you need to call. This function by default returns the viewport values for screen size 1280 X 800.
Prop Default Type Description
timeToInactivity 5 number Minutes to wait until user respond
interval 5 number Seconds for accuracy to check inactive time
onAction undefined function Action to perform if user is inactive for given time
handleAppState false boolean Disable timer if app is in background

Example

Basic

import React, {useState} from 'react';
import {StyleSheet, View, Text, TouchableOpacity, Alert} from 'react-native';
import UserInactiveCheck from 'react-native-user-inactivity-check';

const App = () => {
  const [showContent, setShowContent] = useState(false);
  const action = () => {
    setShowContent(true);
    Alert.alert('User is not active');
  };
  let userInactiveCheckRef = React.createRef();
  const onButtonPress = () => {
    setShowContent(false);
    userInactiveCheckRef && userInactiveCheckRef.resetTimer();
  };
  return (
    <UserInactiveCheck
      timeToInactivity={5}
      interval={10}
      onAction={action}
      ref={ref => {userInactiveCheckRef = ref;}}
      handleAppState={true}>
      <View style={styles.container}>
        <View style={styles.headView}>
          <Text style={styles.heading}>User Inactivity Demo</Text>
        </View>
        <View style={styles.buttonView}>
          {showContent ? (
            <TouchableOpacity
              style={{backgroundColor: '#DDDDDD', padding: 10}}
              onPress={onButtonPress}>
              <Text style={styles.buttonText}>Reset Timer</Text>
            </TouchableOpacity>
          ) : null}
        </View>
      </View>
    </UserInactiveCheck>
  );
};

const styles = StyleSheet.create({
  heading: {
    fontSize: 40,
    marginBottom: 30,
    alignSelf: 'center',
  },
  container: {
    flex: 1,
    padding: 15,
    justifyContent: 'center',
    backgroundColor: '#9d9d9d',
  },
  headView: {flex: 1, justifyContent: 'center', alignItems: 'center'},
  buttonView: {flex: 4, alignItems: 'center', justifyContent: 'center'},
  buttonText: {fontSize: 30},
});

export default App;

react-native-user-inactivity's People

Contributors

saimanideep-mroads avatar snigdhadhanireddy29 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

react-native-user-inactivity's Issues

Button click is not working

Hi,

I need to through the notification if app is been idle for 5 mins or greater.

I am using navigator in app js and on top of that I have implemented this code. But, after that my button click is not working at the first time, I have to click 2 to three times to make the button action trigger.

App.js code:

return (
<UserInactiveCheck
timeToInactivity={1}
interval={10}
onAction={action}
ref={ref => {
userInactiveCheckRef = ref;
}}
handleAppState={true}>


{modalVisible && }


);

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.