Skip to content

Commit bb6dd8c

Browse files
committed
REPLACE DialogService with Factory
1 parent 80d51e3 commit bb6dd8c

File tree

11 files changed

+102
-101
lines changed

11 files changed

+102
-101
lines changed

Drachenhorn.Core/UI/IDialogService.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ namespace Drachenhorn.Core.UI
77
{
88
public interface IDialogService
99
{
10-
Task<int> ShowMessage(string message, string title = null, List<string> buttons = null,
11-
Action<string> afterHideCallback = null);
10+
Task<int> ShowMessage(string message, string title = null, IEnumerable<string> buttons = null,
11+
Action<int> afterHideCallback = null);
1212

13-
Task<int> ShowMessageExternal(string message, string title = null, List<string> buttons = null,
14-
Action<string> afterHideCallback = null);
13+
Task<int> ShowMessageExternal(string message, string title = null, IEnumerable<string> buttons = null,
14+
Action<int> afterHideCallback = null);
1515

1616

1717

Drachenhorn.Core/UI/MessageFactory.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public class MessageFactory
2323
private string _message;
2424
private string _title = "Message";
2525
private List<string> _buttons = new List<string>();
26-
private Action<string> _afterHideCallback;
26+
private Action<int> _afterHideCallback;
2727
private MessageIcon _icons;
2828

29-
public static MessageFactory Message()
29+
public static MessageFactory NewMessage()
3030
{
3131
return new MessageFactory();
3232
}
@@ -103,7 +103,7 @@ public MessageFactory Icon(MessageIcon icons)
103103
return this;
104104
}
105105

106-
public MessageFactory Callback(Action<string> afterHideCallback)
106+
public MessageFactory Callback(Action<int> afterHideCallback)
107107
{
108108
_afterHideCallback = afterHideCallback;
109109
return this;
@@ -119,7 +119,7 @@ public Task<int> ShowMessage(bool allowInternal = true)
119119
{
120120
service.ShowException(_exception, _title, () =>
121121
{
122-
_afterHideCallback?.Invoke(null);
122+
_afterHideCallback?.Invoke(0);
123123
}).Wait();
124124
return 0;
125125
});

Drachenhorn.Core/ViewModels/Common/MainViewModel.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private void ExecutePrint()
127127
private async void ExecuteGeneratePDF()
128128
{
129129
if (CurrentSheetViewModel?.CurrentSheet == null)
130-
await MessageFactory.Message()
130+
await MessageFactory.NewMessage()
131131
.MessageTranslated("UI.NothingSelected")
132132
.Title("UI.NothingSelected.Title")
133133
.ShowMessage();
@@ -153,7 +153,7 @@ private async void ExecuteClose(CharacterSheetViewModel model)
153153
return;
154154
}
155155

156-
var result = await MessageFactory.Message()
156+
var result = await MessageFactory.NewMessage()
157157
.MessageTranslated("UI.SouldClose")
158158
.TitleTranslated("UI.SouldClose.Caption")
159159
.ButtonTranslated("UI.Yes", 0)

Drachenhorn.Core/ViewModels/Common/SettingsViewModel.cs

+1-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ public class SettingsViewModel : ViewModelBase
88
{
99
#region c'tor
1010

11-
public SettingsViewModel(ISettings settings, IDialogService dialogService)
11+
public SettingsViewModel(ISettings settings)
1212
{
1313
Settings = settings;
14-
15-
DialogService = dialogService;
1614
}
1715

1816
#endregion
@@ -33,8 +31,6 @@ public ISettings Settings
3331
}
3432
}
3533

36-
private IDialogService DialogService { get; }
37-
3834
#endregion
3935
}
4036
}

Drachenhorn.Desktop/App.xaml.cs

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
using Easy.Logger.Interfaces;
2828
using GalaSoft.MvvmLight.Ioc;
2929
using GalaSoft.MvvmLight.Messaging;
30-
using GalaSoft.MvvmLight.Views;
3130
using MahApps.Metro;
3231
using Microsoft.Win32;
3332
using SplashScreen = Drachenhorn.Desktop.UI.Splash.SplashScreen;

Drachenhorn.Desktop/UI/Dialogs/CommonMessageBox.xaml

+15-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,24 @@
1616
Margin="10" MaxWidth="350" MinHeight="80"
1717
VerticalAlignment="Center"
1818
TextWrapping="WrapWithOverflow" />
19-
<WrapPanel HorizontalAlignment="Right">
19+
<ItemsControl Name="ButtonControl">
20+
<ItemsControl.ItemsPanel>
21+
<ItemsPanelTemplate>
22+
<WrapPanel IsItemsHost="True" HorizontalAlignment="Right"/>
23+
</ItemsPanelTemplate>
24+
</ItemsControl.ItemsPanel>
25+
<ItemsControl.ItemTemplate>
26+
<DataTemplate>
27+
<Button Content="{Binding .}" Margin="5" MinWidth="80" Padding="5 2 5 2"
28+
Click="DialogButton_OnClick"/>
29+
</DataTemplate>
30+
</ItemsControl.ItemTemplate>
31+
</ItemsControl>
32+
<!--<WrapPanel HorizontalAlignment="Right">
2033
<Button Name="ConfirmButton" Content="confirm" Margin="5" MinWidth="80"
2134
Click="ConfirmButton_OnClick" Padding="5 2 5 2" />
2235
<Button Name="CancelButton" Content="cancel" Margin="5" MinWidth="80"
2336
Click="CancelButton_OnClick" Padding="5 2 5 2" />
24-
</WrapPanel>
37+
</WrapPanel>-->
2538
</StackPanel>
2639
</mah:MetroWindow>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Windows;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Windows;
4+
using System.Windows.Controls;
25

36
namespace Drachenhorn.Desktop.UI.Dialogs
47
{
@@ -7,34 +10,40 @@ namespace Drachenhorn.Desktop.UI.Dialogs
710
/// </summary>
811
public partial class CommonMessageBox
912
{
13+
private int _result = -1;
14+
1015
#region c'tor
1116

12-
public CommonMessageBox(string message, string title, string buttonConfirmText, string buttonCancelText = null)
17+
public CommonMessageBox(string message, string title, string[] buttons)
1318
{
1419
InitializeComponent();
1520

1621
Title = title;
1722
MessageBlock.Text = message;
18-
ConfirmButton.Content = buttonConfirmText;
19-
20-
if (string.IsNullOrEmpty(buttonCancelText))
21-
CancelButton.Visibility = Visibility.Collapsed;
22-
else
23-
CancelButton.Content = buttonCancelText;
23+
ButtonControl.ItemsSource = buttons;
2424
}
2525

2626
#endregion
2727

28-
private void ConfirmButton_OnClick(object sender, RoutedEventArgs e)
28+
private void DialogButton_OnClick(object sender, RoutedEventArgs e)
2929
{
30+
var buttons = (string[]) ButtonControl.ItemsSource;
31+
var clickText = ((Button) sender).Content.ToString();
32+
33+
_result = Array.FindIndex(buttons, x => x == clickText);
34+
3035
DialogResult = true;
3136
Close();
3237
}
3338

34-
private void CancelButton_OnClick(object sender, RoutedEventArgs e)
39+
public new int ShowDialog()
3540
{
36-
DialogResult = false;
37-
Close();
41+
var result = base.ShowDialog() == true;
42+
43+
if (result && _result != -1)
44+
return _result;
45+
46+
return -1;
3847
}
3948
}
4049
}
+17-38
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,42 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Threading.Tasks;
35
using System.Windows;
46
using Drachenhorn.Core.Lang;
7+
using Drachenhorn.Core.UI;
58
using Drachenhorn.Desktop.UI.Dialogs;
6-
using GalaSoft.MvvmLight.Views;
79

810
namespace Drachenhorn.Desktop.UI.MVVM
911
{
1012
public class DialogService : IDialogService
1113
{
12-
public Task ShowError(string message, string title, string buttonText, Action afterHideCallback)
14+
/// <inheritdoc />
15+
public Task<int> ShowMessage(string message, string title = null,
16+
IEnumerable<string> buttons = null, Action<int> afterHideCallback = null)
1317
{
14-
return ShowMessage(message, title, buttonText, afterHideCallback);
18+
//TODO: MetroMessageDialog
19+
return ShowMessageExternal(message, title, buttons, afterHideCallback);
1520
}
1621

17-
public async Task ShowError(Exception error, string title, string buttonText, Action afterHideCallback)
22+
/// <inheritdoc />
23+
public Task<int> ShowMessageExternal(string message, string title = null,
24+
IEnumerable<string> buttons = null, Action<int> afterHideCallback = null)
1825
{
19-
await Task.Run(() => { new ExceptionMessageBox(error, title).ShowDialog(); });
20-
}
21-
22-
public async Task ShowMessage(string message, string title)
23-
{
24-
await Task.Run(() =>
25-
{
26-
MessageBox.Show(
27-
LanguageManager.TextTranslate(message),
28-
LanguageManager.TextTranslate(title),
29-
MessageBoxButton.OK);
30-
});
31-
}
32-
33-
public Task ShowMessage(string message, string title, string buttonText, Action afterHideCallback)
34-
{
35-
var result = new CommonMessageBox(message, title, buttonText).ShowDialog() == true;
26+
if (buttons == null || !buttons.Any())
27+
buttons = new List<string> { LanguageManager.Translate("UI.OK") };
3628

37-
afterHideCallback?.Invoke();
38-
39-
return Task.Run(() => result);
40-
}
41-
42-
public Task<bool> ShowMessage(string message, string title, string buttonConfirmText, string buttonCancelText,
43-
Action<bool> afterHideCallback)
44-
{
45-
var result = new CommonMessageBox(message, title, buttonConfirmText, buttonCancelText).ShowDialog() == true;
29+
var result = new CommonMessageBox(message, title, buttons.ToArray()).ShowDialog();
4630

4731
afterHideCallback?.Invoke(result);
4832

4933
return Task.Run(() => result);
5034
}
5135

52-
public async Task ShowMessageBox(string message, string title)
36+
/// <inheritdoc />
37+
public Task ShowException(Exception e, string title, Action afterHideCallback)
5338
{
54-
await Task.Run(() =>
55-
{
56-
MessageBox.Show(
57-
LanguageManager.TextTranslate(message),
58-
LanguageManager.TextTranslate(title),
59-
MessageBoxButton.OK);
60-
});
39+
return Task.Run(() => { new ExceptionMessageBox(e, title).ShowDialog(); });
6140
}
6241
}
6342
}

Drachenhorn.Desktop/UserControls/Common/SettingsViewControl.xaml.cs

+22-22
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
using System.Windows.Navigation;
99
using Drachenhorn.Core.Lang;
1010
using Drachenhorn.Core.Settings;
11+
using Drachenhorn.Core.UI;
1112
using Drachenhorn.Desktop.UserSettings;
1213
using GalaSoft.MvvmLight.Ioc;
13-
using GalaSoft.MvvmLight.Views;
14+
using IDialogService = GalaSoft.MvvmLight.Views.IDialogService;
1415

1516
namespace Drachenhorn.Desktop.UserControls.Common
1617
{
@@ -72,38 +73,37 @@ private void AskForUpdate(bool result)
7273

7374
if (result)
7475
{
75-
var task = SimpleIoc.Default.GetInstance<IDialogService>().ShowMessage(
76-
LanguageManager.Translate("Updater.UpdateAvailable"),
77-
LanguageManager.Translate("Updater.Title"),
78-
LanguageManager.Translate("Updater.DoUpdate"),
79-
LanguageManager.Translate("Updater.Dismiss"), null);
76+
var task = MessageFactory.NewMessage()
77+
.MessageTranslated("Updater.UpdateAvailable")
78+
.TitleTranslated("Updater.Title")
79+
.ButtonTranslated("Updater.DoUpdate", 0)
80+
.ButtonTranslated("Updater.Dismiss", 1)
81+
.ShowMessage();
8082

81-
if (task.Result)
83+
if (task.Result == 0)
8284
Task.Run(() => SquirrelManager.UpdateSquirrel(null, (x, y) =>
8385
{
84-
Dispatcher.Invoke(() =>
86+
Dispatcher?.Invoke(() =>
8587
{
8688
if (x)
87-
SimpleIoc.Default.GetInstance<IDialogService>().ShowMessage(
88-
LanguageManager.Translate("Updater.UpdateFinished") + "\n" +
89-
LanguageManager.Translate("Updater.UpdateFinished.Sub"),
90-
LanguageManager.Translate("Updater.Title"),
91-
LanguageManager.Translate("UI.OK"), null);
89+
MessageFactory.NewMessage()
90+
.MessageTranslated("%Updater.UpdateFinished\n%Updater.UpdateFinished.Sub", false)
91+
.TitleTranslated("Updater.Title")
92+
.ShowMessage();
9293
else
93-
SimpleIoc.Default.GetInstance<IDialogService>().ShowMessage(
94-
LanguageManager.Translate("Updater.UpdateFailed") + "\n" +
95-
LanguageManager.Translate("Updater.UpdateFailed.Sub"),
96-
LanguageManager.Translate("Updater.Title"),
97-
LanguageManager.Translate("UI.OK"), null);
94+
MessageFactory.NewMessage()
95+
.MessageTranslated("%Updater.UpdateFailed\n%Updater.UpdateFailed.Sub", false)
96+
.Title("Updater.Title")
97+
.ShowMessage();
9898
});
9999
}));
100100
}
101101
else
102102
{
103-
SimpleIoc.Default.GetInstance<IDialogService>().ShowMessage(
104-
LanguageManager.Translate("Updater.NoUpdateAvailable"),
105-
LanguageManager.Translate("Updater.Title"),
106-
LanguageManager.Translate("UI.OK"), null);
103+
MessageFactory.NewMessage()
104+
.MessageTranslated("Updater.NoUpdateAvailable")
105+
.Title("Updater.Title")
106+
.ShowMessage();
107107
}
108108
}
109109

Drachenhorn.Desktop/UserSettings/Settings.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77
using System.Xml.Serialization;
88
using Drachenhorn.Core.Lang;
99
using Drachenhorn.Core.Settings;
10+
using Drachenhorn.Core.UI;
1011
using Drachenhorn.Xml;
1112
using Drachenhorn.Xml.Template;
1213
using Easy.Logger.Interfaces;
1314
using GalaSoft.MvvmLight.Ioc;
14-
using GalaSoft.MvvmLight.Views;
1515
using MahApps.Metro;
16+
using IDialogService = GalaSoft.MvvmLight.Views.IDialogService;
1617

1718
namespace Drachenhorn.Desktop.UserSettings
1819
{
@@ -205,8 +206,10 @@ public static Settings Load()
205206
}
206207
catch (InvalidOperationException)
207208
{
208-
var service = SimpleIoc.Default.GetInstance<IDialogService>();
209-
service.ShowMessage("%Notification.Settings.Corrupted", "%Notification.Header.Error");
209+
MessageFactory.NewMessage()
210+
.MessageTranslated("Notification.Settings.Corrupted")
211+
.Title("Notification.Header.Error")
212+
.ShowMessage();
210213

211214
SimpleIoc.Default.GetInstance<ILogService>().GetLogger<Settings>()
212215
.Warn("Settings corrupted. Generating new.");

0 commit comments

Comments
 (0)