From d4d3def799763f48f5890cbfa99c0f33cc47b68c Mon Sep 17 00:00:00 2001 From: tisfeng Date: Wed, 8 May 2024 11:44:45 +0800 Subject: [PATCH 1/3] perf: remove flag first, then trim result text --- .../Swift/Service/OpenAI/BaseOpenAIService.swift | 14 ++++++++------ .../Extensions/String/String+Extension.swift | 4 ++++ .../CustomOpenAIService+ConfigurableService.swift | 2 +- .../OpenAIService+ConfigurableService.swift | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Easydict/Swift/Service/OpenAI/BaseOpenAIService.swift b/Easydict/Swift/Service/OpenAI/BaseOpenAIService.swift index 01c23fb3b..9812170b1 100644 --- a/Easydict/Swift/Service/OpenAI/BaseOpenAIService.swift +++ b/Easydict/Swift/Service/OpenAI/BaseOpenAIService.swift @@ -197,7 +197,7 @@ public class BaseOpenAIService: QueryService { ) { var normalResults: [String]? if let resultText { - normalResults = [resultText.trimmingCharacters(in: .whitespacesAndNewlines)] + normalResults = [resultText.trim()] } result.isStreamFinished = error != nil @@ -226,16 +226,18 @@ public class BaseOpenAIService: QueryService { } private func getFinalResultText(text: String) -> String { - // Since it is more difficult to accurately remove redundant quotes in streaming, we wait until the end of the request to remove the quotes - let nsText = text as NSString - var resultText = nsText.tryToRemoveQuotes() + var resultText = text - // Remove last , fix Groq mixtral-8x7b-32768 + // Remove last , fix Groq model mixtral-8x7b-32768 let stopFlag = "" if !queryModel.queryText.hasSuffix(stopFlag), resultText.hasSuffix(stopFlag) { - resultText = String(resultText.dropLast(stopFlag.count)) + resultText = String(resultText.dropLast(stopFlag.count)).trim() } + // Since it is more difficult to accurately remove redundant quotes in streaming, we wait until the end of the request to remove the quotes + let nsText = resultText as NSString + resultText = nsText.tryToRemoveQuotes() + return resultText } } diff --git a/Easydict/Swift/Utility/Extensions/String/String+Extension.swift b/Easydict/Swift/Utility/Extensions/String/String+Extension.swift index 9aa84566d..12f4680b7 100644 --- a/Easydict/Swift/Utility/Extensions/String/String+Extension.swift +++ b/Easydict/Swift/Utility/Extensions/String/String+Extension.swift @@ -12,6 +12,10 @@ extension String { func truncated(_ maxLength: Int = 200) -> String { String(prefix(maxLength)) } + + func trim() -> String { + trimmingCharacters(in: .whitespacesAndNewlines) + } } @objc diff --git a/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/CustomOpenAIService+ConfigurableService.swift b/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/CustomOpenAIService+ConfigurableService.swift index e9b75b9c3..d73f6ff21 100644 --- a/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/CustomOpenAIService+ConfigurableService.swift +++ b/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/CustomOpenAIService+ConfigurableService.swift @@ -171,7 +171,7 @@ private class CustomOpenAIViewModel: ObservableObject { guard let availableModels else { return } validModels = availableModels.components(separatedBy: ",") - .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty } + .map { $0.trim() }.filter { !$0.isEmpty } if validModels.isEmpty { model = "" diff --git a/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift b/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift index b9d81a2fe..cf1f7e38c 100644 --- a/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift +++ b/Easydict/Swift/View/SettingView/Tabs/ServiceConfigurationView/QueryService+ConfigurableService/OpenAIService+ConfigurableService.swift @@ -159,7 +159,7 @@ private class OpenAIServiceViewModel: ObservableObject { guard let availableModels else { return } validModels = availableModels.components(separatedBy: ",") - .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }.filter { !$0.isEmpty } + .map { $0.trim() }.filter { !$0.isEmpty } if validModels.isEmpty { model = "" From 69f41593b9dff790218ad5e09620634e6b8aa79a Mon Sep 17 00:00:00 2001 From: tisfeng Date: Wed, 8 May 2024 16:35:08 +0800 Subject: [PATCH 2/3] chore: disable todo swiftlint --- .swiftlint.yml | 3 ++- Easydict/Swift/Service/Ali/AliService.swift | 1 - Easydict/Swift/Service/Gemini/GeminiService.swift | 1 + Easydict/Swift/Service/Tencent/TencentService.swift | 2 -- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index 0aac0708c..e245f0ba2 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -23,6 +23,7 @@ disabled_rules: - force_cast - force_try - large_tuple + - todo opt_in_rules: - convenience_type @@ -68,4 +69,4 @@ excluded: - "**/UNTESTED_TODO" - "vendor" - "fastlane" - - ".build" \ No newline at end of file + - ".build" diff --git a/Easydict/Swift/Service/Ali/AliService.swift b/Easydict/Swift/Service/Ali/AliService.swift index 9baddf749..8cba69b3f 100644 --- a/Easydict/Swift/Service/Ali/AliService.swift +++ b/Easydict/Swift/Service/Ali/AliService.swift @@ -26,7 +26,6 @@ class AliService: QueryService { } override public func supportLanguagesDictionary() -> MMOrderedDictionary { - // TODO: Replace MMOrderedDictionary in the API let orderedDict = MMOrderedDictionary() for (key, value) in AliTranslateType.supportLanguagesDictionary { orderedDict.setObject(value as NSString, forKey: key.rawValue as NSString) diff --git a/Easydict/Swift/Service/Gemini/GeminiService.swift b/Easydict/Swift/Service/Gemini/GeminiService.swift index c61d25d92..75e80c445 100644 --- a/Easydict/Swift/Service/Gemini/GeminiService.swift +++ b/Easydict/Swift/Service/Gemini/GeminiService.swift @@ -12,6 +12,7 @@ import Defaults import Foundation import GoogleGenerativeAI +// TODO: add a LLM stream service base class, make both OpenAI and Gemini inherit from it. @objc(EZGeminiService) public final class GeminiService: QueryService { // MARK: Public diff --git a/Easydict/Swift/Service/Tencent/TencentService.swift b/Easydict/Swift/Service/Tencent/TencentService.swift index 0fb174d51..0c5bf928c 100644 --- a/Easydict/Swift/Service/Tencent/TencentService.swift +++ b/Easydict/Swift/Service/Tencent/TencentService.swift @@ -27,8 +27,6 @@ public final class TencentService: QueryService { } override public func supportLanguagesDictionary() -> MMOrderedDictionary { - // swiftlint:disable:next todo - // TODO: Replace MMOrderedDictionary in the API let orderedDict = MMOrderedDictionary() for (key, value) in TencentTranslateType.supportLanguagesDictionary { orderedDict.setObject(value as NSString, forKey: key.rawValue as NSString) From f5e9ec193d507600cc90c916117a84b05cfa1407 Mon Sep 17 00:00:00 2001 From: tisfeng Date: Wed, 8 May 2024 17:12:39 +0800 Subject: [PATCH 3/3] perf: improve updating title bar when receiving notification --- .../ViewController/View/Titlebar/EZTitlebar.m | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m b/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m index 02d5ee14a..031a1f1f3 100644 --- a/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m +++ b/Easydict/objc/ViewController/View/Titlebar/EZTitlebar.m @@ -54,14 +54,15 @@ - (void)setup { self.buttonSize = CGSizeMake(self.buttonWidth, self.buttonWidth); self.imageSize = CGSizeMake(self.imageWidth, self.imageWidth); + [self addSubview:self.pinButton]; + [self addSubview:self.stackView]; + NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; - [defaultCenter addObserver:self selector:@selector(updateConstraints) name:EZQuickLinkButtonUpdateNotification object:nil]; - [defaultCenter addObserver:self selector:@selector(updateConstraints) name:NSNotification.languagePreferenceChanged object:nil]; + [defaultCenter addObserver:self selector:@selector(updateTitlebar) name:EZQuickLinkButtonUpdateNotification object:nil]; + [defaultCenter addObserver:self selector:@selector(updateTitlebar) name:NSNotification.languagePreferenceChanged object:nil]; } -//- (void)update - -- (void)updateConstraints { +- (void)updateTitlebar { /** Fix appcenter issue, seems cannot remove self.subviews 🤔 @@ -71,16 +72,21 @@ - (void)updateConstraints { */ // Remove and dealloc all views to refresh UI. - + [_pinButton removeFromSuperview]; [_stackView removeFromSuperview]; _stackView = nil; _quickActionButton = nil; - [self addSubview:self.pinButton]; [self updatePinButton]; - [self quickActionMenu]; + + [self addSubview:self.pinButton]; + [self addSubview:self.stackView]; + + [self setNeedsUpdateConstraints:YES]; +} +- (void)updateConstraints { CGFloat margin = EZHorizontalCellSpacing_10; CGFloat topOffset = EZTitlebarHeight_28 - self.buttonWidth; @@ -90,7 +96,6 @@ - (void)updateConstraints { make.top.equalTo(self).offset(topOffset); }]; - [self addSubview:self.stackView]; [self.stackView mas_remakeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self).offset(topOffset); make.right.equalTo(self).offset(-margin);