Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions WooCommerce/Classes/Authentication/Keychain+Entries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}
4 changes: 4 additions & 0 deletions WooCommerce/Classes/System/WooConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions WooCommerce/Classes/ViewRelated/AI Settings/AISettingsView.swift
Original file line number Diff line number Diff line change
@@ -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 = ""
Expand All @@ -20,11 +23,16 @@ import SwiftUI
}

func clearAPIKey() {
// TODO
apiKey = ""
keychain.merchantAIProviderKey = nil
}

func loadSettings() {
apiKey = keychain.merchantAIProviderKey ?? ""
}

private func saveSettings() {
// TODO
keychain.merchantAIProviderKey = apiKey
}
}

Expand Down Expand Up @@ -65,8 +73,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)
Expand Down Expand Up @@ -127,6 +136,9 @@ struct AISettingsView: View {
.foregroundColor(.secondary)
.frame(maxWidth: .infinity, alignment: .center)
}
.onAppear {
viewModel.loadSettings()
}
.padding()
.navigationTitle(Localization.navigationTitle)
}
Expand Down