first
This commit is contained in:
47
src/stores/queue.ts
Normal file
47
src/stores/queue.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
// filepath: src/stores/queue.ts
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import { listen } from '@tauri-apps/api/event'
|
||||
|
||||
export interface DownloadTask {
|
||||
id: string
|
||||
title: string
|
||||
thumbnail: string
|
||||
progress: number
|
||||
speed: string
|
||||
status: 'pending' | 'downloading' | 'finished' | 'error'
|
||||
}
|
||||
|
||||
interface ProgressEvent {
|
||||
id: string
|
||||
progress: number
|
||||
speed: string
|
||||
status: string
|
||||
}
|
||||
|
||||
export const useQueueStore = defineStore('queue', () => {
|
||||
const tasks = ref<DownloadTask[]>([])
|
||||
const isListening = ref(false)
|
||||
|
||||
function addTask(task: DownloadTask) {
|
||||
tasks.value.push(task)
|
||||
}
|
||||
|
||||
async function initListener() {
|
||||
if (isListening.value) return
|
||||
isListening.value = true
|
||||
|
||||
await listen<ProgressEvent>('download-progress', (event) => {
|
||||
const { id, progress, speed, status } = event.payload
|
||||
const task = tasks.value.find(t => t.id === id)
|
||||
if (task) {
|
||||
task.progress = progress
|
||||
task.speed = speed
|
||||
// Map status string to type if needed, or just assign
|
||||
task.status = status as any
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return { tasks, addTask, initListener }
|
||||
})
|
||||
73
src/stores/settings.ts
Normal file
73
src/stores/settings.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
// filepath: src/stores/settings.ts
|
||||
import { defineStore } from 'pinia'
|
||||
import { invoke } from '@tauri-apps/api/core'
|
||||
import { ref } from 'vue'
|
||||
|
||||
export interface Settings {
|
||||
download_path: string
|
||||
theme: 'light' | 'dark' | 'system'
|
||||
last_updated: string | null
|
||||
}
|
||||
|
||||
export const useSettingsStore = defineStore('settings', () => {
|
||||
const settings = ref<Settings>({
|
||||
download_path: '',
|
||||
theme: 'system',
|
||||
last_updated: null
|
||||
})
|
||||
|
||||
const ytdlpVersion = ref('Checking...')
|
||||
const isInitializing = ref(true)
|
||||
|
||||
async function loadSettings() {
|
||||
try {
|
||||
const s = await invoke<Settings>('get_settings')
|
||||
settings.value = s
|
||||
applyTheme(s.theme)
|
||||
} catch (e) {
|
||||
console.error("Failed to load settings", e)
|
||||
}
|
||||
}
|
||||
|
||||
async function save() {
|
||||
try {
|
||||
await invoke('save_settings', { settings: settings.value })
|
||||
applyTheme(settings.value.theme)
|
||||
} catch (e) {
|
||||
console.error("Failed to save settings", e)
|
||||
}
|
||||
}
|
||||
|
||||
async function initYtdlp() {
|
||||
try {
|
||||
isInitializing.value = true
|
||||
// check/download
|
||||
await invoke('init_ytdlp')
|
||||
ytdlpVersion.value = await invoke('get_ytdlp_version')
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
ytdlpVersion.value = 'Error'
|
||||
} finally {
|
||||
isInitializing.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function applyTheme(theme: string) {
|
||||
const root = document.documentElement
|
||||
const isDark = theme === 'dark' || (theme === 'system' && window.matchMedia('(prefers-color-scheme: dark)').matches)
|
||||
if (isDark) {
|
||||
root.classList.add('dark')
|
||||
} else {
|
||||
root.classList.remove('dark')
|
||||
}
|
||||
}
|
||||
|
||||
// Watch system preference changes if theme is system
|
||||
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
|
||||
if (settings.value.theme === 'system') {
|
||||
applyTheme('system')
|
||||
}
|
||||
})
|
||||
|
||||
return { settings, loadSettings, save, initYtdlp, ytdlpVersion, isInitializing }
|
||||
})
|
||||
Reference in New Issue
Block a user