From 9a1a8d4f072166a91f853d9b9eabfc6ebbab3474 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 8 Mar 2026 08:22:10 +0200 Subject: fix: complete code-quality task queue (373-378) --- internal/gui/persistence.go | 111 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 internal/gui/persistence.go (limited to 'internal/gui/persistence.go') 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("") + } + }) + } +} -- cgit v1.2.3