From 1b4ff63d1ca2a794c64bf7ffe4fe4684a61e926d Mon Sep 17 00:00:00 2001 From: Beingpax Date: Wed, 28 May 2025 18:20:37 +0545 Subject: [PATCH] Refactored prompt selection --- VoiceInk/PowerMode/PowerModeConfigView.swift | 48 +++++-------- .../Components/PromptSelectionGrid.swift | 68 +++++++++++++++++++ VoiceInk/Views/EnhancementSettingsView.swift | 47 +++++-------- 3 files changed, 102 insertions(+), 61 deletions(-) create mode 100644 VoiceInk/Views/Components/PromptSelectionGrid.swift diff --git a/VoiceInk/PowerMode/PowerModeConfigView.swift b/VoiceInk/PowerMode/PowerModeConfigView.swift index aa59a05..9365ed4 100644 --- a/VoiceInk/PowerMode/PowerModeConfigView.swift +++ b/VoiceInk/PowerMode/PowerModeConfigView.swift @@ -559,38 +559,26 @@ struct ConfigurationView: View { // Enhancement Prompts Section (reused from EnhancementSettingsView) VStack(alignment: .leading, spacing: 12) { - Text("Enhancement Prompts") - .font(.subheadline) + Text("Enhancement Prompt") + .font(.headline) .foregroundColor(.primary) - if enhancementService.allPrompts.isEmpty { - Text("No prompts available") - .foregroundColor(.secondary) - .font(.caption) - } else { - let columns = [ - GridItem(.adaptive(minimum: 80, maximum: 100), spacing: 36) - ] - - LazyVGrid(columns: columns, spacing: 24) { - ForEach(enhancementService.allPrompts) { prompt in - prompt.promptIcon( - isSelected: selectedPromptId == prompt.id, - onTap: { selectedPromptId = prompt.id }, - onEdit: { selectedPromptForEdit = $0 }, - onDelete: { enhancementService.deletePrompt($0) } - ) - } - - // Plus icon using the same styling as prompt icons - CustomPrompt.addNewButton { - isEditingPrompt = true - } - .help("Add new prompt") - } - .padding(.vertical, 12) - .padding(.horizontal, 16) - } + PromptSelectionGrid( + selectedPromptId: selectedPromptId, + onPromptTap: { prompt in + selectedPromptId = prompt.id + }, + onPromptEdit: { prompt in + selectedPromptForEdit = prompt + }, + onPromptDelete: { prompt in + enhancementService.deletePrompt(prompt) + }, + onAddNew: { + isEditingPrompt = true + }, + assistantTriggerWord: enhancementService.assistantTriggerWord + ) } Divider() diff --git a/VoiceInk/Views/Components/PromptSelectionGrid.swift b/VoiceInk/Views/Components/PromptSelectionGrid.swift new file mode 100644 index 0000000..20c2179 --- /dev/null +++ b/VoiceInk/Views/Components/PromptSelectionGrid.swift @@ -0,0 +1,68 @@ +import SwiftUI + +/// A reusable grid component for selecting prompts with a plus button to add new ones +struct PromptSelectionGrid: View { + @EnvironmentObject private var enhancementService: AIEnhancementService + + let selectedPromptId: UUID? + let onPromptTap: (CustomPrompt) -> Void + let onPromptEdit: (CustomPrompt) -> Void + let onPromptDelete: (CustomPrompt) -> Void + let onAddNew: () -> Void + let assistantTriggerWord: String? + + init( + selectedPromptId: UUID?, + onPromptTap: @escaping (CustomPrompt) -> Void, + onPromptEdit: @escaping (CustomPrompt) -> Void = { _ in }, + onPromptDelete: @escaping (CustomPrompt) -> Void = { _ in }, + onAddNew: @escaping () -> Void, + assistantTriggerWord: String? = nil + ) { + self.selectedPromptId = selectedPromptId + self.onPromptTap = onPromptTap + self.onPromptEdit = onPromptEdit + self.onPromptDelete = onPromptDelete + self.onAddNew = onAddNew + self.assistantTriggerWord = assistantTriggerWord + } + + var body: some View { + VStack(alignment: .leading, spacing: 12) { + if enhancementService.allPrompts.isEmpty { + Text("No prompts available") + .foregroundColor(.secondary) + .font(.caption) + } else { + let columns = [ + GridItem(.adaptive(minimum: 80, maximum: 100), spacing: 36) + ] + + LazyVGrid(columns: columns, spacing: 24) { + ForEach(enhancementService.allPrompts) { prompt in + prompt.promptIcon( + isSelected: selectedPromptId == prompt.id, + onTap: { + withAnimation(.spring(response: 0.3, dampingFraction: 0.7)) { + onPromptTap(prompt) + } + }, + onEdit: onPromptEdit, + onDelete: onPromptDelete, + assistantTriggerWord: assistantTriggerWord + ) + } + + // Plus icon using the same styling as prompt icons + CustomPrompt.addNewButton { + onAddNew() + } + .help("Add new prompt") + } + .padding(.vertical, 12) + .padding(.horizontal, 16) + } + } + } +} + diff --git a/VoiceInk/Views/EnhancementSettingsView.swift b/VoiceInk/Views/EnhancementSettingsView.swift index acfa97c..f7b5184 100644 --- a/VoiceInk/Views/EnhancementSettingsView.swift +++ b/VoiceInk/Views/EnhancementSettingsView.swift @@ -91,37 +91,22 @@ struct EnhancementSettingsView: View { // Prompts Section VStack(alignment: .leading, spacing: 12) { - if enhancementService.allPrompts.isEmpty { - Text("No prompts available") - .foregroundColor(.secondary) - .font(.caption) - } else { - let columns = [ - GridItem(.adaptive(minimum: 80, maximum: 100), spacing: 36) - ] - - LazyVGrid(columns: columns, spacing: 24) { - ForEach(enhancementService.allPrompts) { prompt in - prompt.promptIcon( - isSelected: enhancementService.selectedPromptId == prompt.id, - onTap: { withAnimation(.spring(response: 0.3, dampingFraction: 0.7)) { - enhancementService.setActivePrompt(prompt) - }}, - onEdit: { selectedPromptForEdit = $0 }, - onDelete: { enhancementService.deletePrompt($0) }, - assistantTriggerWord: enhancementService.assistantTriggerWord - ) - } - - // Plus icon using the same styling as prompt icons - CustomPrompt.addNewButton { - isEditingPrompt = true - } - .help("Add new prompt") - } - .padding(.vertical, 12) - .padding(.horizontal, 16) - } + PromptSelectionGrid( + selectedPromptId: enhancementService.selectedPromptId, + onPromptTap: { prompt in + enhancementService.setActivePrompt(prompt) + }, + onPromptEdit: { prompt in + selectedPromptForEdit = prompt + }, + onPromptDelete: { prompt in + enhancementService.deletePrompt(prompt) + }, + onAddNew: { + isEditingPrompt = true + }, + assistantTriggerWord: enhancementService.assistantTriggerWord + ) } Divider()