Skip to content

Commit

Permalink
fix: optimize defaults with picker cell
Browse files Browse the repository at this point in the history
  • Loading branch information
phlpsong committed Feb 5, 2024
1 parent 423e570 commit 8d7d091
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 48 deletions.
4 changes: 2 additions & 2 deletions Easydict/NewApp/Configuration/Configuration+Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ extension Defaults.Keys {
static let openAITranslation = Key<String>("EZOpenAITranslationKey", default: "1")
static let openAIDictionary = Key<String>("EZOpenAIDictionaryKey", default: "1")
static let openAISentence = Key<String>("EZOpenAISentenceKey", default: "1")
static let openAIServiceUsageStatus = Key<String>("EZOpenAIServiceUsageStatusKey", default: "0")
static let openAIServiceUsageStatus = Key<OpenAIUsageStats>("EZOpenAIServiceUsageStatusKey", default: OpenAIUsageStats.default)
static let openAIEndPoint = Key<String?>("EZOpenAIEndPointKey")
static let openAIModel = Key<String>("EZOpenAIModelKey", default: OpenAIModels.gpt3_5_turbo_0125.rawValue)
static let openAIModel = Key<OpenAIModels>("EZOpenAIModelKey", default: OpenAIModels.gpt3_5_turbo_0125)

// DEEPL
static let deepLAuth = Key<String?>("EZDeepLAuthKey")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -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: "")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2024 izual. All rights reserved.
//

import Defaults
import Foundation
import SwiftUI

Expand Down Expand Up @@ -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"
}
Expand All @@ -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"
Expand Down Expand Up @@ -105,3 +100,5 @@ extension OpenAIUsageStats: EnumLocalizedStringConvertible {
}
}
}

extension OpenAIUsageStats: Defaults.Serializable {}
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,21 @@ struct ServiceConfigurationInputCell: View {
}

@available(macOS 13.0, *)
struct ServiceConfigurationPickerCell<T: RawRepresentable & Hashable & EnumLocalizedStringConvertible>: View
where T.RawValue: StringProtocol
{
@Default var value: String
struct ServiceConfigurationPickerCell<T: Hashable & Defaults.Serializable & EnumLocalizedStringConvertible>: View {
@Default var value: T
let titleKey: LocalizedStringKey
let values: [T]

init(titleKey: LocalizedStringKey, key: Defaults.Key<String>, values: [T]) {
init(titleKey: LocalizedStringKey, key: Defaults.Key<T>, values: [T]) {
self.titleKey = titleKey
self.values = values
_value = .init(key)
}

var body: some View {
Picker(titleKey, selection: $value) {
ForEach(values, id: \.rawValue) { value in
ForEach(values, id: \.self) { value in
Text(value.title)
.tag(value.rawValue)
}
}
.padding(10.0)
Expand Down

0 comments on commit 8d7d091

Please sign in to comment.