Skip to content

Commit

Permalink
perf: improve lock logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wx committed Dec 5, 2023
1 parent 4b56a26 commit a8df764
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "joplin-plugin-app-lock",
"version": "1.0.7",
"version": "1.0.8",
"scripts": {
"dist": "webpack --env joplin-plugin-config=buildMain && webpack --env joplin-plugin-config=buildExtraScripts && webpack --env joplin-plugin-config=createArchive",
"prepare": "npm run dist",
Expand Down
29 changes: 17 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import joplin from 'api';
import { SettingItemType } from 'api/types';

// TODO: duplicate dialogs showed
// TODO:lock the screen when app start

joplin.plugins.register({
onStart: async function () {
// insert app locker setting
Expand Down Expand Up @@ -32,6 +29,7 @@ joplin.plugins.register({

let startTime = +new Date();
let checkTimer = null;
let lockId = null;

// relock
const resetLock = (status, pswd) => {
Expand All @@ -42,15 +40,15 @@ joplin.plugins.register({
// lock app
const lock = async (wrong, pswd) => {
const Dialogs = joplin.views.dialogs;
const lockId = 'app.locker' + +new Date();
const lockDialog = await Dialogs.create(lockId);

let lockResult;

if (lockResult?.formData?.appLocker) {
if ((lockResult && lockResult?.formData?.appLocker) || lockId) {
return false;
}

lockId = 'app.locker' + +new Date();
const lockDialog = await Dialogs.create(lockId);

await Dialogs.setHtml(
lockDialog,
`<form style="margin: 100px auto; text-align: center; font-size: 16px;" name="appLocker">
Expand All @@ -70,23 +68,30 @@ joplin.plugins.register({
lockResult = await Dialogs.open(lockDialog);

if (lockResult?.formData?.appLocker?.password !== pswd) {
lockId = null;
resetLock(true, pswd);
} else {
lockId = null;
startTime = +new Date();
clearTimeout(checkTimer);
checkIdle();
checkIdle(false);
}
};

// check app is idle or not
const checkIdle = async () => {
const checkIdle = async (actNow) => {
const lockTimer = parseInt(
(await joplin.settings.value('appLockerTimer')) || '5'
);
const pswd = (
(await joplin.settings.value('appLockerPswd')) || ''
).trim();

if (actNow) {
resetLock(null, pswd);
return false;
}

if (lockTimer > 0 && pswd) {
const now = +new Date();
const checkTime = (lockTimer - 1) * 60 * 1000 + 60 * 1000;
Expand All @@ -106,7 +111,7 @@ joplin.plugins.register({
if (now - startTime + checkTime > lockTimer * 60 * 1000) {
resetLock(null, pswd);
} else {
checkIdle();
checkIdle(false);
}
}, checkTime);
}
Expand All @@ -116,9 +121,9 @@ joplin.plugins.register({
joplin.workspace.onNoteChange(() => {
startTime = +new Date();
clearTimeout(checkTimer);
checkIdle();
checkIdle(false);
});

checkIdle();
checkIdle(true);
},
});
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 1,
"id": "joplin.plugin.app.locker",
"app_min_version": "2.8",
"version": "1.0.7",
"version": "1.0.8",
"name": "joplin plugin app screen locker",
"description": "A joplin plugin which will lock joplin app screen when it is idle.",
"author": "ladyrank",
Expand Down

0 comments on commit a8df764

Please sign in to comment.