The goal is to centralize the API key management into a new object HJHKeys as requested. This will ensure that changing the key (or the default key) only needs to happen in one place, and all components (Repository, ViewModel, Service) access it utilizing the same logic (checking SharedPreferences first).
User Review Required
IMPORTANT
A new file HJHKeys.kt will be created.
Repository.kt, AIChatViewModel.kt, CAGEManager.kt, and SettingsFragment.kt will be modified to reference this new file.
Proposed Changes
Utils Layer
[NEW] HJHKeys.kt
- Create a singleton object
HJHKeys. - Constants:
DEFAULT_API_KEY: The hardcoded “sk-…” key.PREF_NAME: “how.naver.translator1.prefs”KEY_OPENAI_API_KEY: “openai_api_key”
- Methods:
- getApiKey(context: Context): String: Returns the saved key from prefs, or
DEFAULT_API_KEYif empty.
- getApiKey(context: Context): String: Returns the saved key from prefs, or
Data Layer
[MODIFY] Repository.kt
- Remove
DEFAULT_API_KEYconstant. - Replace getApiKey() implementation to delegate to
HJHKeys.getApiKey(context).
[MODIFY] CAGEManager.kt
- Remove
DEFAULT_API_KEYconstant. - Replace getApiKey() implementation to delegate to
HJHKeys.getApiKey(context).
UI Layer
[MODIFY] AIChatViewModel.kt
- Remove
DEFAULT_API_KEYconstant. - Update initialization and preference listeners to use constants from
HJHKeys.
[MODIFY] SettingsFragment.kt
- Use constants from
HJHKeysfor preference lookups (optional but recommended for consistency).
Verification Plan
Automated Tests
- None existing for this specific logic.
Manual Verification
- Default Key Check: Clear app data or ensure no key is saved in settings. Run the app and use a feature requiring the ID (e.g., Chat). It should work using the default key.
- Custom Key Check: Go to Settings, enter a “fake” key or a different valid key.
- Verify that the features now use the new key (logs might show this, or behavior might fail if key is invalid).
- In this case, since we are just trying to ensure the code compiles and runs without hitting “OpenAPI 키가 설정되지 않았습니다”, successful chat response is the success metric.