Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate to expo-sqlite #760

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "azusa-player-mobile",
"displayName": "APM",
"plugins": [
"expo-secure-store"
"expo-secure-store",
"expo-sqlite"
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"expo-image-picker": "~16.0.5",
"expo-keep-awake": "~14.0.2",
"expo-secure-store": "~14.0.1",
"expo-sqlite": "~15.1.2",
"fflate": "^0.8.2",
"ffmpeg-kit-react-native": "https://gitpkg.vercel.app/lovegaoshi/ffmpeg-kit/react-native?4a0122d6dc3fb13237870dfea2533992f19c324a",
"he": "^1.2.0",
Expand Down
7 changes: 7 additions & 0 deletions src/components/setting/developer/DeveloperSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { isAndroid } from '@utils/RNUtils';
import SelectSetting from '../helpers/SelectSetting';
import { SelectDialogChildren } from '../SelectDialogWrapper';
import { Route, Icons } from './enums';
import sqlMigrate from '@utils/sqlMigrate';

const FadeOptions = [0, 250, 500, 1000];
const CrossFadeOptions = [0, 2500, 5000, 7500, 12000];
Expand Down Expand Up @@ -307,6 +308,12 @@ export const Home = ({
}
settingCategory="DeveloperSettings"
/>
<SettingListItem
icon={Icons.clearOrphanCache}
settingName="SQLMigrate"
onPress={() => sqlMigrate(true)}
settingCategory="DeveloperSettings"
/>
</List.Section>
</ScrollView>
);
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/useSetupPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import usePlayStore from './usePlayStore';
import { buildBrowseTree } from '@utils/automotive/androidAuto';
import { NativeModules } from 'react-native';
import useActiveTrack, { useTrackStore } from './useActiveTrack';
import sqlMigrate from '@utils/sqlMigrate';

const { NoxModule } = NativeModules;

Expand Down Expand Up @@ -65,6 +66,7 @@ export default ({ intentData, vip }: NoxComponent.SetupPlayerProps) => {
NoxModule?.loadRN();
}
let unmounted = false;
sqlMigrate();
(async () => {
await appStartupInit;
const storedPlayerSetting =
Expand Down
22 changes: 13 additions & 9 deletions src/utils/ChromeStorageAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import AsyncStorage from '@react-native-async-storage/async-storage';
import AsyncStorage from 'expo-sqlite/kv-store';
import { Appearance, ColorSchemeName } from 'react-native';
import * as SecureStore from 'expo-secure-store';
import { v4 as uuidv4 } from 'uuid';
Expand Down Expand Up @@ -27,7 +27,9 @@ const MAX_SONGLIST_SIZE = 400;
export const saveItem = async (
key: string,
value: unknown,
setFunc: (k: string, v: string) => Promise<void> = AsyncStorage.setItem,
setFunc: (k: string, v: string) => Promise<void> = AsyncStorage.setItem.bind(
AsyncStorage,
),
) => {
try {
await setFunc(key, JSON.stringify(value));
Expand All @@ -39,7 +41,9 @@ export const saveItem = async (
export const getItem = async (
key: string,
defaultVal: unknown = null,
getFunc: (k: string) => Promise<any> = AsyncStorage.getItem,
getFunc: (k: string) => Promise<any> = AsyncStorage.getItem.bind(
AsyncStorage,
),
): Promise<null | any> => {
try {
const retrievedStr = await getFunc(key);
Expand All @@ -52,7 +56,7 @@ export const getItem = async (

export const removeItem = async (key: string) => {
try {
await AsyncStorage.removeItem(key);
await AsyncStorage.removeItem.bind(AsyncStorage)(key);
} catch (e) {
console.warn(e);
}
Expand Down Expand Up @@ -136,17 +140,17 @@ export const savePlaylist = async (

export const delPlaylist = async (playlistId: string) => {
removeItem(playlistId);
const allkeys = await AsyncStorage.getAllKeys();
const allkeys = await AsyncStorage.getAllKeys.bind(AsyncStorage)();
const playlistKeys = allkeys.filter(k => k.startsWith(`${playlistId}.`));
return Promise.all(playlistKeys.map(k => removeItem(k)));
};

const clearStorage = () => AsyncStorage.clear();
const clearStorage = () => AsyncStorage.clear.bind(AsyncStorage)();

export const exportPlayerContent = async (content?: any) => {
if (!content) {
const allKeys = await AsyncStorage.getAllKeys();
content = await AsyncStorage.multiGet(allKeys);
const allKeys = await AsyncStorage.getAllKeys.bind(AsyncStorage)();
content = await AsyncStorage.multiGet.bind(AsyncStorage)(allKeys);
}
return compressSync(strToU8(JSON.stringify(content)));
};
Expand All @@ -173,7 +177,7 @@ export const importPlayerContentRaw = async (
} else {
const content = await getContent();
await clearStorage();
await AsyncStorage.multiSet(parsedContent);
await AsyncStorage.multiSet.bind(AsyncStorage)(parsedContent);
return content;
}
};
Expand Down
13 changes: 13 additions & 0 deletions src/utils/sqlMigrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import AsyncStorage2 from 'expo-sqlite/kv-store';

export default async (f = false) => {
if (AsyncStorage2.getAllKeysSync().length > 0 && !f) {
return;
}
const allKeys = await AsyncStorage.getAllKeys();
const content = await AsyncStorage.multiGet(allKeys);
// @ts-expect-error
AsyncStorage2.multiSet.bind(AsyncStorage2)(content);
AsyncStorage.clear();
};
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5784,6 +5784,7 @@ __metadata:
expo-image-picker: "npm:~16.0.5"
expo-keep-awake: "npm:~14.0.2"
expo-secure-store: "npm:~14.0.1"
expo-sqlite: "npm:~15.1.2"
fflate: "npm:^0.8.2"
ffmpeg-kit-react-native: "https://gitpkg.vercel.app/lovegaoshi/ffmpeg-kit/react-native?4a0122d6dc3fb13237870dfea2533992f19c324a"
gts: "npm:^6.0.2"
Expand Down Expand Up @@ -9276,6 +9277,17 @@ __metadata:
languageName: node
linkType: hard

"expo-sqlite@npm:~15.1.2":
version: 15.1.2
resolution: "expo-sqlite@npm:15.1.2"
peerDependencies:
expo: "*"
react: "*"
react-native: "*"
checksum: 10c0/35987cd9b74cbc90e014318b3deaa05014be9587bd5884f83b2392d354e4ad04a70860f520893aa9a16f28e014263bb67be2dcb72f0f97c54cec4c7f063aa7b7
languageName: node
linkType: hard

"expo-web-browser@npm:~14.0.2":
version: 14.0.2
resolution: "expo-web-browser@npm:14.0.2"
Expand Down
Loading