Skip to content

Instantly share code, notes, and snippets.

@stevenselcuk
Created August 6, 2020 18:41
Show Gist options
  • Save stevenselcuk/29b4b7ae38b48681ae198eb10b667285 to your computer and use it in GitHub Desktop.
Save stevenselcuk/29b4b7ae38b48681ae198eb10b667285 to your computer and use it in GitHub Desktop.
import React, { useEffect, useState } from "react";
import { AppState, Platform } from "react-native";
import navigator from "@saypr/kanvaz-mobile-navigator";
import BackgroundTimer from "react-native-background-timer";
import AsyncStorage from "@react-native-community/async-storage";
import { hasUserSetPinCode } from "./PIN";
const AppLockScreenIdleTime = ({
currentScreen,
timeout,
redirectToAfterFirstInstall,
}) => {
const [pinState, setPINState] = useState();
const [appState, setAppState] = useState(AppState.currentState);
const [lockState, setLock] = useState(false);
const handleAppStateChange = state => {
setAppState(state);
};
const getSetPINState = async () => {
const state = await hasUserSetPinCode();
setPINState(state);
return state;
};
const getItemSafely = async (key) => {
return new Promise((resolve, reject) => {
AsyncStorage.getItem(key).then(
(value) => {
resolve(value);
},
() => {
console.log("async has failed");
resolve({});
}
);
});
};
const setTimeStamp = async (date) => {
try {
await AsyncStorage.setItem("bg", date);
} catch (error) {
console.log("Writing error");
}
};
let lockScreenTimerIOS = {};
const setLockState = async (state) => {
try {
await AsyncStorage.setItem("isLocked", JSON.stringify(state));
} catch (error) {
console.log("Writing error");
}
};
const getLockState = async () => {
return new Promise((resolve, reject) => {
AsyncStorage.getItem("isLocked").then(
(value) => {
setLock(JSON.parse(value));
resolve(JSON.parse(value));
},
() => {
console.log("async has failed");
resolve({});
}
);
});
};
const handleLockScreenStateAndroid = (param) => {
if (param.state === "background") {
setTimeStamp(JSON.stringify(new Date().getTime()));
BackgroundTimer.runBackgroundTimer(() => {
setLockState(true);
}, timeout);
} else {
BackgroundTimer.stopBackgroundTimer();
}
};
const handleLockScreenStateIOS = (param) => {
if (param.state === "background") {
setTimeStamp(JSON.stringify(new Date().getTime()));
lockScreenTimerIOS = setTimeout(() => {
setLockState(true);
}, timeout);
} else {
clearTimeout(lockScreenTimerIOS);
}
};
const handleLocker = async (param) => {
// const isLocked = await getLockState();
const timestamp = await getItemSafely("bg");
if (parseInt(timestamp, 10) + timeout < new Date().getTime()) {
if (param.state === "active" && timestamp !== "null") {
navigator.navigate("PIN", { redirect: redirectToAfterFirstInstall });
} else {
console.log("not yet.");
}
}
};
useEffect(() => {
AppState.addEventListener('change', handleAppStateChange);
getSetPINState();
stateManager();
async function checkLogin() {
await pinChecker();
}
checkLogin();
}, [getSetPINState]);
return null;
};
export default AppLockScreenIdleTime;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment