Skip to content

Commit

Permalink
perf: change translatedText to nullable, show @"" when service return
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Nov 19, 2023
1 parent 63ef8e0 commit c608172
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
10 changes: 8 additions & 2 deletions Easydict/Feature/Service/Model/EZQueryResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,14 @@ NS_ASSUME_NONNULL_BEGIN
/// 普通翻译结果,可以有多条(一个段落对应一个翻译结果)
@property (nonatomic, strong, nullable) NSArray<NSString *> *translatedResults;

/// This is normalResults joined by @"\n"
@property (nonatomic, copy) NSString *translatedText;
/**
This is normalResults joined by @"\n"
Note that translatedText may be returned @"" by service, like Youdao when censored.
eg. https://dict.youdao.com/result?word=%E4%BD%A0%E5%AF%B9%E4%B9%A0%E4%B8%BB%E5%B8%AD%E6%80%8E%E4%B9%88%E7%9C%8B%EF%BC%9F&lang=en
*/
@property (nonatomic, copy, nullable) NSString *translatedText;

@property (nonatomic, strong, nullable) NSError *error;
@property (nonatomic, copy, nullable) NSString *errorMessage;
Expand Down
8 changes: 4 additions & 4 deletions Easydict/Feature/Service/Model/EZQueryResult.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ - (instancetype)init {
return self;
}

- (NSString *)translatedText {
NSString *text = [self.translatedResults componentsJoinedByString:@"\n"] ?: @"";
- (nullable NSString *)translatedText {
NSString *text = [self.translatedResults componentsJoinedByString:@"\n"];
return text;
}

Expand All @@ -124,7 +124,7 @@ - (BOOL)hasShowingResult {
}

- (BOOL)hasTranslatedResult {
if (self.wordResult || self.translatedText.length || self.HTMLString.length) {
if (self.wordResult || self.translatedText || self.HTMLString.length) {
return YES;
}
return NO;
Expand All @@ -135,7 +135,7 @@ - (BOOL)isWarningErrorType {
return warningType;
}

- (NSString *)copiedText {
- (nullable NSString *)copiedText {
if (!self.HTMLString.length) {
return self.translatedText;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ - (void)refreshWithResult:(EZQueryResult *)result {
}

NSString *text = nil;
if (result.translatedText.length > 0) {
if (result.translatedText) {
text = result.translatedText;
} else if (!result.wordResult && errorDescription.length) {
text = errorDescription;
} else if (!result.hasTranslatedResult) {
text = NSLocalizedString(@"no_results_found", nil);
}

if (text.length) {
if (text) {
EZLabel *resultLabel = [[EZLabel alloc] init];
[self addSubview:resultLabel];

Expand Down

0 comments on commit c608172

Please sign in to comment.