feat: save preference to config file
This commit is contained in:
parent
b0bfb3e606
commit
6f5f739059
@ -43,6 +43,7 @@ import {
|
||||
|
||||
import { Status, Item, SaveTo } from './types'
|
||||
import { SelectFiles, SelectFolder, ProcessFiles } from '../wailsjs/go/main/App'
|
||||
import { Load, Save } from '../wailsjs/go/utils/ConfigManager'
|
||||
import { main } from '../wailsjs/go/models'
|
||||
import { EventsOn, OnFileDrop } from '../wailsjs/runtime/runtime'
|
||||
|
||||
@ -81,10 +82,6 @@ export const App = () => {
|
||||
return items.some(item => item.status === 'processing')
|
||||
}, [items])
|
||||
|
||||
const isProcessFinished = useMemo(() => {
|
||||
return items.every(item => item.status === 'done' || item.status === 'error')
|
||||
}, [items])
|
||||
|
||||
const [saveTo, setSaveTo] = useState<SaveTo>('original')
|
||||
const [savePath, setSavePath] = useState('')
|
||||
|
||||
@ -179,6 +176,10 @@ export const App = () => {
|
||||
showDialog('当前文件列表已全部处理完毕,请重新添加新的文件。')
|
||||
return
|
||||
}
|
||||
if(saveTo === 'custom' && savePath === '') {
|
||||
showDialog('保存路径为空,请先设置保存路径。')
|
||||
return
|
||||
}
|
||||
const ncmFiles: main.NcmFile[] = []
|
||||
items.forEach(item => {
|
||||
ncmFiles.push({
|
||||
@ -197,8 +198,20 @@ export const App = () => {
|
||||
return newItems
|
||||
})
|
||||
})
|
||||
Load().then(res => {
|
||||
console.log(res)
|
||||
setSaveTo(res.save_to as SaveTo)
|
||||
setSavePath(res.path)
|
||||
})
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
Save({
|
||||
save_to: saveTo,
|
||||
path: savePath,
|
||||
}).then(_ => {})
|
||||
}, [saveTo, savePath])
|
||||
|
||||
OnFileDrop((_x, _y, paths) => {
|
||||
let length = paths.length
|
||||
for (const path of paths) {
|
||||
|
10
main.go
10
main.go
@ -6,6 +6,8 @@ import (
|
||||
"github.com/wailsapp/wails/v2"
|
||||
"github.com/wailsapp/wails/v2/pkg/options"
|
||||
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
|
||||
|
||||
"git.taurusxin.com/taurusxin/ncmdump-gui/utils"
|
||||
)
|
||||
|
||||
//go:embed all:frontend/dist
|
||||
@ -15,8 +17,13 @@ func main() {
|
||||
// Create an instance of the app structure
|
||||
app := NewApp()
|
||||
|
||||
config_manager, err := utils.NewConfigManager("config.json")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Create application with options
|
||||
err := wails.Run(&options.App{
|
||||
err = wails.Run(&options.App{
|
||||
Title: "ncmdump-gui",
|
||||
Width: 750,
|
||||
Height: 500,
|
||||
@ -27,6 +34,7 @@ func main() {
|
||||
OnStartup: app.startup,
|
||||
Bind: []interface{}{
|
||||
app,
|
||||
config_manager,
|
||||
},
|
||||
DragAndDrop: &options.DragAndDrop{
|
||||
EnableFileDrop: true,
|
||||
|
83
utils/config_manager.go
Normal file
83
utils/config_manager.go
Normal file
@ -0,0 +1,83 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
type SaveToType = string
|
||||
|
||||
const (
|
||||
Original SaveToType = "original"
|
||||
Custom SaveToType = "custom"
|
||||
)
|
||||
|
||||
type Preference struct {
|
||||
SaveTo SaveToType `json:"save_to"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
type ConfigManager struct {
|
||||
FilePath string
|
||||
}
|
||||
|
||||
// NewConfigManager 创建一个新的配置管理器,自动适配不同操作系统的用户目录
|
||||
func NewConfigManager(filename string) (*ConfigManager, error) {
|
||||
configDir, err := os.UserConfigDir() // 获取用户配置目录
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dirPath := filepath.Join(configDir, "ncmdump-gui")
|
||||
err = os.MkdirAll(dirPath, os.ModePerm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
filePath := filepath.Join(dirPath, filename)
|
||||
|
||||
configManager := &ConfigManager{FilePath: filePath}
|
||||
// if not exist, create it with default value
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
defaultConfig := &Preference{
|
||||
SaveTo: Original,
|
||||
Path: "",
|
||||
}
|
||||
configManager.Save(defaultConfig)
|
||||
}
|
||||
return &ConfigManager{FilePath: filePath}, nil
|
||||
}
|
||||
|
||||
// Save 将配置保存到文件
|
||||
func (cm *ConfigManager) Save(preference *Preference) bool {
|
||||
file, err := os.Create(cm.FilePath)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
encoder := json.NewEncoder(file)
|
||||
encoder.SetIndent("", " ") // 格式化输出
|
||||
err = encoder.Encode(preference)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// Load 从文件读取配置
|
||||
func (cm *ConfigManager) Load() *Preference {
|
||||
file, err := os.Open(cm.FilePath)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var preference *Preference = nil
|
||||
decoder := json.NewDecoder(file)
|
||||
err = decoder.Decode(&preference)
|
||||
fmt.Println(preference)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return preference
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user