From cb11cb005e81c64ba9da6608bbd803bd0666c7fb Mon Sep 17 00:00:00 2001 From: iamgabrielma Date: Wed, 17 Sep 2025 13:54:24 +0700 Subject: [PATCH 1/3] register new value for ai key in keychain --- WooCommerce/Classes/Authentication/Keychain+Entries.swift | 7 +++++++ WooCommerce/Classes/System/WooConstants.swift | 4 ++++ .../Classes/ViewRelated/AI Settings/AISettingsView.swift | 8 ++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/WooCommerce/Classes/Authentication/Keychain+Entries.swift b/WooCommerce/Classes/Authentication/Keychain+Entries.swift index 1b23d0bd833..6e05a053ed9 100644 --- a/WooCommerce/Classes/Authentication/Keychain+Entries.swift +++ b/WooCommerce/Classes/Authentication/Keychain+Entries.swift @@ -26,4 +26,11 @@ extension Keychain { get { self[WooConstants.siteCredentialPassword] } set { self[WooConstants.siteCredentialPassword] = newValue } } + + /// AI key provided by the merchant + /// + var merchantAIProviderKey: String? { + get { self[WooConstants.merchantAIProviderKey] } + set { self[WooConstants.merchantAIProviderKey] = newValue } + } } diff --git a/WooCommerce/Classes/System/WooConstants.swift b/WooCommerce/Classes/System/WooConstants.swift index 91090093677..9dd39866514 100644 --- a/WooCommerce/Classes/System/WooConstants.swift +++ b/WooCommerce/Classes/System/WooConstants.swift @@ -45,6 +45,10 @@ public enum WooConstants { /// static let sharedUserDefaultsSuiteName = "group.com.automattic.woocommerce" + /// Keychain Access's Key for the AI key entered by the merchant in AI settings + /// + static let merchantAIProviderKey = "merchantAIProviderKey" + /// Push Notifications ApplicationID /// #if DEBUG diff --git a/WooCommerce/Classes/ViewRelated/AI Settings/AISettingsView.swift b/WooCommerce/Classes/ViewRelated/AI Settings/AISettingsView.swift index ec847f1cd95..483d303d31c 100644 --- a/WooCommerce/Classes/ViewRelated/AI Settings/AISettingsView.swift +++ b/WooCommerce/Classes/ViewRelated/AI Settings/AISettingsView.swift @@ -1,6 +1,9 @@ import SwiftUI +import KeychainAccess @Observable final class AISettingsViewModel { + var keychain = Keychain(service: WooConstants.keychainServiceName) + var usesJetpackAsDefaultAIProviderSource: Bool = false var isEditingApiKey: Bool = false var apiKey: String = "" @@ -20,11 +23,12 @@ import SwiftUI } func clearAPIKey() { - // TODO + apiKey = "" + keychain.merchantAIProviderKey = nil } private func saveSettings() { - // TODO + keychain.merchantAIProviderKey = apiKey } } From 4368cb3990ae0c0e5d6e66de6c6bebdcc62358d9 Mon Sep 17 00:00:00 2001 From: iamgabrielma Date: Wed, 17 Sep 2025 13:59:57 +0700 Subject: [PATCH 2/3] render clear button when editing api key and not empty --- .../Classes/ViewRelated/AI Settings/AISettingsView.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WooCommerce/Classes/ViewRelated/AI Settings/AISettingsView.swift b/WooCommerce/Classes/ViewRelated/AI Settings/AISettingsView.swift index 483d303d31c..2417e11c991 100644 --- a/WooCommerce/Classes/ViewRelated/AI Settings/AISettingsView.swift +++ b/WooCommerce/Classes/ViewRelated/AI Settings/AISettingsView.swift @@ -69,8 +69,9 @@ struct AISettingsView: View { .textFieldStyle(RoundedBorderTextFieldStyle()) .foregroundColor(.primary) .privacySensitive() + .disabled(!viewModel.isEditingApiKey) - if viewModel.isEditingApiKey, !viewModel.apiKey.isEmpty { + if viewModel.isEditingApiKey && !viewModel.apiKey.isEmpty { Button(action: viewModel.clearAPIKey) { Image(systemName: "xmark.circle.fill") .foregroundColor(.gray) From f8e69a201e432227d3a73a4ecf7c56a22f36f8c4 Mon Sep 17 00:00:00 2001 From: iamgabrielma Date: Wed, 17 Sep 2025 14:06:46 +0700 Subject: [PATCH 3/3] load ai api key if any --- .../Classes/ViewRelated/AI Settings/AISettingsView.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/WooCommerce/Classes/ViewRelated/AI Settings/AISettingsView.swift b/WooCommerce/Classes/ViewRelated/AI Settings/AISettingsView.swift index 2417e11c991..9119f2142a6 100644 --- a/WooCommerce/Classes/ViewRelated/AI Settings/AISettingsView.swift +++ b/WooCommerce/Classes/ViewRelated/AI Settings/AISettingsView.swift @@ -27,6 +27,10 @@ import KeychainAccess keychain.merchantAIProviderKey = nil } + func loadSettings() { + apiKey = keychain.merchantAIProviderKey ?? "" + } + private func saveSettings() { keychain.merchantAIProviderKey = apiKey } @@ -132,6 +136,9 @@ struct AISettingsView: View { .foregroundColor(.secondary) .frame(maxWidth: .infinity, alignment: .center) } + .onAppear { + viewModel.loadSettings() + } .padding() .navigationTitle(Localization.navigationTitle) }