Skip to content

Commit

Permalink
perf: callback EZQueryResult cannot be _Nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Nov 15, 2023
1 parent 68e7d4b commit 59fd73e
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ - (NSString *)name {
return orderedDict;
}

- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *, NSError *_Nullable))completion {
if ([self prehandleQueryTextLanguage:text autoConvertChineseText:NO from:from to:to completion:completion]) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion Easydict/Feature/Service/Apple/EZAppleService.m
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ - (MMOrderedDictionary *)ocrLanguageDictionary {
return orderedDict;
}

- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *, NSError *_Nullable))completion {
if (text.length == 0) {
NSLog(@"text is empty");
return;
Expand Down
8 changes: 4 additions & 4 deletions Easydict/Feature/Service/Baidu/EZBaiduTranslate.m
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ - (nullable NSString *)wordLink:(EZQueryModel *)queryModel {
return orderedDict;
}

- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(nonnull void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(nonnull void (^)(EZQueryResult *, NSError *_Nullable))completion {
if (!text.length) {
completion(self.result, EZTranslateError(EZErrorTypeParam, @"翻译的文本为空", nil));
return;
Expand Down Expand Up @@ -459,7 +459,7 @@ - (void)ocrAndTranslate:(NSImage *)image from:(EZLanguage)from to:(EZLanguage)to

#pragma mark - Web translate API

- (void)sendTranslateRequest:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(nonnull void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)sendTranslateRequest:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(nonnull void (^)(EZQueryResult *, NSError *_Nullable))completion {

// 获取sign
JSValue *value = [self.jsFunction callWithArguments:@[ text, self.gtk ]];
Expand Down Expand Up @@ -505,7 +505,7 @@ - (void)sendTranslateRequest:(NSString *)text from:(EZLanguage)from to:(EZLangua
}

// TODO: need to optimize the results of Baidu query words.
- (void)parseResponseObject:(id _Nullable)responseObject completion:(nonnull void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)parseResponseObject:(id _Nullable)responseObject completion:(nonnull void (^)(EZQueryResult *, NSError *_Nullable))completion {
if ([self.queryModel isServiceStopped:self.serviceType]) {
return;
}
Expand Down Expand Up @@ -816,7 +816,7 @@ - (void)updateCookieAndToken {

#pragma mark - WebView Translate.

- (void)webViewTranslate:(nonnull void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)webViewTranslate:(nonnull void (^)(EZQueryResult *, NSError *_Nullable))completion {
NSString *monitorURL = @"https://fanyi.baidu.com/v2transapi";
[self.webViewTranslator monitorBaseURLString:monitorURL
loadURL:[self wordLink:self.queryModel]
Expand Down
2 changes: 1 addition & 1 deletion Easydict/Feature/Service/Bing/EZBingService.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ - (EZQueryTextType)intelligentQueryTextType {
return orderedDict;
}

- (void)translate:(NSString *)text from:(nonnull EZLanguage)from to:(nonnull EZLanguage)to completion:(nonnull void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)translate:(NSString *)text from:(nonnull EZLanguage)from to:(nonnull EZLanguage)to completion:(nonnull void (^)(EZQueryResult *, NSError *_Nullable))completion {
if ([self prehandleQueryTextLanguage:text autoConvertChineseText:NO from:from to:to completion:completion]) {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions Easydict/Feature/Service/DeepL/EZDeepLTranslate.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ - (nullable NSString *)wordLink:(EZQueryModel *)queryModel {
return orderedDict;
}

- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *, NSError *_Nullable))completion {
if ([self prehandleQueryTextLanguage:text autoConvertChineseText:YES from:from to:to completion:completion]) {
return;
}
Expand Down Expand Up @@ -162,7 +162,7 @@ - (void)ocr:(EZQueryModel *)queryModel completion:(void (^)(EZOCRResult *_Nullab

#pragma mark - WebView Translate

- (void)webViewTranslate:(nonnull void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)webViewTranslate:(nonnull void (^)(EZQueryResult *, NSError *_Nullable))completion {
NSString *wordLink = [self wordLink:self.queryModel];

mm_weakify(self);
Expand Down Expand Up @@ -195,7 +195,7 @@ - (void)webViewTranslate:(nonnull void (^)(EZQueryResult *_Nullable, NSError *_N
#pragma mark - DeepL Web Translate

/// DeepL web translate. Ref: https://github.com/akl7777777/bob-plugin-akl-deepl-free-translate/blob/9d194783b3eb8b3a82f21bcfbbaf29d6b28c2761/src/main.js
- (void)deepLWebTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)deepLWebTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *, NSError *_Nullable))completion {
NSString *souceLangCode = [self languageCodeForLanguage:from];
NSString *targetLangCode = [self languageCodeForLanguage:to];

Expand Down Expand Up @@ -322,7 +322,7 @@ - (NSInteger)getTimeStampWithIcount:(NSInteger)iCount {

#pragma mark - DeepL Official Translate API

- (void)deepLTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion{
- (void)deepLTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *, NSError *_Nullable))completion{
// Docs: https://www.deepl.com/zh/docs-api/translating-text

NSString *souceLangCode = [self languageCodeForLanguage:from];
Expand Down
6 changes: 3 additions & 3 deletions Easydict/Feature/Service/Google/EZGoogleTranslate.m
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ - (nullable NSString *)wordLink:(EZQueryModel *)queryModel {
- (void)translate:(NSString *)text
from:(EZLanguage)from
to:(EZLanguage)to
completion:(nonnull void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
completion:(nonnull void (^)(EZQueryResult *, NSError *_Nullable))completion {
if ([self prehandleQueryTextLanguage:text autoConvertChineseText:NO from:from to:to completion:completion]) {
return;
}
Expand Down Expand Up @@ -326,7 +326,7 @@ - (void)ocrAndTranslate:(NSImage *)image
#pragma mark - WebApp, including word info.

/// This API can get word info, like pronunciation, but transaltion may be inaccurate, compare to web transaltion.
- (void)webApptranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(nonnull void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)webApptranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(nonnull void (^)(EZQueryResult *, NSError *_Nullable))completion {
if (!text.length) {
completion(self.result, EZTranslateError(EZErrorTypeParam, @"翻译的文本为空", nil));
return;
Expand Down Expand Up @@ -659,7 +659,7 @@ - (void)sendGTXTranslate:(NSString *)text
- (void)gtxTranslate:(NSString *)text
from:(EZLanguage)from
to:(EZLanguage)to
completion:(nonnull void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
completion:(nonnull void (^)(EZQueryResult *, NSError *_Nullable))completion {
EZQueryResult *result = self.result;

if (!text.length) {
Expand Down
4 changes: 2 additions & 2 deletions Easydict/Feature/Service/Model/EZQueryService.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ NS_ASSUME_NONNULL_BEGIN
/// 是否提前处理查询,如不支持的语言
/// - Parameters:
/// - isAutoConvert: 是否使用本地中文简繁体转换,如 API 服务支持繁简体,则最好交给 API。
- (BOOL)prehandleQueryTextLanguage:(NSString *)text autoConvertChineseText:(BOOL)isAutoConvert from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable result, NSError *_Nullable error))completion;
- (BOOL)prehandleQueryTextLanguage:(NSString *)text autoConvertChineseText:(BOOL)isAutoConvert from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion;

/// Get TTS langauge code.
- (NSString *)getTTSLanguageCode:(EZLanguage)language;
Expand Down Expand Up @@ -100,7 +100,7 @@ NS_ASSUME_NONNULL_BEGIN
/// @param from 文本语言
/// @param to 目标语言
/// @param completion 回调
- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable result, NSError *_Nullable error))completion;
- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion;

/// 获取文本的语言
/// @param text 文本
Expand Down
4 changes: 2 additions & 2 deletions Easydict/Feature/Service/Model/EZQueryService.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ - (nullable NSString *)wordLink:(EZQueryModel *)queryModel {
MethodNotImplemented();
}

- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable result, NSError *_Nullable error))completion {
- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion {

if ([self prehandleQueryTextLanguage:text autoConvertChineseText:YES from:from to:to completion:completion]) {
return;
Expand All @@ -180,7 +180,7 @@ - (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to compl
MethodNotImplemented();
}

- (BOOL)prehandleQueryTextLanguage:(NSString *)text autoConvertChineseText:(BOOL)isConvert from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable result, NSError *_Nullable error))completion {
- (BOOL)prehandleQueryTextLanguage:(NSString *)text autoConvertChineseText:(BOOL)isConvert from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion {
// If translated language is Chinese, use Chinese text convert directly.
NSArray *languages = @[ from, to ];
if (isConvert && [EZLanguageManager.shared onlyContainsChineseLanguages:languages]) {
Expand Down
4 changes: 2 additions & 2 deletions Easydict/Feature/Service/OpenAI/EZOpenAIService.m
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ - (NSString *)name {
}

/// Use OpenAI to translate text.
- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *, NSError *_Nullable))completion {
if ([self prehandleQueryTextLanguage:text autoConvertChineseText:NO from:from to:to completion:completion]) {
return;
}
Expand Down Expand Up @@ -240,7 +240,7 @@ - (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to compl
- (void)handleResultText:(NSString *)resultText
error:(NSError *)error
queryServiceType:(EZQueryTextType)queryServiceType
completion:(void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
completion:(void (^)(EZQueryResult *, NSError *_Nullable))completion {
NSArray *normalResults = [[resultText trim] toParagraphs];

switch (queryServiceType) {
Expand Down
4 changes: 2 additions & 2 deletions Easydict/Feature/Service/Volcano/EZVolcanoTranslate.m
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ - (nullable NSString *)wordLink:(EZQueryModel *)queryModel {
return orderedDict;
}

- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *, NSError *_Nullable))completion {

if ([self prehandleQueryTextLanguage:text autoConvertChineseText:NO from:from to:to completion:completion]) {
return;
Expand All @@ -140,7 +140,7 @@ - (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to compl
[self webViewTranslate:completion];
}

- (void)webViewTranslate:(nonnull void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)webViewTranslate:(nonnull void (^)(EZQueryResult *, NSError *_Nullable))completion {
// https://translate.volcengine.com/?category=&home_language=zh&source_language=en&target_language=zh&text=good
// https://translate.volcengine.com/translate?category=&home_language=zh&source_language=en&target_language=zh&text=good

Expand Down
14 changes: 7 additions & 7 deletions Easydict/Feature/Service/Youdao/EZYoudaoTranslate.m
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ - (nullable NSString *)youdaoDictForeignLangauge:(EZQueryModel *)queryModel {
return orderedDict;
}

- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable result, NSError *_Nullable error))completion {
- (void)translate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion {
if (!text.length) {
completion(self.result, EZTranslateError(EZErrorTypeParam, @"翻译的文本为空", nil));
return;
Expand Down Expand Up @@ -451,7 +451,7 @@ - (void)ocrAndTranslate:(NSImage *)image from:(EZLanguage)from to:(EZLanguage)to

#pragma mark - Youdao Translate

- (void)queryYoudaoDictAndTranslation:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable result, NSError *_Nullable error))completion {
- (void)queryYoudaoDictAndTranslation:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion {
if (!text.length) {
completion(self.result, EZTranslateError(EZErrorTypeParam, @"翻译的文本为空", nil));
return;
Expand Down Expand Up @@ -493,7 +493,7 @@ - (void)queryYoudaoDictAndTranslation:(NSString *)text from:(EZLanguage)from to:
}

/// Query Youdao dict, unofficial API
- (void)queryYoudaoDict:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable result, NSError *_Nullable error))completion {
- (void)queryYoudaoDict:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion {
if (!text.length) {
completion(self.result, EZTranslateError(EZErrorTypeParam, @"翻译的文本为空", nil));
return;
Expand Down Expand Up @@ -572,7 +572,7 @@ - (void)queryYoudaoDict:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to

/// Youdao web translate API,
/// !!!: Deprecated, 2023.5
- (void)youdaoWebTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable result, NSError *_Nullable error))completion {
- (void)youdaoWebTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion {
NSString *fromLanguage = [self languageCodeForLanguage:from];
NSString *toLanguage = [self languageCodeForLanguage:to];

Expand Down Expand Up @@ -645,7 +645,7 @@ - (NSString *)signWithSalt:(NSString *)salt word:(NSString *)word {
}

// TODO: Use a stable Youdao translation API.
- (void)youdaoAIDemoTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable result, NSError *_Nullable error))completion {
- (void)youdaoAIDemoTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion {
if (!text.length) {
completion(self.result, EZTranslateError(EZErrorTypeParam, @"翻译的文本为空", nil));
return;
Expand Down Expand Up @@ -834,7 +834,7 @@ - (void)requestYoudaoCookie {

#pragma mark - WebView Translate

- (void)webViewTranslate:(nonnull void (^)(EZQueryResult *_Nullable, NSError *_Nullable))completion {
- (void)webViewTranslate:(nonnull void (^)(EZQueryResult *, NSError *_Nullable))completion {
NSString *wordLink = [self wordLink:self.queryModel];
if (!wordLink) {
NSError *error = EZTranslateError(EZErrorTypeUnsupportedLanguage, nil, nil);
Expand All @@ -858,7 +858,7 @@ - (void)webViewTranslate:(nonnull void (^)(EZQueryResult *_Nullable, NSError *_N
#pragma mark - New Web Translate, 2023.5

/// New Youdao web translate && dict API, Ref: https://github.com/Chen03/StaticeApp/blob/a8706aaf4806468a663d7986b901b09be5fc9319/Statice/Model/Search/Youdao.swift
- (void)webTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *_Nullable result, NSError *_Nullable error))completion {
- (void)webTranslate:(NSString *)text from:(EZLanguage)from to:(EZLanguage)to completion:(void (^)(EZQueryResult *result, NSError *_Nullable error))completion {
NSString *client = @"fanyideskweb";
NSString *product = @"webfanyi";
NSString *key = @"fsdsogkndfokasodnaso";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ - (void)queryWithModel:(EZQueryModel *)queryModel
// TODO: service already has the model property.
- (void)queryWithModel:(EZQueryModel *)queryModel
service:(EZQueryService *)service
completion:(nonnull void (^)(EZQueryResult *_Nullable result, NSError *_Nullable error))completion {
completion:(nonnull void (^)(EZQueryResult *result, NSError *_Nullable error))completion {
if (!service.enabledQuery) {
NSLog(@"service disabled: %@", service.serviceType);
return;
Expand Down

0 comments on commit 59fd73e

Please sign in to comment.