Skip to content

Commit

Permalink
fix: Fixed the issue where adding services does not synchronize with …
Browse files Browse the repository at this point in the history
…the service list #283
  • Loading branch information
ZGGSONG committed Dec 18, 2024
1 parent 998f70f commit 80601ba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/STranslate.Model/ConfigModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ public ConfigModel Clone()
SourceLangIfAuto = SourceLangIfAuto,
TargetLangIfSourceZh = TargetLangIfSourceZh,
TargetLangIfSourceNotZh = TargetLangIfSourceNotZh,
ReplaceProp = (ReplaceProp)ReplaceProp.Clone(),
ReplaceProp = ReplaceProp.Clone(),
Hotkeys = Hotkeys?.Clone(),
Services = Services?.Clone(),
OCRList = OCRList?.DeepCopy(),
Expand Down
4 changes: 2 additions & 2 deletions src/STranslate.Model/ReplaceProp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace STranslate.Model;

public partial class ReplaceProp : ObservableObject, ICloneable
public partial class ReplaceProp : ObservableObject
{
[ObservableProperty] private ITranslator? _activeService;

Expand Down Expand Up @@ -33,7 +33,7 @@ public partial class ReplaceProp : ObservableObject, ICloneable
/// </summary>
[ObservableProperty] private LangEnum _targetLangIfSourceNotZh = LangEnum.zh_cn;

public object Clone()
public ReplaceProp Clone()
{
return new ReplaceProp
{
Expand Down
25 changes: 17 additions & 8 deletions src/STranslate/ViewModels/Preference/ReplaceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,28 @@ public partial class ReplaceViewModel : ObservableObject
private readonly ConfigHelper _configHelper = Singleton<ConfigHelper>.Instance;
private readonly TranslatorViewModel _translateVm = Singleton<TranslatorViewModel>.Instance;
public InputViewModel InputVm => Singleton<InputViewModel>.Instance;
private Guid? deletedSvc;
public ReplaceViewModel()
{
// View 上绑定结果从List中获取
ReplaceProp.ActiveService = AllServices.FirstOrDefault(x => x.Identify == ReplaceProp.ActiveService?.Identify);

_translateVm.PropertyChanged += (sender, args) =>
_translateVm.CurTransServiceList.ListChanged += (sender, args) =>
{
// 检查是否被删除
if (sender is not BindingList<ITranslator> services || ReplaceProp.ActiveService is null) return;

// 当服务列表中有增删时检查当前活动服务是否被删除
if (services.All(x => x.Identify != ReplaceProp.ActiveService?.Identify))
ReplaceProp.ActiveService = null;
switch (args.ListChangedType)
{
case ListChangedType.ItemAdded:
var svc = _translateVm.CurTransServiceList[args.NewIndex];
if (svc.Identify == deletedSvc)
{
ReplaceProp.ActiveService = svc;
deletedSvc = null;
}
break;
case ListChangedType.ItemDeleted:
deletedSvc = ReplaceProp.ActiveService?.Identify;
break;
}
};
}

Expand Down Expand Up @@ -140,7 +149,7 @@ private async Task TranslateLlmAsync(RequestModel req, CancellationToken token)

#region Property

[ObservableProperty] private BindingList<ITranslator> _allServices = Singleton<TranslatorViewModel>.Instance.CurTransServiceList.Clone();
[ObservableProperty] private BindingList<ITranslator> _allServices = Singleton<TranslatorViewModel>.Instance.CurTransServiceList;

[ObservableProperty] private ReplaceProp _replaceProp = Singleton<ConfigHelper>.Instance.CurrentConfig?.ReplaceProp ?? new ReplaceProp();

Expand Down

0 comments on commit 80601ba

Please sign in to comment.