@@ -29,35 +29,78 @@ public BuildingViewModel(BuildingObject model) : base(model)
2929 ElevatorSequence3 = model . ElevatorHeightSequences . Count > 2 ? new ( model . ElevatorHeightSequences [ 2 ] ) : null ;
3030 ElevatorSequence4 = model . ElevatorHeightSequences . Count > 3 ? new ( model . ElevatorHeightSequences [ 3 ] ) : null ;
3131
32+ #region Building Components Synchronization
33+
3234 // Subscribe to BuildingVariations changes (including nested lists)
33- BuildingVariations . ListChanged += OnBuildingComponentChanged ;
35+ BuildingVariations . ListChanged += OnBuildingVariationsChanged ;
3436 foreach ( var variation in BuildingVariations )
3537 {
36- variation . ListChanged += OnBuildingComponentChanged ;
38+ variation . ListChanged += OnBuildingVariationItemChanged ;
3739 }
3840
3941 // Subscribe to BuildingHeights changes
40- BuildingHeights . ListChanged += OnBuildingComponentChanged ;
42+ BuildingHeights . ListChanged += OnBuildingHeightsChanged ;
4143
4244 // Subscribe to BuildingAnimations changes
43- BuildingAnimations . ListChanged += OnBuildingComponentChanged ;
45+ BuildingAnimations . ListChanged += OnBuildingAnimationsChanged ;
46+
47+ #endregion
48+ }
49+
50+ void OnBuildingVariationsChanged ( object ? sender , ListChangedEventArgs e )
51+ {
52+ // Only handle when the outer list itself changes, not when inner list items change
53+ if ( e . ListChangedType == ListChangedType . ItemAdded && e . NewIndex >= 0 && e . NewIndex < BuildingVariations . Count )
54+ {
55+ BuildingVariations [ e . NewIndex ] . ListChanged += OnBuildingVariationItemChanged ;
56+ SynchronizeBuildingComponentsToModel ( ) ;
57+ NotifyBuildingChanged ( ) ;
58+ }
59+ else if ( e . ListChangedType == ListChangedType . ItemDeleted )
60+ {
61+ SynchronizeBuildingComponentsToModel ( ) ;
62+ NotifyBuildingChanged ( ) ;
63+ }
64+ else if ( e . ListChangedType == ListChangedType . Reset )
65+ {
66+ // Re-subscribe to all items after reset
67+ foreach ( var variation in BuildingVariations )
68+ {
69+ variation . ListChanged += OnBuildingVariationItemChanged ;
70+ }
71+ SynchronizeBuildingComponentsToModel ( ) ;
72+ NotifyBuildingChanged ( ) ;
73+ }
74+ // Ignore ItemChanged events as they're handled by OnBuildingVariationItemChanged
4475 }
4576
46- public override void CopyBackToModel ( )
77+ void OnBuildingVariationItemChanged ( object ? sender , ListChangedEventArgs e )
78+ {
79+ SynchronizeBuildingComponentsToModel ( ) ;
80+ NotifyBuildingChanged ( ) ;
81+ }
82+
83+ void OnBuildingHeightsChanged ( object ? sender , ListChangedEventArgs e )
84+ {
85+ SynchronizeBuildingComponentsToModel ( ) ;
86+ NotifyBuildingChanged ( ) ;
87+ }
88+
89+ void OnBuildingAnimationsChanged ( object ? sender , ListChangedEventArgs e )
90+ {
91+ SynchronizeBuildingComponentsToModel ( ) ;
92+ NotifyBuildingChanged ( ) ;
93+ }
94+
95+ void SynchronizeBuildingComponentsToModel ( )
4796 {
4897 Model . BuildingComponents . BuildingVariations = [ .. BuildingVariations . Select ( x => x . ToList ( ) ) ] ;
4998 Model . BuildingComponents . BuildingHeights = [ .. BuildingHeights ] ;
5099 Model . BuildingComponents . BuildingAnimations = [ .. BuildingAnimations ] ;
51100 }
52101
53- void OnBuildingComponentChanged ( object ? sender , ListChangedEventArgs e )
102+ void NotifyBuildingChanged ( )
54103 {
55- // When a new nested list is added to BuildingVariations, subscribe to it
56- if ( sender == BuildingVariations && e . ListChangedType == ListChangedType . ItemAdded )
57- {
58- BuildingVariations [ e . NewIndex ] . ListChanged += OnBuildingComponentChanged ;
59- }
60-
61104 MessageBus . Current . SendMessage ( new BuildingComponents ( )
62105 {
63106 BuildingAnimations = [ .. BuildingAnimations ] ,
0 commit comments