Skip to content

Commit

Permalink
service configuration view
Browse files Browse the repository at this point in the history
  • Loading branch information
CanglongCl committed Jan 15, 2024
1 parent 4498f74 commit a7977ca
Show file tree
Hide file tree
Showing 12 changed files with 351 additions and 19 deletions.
36 changes: 36 additions & 0 deletions Easydict.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@
EA9943EE2B5353AB00EE7B97 /* WindowTypeExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9943ED2B5353AB00EE7B97 /* WindowTypeExtensions.swift */; };
EA9943F02B5354C400EE7B97 /* ShowWindowPositionExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9943EF2B5354C400EE7B97 /* ShowWindowPositionExtensions.swift */; };
EA9943F22B5358BF00EE7B97 /* LanguageExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA9943F12B5358BF00EE7B97 /* LanguageExtensions.swift */; };
EAED41EC2B54AA920005FE0A /* ServiceConfigurationSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAED41EB2B54AA920005FE0A /* ServiceConfigurationSection.swift */; };
EAED41EF2B54B1430005FE0A /* ConfigurableService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAED41EE2B54B1430005FE0A /* ConfigurableService.swift */; };
EAED41F22B54B39D0005FE0A /* OpenAIService+ConfigurableService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAED41F12B54B39D0005FE0A /* OpenAIService+ConfigurableService.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -760,6 +763,9 @@
EA9943ED2B5353AB00EE7B97 /* WindowTypeExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowTypeExtensions.swift; sourceTree = "<group>"; };
EA9943EF2B5354C400EE7B97 /* ShowWindowPositionExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShowWindowPositionExtensions.swift; sourceTree = "<group>"; };
EA9943F12B5358BF00EE7B97 /* LanguageExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LanguageExtensions.swift; sourceTree = "<group>"; };
EAED41EB2B54AA920005FE0A /* ServiceConfigurationSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServiceConfigurationSection.swift; sourceTree = "<group>"; };
EAED41EE2B54B1430005FE0A /* ConfigurableService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigurableService.swift; sourceTree = "<group>"; };
EAED41F12B54B39D0005FE0A /* OpenAIService+ConfigurableService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OpenAIService+ConfigurableService.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -2069,6 +2075,7 @@
27FE980C2B3DD749000AD654 /* Tabs */ = {
isa = PBXGroup;
children = (
EAED41EA2B54A4900005FE0A /* ServiceConfiguration */,
278540332B3DE04F004E9488 /* GeneralTab.swift */,
0A057D6C2B499A000025C51D /* ServiceTab.swift */,
276742042B3DC230002A2C75 /* PrivacyTab.swift */,
Expand Down Expand Up @@ -2202,6 +2209,7 @@
EA9943DD2B534BAE00EE7B97 /* Utility */ = {
isa = PBXGroup;
children = (
EAED41ED2B54B1390005FE0A /* Protocol */,
EA9943E62B534D7C00EE7B97 /* Extensions */,
);
path = Utility;
Expand All @@ -2218,6 +2226,7 @@
EA9943E62B534D7C00EE7B97 /* Extensions */ = {
isa = PBXGroup;
children = (
EAED41F02B54B1A60005FE0A /* QueryService+ConfigurableService */,
EA9943E72B534D8900EE7B97 /* LanguageDetectOptimizeExtensions.swift */,
EA9943ED2B5353AB00EE7B97 /* WindowTypeExtensions.swift */,
EA9943EF2B5354C400EE7B97 /* ShowWindowPositionExtensions.swift */,
Expand All @@ -2226,6 +2235,30 @@
path = Extensions;
sourceTree = "<group>";
};
EAED41EA2B54A4900005FE0A /* ServiceConfiguration */ = {
isa = PBXGroup;
children = (
EAED41EB2B54AA920005FE0A /* ServiceConfigurationSection.swift */,
);
path = ServiceConfiguration;
sourceTree = "<group>";
};
EAED41ED2B54B1390005FE0A /* Protocol */ = {
isa = PBXGroup;
children = (
EAED41EE2B54B1430005FE0A /* ConfigurableService.swift */,
);
path = Protocol;
sourceTree = "<group>";
};
EAED41F02B54B1A60005FE0A /* QueryService+ConfigurableService */ = {
isa = PBXGroup;
children = (
EAED41F12B54B39D0005FE0A /* OpenAIService+ConfigurableService.swift */,
);
path = "QueryService+ConfigurableService";
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -2633,6 +2666,7 @@
03F14A3B2956016B00CB7379 /* EZVolcanoTranslate.m in Sources */,
03B0230429231FA6001C7E63 /* EZHoverButton.m in Sources */,
0342A9812AD64924002A9F5F /* NSString+EZSplit.m in Sources */,
EAED41EF2B54B1430005FE0A /* ConfigurableService.swift in Sources */,
03BD2825294875AE00F5891A /* EZMyLabel.m in Sources */,
03B0233029231FA6001C7E63 /* MMCrashUncaughtExceptionHandler.m in Sources */,
03D5FCFF2A5EF4E400AD26BE /* EZDeviceSystemInfo.m in Sources */,
Expand Down Expand Up @@ -2773,6 +2807,7 @@
039F5508294B6E29004AB940 /* EZAboutViewController.m in Sources */,
03D8A6592A42A1A300D9A968 /* EZAppModel.m in Sources */,
036E7D7B293F4FC8002675DF /* EZOpenLinkButton.m in Sources */,
EAED41EC2B54AA920005FE0A /* ServiceConfigurationSection.swift in Sources */,
276742092B3DC230002A2C75 /* AboutTab.swift in Sources */,
03008B2E2941956D0062B821 /* EZURLSchemeHandler.m in Sources */,
DC6D9C872B352EBC0055EFFC /* FontSizeHintView.swift in Sources */,
Expand All @@ -2788,6 +2823,7 @@
03008B3F29444B0A0062B821 /* NSView+EZAnimatedHidden.m in Sources */,
03B022FD29231FA6001C7E63 /* EZFixedQueryWindow.m in Sources */,
03B0232C29231FA6001C7E63 /* NSView+MM.m in Sources */,
EAED41F22B54B39D0005FE0A /* OpenAIService+ConfigurableService.swift in Sources */,
033C31002A74CECE0095926A /* EZAppleDictionary.m in Sources */,
03E2BF752A298F2B00E010F3 /* NSString+EZUtils.m in Sources */,
03B022F529231FA6001C7E63 /* EZDetectManager.m in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions Easydict/App/Easydict-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
#import "NSString+EZConvenience.h"
#import "EZWindowManager.h"
#import "NSViewController+EZWindow.h"

#import "EZOpenAIService.h"
117 changes: 116 additions & 1 deletion Easydict/App/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,92 @@
}
}
},
"service.configuration.openai.api_key.footer" : {
"localizations" : {
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "API Key的一些说明或者加入链接"
}
}
}
},
"service.configuration.openai.api_key.header" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "OpenAI API Key"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "OpenAI API Key"
}
}
}
},
"service.configuration.openai.api_key.prompt" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
},
"service.configuration.openai.api_key.title" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "OpenAI API Key"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "OpenAI API Key"
}
}
}
},
"service.configuration.openai.translation.footer" : {

},
"service.configuration.openai.translation.header" : {

},
"service.configuration.openai.translation.prompt" : {

},
"service.configuration.openai.translation.title" : {

},
"service.service_configuration.reset" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Reset"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "重置"
}
}
}
},
"setting_general" : {
"localizations" : {
"en" : {
Expand Down Expand Up @@ -2743,8 +2829,37 @@
}
}
},
"setting.service.detail.no_configuration %@" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "No configuration for %@"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "%@没有可供配置的选项"
}
}
}
},
"setting.service.detail.no_selection" : {

"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Select a service to show configuration"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "选择服务以查看配置"
}
}
}
},
"setting.tts_service.options.apple" : {
"localizations" : {
Expand Down
7 changes: 5 additions & 2 deletions Easydict/Feature/Service/Ali/AliService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Alamofire
import CryptoKit
import Defaults
import Foundation

@objc(EZAliService)
Expand Down Expand Up @@ -76,8 +77,10 @@ class AliService: QueryService {
easydict://writeKeyValue?EZAliAccessKeyId=
easydict://writeKeyValue?EZAliAccessKeySecret=
*/
if let id = UserDefaults.standard.string(forKey: EZAliAccessKeyId),
let secret = UserDefaults.standard.string(forKey: EZAliAccessKeySecret), !id.isEmpty, !secret.isEmpty
if let id = Defaults[.aliAccessKeyId],
let secret = Defaults[.aliAccessKeySecret],
!id.isEmpty,
!secret.isEmpty
{
requestByAPI(id: id, secret: secret, transType: transType, text: text, from: from, to: to, completion: completion)
} else { // use web api
Expand Down
3 changes: 2 additions & 1 deletion Easydict/Feature/Service/Caiyun/CaiyunService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Alamofire
import Defaults
import Foundation

@objc(EZCaiyunService)
Expand Down Expand Up @@ -44,7 +45,7 @@ public final class CaiyunService: QueryService {

// easydict://writeKeyValue?EZCaiyunToken=
private var token: String {
let token = UserDefaults.standard.string(forKey: EZCaiyunToken)
let token = Defaults[.caiyunToken]
if let token, !token.isEmpty {
return token
} else {
Expand Down
1 change: 1 addition & 0 deletions Easydict/Feature/Service/OpenAI/EZOpenAIService.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ @interface EZOpenAIService ()

@end


@implementation EZOpenAIService

- (instancetype)init {
Expand Down
5 changes: 3 additions & 2 deletions Easydict/Feature/Service/Tencent/TencentService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import Alamofire
import Defaults
import Foundation

@objc(EZTencentService)
Expand Down Expand Up @@ -64,7 +65,7 @@ public final class TencentService: QueryService {

// easydict://writeKeyValue?EZTencentSecretId=xxx
private var secretId: String {
let secretId = UserDefaults.standard.string(forKey: EZTencentSecretId)
let secretId = Defaults[.tencentSecretId]
if let secretId, !secretId.isEmpty {
return secretId
} else {
Expand All @@ -74,7 +75,7 @@ public final class TencentService: QueryService {

// easydict://writeKeyValue?EZTencentSecretKey=xxx
private var secretKey: String {
let secretKey = UserDefaults.standard.string(forKey: EZTencentSecretKey)
let secretKey = Defaults[.tencentSecretKey]
if let secretKey, !secretKey.isEmpty {
return secretKey
} else {
Expand Down
35 changes: 35 additions & 0 deletions Easydict/NewApp/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Defaults
import Foundation

// Setting
extension Defaults.Keys {
// rename `from`
static let queryFromLanguage = Key<Language>("EZConfiguration_kFromKey", default: .auto)
Expand Down Expand Up @@ -53,3 +54,37 @@ extension Defaults.Keys {
static let appearanceType = Key<AppearenceType>("EZConfiguration_kApperanceKey", default: .followSystem)
static let fontSizeOptionIndex = Key<UInt>("EZConfiguration_kTranslationControllerFontKey", default: 0)
}

// Service Configuration
extension Defaults.Keys {
// OPENAI
static let openAIAPI = Key<String?>("EZOpenAIAPIKey")
static let openAITranslation = Key<String?>("EZOpenAITranslationKey")
static let openAIDictionary = Key<String?>("EZOpenAIDictionaryKey")
static let openAISentence = Key<String?>("EZOpenAISentenceKey")
static let openAIServiceUsageStatus = Key<String?>("EZOpenAIServiceUsageStatusKey")
static let openAIDomain = Key<String?>("EZOpenAIDomainKey")
static let openAIEndPoint = Key<String?>("EZOpenAIEndPointKey")
static let openAIModel = Key<String?>("EZOpenAIModelKey")

// DEEPL
static let deepLAuth = Key<String?>("EZDeepLAuthKey")
static let deepLTranslateEndPointKey = Key<String?>("EZDeepLTranslateEndPointKey")

// BING
static let bingCookieKey = Key<String?>("EZBingCookieKey")

// niu
static let niuTransAPIKey = Key<String?>("EZNiuTransAPIKey")

// Caiyun
static let caiyunToken = Key<String?>("EZCaiyunToken")

// tencent
static let tencentSecretId = Key<String?>("EZTencentSecretId")
static let tencentSecretKey = Key<String?>("EZTencentSecretKey")

// ALI
static let aliAccessKeyId = Key<String?>("EZAliAccessKeyId")
static let aliAccessKeySecret = Key<String?>("EZAliAccessKeySecret")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// OpenAIService+ConfigurableService.swift
// Easydict
//
// Created by 戴藏龙 on 2024/1/14.
// Copyright © 2024 izual. All rights reserved.
//

import Foundation
import SwiftUI

@available(macOS 12.0, *)
extension EZOpenAIService: ConfigurableService {
func configurationListItems() -> some View {
ServiceStringConfigurationSection(
textFieldTitleKey: "service.configuration.openai.api_key.header",
headerTitleKey: "service.configuration.openai.api_key.title",
key: .openAIAPI,
prompt: "service.configuration.openai.api_key.prompt",
footer: {
Text("service.configuration.openai.api_key.footer")
}
)

ServiceStringConfigurationSection(
textFieldTitleKey: "service.configuration.openai.translation.header",
headerTitleKey: "service.configuration.openai.translation.title",
key: .openAITranslation,
prompt: "service.configuration.openai.translation.prompt",
footer: {
Text("service.configuration.openai.translation.footer")
}
)
}
}
Loading

0 comments on commit a7977ca

Please sign in to comment.