22// <copyright file="ModelIterationDomainSwitchDialogViewModel.cs" company="Starion Group S.A.">
33// Copyright (c) 2015-2022 Starion Group S.A.
44//
5- // Author: Sam Gerené , Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski, Antoine Théate , Omar Elebiary
5+ // Author: Sam Geren� , Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski, Antoine Th�ate , Omar Elebiary
66//
77// This file is part of COMET-IME Community Edition.
88// The COMET-IME Community Edition is the Starion Concurrent Design Desktop Application and Excel Integration
@@ -49,6 +49,16 @@ namespace CDP4ShellDialogs.ViewModels
4949 /// </summary>
5050 public class ModelIterationDomainSwitchDialogViewModel : DialogViewModelBase
5151 {
52+ /// <summary>
53+ /// Backing field for the <see cref="SelectedRowSession" /> property.
54+ /// </summary>
55+ private SwitchDomainSessionRowViewModel selectedRowSession ;
56+
57+ /// <summary>
58+ /// Backing field for the <see cref="IterationRows" /> property.
59+ /// </summary>
60+ private List < SwitchDomainIterationSetupRowViewModel > iterationRows ;
61+
5262 /// <summary>
5363 /// Initializes a new instance of the <see cref="ModelClosingDialogViewModel" /> class.
5464 /// </summary>
@@ -73,6 +83,24 @@ public ModelIterationDomainSwitchDialogViewModel(IEnumerable<ISession> sessionAv
7383 /// </summary>
7484 public DisposableReactiveList < SwitchDomainSessionRowViewModel > SessionsAvailable { get ; private set ; }
7585
86+ /// <summary>
87+ /// Gets or sets the selected source <see cref="SwitchDomainSessionRowViewModel" />
88+ /// </summary>
89+ public SwitchDomainSessionRowViewModel SelectedRowSession
90+ {
91+ get => this . selectedRowSession ;
92+ set => this . RaiseAndSetIfChanged ( ref this . selectedRowSession , value ) ;
93+ }
94+
95+ /// <summary>
96+ /// Gets the flat list of <see cref="SwitchDomainIterationSetupRowViewModel" /> of the <see cref="SelectedRowSession" />
97+ /// </summary>
98+ public List < SwitchDomainIterationSetupRowViewModel > IterationRows
99+ {
100+ get => this . iterationRows ;
101+ private set => this . RaiseAndSetIfChanged ( ref this . iterationRows , value ) ;
102+ }
103+
76104 /// <summary>
77105 /// Gets the list of <see cref="IterationSetup" /> selected
78106 /// </summary>
@@ -96,7 +124,7 @@ private void InitializeReactiveCommands()
96124 this . SelectedIterations = new ReactiveList < IViewModelBase < Thing > > ( ) ;
97125 this . IsBusy = false ;
98126
99- var canOk = this . WhenAnyValue ( x => x . SelectedIterations . Count , count => count != 0 ) ;
127+ var canOk = this . WhenAnyValue ( x => x . IterationRows , rows => rows != null && rows . Count != 0 ) ;
100128
101129 this . SwitchCommand = ReactiveCommandCreator . CreateAsyncTask ( this . ExecuteDomainSwitch , canOk , RxApp . MainThreadScheduler ) ;
102130
@@ -110,6 +138,21 @@ private void InitializeReactiveCommands()
110138 this . CancelCommand = ReactiveCommandCreator . Create ( this . ExecuteCancel ) ;
111139
112140 this . SelectedIterations . ItemsAdded . Subscribe ( this . FilterIterationSelectionItems ) ;
141+
142+ this . WhenAnyValue ( x => x . SelectedRowSession ) . Subscribe ( this . PopulateIterationRows ) ;
143+ }
144+
145+ /// <summary>
146+ /// Populates the flat <see cref="IterationRows" /> from the <paramref name="session" /> and resets the current selection
147+ /// </summary>
148+ /// <param name="session">The selected <see cref="SwitchDomainSessionRowViewModel" /></param>
149+ private void PopulateIterationRows ( SwitchDomainSessionRowViewModel session )
150+ {
151+ this . SelectedIterations . Clear ( ) ;
152+
153+ this . IterationRows = session == null
154+ ? new List < SwitchDomainIterationSetupRowViewModel > ( )
155+ : session . EngineeringModelSetupRowViewModels . SelectMany ( m => m . IterationSetupRowViewModels ) . ToList ( ) ;
113156 }
114157
115158 /// <summary>
@@ -123,16 +166,22 @@ private async Task ExecuteDomainSwitch()
123166 this . IsBusy = true ;
124167 this . LoadingMessage = "Switching..." ;
125168
126- foreach ( var iteration in this . SelectedIterations )
169+ foreach ( var row in this . IterationRows )
127170 {
128- var modelrow = ( SwitchDomainIterationSetupRowViewModel ) iteration ;
129- var session = modelrow . Session ;
171+ var session = row . Session ;
130172
131- var openIteration = session . OpenIterations . Keys . FirstOrDefault ( x => x . Iid == modelrow . IterationIid ) ;
173+ var openIteration = session . OpenIterations . Keys . FirstOrDefault ( x => x . Iid == row . IterationIid ) ;
132174
133- if ( openIteration ! = null )
175+ if ( openIteration == null || row . SelectedDomain = = null )
134176 {
135- session . SwitchDomain ( modelrow . IterationIid , modelrow . SelectedDomain ) ;
177+ continue ;
178+ }
179+
180+ var currentDomain = session . QuerySelectedDomainOfExpertise ( openIteration ) ;
181+
182+ if ( ! Equals ( currentDomain , row . SelectedDomain ) )
183+ {
184+ session . SwitchDomain ( row . IterationIid , row . SelectedDomain ) ;
136185 }
137186 }
138187
@@ -164,6 +213,8 @@ private void PopulateSessionsRowViewModel(IEnumerable<ISession> sessions)
164213 {
165214 this . SessionsAvailable . Add ( new SwitchDomainSessionRowViewModel ( session . RetrieveSiteDirectory ( ) , session ) ) ;
166215 }
216+
217+ this . SelectedRowSession = this . SessionsAvailable . FirstOrDefault ( ) ;
167218 }
168219
169220 /// <summary>
0 commit comments