11import { Component , DestroyRef , Inject , OnInit } from '@angular/core' ;
22import { FormControl } from '@angular/forms' ;
33import { MAT_DIALOG_DATA } from '@angular/material/dialog' ;
4- import { BehaviorSubject , combineLatest } from 'rxjs' ;
4+ import { BehaviorSubject } from 'rxjs' ;
55import { debounceTime , map , skip , startWith } from 'rxjs/operators' ;
66import { OnlineStatusService } from 'xforge-common/online-status.service' ;
77import { quietTakeUntilDestroyed } from 'xforge-common/util/rxjs-util' ;
@@ -21,44 +21,46 @@ export interface TranslatorSettingsDialogData {
2121 standalone : false
2222} )
2323export class TranslatorSettingsDialogComponent implements OnInit {
24- suggestionsEnabledSwitch = new FormControl < boolean > ( { value : false , disabled : ! this . onlineStatusService . isOnline } ) ;
25- lynxMasterSwitch = new FormControl < boolean > ( false ) ;
26- lynxAssessmentsEnabled = new FormControl < boolean > ( false ) ;
27- lynxAutoCorrectEnabled = new FormControl < boolean > ( false ) ;
24+ readonly suggestionsEnabledSwitch = new FormControl < boolean > ( {
25+ value : false ,
26+ disabled : ! this . onlineStatusService . isOnline
27+ } ) ;
28+ readonly lynxMasterSwitch = new FormControl < boolean > ( false ) ;
29+ readonly lynxAssessmentsEnabled = new FormControl < boolean > ( false ) ;
30+ readonly lynxAutoCorrectEnabled = new FormControl < boolean > ( false ) ;
31+
32+ showSuggestionsSettings = false ;
33+ showLynxSettings = false ;
34+ translationSuggestionsDisabled = false ;
35+ lynxAssessmentsProjectEnabled = false ;
36+ lynxAutoCorrectProjectEnabled = false ;
37+ numSuggestions = '1' ;
38+
39+ readonly confidenceThreshold$ = new BehaviorSubject < number > ( 20 ) ;
2840
2941 private readonly projectDoc : SFProjectProfileDoc = this . data . projectDoc ;
3042 private readonly projectUserConfigDoc : SFProjectUserConfigDoc = this . data . projectUserConfigDoc ;
31- private confidenceThreshold$ = new BehaviorSubject < number > ( 20 ) ;
3243
3344 constructor (
3445 @Inject ( MAT_DIALOG_DATA ) private readonly data : TranslatorSettingsDialogData ,
3546 readonly onlineStatusService : OnlineStatusService ,
36- private destroyRef : DestroyRef
47+ private readonly destroyRef : DestroyRef
3748 ) { }
3849
3950 ngOnInit ( ) : void {
40- if ( this . projectUserConfigDoc . data != null ) {
41- const percent = Math . round ( this . projectUserConfigDoc . data . confidenceThreshold * 100 ) ;
42- this . confidenceThreshold$ . next ( percent ) ;
43- }
51+ this . updateComponentState ( ) ;
4452
45- this . suggestionsEnabledSwitch . setValue ( this . translationSuggestionsUserEnabled ) ;
46- this . lynxAssessmentsEnabled . setValue ( this . lynxAssessmentsUserEnabled ) ;
47- this . lynxAutoCorrectEnabled . setValue ( this . lynxAutoCorrectUserEnabled ) ;
48- this . lynxMasterSwitch . setValue ( this . lynxMasterEnabled ) ;
53+ this . onlineStatusService . onlineStatus$
54+ . pipe ( quietTakeUntilDestroyed ( this . destroyRef ) )
55+ . subscribe ( ( ) => this . onOnlineStatusChange ( ) ) ;
56+
57+ this . projectDoc . changes$
58+ . pipe ( startWith ( null ) , quietTakeUntilDestroyed ( this . destroyRef ) )
59+ . subscribe ( ( ) => this . updateComponentState ( ) ) ;
4960
50- combineLatest ( [ this . onlineStatusService . onlineStatus$ , this . projectDoc . changes$ . pipe ( startWith ( null ) ) ] )
61+ this . projectUserConfigDoc . changes$
5162 . pipe ( quietTakeUntilDestroyed ( this . destroyRef ) )
52- . subscribe ( ( ) => {
53- this . updateTranslationSuggestionsSwitch ( ) ;
54- } ) ;
55-
56- this . projectUserConfigDoc . changes$ . pipe ( quietTakeUntilDestroyed ( this . destroyRef ) ) . subscribe ( ( ) => {
57- this . updateTranslationSuggestionsSwitch ( ) ;
58- this . lynxAssessmentsEnabled . setValue ( this . lynxAssessmentsUserEnabled , { emitEvent : false } ) ;
59- this . lynxAutoCorrectEnabled . setValue ( this . lynxAutoCorrectUserEnabled , { emitEvent : false } ) ;
60- this . lynxMasterSwitch . setValue ( this . lynxMasterEnabled , { emitEvent : false } ) ;
61- } ) ;
63+ . subscribe ( ( ) => this . updateComponentState ( ) ) ;
6264
6365 this . confidenceThreshold$
6466 . pipe (
@@ -73,88 +75,82 @@ export class TranslatorSettingsDialogComponent implements OnInit {
7375 ) ;
7476 }
7577
76- get translationSuggestionsDisabled ( ) : boolean {
77- return ! this . translationSuggestionsUserEnabled || ! this . onlineStatusService . isOnline ;
78+ setConfidenceThreshold ( value : number ) : void {
79+ this . confidenceThreshold$ . next ( value ) ;
7880 }
7981
80- get translationSuggestionsUserEnabled ( ) : boolean {
81- return this . projectUserConfigDoc . data == null ? true : this . projectUserConfigDoc . data . translationSuggestionsEnabled ;
82+ setNumSuggestions ( value : string ) : void {
83+ this . numSuggestions = value ;
84+ void this . projectUserConfigDoc . submitJson0Op ( op => op . set ( puc => puc . numSuggestions , parseInt ( value , 10 ) ) ) ;
8285 }
8386
84- get numSuggestions ( ) : string {
85- return this . projectUserConfigDoc . data == null ? '1' : this . projectUserConfigDoc . data . numSuggestions . toString ( ) ;
87+ setTranslationSettingsEnabled ( value : boolean ) : void {
88+ void this . projectUserConfigDoc . submitJson0Op ( op =>
89+ op . set < boolean > ( puc => puc . translationSuggestionsEnabled , value )
90+ ) ;
8691 }
8792
88- set numSuggestions ( value : string ) {
89- void this . projectUserConfigDoc . submitJson0Op ( op => op . set ( puc => puc . numSuggestions , parseInt ( value , 10 ) ) ) ;
93+ setLynxAssessmentsEnabled ( value : boolean ) : void {
94+ this . updateLynxInsightState ( { assessmentsEnabled : value } ) ;
9095 }
9196
92- get confidenceThreshold ( ) : number {
93- return this . confidenceThreshold$ . value ;
97+ setLynxAutoCorrectEnabled ( value : boolean ) : void {
98+ this . updateLynxInsightState ( { autoCorrectionsEnabled : value } ) ;
9499 }
95100
96- set confidenceThreshold ( value : number ) {
97- this . confidenceThreshold$ . next ( value ) ;
101+ setLynxMasterEnabled ( value : boolean ) : void {
102+ this . updateLynxInsightState ( {
103+ assessmentsEnabled : value ,
104+ autoCorrectionsEnabled : value
105+ } ) ;
106+ }
107+ private get translationSuggestionsUserEnabled ( ) : boolean {
108+ return this . projectUserConfigDoc . data ?. translationSuggestionsEnabled ?? true ;
98109 }
99110
100- get lynxAssessmentsUserEnabled ( ) : boolean {
111+ private get lynxAssessmentsUserEnabled ( ) : boolean {
101112 return this . projectUserConfigDoc . data ?. lynxInsightState ?. assessmentsEnabled ?? true ;
102113 }
103114
104- get lynxAutoCorrectUserEnabled ( ) : boolean {
115+ private get lynxAutoCorrectUserEnabled ( ) : boolean {
105116 return this . projectUserConfigDoc . data ?. lynxInsightState ?. autoCorrectionsEnabled ?? true ;
106117 }
107118
108- get lynxAssessmentsProjectEnabled ( ) : boolean {
109- return ! ! this . projectDoc . data ?. lynxConfig ?. assessmentsEnabled ;
110- }
111-
112- get lynxAutoCorrectProjectEnabled ( ) : boolean {
113- return ! ! this . projectDoc . data ?. lynxConfig ?. autoCorrectionsEnabled ;
114- }
115-
116- get lynxMasterEnabled ( ) : boolean {
119+ private get lynxMasterEnabled ( ) : boolean {
117120 return (
118121 ( this . lynxAssessmentsProjectEnabled && this . lynxAssessmentsUserEnabled ) ||
119122 ( this . lynxAutoCorrectProjectEnabled && this . lynxAutoCorrectUserEnabled )
120123 ) ;
121124 }
122125
123- get showSuggestionsSettings ( ) : boolean {
124- return ! ! this . projectDoc . data ?. translateConfig . translationSuggestionsEnabled ;
125- }
126-
127- get showLynxSettings ( ) : boolean {
128- return this . lynxAssessmentsProjectEnabled || this . lynxAutoCorrectProjectEnabled ;
129- }
130-
131- setTranslationSettingsEnabled ( value : boolean ) : void {
132- void this . projectUserConfigDoc . submitJson0Op ( op =>
133- op . set < boolean > ( puc => puc . translationSuggestionsEnabled , value )
134- ) ;
135- }
136-
137- updateTranslationSuggestionsSwitch ( ) : void {
126+ private onOnlineStatusChange ( ) : void {
138127 if ( this . onlineStatusService . isOnline ) {
139128 this . suggestionsEnabledSwitch . enable ( ) ;
129+ this . translationSuggestionsDisabled = ! this . translationSuggestionsUserEnabled ;
140130 } else {
141131 this . suggestionsEnabledSwitch . disable ( ) ;
132+ this . translationSuggestionsDisabled = true ;
142133 }
143134 }
144135
145- setLynxAssessmentsEnabled ( value : boolean ) : void {
146- this . updateLynxInsightState ( { assessmentsEnabled : value } ) ;
147- }
136+ private updateComponentState ( ) : void {
137+ this . showSuggestionsSettings = ! ! this . projectDoc . data ?. translateConfig . translationSuggestionsEnabled ;
138+ this . lynxAssessmentsProjectEnabled = ! ! this . projectDoc . data ?. lynxConfig ?. assessmentsEnabled ;
139+ this . lynxAutoCorrectProjectEnabled = ! ! this . projectDoc . data ?. lynxConfig ?. autoCorrectionsEnabled ;
140+ this . showLynxSettings = this . lynxAssessmentsProjectEnabled || this . lynxAutoCorrectProjectEnabled ;
141+ this . translationSuggestionsDisabled = ! this . translationSuggestionsUserEnabled || ! this . onlineStatusService . isOnline ;
148142
149- setLynxAutoCorrectEnabled ( value : boolean ) : void {
150- this . updateLynxInsightState ( { autoCorrectionsEnabled : value } ) ;
151- }
143+ if ( this . projectUserConfigDoc . data != null ) {
144+ const percent = Math . round ( this . projectUserConfigDoc . data . confidenceThreshold * 100 ) ;
145+ this . numSuggestions = this . projectUserConfigDoc . data . numSuggestions . toString ( ) ;
146+ this . confidenceThreshold$ . next ( percent ) ;
147+ }
152148
153- setLynxMasterEnabled ( value : boolean ) : void {
154- this . updateLynxInsightState ( {
155- assessmentsEnabled : value ,
156- autoCorrectionsEnabled : value
157- } ) ;
149+ // Update form control state
150+ this . suggestionsEnabledSwitch . setValue ( this . translationSuggestionsUserEnabled , { emitEvent : false } ) ;
151+ this . lynxAssessmentsEnabled . setValue ( this . lynxAssessmentsUserEnabled , { emitEvent : false } ) ;
152+ this . lynxAutoCorrectEnabled . setValue ( this . lynxAutoCorrectUserEnabled , { emitEvent : false } ) ;
153+ this . lynxMasterSwitch . setValue ( this . lynxMasterEnabled , { emitEvent : false } ) ;
158154 }
159155
160156 private updateLynxInsightState ( updates : { assessmentsEnabled ?: boolean ; autoCorrectionsEnabled ?: boolean } ) : void {
0 commit comments