Configuration and Settings (UserDefaultsManager)
Relevant source files
The following files were used as context for generating this wiki page:
The VoiceInk application manages user preferences and configuration through a decentralized system that primarily relies on macOS's UserDefaults. Unlike systems with a single settings manager, VoiceInk distributes the responsibility of managing settings to the specific services and views that use them. This approach leverages SwiftUI's @AppStorage property wrapper for simple key-value settings and direct UserDefaults.standard access for more complex data structures, which are often encoded as JSON.
A critical component of this system is the ImportExportService, which consolidates all distributed settings into a single JSON file for backup and migration. This service interacts with various managers (e.g., PowerModeManager, AIEnhancementService) to gather and apply settings, providing a unified interface for data portability.
Core Architecture
The settings architecture is based on direct interaction with UserDefaults from multiple points in the application. There is no central UserDefaultsManager class; instead, each module is responsible for its own persistence.
This diagram illustrates the decentralized nature of settings management.
Key-Value Storage
Simple settings are often managed using the @AppStorage property wrapper directly within SwiftUI views. This provides a declarative and convenient way to bind UI controls to UserDefaults values.
Sources: VoiceInk/Views/Settings/SettingsView.swift:15-18
Complex Data Storage
More complex data, such as arrays of custom objects, are handled by encoding the data into JSON format and storing the resulting Data or String in UserDefaults.
- Power Modes:
PowerModeManagerencodes an array ofPowerModeConfigobjects to JSON and saves it under the keypowerModeConfigurationsV2. - Custom Prompts:
AIEnhancementServicesaves an array ofCustomPromptobjects under the keycustomPrompts. - Dictionary Items: Custom dictionary spellings are stored as an encoded array of
DictionaryItemunder the keyCustomDictionaryItems.
Sources: VoiceInk/PowerMode/PowerModeConfig.swift:144-147, VoiceInk/Services/AIEnhancementService.swift:44-48, VoiceInk/Services/ImportExportService.swift:85-88
Settings Categories
Settings are logically grouped by feature. The following table summarizes many of the keys stored in UserDefaults.
| Key | Description | Managed By |
|---|---|---|
powerModeConfigurationsV2 | JSON array of all user-defined Power Mode configurations. | PowerModeManager |
customPrompts | JSON array of custom AI enhancement prompts. | AIEnhancementService |
selectedPromptId | The UUID of the currently active AI enhancement prompt. | AIEnhancementService |
isAIEnhancementEnabled | A boolean flag to enable or disable AI enhancement features. | AIEnhancementService |
useScreenCaptureContext | A boolean flag to enable screen context for AI enhancement. | AIEnhancementService |
CustomDictionaryItems | JSON array of words for the custom spelling dictionary. | DictionaryView / ImportExportService |
wordReplacements | A dictionary of word-to-replacement mappings. | WordReplacementView / ImportExportService |
IsMenuBarOnly | A boolean to hide the Dock icon and run only from the menu bar. | MenuBarManager |
UseAppleScriptPaste | A boolean to use AppleScript for pasting text, for non-standard keyboards. | SettingsView |
RecorderType | A string ("notch" or "mini") defining the recorder UI style. | WhisperState |
IsAudioCleanupEnabled | A boolean to enable automatic deletion of old audio files. | SettingsView |
AudioRetentionPeriod | An integer representing the retention period for audio files. | SettingsView |
IsTranscriptionCleanupEnabled | A boolean to enable automatic deletion of old transcriptions. | SettingsView |
TranscriptionRetentionMinutes | An integer for the transcription retention period in minutes. | SettingsView |
selectedHotkey1 / selectedHotkey2 | Raw string values for the primary and secondary hotkey choices. | HotkeyManager / SystemInfoService |
CurrentTranscriptionModel | The name of the selected transcription model. | WhisperState |
SelectedLanguage | The language code for transcription (e.g., "en", "auto"). | WhisperState |
selectedAIProvider | The raw string value of the selected AI provider (e.g., "openai"). | AIService |
Sources: VoiceInk/PowerMode/PowerModeConfig.swift:144, VoiceInk/Services/AIEnhancementService.swift:30, 36, 46, 52, VoiceInk/Services/ImportExportService.swift:45-57, VoiceInk/Services/SystemInfoService.swift:103-146, VoiceInk/Views/Settings/SettingsView.swift
Import/Export Service
The ImportExportService is a singleton class responsible for consolidating all application settings into a single, portable JSON file. This allows users to back up their configurations and migrate them between installations.
Sources: VoiceInk/Services/ImportExportService.swift:39-41
Data Structures
The service uses two primary Codable structs to structure the exported data.
VoiceInkExportedSettings
This is the top-level container for all exported data.
// From VoiceInk/Services/ImportExportService.swift
struct VoiceInkExportedSettings: Codable {
let version: String
let customPrompts: [CustomPrompt]
let powerModeConfigs: [PowerModeConfig]
let dictionaryItems: [DictionaryItem]?
let wordReplacements: [String: String]?
let generalSettings: GeneralSettings?
let customEmojis: [String]?
let customCloudModels: [CustomCloudModel]?
}
Sources: VoiceInk/Services/ImportExportService.swift:31-38
GeneralSettings
This struct aggregates various individual settings from UserDefaults and other managers into a single object for export.
// From VoiceInk/Services/ImportExportService.swift
struct GeneralSettings: Codable {
let toggleMiniRecorderShortcut: KeyboardShortcuts.Shortcut?
let toggleMiniRecorderShortcut2: KeyboardShortcuts.Shortcut?
let retryLastTranscriptionShortcut: KeyboardShortcuts.Shortcut?
let selectedHotkey1RawValue: String?
let selectedHotkey2RawValue: String?
let launchAtLoginEnabled: Bool?
let isMenuBarOnly: Bool?
let useAppleScriptPaste: Bool?
let recorderType: String?
let isTranscriptionCleanupEnabled: Bool?
let transcriptionRetentionMinutes: Int?
let isAudioCleanupEnabled: Bool?
let audioRetentionPeriod: Int?
let isSoundFeedbackEnabled: Bool?
let isSystemMuteEnabled: Bool?
let isPauseMediaEnabled: Bool?
let isTextFormattingEnabled: Bool?
let isExperimentalFeaturesEnabled: Bool?
}
Sources: VoiceInk/Services/ImportExportService.swift:6-29
Export Process
When a user initiates an export, the exportSettings function is called. It gathers data from multiple sources, encodes it into the VoiceInkExportedSettings structure, and writes it to a user-selected file location.
This sequence diagram shows the export data flow.
Import Process
The import process is the reverse of the export. The importSettings function reads a JSON file, decodes it into a VoiceInkExportedSettings object, and then distributes the settings back to the respective services and UserDefaults keys.
This sequence diagram shows the import data flow.
Conclusion
VoiceInk's configuration system is a pragmatic, decentralized architecture that leverages standard UserDefaults capabilities. While it lacks a single manager class, it effectively persists user preferences across various application modules. The ImportExportService plays a vital role by providing a centralized mechanism for data aggregation and restoration, ensuring that all user settings are portable and can be easily backed up. This design balances modular independence with the practical need for unified data management.