Skip to content

Repository files navigation

react-native-last-active-state

The library determines the time of the last active state of the application.

🚀 Motivation

When an application is destroyed, it does not receive control, thus there is no way to detect the time spent in an inactive state. This library remembers the time of the last active state, you can get it the next time you start the application.

📋 Requirements

Requirement Version
React Native ≥ 0.75
react-native-nitro-modules ≥ 0.35
iOS ≥ 15.1
Android minSdkVersion ≥ 24

Note: This library uses Nitro Modules.

📥 Installation

yarn add @kode-frontend/react-native-last-active-state react-native-nitro-modules

🎮 Usage

import LastActiveState from "@kode-frontend/react-native-last-active-state";

export default function App() {
    const [result, setResult] = React.useState<number | undefined>(
        LastActiveState.initialLastActiveTime
    );

    React.useEffect(() => {
        const listener = LastActiveState.addListener(({ lastActiveTime }) => {
            setResult(lastActiveTime);
        });
        return () => {
            listener.remove();
        };
    }, []);

  return (
        <View>
            <Text>Last active time: {result}</Text>
            <Button
                onPress={() =>
                Alert.alert(
                    'Last active time',
                    String(LastActiveState.getLastActiveTimeSync())
                )
                }
                title="Get last active time"
            />
        </View>
  );
}

📦 Available methods

method description
addListener Subscribing to data changes
getLastActiveTime Recommended method for getting the time of the last active state
getLastActiveTimeSync Synchronous method for getting the time of the last active state

Types

type LastActiveStateEvent = { lastActiveTime: number };

type LastActiveStateEventHandle = (e: LastActiveStateEvent) => void;

type LastActiveStateType = {
  getLastActiveTime: () => Promise<number>;
  getLastActiveTimeSync: () => number;
  initialLastActiveTime: number;
  addListener: (handle: LastActiveStateEventHandle) => EmitterSubscription;
};

Tags

release - create release tag and increase version

About

No description, website, or topics provided.

Resources

Contributing

Stars

0 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages