From e9c06c16b9b1c7d37130aa7e55a9aea3748cd6f5 Mon Sep 17 00:00:00 2001 From: zggsong Date: Sun, 14 Jan 2024 14:25:44 +0800 Subject: [PATCH] perf: The same service that optimizes the GUID change cannot get the cache, so that another service cannot get the cache --- STranslate/Helper/ConfigHelper.cs | 2 +- STranslate/ViewModels/InputViewModel.cs | 16 +++++++++++----- .../History/HistoryContentViewModel.cs | 2 +- .../ViewModels/Preference/ServiceViewModel.cs | 3 +++ .../Preference/Services/TranslatorBing.cs | 1 - 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/STranslate/Helper/ConfigHelper.cs b/STranslate/Helper/ConfigHelper.cs index 418da213..d59eddea 100644 --- a/STranslate/Helper/ConfigHelper.cs +++ b/STranslate/Helper/ConfigHelper.cs @@ -294,7 +294,7 @@ JsonSerializer serializer (int)ServiceType.ApiService => new TranslatorApi(), (int)ServiceType.BaiduService => new TranslatorBaidu(), (int)ServiceType.BingService => new TranslatorBing(), - //TODO: 更多其他服务在这里添加 + //TODO: 新接口需要适配 _ => throw new NotSupportedException($"Unsupported ServiceType: {type}") }; diff --git a/STranslate/ViewModels/InputViewModel.cs b/STranslate/ViewModels/InputViewModel.cs index b55ce54f..1badde1b 100644 --- a/STranslate/ViewModels/InputViewModel.cs +++ b/STranslate/ViewModels/InputViewModel.cs @@ -16,6 +16,7 @@ using STranslate.Model; using STranslate.Util; using STranslate.ViewModels.Preference; +using STranslate.ViewModels.Preference.Services; namespace STranslate.ViewModels { @@ -184,6 +185,7 @@ await Parallel.ForEachAsync( } //根据不同服务类型区分 + //TODO: 新接口需要适配 switch (service.Type) { case ServiceType.ApiService: @@ -435,7 +437,7 @@ protected override IList CreateProperties(Type type, MemberSeriali /// public class CurrentTranslatorConverter : JsonConverter { - public override ITranslator ReadJson( + public override ITranslator? ReadJson( JsonReader reader, Type objectType, ITranslator? existingValue, @@ -451,13 +453,17 @@ JsonSerializer serializer // 从 JSON 中提取 Identify 字段的值,用于确定具体实现类 var identify = jsonObject["Identify"]!.Value(); + var type = jsonObject["Type"]!.Value(); ITranslator translator; // 根据 Identify 查找匹配的翻译服务 - // TODO: 优化删除配置后Identify更新后与数据库不一致导致的报错问题 - translator = - translators.FirstOrDefault(x => x.Identify.ToString() == identify) - ?? throw new NotSupportedException($"Unsupported Service: {identify}"); + //TODO: 新接口需要适配 + translator = translators.FirstOrDefault(x => x.Identify.ToString() == identify) ?? type switch + { + (int)ServiceType.BaiduService => new TranslatorBaidu(), + (int)ServiceType.BingService => new TranslatorBing(), + _ => new TranslatorApi(), + }; // 从 JSON 中提取 Data 字段的值,设置到 translator 的 Data 属性中 translator.Data = jsonObject["Data"]!.Value()!; diff --git a/STranslate/ViewModels/Preference/History/HistoryContentViewModel.cs b/STranslate/ViewModels/Preference/History/HistoryContentViewModel.cs index 791fe3fb..0253d2a7 100644 --- a/STranslate/ViewModels/Preference/History/HistoryContentViewModel.cs +++ b/STranslate/ViewModels/Preference/History/HistoryContentViewModel.cs @@ -87,7 +87,7 @@ JsonSerializer serializer (int)ServiceType.ApiService => new TranslatorApi(), (int)ServiceType.BaiduService => new TranslatorBaidu(), (int)ServiceType.BingService => new TranslatorBing(), - //TODO: 更多其他服务在这里添加 + //TODO: 新接口需要适配 _ => throw new NotSupportedException($"Unsupported ServiceType: {type}") }; diff --git a/STranslate/ViewModels/Preference/ServiceViewModel.cs b/STranslate/ViewModels/Preference/ServiceViewModel.cs index 72e5ef41..7b67be46 100644 --- a/STranslate/ViewModels/Preference/ServiceViewModel.cs +++ b/STranslate/ViewModels/Preference/ServiceViewModel.cs @@ -21,6 +21,7 @@ public partial class ServiceViewModel : ObservableObject public ServiceViewModel() { //添加默认支持服务 + //TODO: 新接口需要适配 TransServices.Add(new TranslatorApi()); TransServices.Add(new TranslatorBaidu()); TransServices.Add(new TranslatorBing()); @@ -78,6 +79,7 @@ private void TogglePage(ITranslator service) tmpIndex = SelectedIndex; string head = "STranslate.Views.Preference.Service."; + //TODO: 新接口需要适配 var name = service.Type switch { ServiceType.ApiService => string.Format("{0}TextApiServicePage", head), @@ -100,6 +102,7 @@ private void Add(List list) { var service = list.First(); + //TODO: 新接口需要适配 CurTransServiceList.Add(service switch { TranslatorApi api => api.DeepClone(), diff --git a/STranslate/ViewModels/Preference/Services/TranslatorBing.cs b/STranslate/ViewModels/Preference/Services/TranslatorBing.cs index 04f35607..12ef58a1 100644 --- a/STranslate/ViewModels/Preference/Services/TranslatorBing.cs +++ b/STranslate/ViewModels/Preference/Services/TranslatorBing.cs @@ -119,7 +119,6 @@ public async Task TranslateAsync(object request, CancellationToken token if (string.IsNullOrEmpty(resp)) throw new Exception("请求结果为空"); - //TODO: 有问题 var ret = JsonConvert.DeserializeObject(resp ?? ""); //如果出错就将整个返回信息写入取值处