1010using Kamishibai ;
1111using Microsoft . Extensions . Configuration ;
1212using Microsoft . Extensions . DependencyInjection ;
13+ using Microsoft . Extensions . Logging ;
1314using Microsoft . Extensions . Options ;
1415using Microsoft . Win32 ;
1516using PropertyTools . DataAnnotations ;
@@ -46,6 +47,7 @@ sealed partial class AllSettingsViewModel : ObservableObject, IDisposable
4647 private readonly IAutoTargetStore autoTargetStore ;
4748 private readonly IEnumerable < ITargetSettingsValidator > validators ;
4849 private readonly IMainWindowModule mainWindowModule ;
50+ private readonly ILogger < AllSettingsViewModel > logger ;
4951 private readonly IConfigurationRoot ? rootConfig ;
5052 private readonly string target ;
5153 [ ObservableProperty ]
@@ -109,7 +111,9 @@ public AllSettingsViewModel(
109111 [ Inject ] IConfiguration config ,
110112 [ Inject ] IEnumerable < ITargetSettingsValidator > validators ,
111113 [ Inject ] IMainWindowModule mainWindowModule ,
112- string target )
114+ [ Inject ] ILogger < AllSettingsViewModel > logger ,
115+ string target ,
116+ bool ? applyMode = null )
113117 {
114118 var items = provider . GetPlugins ( ) ;
115119 var ocrModules = items . Where ( p => typeof ( IOcrModule ) . IsAssignableFrom ( p . Type ) ) . Select ( Convert ) . ToArray ( ) ;
@@ -128,7 +132,7 @@ public AllSettingsViewModel(
128132 . DefaultIfEmpty ( new KeyValuePair < string , TargetSettings > ( string . Empty , new ( ) ) )
129133 . Select ( t => new TargetSettingsViewModel ( t . Key , sp , t . Value , ocrModules , translateModules , cacheModules ) ) ] ;
130134
131- this . ApplyMode = ! string . IsNullOrEmpty ( target ) && options . Value . Targets . ContainsKey ( target ) ;
135+ this . ApplyMode = applyMode ?? ! string . IsNullOrEmpty ( target ) && options . Value . Targets . ContainsKey ( target ) ;
132136 if ( this . Targets . FirstOrDefault ( t => t . Name == target ) is not { } selected )
133137 {
134138 selected = new ( target , sp , options . Value . Targets . TryGetValue ( string . Empty , out var d ) ? d : new ( ) , ocrModules , translateModules , cacheModules ) ;
@@ -146,6 +150,7 @@ public AllSettingsViewModel(
146150 this . autoTargetStore = autoTargetStore ;
147151 this . validators = validators ;
148152 this . mainWindowModule = mainWindowModule ;
153+ this . logger = logger ;
149154 this . target = target ;
150155 this . rootConfig = config as IConfigurationRoot ;
151156 this . updateChecker . UpdateAvailable += UpdateChecker_UpdateAvailable ;
@@ -257,33 +262,10 @@ public async Task SaveAsync(object window)
257262 var results = new Dictionary < string , IReadOnlyList < ValidateResult > > ( ) ;
258263 foreach ( var ( name , target ) in string . IsNullOrEmpty ( this . target ) ? settings . Targets . ToArray ( ) : [ new KeyValuePair < string , TargetSettings > ( this . target , settings . Targets [ this . target ] ) ] )
259264 {
260- var r = new List < ValidateResult > ( ) ;
261- if ( target . Language . Source == target . Language . Target )
265+ var validationResults = await this . validators . ValidateAsync ( target ) ;
266+ if ( validationResults . Any ( ) )
262267 {
263- r . Add ( ValidateResult . Invalid ( Resources . TranslateLanguage , Resources . SameSourceTargetLanguage ) ) ;
264- }
265-
266- if ( ! target . SelectedPlugins . TryGetValue ( nameof ( ITranslateModule ) , out var t ) || string . IsNullOrEmpty ( t ) )
267- {
268- r . Add ( ValidateResult . Invalid ( Resources . TranslateModule , """
269- 翻訳モジュールが選択されていません。
270- 「対象ごとの設定」→「全体設定」タブの「翻訳モジュール」を設定してください。
271- """ ) ) ;
272- }
273- if ( ! target . SelectedPlugins . TryGetValue ( nameof ( ICacheModule ) , out var c ) || string . IsNullOrEmpty ( c ) )
274- {
275- r . Add ( ValidateResult . Invalid ( Resources . CacheModule , """
276- キャッシュモジュールが選択されていません。
277- 「対象ごとの設定」→「全体設定」タブの「キャッシュモジュール」を設定してください。
278- """ ) ) ;
279- }
280- foreach ( var validator in this . validators )
281- {
282- r . Add ( await validator . Validate ( target ) ) ;
283- }
284- if ( r . Any ( r => ! r . IsValid ) )
285- {
286- results . Add ( name , r . Where ( r => ! r . IsValid ) . ToArray ( ) ) ;
268+ results . Add ( name , validationResults ) ;
287269 }
288270 }
289271 if ( results . Any ( ) )
@@ -299,6 +281,7 @@ public async Task SaveAsync(object window)
299281 {
300282 return ;
301283 }
284+ this . logger . LogWarning ( "Settings are invalid, but user chose to save them anyway." ) ;
302285 }
303286
304287 // 値の保存
0 commit comments