chore: 合并工具函数

This commit is contained in:
maotoumao
2025-05-11 15:16:52 +08:00
parent 593b918338
commit c217a84a98
14 changed files with 56 additions and 98 deletions
@@ -1,11 +1,11 @@
import React from 'react';
import {InteractionManager, StyleSheet, View} from 'react-native';
import rpx from '@/utils/rpx';
import IconTextButton from '@/components/base/iconTextButton';
import ThemeText from '@/components/base/themeText';
import repeatModeConst from '@/constants/repeatModeConst';
import IconTextButton from '@/components/base/iconTextButton';
import TrackPlayer, { usePlayList, useRepeatMode } from '@/core/trackPlayer';
import sleep from '@/utils/sleep.ts';
import delay from '@/utils/delay';
import rpx from '@/utils/rpx';
import React from 'react';
import { InteractionManager, StyleSheet, View } from 'react-native';
export default function Header() {
const repeatMode = useRepeatMode();
@@ -26,7 +26,7 @@ export default function Header() {
<IconTextButton
onPress={() => {
InteractionManager.runAfterInteractions(async () => {
await sleep(20);
await delay(20, false);
TrackPlayer.toggleRepeatMode();
});
}}
+1 -1
View File
@@ -2,9 +2,9 @@ import {useMMKVObject} from 'react-native-mmkv';
import {getStorage, removeStorage} from '@/utils/storage';
import getOrCreateMMKV from '@/utils/getOrCreateMMKV.ts';
import safeStringify from '@/utils/safeStringify.ts';
import type {AppConfigPropertyKey, IAppConfig, IAppConfigProperties} from '@/types/core/config';
import { safeStringify } from '@/utils/jsonUtil';
const configStore = getOrCreateMMKV('App.config');
+1 -1
View File
@@ -1,7 +1,7 @@
import { addFileScheme } from "@/utils/fileUtils";
import getOrCreateMMKV from "@/utils/getOrCreateMMKV";
import { safeParse } from "@/utils/jsonUtil";
import { getMediaKey } from "@/utils/mediaItem";
import safeParse from "@/utils/safeParse";
import { exists, unlink } from "react-native-fs";
// Internal Method
+1 -2
View File
@@ -1,8 +1,7 @@
import getOrCreateMMKV from '@/utils/getOrCreateMMKV.ts';
import safeParse from '@/utils/safeParse.ts';
import {InteractionManager} from 'react-native';
import safeStringify from '@/utils/safeStringify.ts';
import {SortType} from '@/constants/commonConst.ts';
import { safeParse, safeStringify } from '@/utils/jsonUtil';
function getStorageData(key: string) {
const mmkv = getOrCreateMMKV(`LocalSheet.${key}`);
@@ -1,14 +1,14 @@
import React from 'react';
import {InteractionManager, StyleSheet, View} from 'react-native';
import rpx from '@/utils/rpx';
import repeatModeConst from '@/constants/repeatModeConst';
import rpx from '@/utils/rpx';
import React from 'react';
import { InteractionManager, StyleSheet, View } from 'react-native';
import useOrientation from '@/hooks/useOrientation';
import {showPanel} from '@/components/panels/usePanel';
import TrackPlayer, { useMusicState, useRepeatMode } from '@/core/trackPlayer';
import {musicIsPaused} from '@/utils/trackUtils';
import Icon from '@/components/base/icon.tsx';
import sleep from '@/utils/sleep';
import { showPanel } from '@/components/panels/usePanel';
import TrackPlayer, { useMusicState, useRepeatMode } from '@/core/trackPlayer';
import useOrientation from '@/hooks/useOrientation';
import delay from '@/utils/delay';
import { musicIsPaused } from '@/utils/trackUtils';
export default function () {
const repeatMode = useRepeatMode();
@@ -33,7 +33,7 @@ export default function () {
size={rpx(56)}
onPress={async () => {
InteractionManager.runAfterInteractions(async () => {
await sleep(20);
await delay(20, false);
TrackPlayer.toggleRepeatMode();
});
}}
@@ -1,23 +1,23 @@
import React from "react";
import { ScrollView, StyleSheet } from "react-native";
import ListItem, { ListItemHeader } from "@/components/base/listItem";
import Toast from "@/utils/toast";
import Backup from "@/core/backup";
import { ROUTE_PATH, useNavigate } from "@/core/router";
import Toast from "@/utils/toast";
import React from "react";
import { ScrollView, StyleSheet } from "react-native";
import axios from "axios";
import { showDialog } from "@/components/dialogs/useDialog";
import { showPanel } from "@/components/panels/usePanel";
import axios from "axios";
import { AuthType, createClient } from "webdav";
import { writeInChunks } from "@/utils/fileUtils.ts";
import { getDocumentAsync } from "expo-document-picker";
import { readAsStringAsync } from "expo-file-system";
import sleep from "@/utils/sleep";
import { ResumeMode } from "@/constants/commonConst.ts";
import strings from "@/constants/strings.ts";
import { errorLog } from "@/utils/log.ts";
import Config, { useAppConfig } from "@/core/config";
import delay from "@/utils/delay";
import { writeInChunks } from "@/utils/fileUtils.ts";
import { errorLog } from "@/utils/log.ts";
import { getDocumentAsync } from "expo-document-picker";
import { readAsStringAsync } from "expo-file-system";
import { AuthType, createClient } from "webdav";
export default function BackupSetting() {
const navigate = useNavigate();
@@ -82,7 +82,7 @@ export default function BackupSetting() {
title: '从本地文件恢复',
loadingText: '恢复中...',
async task() {
await sleep(300);
await delay(300, false);
return Backup.resume(result, resumeMode);
},
onResolve(_, hideDialog) {
-34
View File
@@ -1,34 +0,0 @@
import {nanoid} from 'nanoid';
const locks = new Map<string, string>();
export interface ILock {
key: string;
lockId: string;
valid: () => boolean;
release: () => void;
}
function requireLock(key: string): ILock {
const lockId = nanoid();
locks.set(key, lockId);
return {
key,
lockId,
/** 锁是否有效 */
valid() {
const currentLockId = locks.get(key);
return !currentLockId || currentLockId === lockId;
},
/** 释放后赋空 */
release() {
const currentLockId = locks.get(key);
if (currentLockId === lockId) {
locks.delete(key);
}
},
};
}
export {requireLock};
+6 -4
View File
@@ -1,9 +1,11 @@
import BackgroundTimer from 'react-native-background-timer';
export default function (millsecond: number) {
export default function (millsecond: number, background = true) {
return new Promise<void>(resolve => {
BackgroundTimer.setTimeout(() => {
resolve();
}, millsecond);
if (background) {
BackgroundTimer.setTimeout(resolve, millsecond);
} else {
setTimeout(resolve, millsecond);
}
});
}
+19
View File
@@ -0,0 +1,19 @@
export function safeStringify(raw: any): string {
try {
return JSON.stringify(raw);
} catch {
return '';
}
}
export function safeParse<T = any>(raw?: string) {
try {
if (!raw) {
return null;
}
return JSON.parse(raw) as T;
} catch {
return null;
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
import getOrCreateMMKV from '@/utils/getOrCreateMMKV';
import { getMediaKey } from '@/utils/mediaItem';
import safeParse from '@/utils/safeParse';
import { useEffect, useState } from 'react';
import { safeParse } from './jsonUtil';
// Internal Method
const getPluginStore = (pluginName: string) => {
+1 -1
View File
@@ -3,8 +3,8 @@
*/
import getOrCreateMMKV from "@/utils/getOrCreateMMKV";
import safeParse from "@/utils/safeParse";
import { useEffect, useState } from "react";
import { safeParse } from "./jsonUtil";
// Internal Method
const getStore = () => {
-10
View File
@@ -1,10 +0,0 @@
export default function <T = any>(raw?: string) {
try {
if (!raw) {
return null;
}
return JSON.parse(raw) as T;
} catch {
return null;
}
}
-7
View File
@@ -1,7 +0,0 @@
export default function (raw: any): string {
try {
return JSON.stringify(raw);
} catch {
return '';
}
}
-11
View File
@@ -1,11 +0,0 @@
/**
* Sleep是用的settimeout,delay用的是backgroundtimer
* @param ms
*/
export default function sleep(ms = 200) {
return new Promise<void>(resolve => {
setTimeout(() => {
resolve();
}, ms);
});
}