Skip to content

Commit faf0c2b

Browse files
committed
Chore: Refactor dialogs
1 parent c0d4c62 commit faf0c2b

File tree

3 files changed

+76
-79
lines changed

3 files changed

+76
-79
lines changed

Source/NETworkManager/ViewModels/SettingsProfilesViewModel.cs

Lines changed: 57 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,16 @@ private async Task AddProfileFileAction()
133133

134134
await (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow);
135135

136+
// Re-select the profile file
136137
if (string.IsNullOrEmpty(profileName))
137138
return;
138139

139140
SelectedProfileFile = ProfileFiles.Cast<ProfileFileInfo>()
140141
.FirstOrDefault(p => p.Name.Equals(profileName, StringComparison.OrdinalIgnoreCase));
141142

142-
// Ask to enable encryption for the new profile file
143-
var result = await DialogHelper.ShowOKCancelMessageAsync(Application.Current.MainWindow,
144-
Strings.EnableEncryptionQuestion,
143+
// Ask the user if they want to enable encryption for the new profile file
144+
var result = await DialogHelper.ShowOKCancelMessageAsync(Application.Current.MainWindow,
145+
Strings.EnableEncryptionQuestion,
145146
Strings.EnableEncryptionForProfileFileMessage);
146147

147148
if (result)
@@ -178,6 +179,7 @@ private async Task EditProfileFileAction()
178179

179180
await (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow);
180181

182+
// Re-select the profile file
181183
if (string.IsNullOrEmpty(profileName))
182184
return;
183185

@@ -193,7 +195,7 @@ private bool DeleteProfileFile_CanExecute(object obj)
193195
return ProfileFiles.Cast<ProfileFileInfo>().Count() > 1;
194196
}
195197

196-
private Task DeleteProfileFileAction()
198+
private async Task DeleteProfileFileAction()
197199
{
198200
var childWindow = new OKCancelMessageChildWindow();
199201

@@ -216,14 +218,20 @@ private Task DeleteProfileFileAction()
216218

217219
ConfigurationManager.Current.IsChildWindowOpen = true;
218220

219-
return (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow);
221+
await (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow);
222+
223+
// Select the first profile file
224+
SelectedProfileFile = ProfileFiles.Cast<ProfileFileInfo>().FirstOrDefault();
220225
}
221226

222227
public ICommand EnableEncryptionCommand => new RelayCommand(_ => EnableEncryptionAction());
223228

224229
private async void EnableEncryptionAction()
225230
{
226-
if (!await ShowEncryptionDisclaimerAsync())
231+
// Show encryption disclaimer
232+
if (!await DialogHelper.ShowOKCancelMessageAsync(Application.Current.MainWindow,
233+
Strings.Disclaimer,
234+
Strings.ProfileEncryptionDisclaimer))
227235
return;
228236

229237
var customDialog = new CustomDialog
@@ -257,87 +265,66 @@ await _dialogCoordinator.ShowMessageAsync(this, Strings.EncryptionError,
257265

258266
await _dialogCoordinator.ShowMetroDialogAsync(this, customDialog);
259267
}
268+
269+
public ICommand ChangeMasterPasswordCommand => new RelayCommand(async _ => await ChangeMasterPasswordAction().ConfigureAwait(false));
260270

261-
private async Task<bool> ShowEncryptionDisclaimerAsync()
271+
private async Task ChangeMasterPasswordAction()
262272
{
263-
var result = false;
264-
265-
var childWindow = new OKCancelMessageChildWindow();
273+
var profileName = SelectedProfileFile.Name;
266274

267-
var childWindowViewModel = new OKCancelMessageViewModel(_ =>
268-
{
269-
childWindow.IsOpen = false;
270-
ConfigurationManager.Current.IsChildWindowOpen = false;
275+
var childWindow = new CredentialsChangePasswordChildWindow();
271276

272-
result = true;
273-
}, _ =>
277+
var childWindowViewModel = new CredentialsChangePasswordViewModel(async instance =>
274278
{
275279
childWindow.IsOpen = false;
276280
ConfigurationManager.Current.IsChildWindowOpen = false;
277-
},
278-
Strings.ProfileEncryptionDisclaimer
279-
);
280-
281-
childWindow.Title = Strings.Disclaimer;
282-
283-
childWindow.DataContext = childWindowViewModel;
284-
285-
ConfigurationManager.Current.IsChildWindowOpen = true;
286-
287-
await (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow);
288-
289-
return result;
290-
}
291-
292-
public ICommand ChangeMasterPasswordCommand => new RelayCommand(_ => ChangeMasterPasswordAction());
293-
294-
private async void ChangeMasterPasswordAction()
295-
{
296-
var customDialog = new CustomDialog
297-
{
298-
Title = Strings.ChangeMasterPassword
299-
};
300-
301-
var credentialsPasswordViewModel = new CredentialsChangePasswordViewModel(async instance =>
302-
{
303-
await _dialogCoordinator.HideMetroDialogAsync(this, customDialog);
304281

305282
try
306283
{
307284
ProfileManager.ChangeMasterPassword(SelectedProfileFile, instance.Password, instance.NewPassword);
308285
}
309286
catch (CryptographicException)
310287
{
311-
var settings = AppearanceManager.MetroDialog;
312-
settings.AffirmativeButtonText = Strings.OK;
313-
314-
await _dialogCoordinator.ShowMessageAsync(this, Strings.WrongPassword,
315-
Strings.WrongPasswordDecryptionFailedMessage, MessageDialogStyle.Affirmative,
316-
settings);
288+
await DialogHelper.ShowOKMessageAsync(Application.Current.MainWindow,
289+
Strings.WrongPassword,
290+
Strings.WrongPasswordDecryptionFailedMessage,
291+
ChildWindowIcon.Error).ConfigureAwait(false);
317292
}
318293
catch (Exception ex)
319294
{
320-
var settings = AppearanceManager.MetroDialog;
321-
settings.AffirmativeButtonText = Strings.OK;
322-
323-
await _dialogCoordinator.ShowMessageAsync(this, Strings.DecryptionError,
295+
await DialogHelper.ShowOKMessageAsync(Application.Current.MainWindow,
296+
Strings.DecryptionError,
324297
$"{Strings.DecryptionErrorMessage}\n\n{ex.Message}",
325-
MessageDialogStyle.Affirmative, settings);
298+
ChildWindowIcon.Error).ConfigureAwait(false);
326299
}
327-
}, async _ => { await _dialogCoordinator.HideMetroDialogAsync(this, customDialog); });
328-
329-
customDialog.Content = new CredentialsChangePasswordDialog
300+
}, _ =>
330301
{
331-
DataContext = credentialsPasswordViewModel
332-
};
302+
childWindow.IsOpen = false;
303+
ConfigurationManager.Current.IsChildWindowOpen = false;
304+
});
333305

334-
await _dialogCoordinator.ShowMetroDialogAsync(this, customDialog);
306+
childWindow.Title = Strings.ChangeMasterPassword;
307+
308+
childWindow.DataContext = childWindowViewModel;
309+
310+
ConfigurationManager.Current.IsChildWindowOpen = true;
311+
312+
await (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow);
313+
314+
// Re-select the profile file
315+
if (string.IsNullOrEmpty(profileName))
316+
return;
317+
318+
SelectedProfileFile = ProfileFiles.Cast<ProfileFileInfo>()
319+
.FirstOrDefault(p => p.Name.Equals(profileName, StringComparison.OrdinalIgnoreCase));
335320
}
336321

337322
public ICommand DisableEncryptionCommand => new RelayCommand(async _ => await DisableEncryptionAction().ConfigureAwait(false));
338323

339-
private Task DisableEncryptionAction()
324+
private async Task DisableEncryptionAction()
340325
{
326+
var profileName = SelectedProfileFile.Name;
327+
341328
var childWindow = new CredentialsPasswordChildWindow();
342329

343330
var childWindowViewModel = new CredentialsPasswordViewModel(async instance =>
@@ -376,8 +363,15 @@ await DialogHelper.ShowOKMessageAsync(Application.Current.MainWindow,
376363

377364
ConfigurationManager.Current.IsChildWindowOpen = true;
378365

379-
return (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow);
366+
await (Application.Current.MainWindow as MainWindow).ShowChildWindowAsync(childWindow);
367+
368+
// Re-select the profile file
369+
if (string.IsNullOrEmpty(profileName))
370+
return;
371+
372+
SelectedProfileFile = ProfileFiles.Cast<ProfileFileInfo>()
373+
.FirstOrDefault(p => p.Name.Equals(profileName, StringComparison.OrdinalIgnoreCase));
380374
}
381375

382376
#endregion
383-
}
377+
}

Source/NETworkManager/Views/CredentialsChangePasswordDialog.xaml renamed to Source/NETworkManager/Views/CredentialsChangePasswordChildWindow.xaml

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<UserControl x:Class="NETworkManager.Views.CredentialsChangePasswordDialog"
1+
<simpleChildWindow:ChildWindow x:Class="NETworkManager.Views.CredentialsChangePasswordChildWindow"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -7,9 +7,12 @@
77
xmlns:interactivity="http://schemas.microsoft.com/xaml/behaviors"
88
xmlns:viewModels="clr-namespace:NETworkManager.ViewModels"
99
xmlns:localization="clr-namespace:NETworkManager.Localization.Resources;assembly=NETworkManager.Localization"
10-
mc:Ignorable="d" Loaded="ChildWindow_OnLoaded"
11-
d:DataContext="{d:DesignInstance viewModels:CredentialsChangePasswordViewModel}">
12-
<Grid Margin="0,20">
10+
xmlns:simpleChildWindow="clr-namespace:MahApps.Metro.SimpleChildWindow;assembly=MahApps.Metro.SimpleChildWindow"
11+
CloseButtonCommand="{Binding Path=CancelCommand}"
12+
Style="{StaticResource DefaultChildWindow}"
13+
Loaded="ChildWindow_OnLoaded"
14+
mc:Ignorable="d" d:DataContext="{d:DesignInstance viewModels:CredentialsChangePasswordViewModel}">
15+
<Grid Margin="10">
1316
<Grid.RowDefinitions>
1417
<RowDefinition Height="*" />
1518
<RowDefinition Height="Auto" />
@@ -35,31 +38,31 @@
3538
</Grid.RowDefinitions>
3639
<TextBlock Grid.Column="0" Grid.Row="0" Text="{x:Static localization:Strings.CurrentPassword}" />
3740
<PasswordBox x:Name="PasswordBoxPassword" Grid.Column="2" Grid.Row="0"
38-
Style="{StaticResource DefaultPasswordBox}">
41+
Style="{StaticResource DefaultPasswordBox}">
3942
<interactivity:Interaction.Behaviors>
4043
<wpfHelpers:PasswordBoxBindingBehavior
41-
Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
44+
Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
4245
</interactivity:Interaction.Behaviors>
4346
</PasswordBox>
4447
<TextBlock Grid.Column="0" Grid.Row="2" Text="{x:Static localization:Strings.NewPassword}" />
4548
<PasswordBox x:Name="PasswordBoxNewPassword" Grid.Column="2" Grid.Row="2"
46-
Style="{StaticResource DefaultPasswordBox}">
49+
Style="{StaticResource DefaultPasswordBox}">
4750
<interactivity:Interaction.Behaviors>
4851
<wpfHelpers:PasswordBoxBindingBehavior
49-
Password="{Binding NewPassword, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
52+
Password="{Binding NewPassword, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
5053
</interactivity:Interaction.Behaviors>
5154
</PasswordBox>
5255
<TextBlock Grid.Column="0" Grid.Row="4" Text="{x:Static localization:Strings.Repeat}" />
5356
<PasswordBox x:Name="PasswordBoxNewPasswordRepeat" Grid.Column="2" Grid.Row="4"
54-
Style="{StaticResource DefaultPasswordBox}">
57+
Style="{StaticResource DefaultPasswordBox}">
5558
<interactivity:Interaction.Behaviors>
5659
<wpfHelpers:PasswordBoxBindingBehavior
57-
Password="{Binding NewPasswordRepeat, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
60+
Password="{Binding NewPasswordRepeat, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
5861
</interactivity:Interaction.Behaviors>
5962
</PasswordBox>
6063
</Grid>
6164
<TextBlock Grid.Row="1" Foreground="{DynamicResource MahApps.Brushes.Accent}"
62-
Text="{x:Static localization:Strings.PasswordsDoNotMatch}" Margin="0,10,0,0">
65+
Text="{x:Static localization:Strings.PasswordsDoNotMatch}" Margin="0,10,0,0">
6366
<TextBlock.Style>
6467
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource DefaultTextBlock}">
6568
<Setter Property="Visibility" Value="Collapsed" />
@@ -79,7 +82,7 @@
7982
</TextBlock>
8083
<StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right">
8184
<Button Content="{x:Static localization:Strings.OK}" Command="{Binding OKCommand}" IsDefault="True"
82-
Margin="0,0,10,0">
85+
Margin="0,0,10,0">
8386
<Button.Style>
8487
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource HighlightedButton}">
8588
<Setter Property="IsEnabled" Value="False" />
@@ -98,7 +101,7 @@
98101
</Button.Style>
99102
</Button>
100103
<Button Content="{x:Static localization:Strings.Cancel}" Command="{Binding CancelCommand}" IsCancel="True"
101-
Style="{StaticResource DefaultButton}" />
104+
Style="{StaticResource DefaultButton}" />
102105
</StackPanel>
103106
</Grid>
104-
</UserControl>
107+
</simpleChildWindow:ChildWindow>

Source/NETworkManager/Views/CredentialsChangePasswordDialog.xaml.cs renamed to Source/NETworkManager/Views/CredentialsChangePasswordChildWindow.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
namespace NETworkManager.Views;
66

7-
public partial class CredentialsChangePasswordDialog
7+
public partial class CredentialsChangePasswordChildWindow
88
{
9-
public CredentialsChangePasswordDialog()
9+
public CredentialsChangePasswordChildWindow()
1010
{
1111
InitializeComponent();
1212
}

0 commit comments

Comments
 (0)