Skip to content

Commit

Permalink
Reduced code duplication between WPF and Avalonia namespace, Prism.Na…
Browse files Browse the repository at this point in the history
…vigation.Regions.Behaviors
  • Loading branch information
DamianSuess committed Dec 15, 2024
1 parent 58b02da commit 424a303
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 1,527 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Avalonia;
using Avalonia;
using Prism.Common;
using System.Collections;
using System.Collections.Specialized;
Expand All @@ -12,91 +12,92 @@ namespace Prism.Navigation.Regions.Behaviors
/// </summary>
public class BindRegionContextToAvaloniaObjectBehavior : IRegionBehavior
{
/// <summary>The key of this behavior.</summary>
/// <remarks>TODO (DS 2024-04-11): This SHOULD be ''ContextToAvaloniaObject'.</remarks>
public const string BehaviorKey = "ContextToDependencyObject";

/// <summary>Behavior's attached region.</summary>
public IRegion Region { get; set; }
/// <summary>The key of this behavior.</summary>
/// <remarks>(2024-04-11_Suess): This SHOULD be ''ContextToAvaloniaObject'.</remarks>
public const string BehaviorKey = "ContextToDependencyObject";

/// <summary>Attaches the behavior to the specified region.</summary>
public void Attach()
{
Region.Views.CollectionChanged += Views_CollectionChanged;
Region.PropertyChanged += Region_PropertyChanged;
SetContextToViews(Region.Views, Region.Context);
AttachNotifyChangeEvent(Region.Views);
}
/// <summary>Behavior's attached region.</summary>
public IRegion Region { get; set; }

private static void SetContextToViews(IEnumerable views, object context)
{
foreach (var view in views)
/// <summary>Attaches the behavior to the specified region.</summary>
public void Attach()
{
AvaloniaObject avaloniaObjectView = view as AvaloniaObject;
if (avaloniaObjectView != null)
{
ObservableObject<object> contextWrapper = RegionContext.GetObservableContext(avaloniaObjectView);
contextWrapper.Value = context;
}
Region.Views.CollectionChanged += Views_CollectionChanged;
Region.PropertyChanged += Region_PropertyChanged;
SetContextToViews(Region.Views, Region.Context);
AttachNotifyChangeEvent(Region.Views);
}
}

private void AttachNotifyChangeEvent(IEnumerable views)
{
foreach (var view in views)
private static void SetContextToViews(IEnumerable views, object context)
{
var avaloniaObject = view as AvaloniaObject;
if (avaloniaObject != null)
foreach (var view in views)
{
ObservableObject<object> viewRegionContext = RegionContext.GetObservableContext(avaloniaObject);
viewRegionContext.PropertyChanged += ViewRegionContext_OnPropertyChangedEvent;
AvaloniaObject avaloniaObjectView = view as AvaloniaObject;
if (avaloniaObjectView != null)
{
ObservableObject<object> contextWrapper = RegionContext.GetObservableContext(avaloniaObjectView);
contextWrapper.Value = context;
}
}
}
}

private void DetachNotifyChangeEvent(IEnumerable views)
{
foreach (var view in views)
private void AttachNotifyChangeEvent(IEnumerable views)
{
var avaloniaObject = view as AvaloniaObject;
if (avaloniaObject != null)
foreach (var view in views)
{
ObservableObject<object> viewRegionContext = RegionContext.GetObservableContext(avaloniaObject);
viewRegionContext.PropertyChanged -= ViewRegionContext_OnPropertyChangedEvent;
var avaloniaObject = view as AvaloniaObject;
if (avaloniaObject != null)
{
ObservableObject<object> viewRegionContext = RegionContext.GetObservableContext(avaloniaObject);
viewRegionContext.PropertyChanged += ViewRegionContext_OnPropertyChangedEvent;
}
}
}
}

private void ViewRegionContext_OnPropertyChangedEvent(object sender, PropertyChangedEventArgs args)
{
if (args.PropertyName == "Value")
private void DetachNotifyChangeEvent(IEnumerable views)
{
var context = (ObservableObject<object>)sender;
Region.Context = context.Value;
foreach (var view in views)
{
var avaloniaObject = view as AvaloniaObject;
if (avaloniaObject != null)
{
ObservableObject<object> viewRegionContext = RegionContext.GetObservableContext(avaloniaObject);
viewRegionContext.PropertyChanged -= ViewRegionContext_OnPropertyChangedEvent;
}
}
}
}

private void Views_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
private void ViewRegionContext_OnPropertyChangedEvent(object sender, PropertyChangedEventArgs args)
{
SetContextToViews(e.NewItems, Region.Context);
AttachNotifyChangeEvent(e.NewItems);
if (args.PropertyName == "Value")
{
var context = (ObservableObject<object>)sender;
Region.Context = context.Value;
}
}
else if (e.Action == NotifyCollectionChangedAction.Remove && Region.Context != null)

private void Views_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
DetachNotifyChangeEvent(e.OldItems);
SetContextToViews(e.OldItems, null);
if (e.Action == NotifyCollectionChangedAction.Add)
{
SetContextToViews(e.NewItems, Region.Context);
AttachNotifyChangeEvent(e.NewItems);
}
else if (e.Action == NotifyCollectionChangedAction.Remove && Region.Context != null)
{
DetachNotifyChangeEvent(e.OldItems);
SetContextToViews(e.OldItems, null);

}
}
}

private void Region_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Context")
private void Region_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
SetContextToViews(Region.Views, Region.Context);
if (e.PropertyName == "Context")
{
SetContextToViews(Region.Views, Region.Context);
}
}
}
}
}

This file was deleted.

Loading

0 comments on commit 424a303

Please sign in to comment.