Skip to content

Commit 59f3685

Browse files
committed
Cleaned up some code
1 parent ede9bc4 commit 59f3685

File tree

50 files changed

+183
-194
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+183
-194
lines changed

SecureFolderFS.Backend/Extensions/LocalizationExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class LocalizationExtensions
99

1010
public static string? ToLocalized(this string resourceKey, ILocalizationService? localizationService = null)
1111
{
12-
if (localizationService == null)
12+
if (localizationService is null)
1313
{
1414
FallbackLocalizationService ??= Ioc.Default.GetService<ILocalizationService>();
1515
return FallbackLocalizationService?.LocalizeFromResourceKey(resourceKey) ?? string.Empty;

SecureFolderFS.Backend/Models/DashboardNavigationModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private void NavigateToPage(VaultDashboardPageType vaultDashboardPageType, BaseD
5050
public void Receive(DashboardNavigationRequestedMessage message)
5151
{
5252
BaseDashboardPageViewModel? baseDashboardPageViewModel;
53-
if (message.Value == null)
53+
if (message.Value is null)
5454
{
5555
baseDashboardPageViewModel = NavigateToPage(message.VaultDashboardPageType, message.VaultViewModel);
5656
}

SecureFolderFS.Backend/Models/IGraphManagerModel.cs SecureFolderFS.Backend/Models/IGraphModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace SecureFolderFS.Backend.Models
22
{
3-
public interface IGraphManagerModel
3+
public interface IGraphModel
44
{
55
void AddPoint(GraphPointModel point);
66
}

SecureFolderFS.Backend/Models/SavedVaultsModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public SavedVaultsModel()
2626

2727
public void Initialize()
2828
{
29-
if (InitializableSource != null && (SettingsService.IsAvailable && ConfidentialStorageService.IsAvailable))
29+
if (InitializableSource is not null && (SettingsService.IsAvailable && ConfidentialStorageService.IsAvailable))
3030
{
3131
var savedVaults = SettingsService.SavedVaults;
3232
var savedVaultModels = ConfidentialStorageService.SavedVaultModels;

SecureFolderFS.Backend/Models/SearchModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public bool SubmitQuery(string? query)
4545

4646
public void ResetSearch()
4747
{
48-
if (_savedItems != null)
48+
if (_savedItems is not null)
4949
{
5050
Collection!.EnumeratedAdd(_savedItems);
5151
_savedItems = null;

SecureFolderFS.Backend/Models/VaultIoSpeedReporterModel.cs

+9-10
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ public sealed class VaultIoSpeedReporterModel : BaseVaultIoSpeedReporterModel
1818

1919
private readonly List<long> _writeRates;
2020

21-
private long _readAmountBeforeFlush;
21+
private int _updateTimerInterval;
2222

23-
private long _writeAmountBeforeFlush;
23+
private long _readAmountBeforeFlush;
2424

25-
private int _updateTimerInterval;
25+
private long _writeAmountBeforeFlush;
2626

27-
public IProgress<double>? ReadGraphProgress { get; }
27+
private IProgress<double>? ReadGraphProgress { get; }
2828

29-
public IProgress<double>? WriteGraphProgress { get; }
29+
private IProgress<double>? WriteGraphProgress { get; }
3030

31-
public IGraphManagerModel? ReadGraphManagerModel { get; init; }
31+
public IGraphModel? ReadGraphModel { get; init; }
3232

33-
public IGraphManagerModel? WriteGraphManagerModel { get; init; }
33+
public IGraphModel? WriteGraphModel { get; init; }
3434

3535
public VaultIoSpeedReporterModel(IProgress<double>? readGraphProgress, IProgress<double>? writeGraphProgress)
3636
{
@@ -70,12 +70,12 @@ private async void UpdateTimer_Elapsed(object? sender, ElapsedEventArgs e)
7070

7171
await ThreadingService.ExecuteOnUiThreadAsync(() =>
7272
{
73-
ReadGraphManagerModel?.AddPoint(new()
73+
ReadGraphModel?.AddPoint(new()
7474
{
7575
Date = now,
7676
High = Convert.ToInt64(Math.Round(ByteSize.FromBytes(_readAmountBeforeFlush).MegaBytes))
7777
});
78-
WriteGraphManagerModel?.AddPoint(new()
78+
WriteGraphModel?.AddPoint(new()
7979
{
8080
Date = now,
8181
High = Convert.ToInt64(Math.Round(ByteSize.FromBytes(_writeAmountBeforeFlush).MegaBytes))
@@ -92,7 +92,6 @@ await ThreadingService.ExecuteOnUiThreadAsync(() =>
9292
if (_updateTimerInterval == 4)
9393
{
9494
CalculateRates();
95-
9695
_updateTimerInterval = 0;
9796
}
9897
}

SecureFolderFS.Backend/Utils/ICanceledFlag.cs

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace SecureFolderFS.Backend.Utils
2+
{
3+
/// <summary>
4+
/// Provides dispatch for notifying the caller whether the event should be forwarded or not.
5+
/// </summary>
6+
public interface IEventDispatchFlag
7+
{
8+
/// <summary>
9+
/// Prevents forwarding of the event.
10+
/// </summary>
11+
void NoForwarding();
12+
}
13+
}

SecureFolderFS.Backend/Utils/IHandledFlag.cs

-9
This file was deleted.

SecureFolderFS.Backend/ViewModels/Controls/GraphControlViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace SecureFolderFS.Backend.ViewModels.Controls
88
{
9-
public sealed class GraphControlViewModel : ObservableObject, IGraphManagerModel, IProgress<double>
9+
public sealed class GraphControlViewModel : ObservableObject, IGraphModel, IProgress<double>
1010
{
1111
private IThreadingService ThreadingService { get; } = Ioc.Default.GetRequiredService<IThreadingService>();
1212

SecureFolderFS.Backend/ViewModels/Dashboard/Widgets/BaseWidgetViewModel.cs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using CommunityToolkit.Mvvm.ComponentModel;
1+
using CommunityToolkit.Mvvm.ComponentModel;
72

83
namespace SecureFolderFS.Backend.ViewModels.Dashboard.Widgets
94
{

SecureFolderFS.Backend/ViewModels/Dashboard/Widgets/GraphsWidgetViewModel.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public GraphsWidgetViewModel()
1717
WriteGraphViewModel = new();
1818
VaultIoSpeedReporterModel = new(ReadGraphViewModel, WriteGraphViewModel)
1919
{
20-
ReadGraphManagerModel = ReadGraphViewModel,
21-
WriteGraphManagerModel = WriteGraphViewModel
20+
ReadGraphModel = ReadGraphViewModel,
21+
WriteGraphModel = WriteGraphViewModel
2222
};
2323
}
2424

Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
using CommunityToolkit.Mvvm.Messaging;
2-
using SecureFolderFS.Backend.Messages;
32
using SecureFolderFS.Backend.ViewModels.Pages.VaultWizard;
4-
using System.Windows.Input;
3+
using CommunityToolkit.Mvvm.Input;
4+
using SecureFolderFS.Backend.Messages;
5+
using SecureFolderFS.Backend.Utils;
56

67
namespace SecureFolderFS.Backend.ViewModels.Dialogs
78
{
8-
public sealed class VaultWizardDialogViewModel : BaseDialogViewModel
9+
public sealed class VaultWizardDialogViewModel : BaseDialogViewModel, IRecipient<VaultWizardNavigationRequestedMessage>
910
{
1011
public IMessenger Messenger { get; }
1112

13+
public BaseVaultWizardPageViewModel? CurrentPageViewModel { get; set; }
14+
1215
public VaultViewModel? VaultViewModel { get; set; }
1316

14-
public new ICommand? PrimaryButtonClickCommand { get; set; }
17+
public VaultWizardDialogViewModel()
18+
{
19+
Messenger = new WeakReferenceMessenger();
20+
Messenger.Register(this);
21+
22+
PrimaryButtonClickCommand = new AsyncRelayCommand<IEventDispatchFlag?>(PrimaryButtonClick);
23+
SecondaryButtonClickCommand = new AsyncRelayCommand<IEventDispatchFlag?>(SecondaryButtonClick);
24+
}
1525

16-
public new ICommand? SecondaryButtonClickCommand { get; set; }
26+
private Task PrimaryButtonClick(IEventDispatchFlag? flag)
27+
{
28+
return CurrentPageViewModel?.PrimaryButtonClick(flag) ?? Task.CompletedTask;
29+
}
1730

18-
public VaultWizardDialogViewModel()
31+
private Task SecondaryButtonClick(IEventDispatchFlag? flag)
1932
{
20-
this.Messenger = new WeakReferenceMessenger();
33+
return CurrentPageViewModel?.SecondaryButtonClick(flag) ?? Task.CompletedTask;
2134
}
2235

23-
public void StartNavigation()
36+
void IRecipient<VaultWizardNavigationRequestedMessage>.Receive(VaultWizardNavigationRequestedMessage message)
2437
{
25-
Messenger.Send(new VaultWizardNavigationRequestedMessage(new VaultWizardMainPageViewModel(Messenger, this)));
38+
CurrentPageViewModel = message.Value;
2639
}
2740
}
2841
}

SecureFolderFS.Backend/ViewModels/Pages/DashboardPages/VaultMainDashboardPageViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public VaultMainDashboardPageViewModel(IMessenger messenger, VaultViewModel vaul
4343

4444
private async Task ShowInFileExplorer()
4545
{
46-
if (VaultViewModel.VaultInstance != null)
46+
if (VaultViewModel.VaultInstance is not null)
4747
{
4848
await FileExplorerService.OpenPathInFileExplorerAsync(VaultViewModel.VaultInstance.SecureFolderFSInstance.MountLocation);
4949
}

SecureFolderFS.Backend/ViewModels/Pages/VaultDashboardPageViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void StartNavigation()
5151
{
5252
Messenger.Send(new DashboardNavigationRequestedMessage(CurrentPage?.VaultDashboardPageType ?? VaultDashboardPageType.MainDashboardPage, VaultViewModel, CurrentPage)
5353
{
54-
Transition = CurrentPage == null ? new ContinuumTransitionModel() : new SuppressTransitionModel()
54+
Transition = CurrentPage is null ? new ContinuumTransitionModel() : new SuppressTransitionModel()
5555
});
5656
}
5757

SecureFolderFS.Backend/ViewModels/Pages/VaultLoginPageViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public VaultLoginPageViewModel(VaultViewModel vaultModel)
3030

3131
private void UnlockVault(DisposablePassword? password)
3232
{
33-
if (password == null || password.Length == 0)
33+
if (password is null || password.Length == 0)
3434
{
3535
return;
3636
}

SecureFolderFS.Backend/ViewModels/Pages/VaultWizard/AddExistingVaultPageViewModel.cs

+9-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace SecureFolderFS.Backend.ViewModels.Pages.VaultWizard
1212
{
13-
public sealed class AddExistingVaultPageViewModel : BaseVaultWizardPageViewModel
13+
public sealed class AddExistingVaultPageViewModel : BaseVaultWizardPageViewModel // TODO: Refactor
1414
{
1515
private IFileExplorerService FileExplorerService { get; } = Ioc.Default.GetRequiredService<IFileExplorerService>();
1616

@@ -21,9 +21,7 @@ public string? PathSourceText
2121
set
2222
{
2323
if (SetProperty(ref _PathSourceText, value))
24-
{
2524
DialogViewModel.PrimaryButtonEnabled = CheckAvailability(value);
26-
}
2725
}
2826
}
2927

@@ -32,16 +30,18 @@ public string? PathSourceText
3230
public AddExistingVaultPageViewModel(IMessenger messenger, VaultWizardDialogViewModel dialogViewModel)
3331
: base(messenger, dialogViewModel)
3432
{
35-
DialogViewModel.PrimaryButtonClickCommand = new RelayCommand<IHandledFlag?>(PrimaryButtonClick);
33+
DialogViewModel.PrimaryButtonEnabled = false;
3634
BrowseForFolderCommand = new AsyncRelayCommand(BrowseForFolder);
3735
}
3836

39-
private void PrimaryButtonClick(IHandledFlag? e)
37+
public override Task PrimaryButtonClick(IEventDispatchFlag? flag)
4038
{
41-
e?.Handle();
42-
DialogViewModel.VaultViewModel = new(new(), Path.GetDirectoryName(PathSourceText!)!);
39+
flag?.NoForwarding();
4340

41+
DialogViewModel.VaultViewModel = new(new(), Path.GetDirectoryName(PathSourceText!)!);
4442
Messenger.Send(new VaultWizardNavigationRequestedMessage(new VaultWizardFinishPageViewModel(Messenger, DialogViewModel)));
43+
44+
return Task.CompletedTask;
4545
}
4646

4747
private async Task BrowseForFolder()
@@ -68,10 +68,8 @@ private bool CheckAvailability(string? path)
6868
var rawVaultConfiguration = RawVaultConfiguration.Load(fileStream);
6969
return VaultVersion.IsVersionSupported((VaultVersion)rawVaultConfiguration);
7070
}
71-
else
72-
{
73-
return false;
74-
}
71+
72+
return false;
7573
}
7674
}
7775
}

SecureFolderFS.Backend/ViewModels/Pages/VaultWizard/BaseVaultWizardPageViewModel.cs

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
using CommunityToolkit.Mvvm.ComponentModel;
22
using CommunityToolkit.Mvvm.Messaging;
3+
using SecureFolderFS.Backend.Utils;
34
using SecureFolderFS.Backend.ViewModels.Dialogs;
45

56
namespace SecureFolderFS.Backend.ViewModels.Pages.VaultWizard
67
{
78
public abstract class BaseVaultWizardPageViewModel : ObservableObject, IDisposable
89
{
9-
public VaultWizardDialogViewModel DialogViewModel { get; }
10-
1110
public IMessenger Messenger { get; }
1211

12+
protected BaseVaultWizardPageViewModel? NextViewModel { get; set; }
13+
14+
public VaultWizardDialogViewModel DialogViewModel { get; }
15+
1316
public bool CanGoBack { get; protected init; }
1417

1518
protected BaseVaultWizardPageViewModel(IMessenger messenger, VaultWizardDialogViewModel dialogViewModel)
1619
{
17-
this.Messenger = messenger;
18-
this.DialogViewModel = dialogViewModel;
19-
this.CanGoBack = true;
20+
Messenger = messenger;
21+
DialogViewModel = dialogViewModel;
22+
CanGoBack = true;
2023
}
2124

22-
public virtual void ReattachCommands() { }
25+
public virtual void ReturnToViewModel() { }
26+
27+
public virtual Task PrimaryButtonClick(IEventDispatchFlag? flag) => Task.CompletedTask;
28+
29+
public virtual Task SecondaryButtonClick(IEventDispatchFlag? flag) => Task.CompletedTask;
2330

2431
public virtual void Dispose() { }
2532
}

SecureFolderFS.Backend/ViewModels/Pages/VaultWizard/ChooseEncryptionPageViewModel.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ public int SelectedFileNameEncryptionIndex
2929
public ChooseEncryptionPageViewModel(IVaultCreationRoutineStep9 step9, IMessenger messenger, VaultWizardDialogViewModel dialogViewModel)
3030
: base(messenger, dialogViewModel)
3131
{
32-
this._step9 = step9;
33-
base.CanGoBack = false;
32+
_step9 = step9;
33+
CanGoBack = false;
3434

3535
DialogViewModel.PrimaryButtonEnabled = true;
36-
DialogViewModel.PrimaryButtonClickCommand = new RelayCommand<IHandledFlag?>(PrimaryButtonClick);
36+
DialogViewModel.SecondaryButtonText = null; // Don't show the option to cancel the dialog
3737
}
3838

39-
private void PrimaryButtonClick(IHandledFlag? e)
39+
public override Task PrimaryButtonClick(IEventDispatchFlag? flag)
4040
{
41-
e?.Handle();
41+
flag?.NoForwarding();
4242

4343
var contentCipher = SelectedDataEncryptionIndex switch
4444
{
@@ -61,6 +61,8 @@ private void PrimaryButtonClick(IHandledFlag? e)
6161
.Deploy();
6262

6363
Messenger.Send(new VaultWizardNavigationRequestedMessage(new VaultWizardFinishPageViewModel(Messenger, DialogViewModel)));
64+
65+
return Task.CompletedTask;
6466
}
6567

6668
public override void Dispose()

0 commit comments

Comments
 (0)