chore: prettier cleanup

This commit is contained in:
Savely Savianok
2025-02-26 00:33:40 +03:00
parent cebe95acf5
commit 24e097a253
9 changed files with 318 additions and 296 deletions

View File

@@ -1,43 +1,42 @@
import { RenderAddButton } from './render/totpRenderer'
import { initLoop } from './render/index/renderer'
import { BasePage } from '@zeppos/zml/base-page'
import { RenderAddButton } from "./render/totpRenderer";
import { initLoop } from "./render/index/renderer";
import { BasePage } from "@zeppos/zml/base-page";
const app = getApp()
const app = getApp();
let waitForFetch = true;
Page(
BasePage({
onInit() {
this.getTOTPData().then((x) => {
app._options.globalData.TOTPS = JSON.parse(x) ?? []
this.initPage()
})
.catch((x) => {
app._options.globalData.TOTPS = []
this.initPage()
})
},
build() {
let fetch = setInterval(() => {
if(waitForFetch)
return;
BasePage({
onInit() {
this.getTOTPData()
.then((x) => {
app._options.globalData.TOTPS = JSON.parse(x) ?? [];
this.initPage();
})
.catch((x) => {
app._options.globalData.TOTPS = [];
this.initPage();
});
},
build() {
let fetch = setInterval(() => {
if (waitForFetch) return;
clearInterval(fetch);
const buffer = app._options.globalData.TOTPS
if (buffer.length < 1){
RenderAddButton('page/tip')
}
else {
initLoop(buffer)
}
}, 100);
},
initPage() {
waitForFetch = false;
},
getTOTPData() {
return this.request({
method: 'totps'
})
}
})
)
clearInterval(fetch);
const buffer = app._options.globalData.TOTPS;
if (buffer.length < 1) {
RenderAddButton("page/tip");
} else {
initLoop(buffer);
}
}, 100);
},
initPage() {
waitForFetch = false;
},
getTOTPData() {
return this.request({
method: "totps",
});
},
})
);

View File

@@ -1,6 +1,6 @@
import { px } from "@zos/utils";
export const TEXT_STYLE = {
x: px(0),
y: px(0),
}
x: px(0),
y: px(0),
};

View File

@@ -1,45 +1,57 @@
import { prop } from "@zos/ui";
import { TOTP } from "../../../lib/totp-quickjs";
import { RenderExpireBar, RenderOTPValue, RenderTOTPContainer } from '../totpRenderer'
import {
RenderExpireBar,
RenderOTPValue,
RenderTOTPContainer,
} from "../totpRenderer";
/**
*
* @param {Array<TOTP>} buffer
*
* @param {Array<TOTP>} buffer
*/
export function initLoop(buffer) {
renderContainers(buffer)
renderTOTPs(buffer)
renderContainers(buffer);
renderTOTPs(buffer);
}
function renderContainers(buffer) {
for (let i = 0; i < buffer.length; i++) {
RenderTOTPContainer(i, buffer[i].issuer, buffer[i].client)
RenderTOTPContainer(i, buffer[i].issuer, buffer[i].client);
}
}
const renderData = []
const renderData = [];
function renderTOTPs(buffer) {
for (let i = 0; i < buffer.length; i++) {
let otpData = TOTP.copy(buffer[i]).getOTP()
let otpData = TOTP.copy(buffer[i]).getOTP();
renderData[i] = {
OTP: RenderOTPValue(i, otpData.otp),
expireBar: RenderExpireBar(i, otpData.createdTime, buffer[i].fetchTime)
}
expireBar: RenderExpireBar(
i,
otpData.createdTime,
buffer[i].fetchTime
),
};
setInterval(() => {
const expireDif = Math.abs((((Date.now() - otpData.createdTime) / 1000)
/ buffer[i].fetchTime) - 1)
const expireDif = Math.abs(
(Date.now() - otpData.createdTime) /
1000 /
buffer[i].fetchTime -
1
);
renderData[i].expireBar.setProperty(prop.MORE, {
end_angle: (expireDif * 360) - 90,
end_angle: expireDif * 360 - 90,
color: expireDif > 0.25 ? 0x1ca9c9 : 0xfa0404,
})
});
if (otpData.expireTime < Date.now()) {
otpData = TOTP.copy(buffer[i]).getOTP()
otpData = TOTP.copy(buffer[i]).getOTP();
renderData[i].OTP.setProperty(prop.MORE, {
text: otpData.otp
})
text: otpData.otp,
});
}
}, 50)
}, 50);
}
}
}

View File

@@ -1,35 +1,35 @@
import { getDeviceInfo } from '@zos/device'
import { push } from '@zos/router'
import { createWidget, widget, align, text_style } from '@zos/ui'
import { getDeviceInfo } from "@zos/device";
import { push } from "@zos/router";
import { createWidget, widget, align, text_style } from "@zos/ui";
//Renderer module for TOTPs page
const { width, height } = getDeviceInfo()
const buttonWidth = width - width / 20 //Width of container
const buttonHeight = height / 4 //Height of container
const containerColor = 0x303030 //Color of container
const containerRadius = 20 //Corner radius of container
const { width, height } = getDeviceInfo();
const buttonWidth = width - width / 20; //Width of container
const buttonHeight = height / 4; //Height of container
const containerColor = 0x303030; //Color of container
const containerRadius = 20; //Corner radius of container
const textColor = 0xa0a0a0 //Color of TOTP description text
const textSize = 24 //Size of TOTP description text
const statusBarPading = 65
const textColor = 0xa0a0a0; //Color of TOTP description text
const textSize = 24; //Size of TOTP description text
const statusBarPading = 65;
/** Function for render box container for TOTP values
*
*
* @param {number} position position offset of container
* @param {string} issuer issuer of TOTP
* @param {string} client client of TOTP
*/
export function RenderTOTPContainer(position, issuer, client) {
const yPos = getYPos(position)
const yPos = getYPos(position);
createWidget(widget.FILL_RECT, {
x: width / 2 - buttonWidth / 2,
y: yPos,
w: buttonWidth,
h: buttonHeight,
color: containerColor,
radius: containerRadius
})
radius: containerRadius,
});
createWidget(widget.TEXT, {
x: 0 + (width - buttonWidth) / 2,
y: yPos + 10,
@@ -40,18 +40,18 @@ export function RenderTOTPContainer(position, issuer, client) {
align_h: align.CENTER_H,
align_v: align.CENTER_V,
text_style: text_style.NONE,
text: issuer + ': ' + client
})
text: issuer + ": " + client,
});
}
/** Render OTP Value on container
*
*
* @param {number} position position offset of container
* @param {string} otpValue value to display
* @returns widget with OTP
*/
export function RenderOTPValue(position, otpValue) {
const yPos = getYPos(position)
const yPos = getYPos(position);
return createWidget(widget.TEXT, {
x: 0,
y: yPos + 50,
@@ -62,21 +62,22 @@ export function RenderOTPValue(position, otpValue) {
align_h: align.CENTER_H,
align_v: align.CENTER_V,
text_style: text_style.NONE,
text: otpValue
})
text: otpValue,
});
}
/** Render expire bar
*
*
* @param {} position position offset of container
* @param {*} createdTime UNIX epoch time of creation
* @param {*} fetchTime time in seconds to fetch value
* @returns widget with bar
*/
export function RenderExpireBar(position, createdTime, fetchTime) {
const yPos = getYPos(position)
const expireDif = Math.abs((((Date.now() - createdTime) / 1000)
/ fetchTime) - 1)
const yPos = getYPos(position);
const expireDif = Math.abs(
(Date.now() - createdTime) / 1000 / fetchTime - 1
);
return createWidget(widget.ARC, {
x: buttonWidth - 50,
y: yPos + 52,
@@ -85,13 +86,13 @@ export function RenderExpireBar(position, createdTime, fetchTime) {
line_width: 5,
color: expireDif > 0.25 ? 0x1ca9c9 : 0xfa0404,
start_angle: -90,
end_angle: (expireDif * 360) - 90,
text: expireDif
end_angle: expireDif * 360 - 90,
text: expireDif,
});
}
/** Function for render 'Add' button on center of screen
*
*
* @param {string} pagePath destination to page onClick ex: 'page/index'
*/
export function RenderAddButton(pagePath) {
@@ -100,17 +101,19 @@ export function RenderAddButton(pagePath) {
y: height / 2 - 20,
w: 80,
h: 80,
text: '+',
text: "+",
radius: 50,
text_size: 40,
normal_color: 0x303030,
press_color: 0x181c18,
click_func: () => {
push({
url: pagePath
})
}
})
url: pagePath,
});
},
});
}
function getYPos(position) { return position * (buttonHeight + 10) + statusBarPading }
function getYPos(position) {
return position * (buttonHeight + 10) + statusBarPading;
}

View File

@@ -1,6 +1,6 @@
import { createWidget, widget, align } from "@zos/ui";
import { getDeviceInfo } from "@zos/device";
import { onGesture, GESTURE_LEFT } from '@zos/interaction'
import { onGesture, GESTURE_LEFT } from "@zos/interaction";
import { back } from "@zos/router";
import { BasePage } from "@zeppos/zml/base-page";
Page(
@@ -9,13 +9,13 @@ Page(
onGesture({
callback(event) {
if (event === GESTURE_LEFT) {
back()
back();
}
}
})
},
});
},
build() {
const { width, height } = getDeviceInfo()
const { width, height } = getDeviceInfo();
createWidget(widget.TEXT, {
x: 0,
w: width,
@@ -24,8 +24,8 @@ Page(
text_size: 30,
align_h: align.CENTER_H,
align_v: align.CENTER_V,
text: 'To add TOTP record open\n settings on Zepp app'
})
}
text: "To add TOTP record open\n settings on Zepp app",
});
},
})
)
);