summaryrefslogtreecommitdiff
path: root/internal/gui/persistence.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-03-08 08:22:10 +0200
committerPaul Buetow <paul@buetow.org>2026-03-08 08:22:10 +0200
commit9a1a8d4f072166a91f853d9b9eabfc6ebbab3474 (patch)
tree4d7cd2efb0b78ca9250154abe293056e06bbb814 /internal/gui/persistence.go
parentb0ae81a4abaddb02fa0ec4b9d868ac1aee662fb9 (diff)
fix: complete code-quality task queue (373-378)
Diffstat (limited to 'internal/gui/persistence.go')
-rw-r--r--internal/gui/persistence.go111
1 files changed, 111 insertions, 0 deletions
diff --git a/internal/gui/persistence.go b/internal/gui/persistence.go
new file mode 100644
index 0000000..0919974
--- /dev/null
+++ b/internal/gui/persistence.go
@@ -0,0 +1,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("")
+ }
+ })
+ }
+}