From 8d7d0916315f4df46bda20283373c57ef09ae2a2 Mon Sep 17 00:00:00 2001 From: phlpsong Date: Mon, 5 Feb 2024 23:06:03 +0800 Subject: [PATCH] fix: optimize defaults with picker cell --- .../Configuration+Defaults.swift | 4 +- .../DeepLTranslate+ConfigurableService.swift | 56 +++++++++---------- .../OpenAIService+ConfigurableService.swift | 15 ++--- .../ServiceConfigurationCells.swift | 11 ++-- 4 files changed, 38 insertions(+), 48 deletions(-) diff --git a/Easydict/NewApp/Configuration/Configuration+Defaults.swift b/Easydict/NewApp/Configuration/Configuration+Defaults.swift index 7ff6dd2eb..7e3427324 100644 --- a/Easydict/NewApp/Configuration/Configuration+Defaults.swift +++ b/Easydict/NewApp/Configuration/Configuration+Defaults.swift @@ -139,9 +139,9 @@ extension Defaults.Keys { static let openAITranslation = Key("EZOpenAITranslationKey", default: "1") static let openAIDictionary = Key("EZOpenAIDictionaryKey", default: "1") static let openAISentence = Key("EZOpenAISentenceKey", default: "1") - static let openAIServiceUsageStatus = Key("EZOpenAIServiceUsageStatusKey", default: "0") + static let openAIServiceUsageStatus = Key("EZOpenAIServiceUsageStatusKey", default: OpenAIUsageStats.default) static let openAIEndPoint = Key("EZOpenAIEndPointKey") - static let openAIModel = Key("EZOpenAIModelKey", default: OpenAIModels.gpt3_5_turbo_0125.rawValue) + static let openAIModel = Key("EZOpenAIModelKey", default: OpenAIModels.gpt3_5_turbo_0125) // DEEPL static let deepLAuth = Key("EZDeepLAuthKey") diff --git a/Easydict/NewApp/Utility/Extensions/QueryService+ConfigurableService/DeepLTranslate+ConfigurableService.swift b/Easydict/NewApp/Utility/Extensions/QueryService+ConfigurableService/DeepLTranslate+ConfigurableService.swift index 32ed9b786..3c6a7d17b 100644 --- a/Easydict/NewApp/Utility/Extensions/QueryService+ConfigurableService/DeepLTranslate+ConfigurableService.swift +++ b/Easydict/NewApp/Utility/Extensions/QueryService+ConfigurableService/DeepLTranslate+ConfigurableService.swift @@ -10,27 +10,6 @@ import Defaults import Foundation import SwiftUI -enum DeepLAPIUsagePriority: String, CaseIterable, Hashable { - case webFirst = "0" - case authKeyFirst = "1" - case authKeyOnly = "2" -} - -extension DeepLAPIUsagePriority: Defaults.Serializable {} - -extension DeepLAPIUsagePriority: EnumLocalizedStringConvertible { - var title: String { - switch self { - case .webFirst: - return NSLocalizedString("service.configuration.deepl.web_first.title", bundle: .main, comment: "") - case .authKeyFirst: - return NSLocalizedString("service.configuration.deepl.authkey_first.title", bundle: .main, comment: "") - case .authKeyOnly: - return NSLocalizedString("service.configuration.deepl.authkey_only.title", bundle: .main, comment: "") - } - } -} - @available(macOS 13.0, *) extension EZDeepLTranslate: ConfigurableService { func configurationListItems() -> some View { @@ -42,8 +21,6 @@ extension EZDeepLTranslate: ConfigurableService { private struct EZDeepLTranslateConfigurationView: View { let service: EZDeepLTranslate - @Default(.deepLTranslation) var apiUsagePriority - var body: some View { ServiceConfigurationSecretSectionView(service: service, observeKeys: [.deepLAuth]) { ServiceConfigurationSecureInputCell( @@ -57,13 +34,32 @@ private struct EZDeepLTranslateConfigurationView: View { placeholder: "service.configuration.deepl.endpoint.placeholder" ) - Picker("service.configuration.deepl.translation.title", selection: $apiUsagePriority) { - ForEach(DeepLAPIUsagePriority.allCases, id: \.rawValue) { value in - Text(value.title) - .tag(value) - } - } - .padding(10) + ServiceConfigurationPickerCell( + titleKey: "service.configuration.deepl.translation.title", + key: .deepLTranslation, + values: DeepLAPIUsagePriority.allCases + ) + } + } +} + +enum DeepLAPIUsagePriority: String, CaseIterable { + case webFirst = "0" + case authKeyFirst = "1" + case authKeyOnly = "2" +} + +extension DeepLAPIUsagePriority: Defaults.Serializable {} + +extension DeepLAPIUsagePriority: EnumLocalizedStringConvertible { + var title: String { + switch self { + case .webFirst: + return NSLocalizedString("service.configuration.deepl.web_first.title", bundle: .main, comment: "") + case .authKeyFirst: + return NSLocalizedString("service.configuration.deepl.authkey_first.title", bundle: .main, comment: "") + case .authKeyOnly: + return NSLocalizedString("service.configuration.deepl.authkey_only.title", bundle: .main, comment: "") } } } diff --git a/Easydict/NewApp/Utility/Extensions/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift b/Easydict/NewApp/Utility/Extensions/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift index 54b3f144f..091631ed8 100644 --- a/Easydict/NewApp/Utility/Extensions/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift +++ b/Easydict/NewApp/Utility/Extensions/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift @@ -6,6 +6,7 @@ // Copyright © 2024 izual. All rights reserved. // +import Defaults import Foundation import SwiftUI @@ -56,11 +57,7 @@ protocol EnumLocalizedStringConvertible { var title: String { get } } -enum OpenAIModels: String, CaseIterable, Identifiable { - var id: Self { - self - } - +enum OpenAIModels: String, CaseIterable { case gpt3_5_turbo_0125 = "gpt-3.5-turbo-0125" case gpt4_0125_preview = "gpt-4-0125-preview" } @@ -71,11 +68,9 @@ extension OpenAIModels: EnumLocalizedStringConvertible { } } -enum OpenAIUsageStats: String, CaseIterable, Identifiable { - var id: Self { - self - } +extension OpenAIModels: Defaults.Serializable {} +enum OpenAIUsageStats: String, CaseIterable { case `default` = "0" case alwaysOff = "1" case alwaysOn = "2" @@ -105,3 +100,5 @@ extension OpenAIUsageStats: EnumLocalizedStringConvertible { } } } + +extension OpenAIUsageStats: Defaults.Serializable {} diff --git a/Easydict/NewApp/View/SettingView/Tabs/ServiceConfiguration/ServiceConfigurationCells.swift b/Easydict/NewApp/View/SettingView/Tabs/ServiceConfiguration/ServiceConfigurationCells.swift index 7237dd028..83fa05c33 100644 --- a/Easydict/NewApp/View/SettingView/Tabs/ServiceConfiguration/ServiceConfigurationCells.swift +++ b/Easydict/NewApp/View/SettingView/Tabs/ServiceConfiguration/ServiceConfigurationCells.swift @@ -49,14 +49,12 @@ struct ServiceConfigurationInputCell: View { } @available(macOS 13.0, *) -struct ServiceConfigurationPickerCell: View - where T.RawValue: StringProtocol -{ - @Default var value: String +struct ServiceConfigurationPickerCell: View { + @Default var value: T let titleKey: LocalizedStringKey let values: [T] - init(titleKey: LocalizedStringKey, key: Defaults.Key, values: [T]) { + init(titleKey: LocalizedStringKey, key: Defaults.Key, values: [T]) { self.titleKey = titleKey self.values = values _value = .init(key) @@ -64,9 +62,8 @@ struct ServiceConfigurationPickerCell