Ariver
2026-06-30 5b5cba7030b7f4dd519868fe310bb4fce893bf91
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package services
 
import (
    "os"
    "path/filepath"
    "strings"
    "time"
    "voicesnap/internal/userdict"
 
    "github.com/wailsapp/wails/v3/pkg/application"
)
 
// UserDictService exposes user dictionary management to the frontend.
type UserDictService struct {
    store *userdict.Store
    app   *application.App
    win   application.Window
}
 
func NewUserDictService(store *userdict.Store) *UserDictService {
    return &UserDictService{store: store}
}
 
// SetApp sets the Wails app and parent window used for native dialogs.
func (s *UserDictService) SetApp(app *application.App, win application.Window) {
    s.app = app
    s.win = win
}
 
// GetAll returns all replacement entries.
func (s *UserDictService) GetAll() []userdict.Entry {
    return s.store.GetAll()
}
 
// GetPage returns one page of replacement entries.
func (s *UserDictService) GetPage(page int, pageSize int) userdict.PageResult {
    return s.store.GetPage(page, pageSize)
}
 
// Add creates a replacement entry.
func (s *UserDictService) Add(from, to string) (userdict.Entry, error) {
    return s.store.Add(from, to)
}
 
// Update modifies an existing replacement entry.
func (s *UserDictService) Update(id int64, from, to string, enabled bool) (userdict.Entry, error) {
    return s.store.Update(id, from, to, enabled)
}
 
// Delete removes an entry by id.
func (s *UserDictService) Delete(id int64) {
    s.store.Delete(id)
}
 
// ClearAll removes all entries.
func (s *UserDictService) ClearAll() {
    s.store.ClearAll()
}
 
// ExportJSON returns the full userdict.json payload.
func (s *UserDictService) ExportJSON() string {
    return s.store.ExportJSON()
}
 
// ExportToFile asks the user where to save userdict.json, writes it, and returns the path.
func (s *UserDictService) ExportToFile() (string, error) {
    if s.app == nil {
        return "", os.ErrInvalid
    }
 
    home, _ := os.UserHomeDir()
    dialog := s.app.Dialog.SaveFile().
        SetMessage("导出用户词库").
        SetButtonText("导出").
        SetDirectory(filepath.Join(home, "Downloads")).
        SetFilename("userdict-"+time.Now().Format("20060102-1504")+".json").
        AddFilter("JSON Files", "*.json").
        CanCreateDirectories(true)
 
    if s.win != nil {
        dialog.AttachToWindow(s.win)
    }
 
    path, err := dialog.PromptForSingleSelection()
    if err != nil {
        return "", err
    }
    if path == "" {
        return "", nil
    }
    if strings.TrimSpace(filepath.Ext(path)) == "" {
        path += ".json"
    }
 
    if err := os.WriteFile(path, []byte(s.store.ExportJSON()), 0644); err != nil {
        return "", err
    }
    return path, nil
}
 
// ImportJSON replaces the dictionary from a JSON payload.
func (s *UserDictService) ImportJSON(content string) error {
    return s.store.ImportJSON(content)
}
 
// GetPath returns the local userdict.json path.
func (s *UserDictService) GetPath() string {
    return s.store.Path()
}