summaryrefslogtreecommitdiff
path: root/internal/gui/app.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-07-18 19:45:35 +0300
committerPaul Buetow <paul@buetow.org>2025-07-18 19:45:35 +0300
commite1b3ba69499547424502cc7e4dacf1e10d5efde9 (patch)
tree17110ac06239ff24150cd87df7df1729f7a285ae /internal/gui/app.go
parentcd3b1e5b2fab8075303c064ba33996a0250cd6a6 (diff)
fix: ensure single directory per card with consistent ID
- Fixed issue where multiple directories were created for the same word - Added findCardDirectory to check for existing directories before creating new ones - Updated all file creation functions to reuse existing card directories - Applied fix to both GUI and CLI code - Added phonetic fetching to processing counter in generateMaterials - Now each card gets one directory with all its files 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat (limited to 'internal/gui/app.go')
-rw-r--r--internal/gui/app.go88
1 files changed, 70 insertions, 18 deletions
diff --git a/internal/gui/app.go b/internal/gui/app.go
index c4b609e..28abb7b 100644
--- a/internal/gui/app.go
+++ b/internal/gui/app.go
@@ -507,9 +507,17 @@ func (a *Application) generateMaterials(word string) {
// Save translation to disk regardless
if translation != "" {
- cardID := internal.GenerateCardID(word)
- wordDir := filepath.Join(a.config.OutputDir, cardID)
- os.MkdirAll(wordDir, 0755) // Ensure directory exists
+ // Find existing card directory first
+ wordDir := a.findCardDirectory(word)
+ if wordDir == "" {
+ // No existing directory, create new one with card ID
+ cardID := internal.GenerateCardID(word)
+ wordDir = filepath.Join(a.config.OutputDir, cardID)
+ os.MkdirAll(wordDir, 0755) // Ensure directory exists
+ // Save word metadata
+ metadataFile := filepath.Join(wordDir, "word.txt")
+ os.WriteFile(metadataFile, []byte(word), 0644)
+ }
translationFile := filepath.Join(wordDir, "translation.txt")
content := fmt.Sprintf("%s = %s\n", word, translation)
os.WriteFile(translationFile, []byte(content), 0644)
@@ -580,6 +588,36 @@ func (a *Application) generateMaterials(word string) {
a.mu.Unlock()
}
+ // Fetch phonetic information in a separate goroutine
+ go func() {
+ fyne.Do(func() {
+ a.incrementProcessing() // Phonetic processing starts
+ })
+
+ phoneticInfo, err := a.getPhoneticInfo(word)
+ if err != nil {
+ // Log error but don't fail - phonetic info is optional
+ fmt.Printf("Warning: Failed to get phonetic info: %v\n", err)
+ phoneticInfo = "Failed to fetch phonetic information"
+ }
+
+ // Update UI with phonetic info if this is still the current word
+ a.mu.Lock()
+ if a.currentWord == word {
+ fyne.Do(func() {
+ a.phoneticDisplay.SetText(phoneticInfo)
+ })
+ }
+ a.mu.Unlock()
+
+ // Save phonetic info to disk
+ if phoneticInfo != "" && phoneticInfo != "Failed to fetch phonetic information" {
+ a.savePhoneticInfoForWord(word, phoneticInfo)
+ }
+
+ a.decrementProcessing() // Phonetic processing ends
+ }()
+
// Enable action buttons
fyne.Do(func() {
a.hideProgress()
@@ -1114,12 +1152,15 @@ func (a *Application) processWordJob(job *WordJob) {
// Save translation to disk immediately for this specific word
if translation != "" {
- cardID := internal.GenerateCardID(job.Word)
- wordDir := filepath.Join(a.config.OutputDir, cardID)
- os.MkdirAll(wordDir, 0755) // Ensure directory exists
- // Save word metadata if not already present
- metadataFile := filepath.Join(wordDir, "word.txt")
- if _, err := os.Stat(metadataFile); os.IsNotExist(err) {
+ // Find existing card directory first
+ wordDir := a.findCardDirectory(job.Word)
+ if wordDir == "" {
+ // No existing directory, create new one with card ID
+ cardID := internal.GenerateCardID(job.Word)
+ wordDir = filepath.Join(a.config.OutputDir, cardID)
+ os.MkdirAll(wordDir, 0755) // Ensure directory exists
+ // Save word metadata
+ metadataFile := filepath.Join(wordDir, "word.txt")
os.WriteFile(metadataFile, []byte(job.Word), 0644)
}
translationFile := filepath.Join(wordDir, "translation.txt")
@@ -1155,12 +1196,15 @@ func (a *Application) processWordJob(job *WordJob) {
// Save phonetic info to disk immediately for this specific word
if phoneticInfo != "" && phoneticInfo != "Failed to fetch phonetic information" {
- cardID := internal.GenerateCardID(job.Word)
- wordDir := filepath.Join(a.config.OutputDir, cardID)
- os.MkdirAll(wordDir, 0755) // Ensure directory exists
- // Save word metadata if not already present
- metadataFile := filepath.Join(wordDir, "word.txt")
- if _, err := os.Stat(metadataFile); os.IsNotExist(err) {
+ // Find existing card directory first
+ wordDir := a.findCardDirectory(job.Word)
+ if wordDir == "" {
+ // No existing directory, create new one with card ID
+ cardID := internal.GenerateCardID(job.Word)
+ wordDir = filepath.Join(a.config.OutputDir, cardID)
+ os.MkdirAll(wordDir, 0755) // Ensure directory exists
+ // Save word metadata
+ metadataFile := filepath.Join(wordDir, "word.txt")
os.WriteFile(metadataFile, []byte(job.Word), 0644)
}
phoneticFile := filepath.Join(wordDir, "phonetic.txt")
@@ -1585,9 +1629,17 @@ func (a *Application) savePhoneticInfoForWord(word, phoneticText string) {
if word != "" && phoneticText != "" &&
phoneticText != "Failed to fetch phonetic information" &&
phoneticText != "Phonetic information will appear here..." {
- cardID := internal.GenerateCardID(word)
- wordDir := filepath.Join(a.config.OutputDir, cardID)
- os.MkdirAll(wordDir, 0755) // Ensure directory exists
+ // Find existing card directory first
+ wordDir := a.findCardDirectory(word)
+ if wordDir == "" {
+ // No existing directory, create new one with card ID
+ cardID := internal.GenerateCardID(word)
+ wordDir = filepath.Join(a.config.OutputDir, cardID)
+ os.MkdirAll(wordDir, 0755) // Ensure directory exists
+ // Save word metadata
+ metadataFile := filepath.Join(wordDir, "word.txt")
+ os.WriteFile(metadataFile, []byte(word), 0644)
+ }
phoneticFile := filepath.Join(wordDir, "phonetic.txt")
os.WriteFile(phoneticFile, []byte(phoneticText), 0644)
}