feat: new config format

This commit is contained in:
2025-03-13 23:26:00 +00:00
parent 48dc9ec4df
commit fd27000c05
30 changed files with 1447 additions and 833 deletions

View File

@ -1,4 +1,4 @@
import { Ref, computed, ref } from 'vue';
import { Ref, computed, ref, watchEffect } from 'vue';
import { defineStore } from 'pinia';
import { listen } from '@tauri-apps/api/event';
import * as path from '@tauri-apps/api/path';
@ -110,43 +110,25 @@ export const usePrfStore = defineStore('prf', () => {
() =>
pkg !== undefined &&
current.value !== null &&
current.value?.data.mods.includes(pkgKey(pkg))
current.value?.mods.includes(pkgKey(pkg))
);
const reload = async () => {
current.value = await invoke('get_current_profile');
const p: any = await invoke('get_current_profile');
if (p['OngekiProfile'] !== undefined) {
current.value = { ...p.OngekiProfile, game: 'ongeki' };
}
if (current.value !== null) {
changePrimaryColor(current.value.game);
}
};
const save = async () => {
await invoke('save_current_profile');
};
const cfg = <T extends string | boolean | number>(key: string, dflt: T) =>
computed({
get() {
return (current.value?.data.cfg[key] as T | undefined) ?? dflt;
},
async set(value) {
if (value !== undefined) {
await invoke('set_cfg', { key, value: value });
await reload();
await save();
}
},
});
// Hack around PrimeVu not supporting WritableComputedRef
const cfgAny = <T extends string | boolean | number>(
key: string,
dflt: T
) => cfg(key, dflt) as any;
const create = async (game: Game) => {
try {
await invoke('init_profile', { game, name: 'new-profile' });
await invoke('init_profile', {
game,
name: 'new-profile',
});
await reload();
await reloadList();
} catch (e) {
@ -159,6 +141,22 @@ export const usePrfStore = defineStore('prf', () => {
}
};
const rename = async (profile: ProfileMeta, name: string) => {
await invoke('rename_profile', {
profile,
name,
});
if (
current.value?.game === profile.game &&
current.value.name === profile.name
) {
current.value.name = name;
}
await reloadList();
};
const switchTo = async (game: Game, name: string) => {
await invoke('load_profile', { game, name });
await reload();
@ -169,14 +167,9 @@ export const usePrfStore = defineStore('prf', () => {
};
const reloadList = async () => {
const raw = (await invoke('list_profiles')) as [Game, string][];
list.value = raw.map(([game, name]) => {
return {
game,
name,
};
});
// list.value.splice(0, list.value.length);
list.value = (await invoke('list_profiles')) as ProfileMeta[];
console.log(list.value);
};
const togglePkg = async (pkg: Package | undefined, enable: boolean) => {
@ -185,7 +178,6 @@ export const usePrfStore = defineStore('prf', () => {
}
await invoke('toggle_package', { key: pkgKey(pkg), enable });
await reload();
await save();
};
const generalStore = useGeneralStore();
@ -201,15 +193,21 @@ export const usePrfStore = defineStore('prf', () => {
await reload();
});
watchEffect(async () => {
if (current.value !== null) {
await invoke('save_current_profile', {
profile: { OngekiProfile: current.value },
});
}
});
return {
current,
list,
isPkgEnabled,
reload,
save,
cfg,
cfgAny,
create,
rename,
switchTo,
reloadList,
togglePkg,