Recently I started to use mvvm light messenger and it lead to creating this ugliness which I have to refactor now. What would be the best future proof way to refactor this whole class?
public partial class RootView : Window
{
public RootView()
{
try
{
InitializeComponent();
greyOutAdorner = new GreyOutAdorner(MainWindow);
Messenger.Default.Register<NotificationMessage>(this, NotificationMessageRecived);
Messenger.Default.Register<string>(this, StringMessageRecived);
Messenger.Default.Register<string>(this,"Sending DesignID", DesignIdRecived);
Messenger.Default.Register<DesignInformation>(this, "Sending DesignInfo", DesignInfoReciver);
Messenger.Default.Register<SpecialDesignInformation>(this, "Sending SpecialDesignInfo", SpecialDesignInfoReciver);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
string report;
string desingName;
GreyOutAdorner greyOutAdorner;
DesignInformation designInfo;
SpecialDesignInformation specDesignInfo;
DatabaseDesingview databaseDesingview;
SpecialDesignView specialDesignView;
private void SpecialDesignInfoReciver(SpecialDesignInformation msg)
{
specDesignInfo = msg;
}
private void DesignInfoReciver(DesignInformation msg)
{
designInfo = msg;
}
private void StringMessageRecived(string msg)
{
report = msg;
}
private void DesignIdRecived(string msg)
{
desingName = msg;
}
private void NotificationMessageRecived(NotificationMessage msg)
{
if (msg.Notification == "Open load from database windows")
{
this.AttachAdorner();
databaseDesingview = new DatabaseDesingview()
{
Owner = GetWindow(this),
ShowInTaskbar = false,
WindowStartupLocation = WindowStartupLocation.CenterOwner,
};
databaseDesingview.ShowDialog();
}
if (msg.Notification == "Validation error message")
{
MessageBox.Show("Please Correct Validation Errors", "Validation", MessageBoxButton.OK);
}
if (msg.Notification == "Design already exist message")
{
MessageBoxResult result = MessageBoxResult.Yes;
result = MessageBox.Show("Design already exist in database would you like to replace it ?", "Replace design", MessageBoxButton.YesNo);
Messenger.Default.Send(result);
}
if (msg.Notification == "Design is not valid message")
{
MessageBoxResult result = MessageBoxResult.Yes;
result = MessageBox.Show("Design is not valid. Are you sure you want to save it?", "Confirmation", MessageBoxButton.YesNo);
Messenger.Default.Send(result);
}
if (msg.Notification == "Window closed")
{
this.DettachAdorner();
}
if (msg.Notification == "Calculation window closed")
{
this.DettachAdorner();
}
if (msg.Notification == "Open old calculation report window")
{
var calcualtionReportOldView = new CalculationReportOldView(report, databaseDesingview)
{
Title = desingName +" " +"Calculation report"
};
calcualtionReportOldView.Show();
}
if(msg.Notification == "Open old calculation winding instructions window")
{
var calcualtionReportOldView = new CalculationReportOldView(report, databaseDesingview)
{
Title = desingName +" " +"Winding instructions"
};
calcualtionReportOldView.Show();
}
if (msg.Notification == "Open dialog old calculation report window")
{
var windowsCollecttion = Application.Current.Windows;
var calcualtionReportOldView = new CalculationReportOldView(report, databaseDesingview)
{
Title = desingName + " " + "Calculation report"
};
if (windowsCollecttion.Cast<Window>().Any(x => x == specialDesignView))
{
specialDesignView.Close();
}
this.AttachAdorner();
calcualtionReportOldView.ShowDialog();
}
if (msg.Notification == "Open special design window")
{
this.AttachAdorner();
specialDesignView = new SpecialDesignView(designInfo, specDesignInfo)
{
Owner = GetWindow(this),
};
designInfo = null;
specDesignInfo = null;
specialDesignView.ShowDialog();
}
}
private void AttachAdorner()
{
AdornerLayer parentAdorner = AdornerLayer.GetAdornerLayer(MainWindow);
parentAdorner.Add(greyOutAdorner);
}
private void DettachAdorner()
{
AdornerLayer parentAdorner = AdornerLayer.GetAdornerLayer(MainWindow);
parentAdorner.Remove(greyOutAdorner);
}
}