fix sync ui

This commit is contained in:
Julian Freeman
2026-03-14 17:34:43 -04:00
parent 7743a25f7b
commit 65ce05b36b
3 changed files with 107 additions and 29 deletions

View File

@@ -28,12 +28,12 @@ fn get_essentials(app: AppHandle) -> Vec<Software> {
description: Some("Microsoft PowerToys 是一组实用程序,供高级用户调整和简化其 Windows 10 和 11 体验。".to_string()), description: Some("Microsoft PowerToys 是一组实用程序,供高级用户调整和简化其 Windows 10 和 11 体验。".to_string()),
version: None, version: None,
available_version: None, available_version: None,
icon_url: Some("https://raw.githubusercontent.com/microsoft/PowerToys/master/doc/images/logo.png".to_string()), icon_url: Some("https://raw.githubusercontent.com/microsoft/PowerToys/main/doc/images/icons/PowerToys icon/PNG/PowerToysAppList.targetsize-48.png".to_string()),
status: "idle".to_string(), status: "idle".to_string(),
progress: 0.0, progress: 0.0,
}, },
Software { Software {
id: "Google.Chrome".to_string(), id: "Google.Chrome.EXE".to_string(),
name: "Google Chrome".to_string(), name: "Google Chrome".to_string(),
description: Some("Google Chrome 是一款快速、安全且免费的浏览器。".to_string()), description: Some("Google Chrome 是一款快速、安全且免费的浏览器。".to_string()),
version: None, version: None,

View File

@@ -7,7 +7,8 @@ export const useSoftwareStore = defineStore('software', {
essentials: [] as any[], essentials: [] as any[],
updates: [] as any[], updates: [] as any[],
allSoftware: [] as any[], allSoftware: [] as any[],
loading: false loading: false,
lastFetched: 0 // 记录上次完整同步的时间戳
}), }),
getters: { getters: {
mergedEssentials: (state) => { mergedEssentials: (state) => {
@@ -49,18 +50,34 @@ export const useSoftwareStore = defineStore('software', {
this.allSoftware = await invoke('get_all_software') this.allSoftware = await invoke('get_all_software')
this.loading = false this.loading = false
}, },
// 智能同步:如果数据较新,则直接返回
async syncDataIfNeeded(force = false) {
const now = Date.now();
const CACHE_TIMEOUT = 5 * 60 * 1000; // 5 分钟缓存
if (!force && this.allSoftware.length > 0 && (now - this.lastFetched < CACHE_TIMEOUT)) {
// 如果不是强制刷新,且数据在 5 分钟内,且已经有数据,则跳过
if (this.essentials.length === 0) await this.fetchEssentials();
return;
}
await this.fetchAllData();
},
async fetchAllData() { async fetchAllData() {
this.loading = true; this.loading = true;
// 并行获取三类数据 try {
const [essentials, all, updates] = await Promise.all([ const [essentials, all, updates] = await Promise.all([
invoke('get_essentials'), invoke('get_essentials'),
invoke('get_all_software'), invoke('get_all_software'),
invoke('get_updates') invoke('get_updates')
]); ]);
this.essentials = essentials as any[]; this.essentials = essentials as any[];
this.allSoftware = all as any[]; this.allSoftware = all as any[];
this.updates = updates as any[]; this.updates = updates as any[];
this.loading = false; this.lastFetched = Date.now();
} finally {
this.loading = false;
}
}, },
async install(id: string) { async install(id: string) {
const software = this.findSoftware(id) const software = this.findSoftware(id)
@@ -68,11 +85,16 @@ export const useSoftwareStore = defineStore('software', {
await invoke('install_software', { id }) await invoke('install_software', { id })
}, },
findSoftware(id: string) { findSoftware(id: string) {
// 这里的逻辑会从多个列表中查找对象
return this.essentials.find(s => s.id === id) || return this.essentials.find(s => s.id === id) ||
this.updates.find(s => s.id === id) || this.updates.find(s => s.id === id) ||
this.allSoftware.find(s => s.id === id) this.allSoftware.find(s => s.id === id)
}, },
initListener() { initListener() {
// 避免重复监听
if ((window as any).__tauri_listener_init) return;
(window as any).__tauri_listener_init = true;
listen('install-status', (event: any) => { listen('install-status', (event: any) => {
const { id, status, progress } = event.payload const { id, status, progress } = event.payload
const software = this.findSoftware(id) const software = this.findSoftware(id)
@@ -80,6 +102,11 @@ export const useSoftwareStore = defineStore('software', {
software.status = status software.status = status
software.progress = progress software.progress = progress
} }
// 如果安装成功,标记数据过期,以便下次切换或刷新时同步最新状态
if (status === 'success') {
this.lastFetched = 0;
}
}) })
} }
} }

View File

@@ -3,9 +3,31 @@
<header class="content-header"> <header class="content-header">
<div class="header-left"> <div class="header-left">
<h1>装机必备</h1> <h1>装机必备</h1>
<p class="subtitle" v-if="store.loading">正在同步软件状态...</p>
</div> </div>
<button @click="installAll" class="primary-btn" :disabled="store.loading">一键安装全部</button> <div class="header-actions">
<button
@click="store.syncDataIfNeeded(true)"
class="secondary-btn action-btn"
:disabled="store.loading"
>
<span class="icon" :class="{ 'spinning': store.loading }">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
<path d="M21 2v6h-6"></path>
<path d="M3 12a9 9 0 0 1 15-6.7L21 8"></path>
<path d="M3 22v-6h6"></path>
<path d="M21 12a9 9 0 0 1-15 6.7L3 16"></path>
</svg>
</span>
{{ store.loading ? '正在同步...' : '同步状态' }}
</button>
<button
@click="installAll"
class="primary-btn action-btn"
:disabled="store.loading"
>
一键安装全部
</button>
</div>
</header> </header>
<div v-if="store.loading && store.mergedEssentials.length === 0" class="loading-state"> <div v-if="store.loading && store.mergedEssentials.length === 0" class="loading-state">
@@ -33,7 +55,7 @@ import { onMounted } from 'vue';
const store = useSoftwareStore(); const store = useSoftwareStore();
onMounted(() => { onMounted(() => {
store.fetchAllData(); store.syncDataIfNeeded();
store.initListener(); store.initListener();
}); });
@@ -57,32 +79,40 @@ const installAll = () => {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;
align-items: center; align-items: center;
margin-bottom: 30px; margin-bottom: 32px;
} }
.content-header h1 { .content-header h1 {
font-size: 32px; font-size: 32px;
font-weight: 700; font-weight: 700;
letter-spacing: -0.5px; letter-spacing: -0.5px;
color: var(--text-main);
} }
.subtitle { .header-actions {
font-size: 14px; display: flex;
color: var(--text-sec); gap: 12px;
margin-top: 4px; align-items: center;
} }
.primary-btn { .action-btn {
background-color: var(--primary-color); display: flex;
color: white; align-items: center;
border: none; gap: 8px;
padding: 10px 20px; padding: 10px 20px;
border-radius: 12px; border-radius: 12px;
font-weight: 600; font-weight: 600;
font-size: 14px; font-size: 14px;
cursor: pointer; cursor: pointer;
box-shadow: var(--btn-shadow);
transition: all 0.2s ease; transition: all 0.2s ease;
border: none;
white-space: nowrap;
}
.primary-btn {
background-color: var(--primary-color);
color: white;
box-shadow: var(--btn-shadow);
} }
.primary-btn:hover { .primary-btn:hover {
@@ -90,10 +120,31 @@ const installAll = () => {
transform: translateY(-1px); transform: translateY(-1px);
} }
.primary-btn:disabled { .secondary-btn {
background-color: var(--border-color); background-color: var(--bg-light);
box-shadow: none; color: var(--text-main);
border: 1px solid var(--border-color);
}
.secondary-btn:hover {
background-color: white;
border-color: var(--primary-color);
color: var(--primary-color);
}
.action-btn:disabled {
opacity: 0.6;
cursor: not-allowed; cursor: not-allowed;
transform: none !important;
}
.icon {
display: flex;
align-items: center;
}
.icon.spinning {
animation: spin 1s linear infinite;
} }
.software-list { .software-list {