Skip to content

Commit

Permalink
fix: picker selection issue
Browse files Browse the repository at this point in the history
  • Loading branch information
phlpsong committed Feb 5, 2024
1 parent 5333ac2 commit d5bf0a1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Easydict/NewApp/Configuration/Configuration+Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ extension Defaults.Keys {

// DEEPL
static let deepLAuth = Key<String?>("EZDeepLAuthKey")
static let deepLTranslation = Key<String>("EZDeepLTranslationAPIKey", default: DeepLAPIUsagePriority.webFirst.rawValue)
static let deepLTranslation = Key<DeepLAPIUsagePriority>("EZDeepLTranslationAPIKey", default: DeepLAPIUsagePriority.webFirst)
static let deepLTranslateEndPointKey = Key<String?>("EZDeepLTranslateEndPointKey")

// BING
Expand Down
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 All @@ -15,6 +16,26 @@ enum DeepLAPIUsagePriority: String, CaseIterable, Hashable {
case authKeyOnly = "2"
}

extension DeepLAPIUsagePriority: Defaults.Serializable {
public static var bridge: Bridge = .init()

public struct Bridge: Defaults.Bridge {
public func serialize(_ value: DeepLAPIUsagePriority?) -> String? {
guard let value else { return DeepLAPIUsagePriority.webFirst.rawValue }
return "\(value.rawValue)"
}

public func deserialize(_ object: String?) -> DeepLAPIUsagePriority? {
guard let object else { return DeepLAPIUsagePriority.webFirst }
return DeepLAPIUsagePriority(rawValue: object) ?? DeepLAPIUsagePriority.webFirst
}

public typealias Value = DeepLAPIUsagePriority

public typealias Serializable = String
}
}

extension DeepLAPIUsagePriority: EnumLocalizedStringConvertible {
var title: String {
switch self {
Expand All @@ -31,7 +52,18 @@ extension DeepLAPIUsagePriority: EnumLocalizedStringConvertible {
@available(macOS 13.0, *)
extension EZDeepLTranslate: ConfigurableService {
func configurationListItems() -> some View {
ServiceConfigurationSecretSectionView(service: self, observeKeys: [.deepLAuth]) {
EZDeepLTranslateConfigurationView(service: self)
}
}

@available(macOS 13.0, *)
private struct EZDeepLTranslateConfigurationView: View {
let service: EZDeepLTranslate

@Default(.deepLTranslation) var apiUsagePriority

var body: some View {
ServiceConfigurationSecretSectionView(service: service, observeKeys: [.deepLAuth]) {
ServiceConfigurationSecureInputCell(
textFieldTitleKey: "service.configuration.deepl.auth_key.title",
key: .deepLAuth
Expand All @@ -43,11 +75,13 @@ extension EZDeepLTranslate: ConfigurableService {
placeholder: "service.configuration.deepl.endpoint.placeholder"
)

ServiceConfigurationPickerCell(
titleKey: "service.configuration.deepl.translation.title",
key: .deepLTranslation,
values: DeepLAPIUsagePriority.allCases
)
Picker("service.configuration.deepl.translation.title", selection: $apiUsagePriority) {
ForEach(DeepLAPIUsagePriority.allCases, id: \.rawValue) { value in
Text(value.title)
.tag(value)
}
}
.padding(10)
}
}
}

0 comments on commit d5bf0a1

Please sign in to comment.