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
110
111
|
package gui
import (
"fmt"
"os"
"path/filepath"
"fyne.io/fyne/v2"
"codeberg.org/snonux/totalrecall/internal"
)
// ensureWordDirectoryAndMetadata creates a new card directory and writes word metadata.
func (a *Application) ensureWordDirectoryAndMetadata(word string) (string, error) {
cardID := internal.GenerateCardID(word)
wordDir := filepath.Join(a.config.OutputDir, cardID)
if err := os.MkdirAll(wordDir, 0755); err != nil {
return "", fmt.Errorf("failed to create card directory: %w", err)
}
metadataFile := filepath.Join(wordDir, "word.txt")
if err := os.WriteFile(metadataFile, []byte(word), 0644); err != nil {
return "", fmt.Errorf("failed to save word metadata: %w", err)
}
return wordDir, nil
}
// ensureCardDirectory ensures a card directory exists for the given word and returns its path.
func (a *Application) ensureCardDirectory(word string) (string, error) {
wordDir := a.findCardDirectory(word)
if wordDir != "" {
return wordDir, nil
}
return a.ensureWordDirectoryAndMetadata(word)
}
// saveTranslation saves the current translation to a file.
func (a *Application) saveTranslation() {
if a.currentWord == "" || a.currentTranslation == "" {
return
}
wordDir := a.findCardDirectory(a.currentWord)
if wordDir == "" {
newWordDir, err := a.ensureWordDirectoryAndMetadata(a.currentWord)
if err != nil {
a.showError(err)
return
}
wordDir = newWordDir
}
translationFile := filepath.Join(wordDir, "translation.txt")
content := fmt.Sprintf("%s = %s\n", a.currentWord, a.currentTranslation)
if err := os.WriteFile(translationFile, []byte(content), 0644); err != nil {
a.showError(fmt.Errorf("failed to save translation: %w", err))
}
}
// saveImagePrompt saves the current image prompt to a file.
func (a *Application) saveImagePrompt() {
// With timestamp-based card IDs, we can't update existing prompts.
// The prompt is saved when the image is generated.
// This function is kept for compatibility but does nothing.
}
// savePhoneticInfo saves the phonetic information to a file.
func (a *Application) savePhoneticInfo() {
phoneticText := a.currentPhonetic
if a.currentWord == "" || phoneticText == "" || phoneticText == "Failed to fetch phonetic information" {
return
}
wordDir := a.findCardDirectory(a.currentWord)
if wordDir == "" {
newWordDir, err := a.ensureWordDirectoryAndMetadata(a.currentWord)
if err != nil {
a.showError(err)
return
}
wordDir = newWordDir
}
phoneticFile := filepath.Join(wordDir, "phonetic.txt")
if err := os.WriteFile(phoneticFile, []byte(phoneticText), 0644); err != nil {
a.showError(fmt.Errorf("failed to save phonetic info: %w", err))
}
}
// loadPhoneticInfo loads phonetic information from a file if it exists.
func (a *Application) loadPhoneticInfo(word string) {
wordDir := a.findCardDirectory(word)
if wordDir == "" {
return
}
phoneticFile := filepath.Join(wordDir, "phonetic.txt")
if data, err := os.ReadFile(phoneticFile); err == nil {
phoneticText := string(data)
a.currentPhonetic = phoneticText
fyne.Do(func() {
if phoneticText != "" {
a.audioPlayer.SetPhonetic(phoneticText)
} else {
a.audioPlayer.SetPhonetic("")
}
})
}
}
|