Skip to content

Commit a82a3d8

Browse files
author
Rudy Huyn
committed
update
1 parent d0fa404 commit a82a3d8

16 files changed

+400
-1384
lines changed

src/Calculator.ViewModels/ApplicationViewModel.cs

Lines changed: 52 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
using System.Collections.Generic;
55
using System.ComponentModel;
66
using System.Diagnostics;
7-
using System.Runtime.CompilerServices;
87
using System.Threading.Tasks;
98
using System.Windows.Input;
109

10+
using CommunityToolkit.Mvvm.ComponentModel;
11+
using CommunityToolkit.Mvvm.Input;
12+
1113
using Windows.Foundation;
1214
using Windows.Storage;
1315
using Windows.UI.Core;
@@ -20,24 +22,37 @@
2022

2123
namespace CalculatorApp.ViewModel
2224
{
23-
public class ApplicationViewModel : INotifyPropertyChanged
25+
public partial class ApplicationViewModel : ObservableObject
2426
{
25-
private StandardCalculatorViewModel _calcVm;
26-
private DateCalculatorViewModel _dateVm;
27-
private GraphingCalculatorViewModel _graphVm;
28-
private UnitConverterViewModel _unitVm;
27+
[ObservableProperty]
28+
private StandardCalculatorViewModel _calculatorViewModel;
29+
30+
[ObservableProperty]
31+
private DateCalculatorViewModel _dateCalcViewModel;
32+
33+
[ObservableProperty]
34+
private GraphingCalculatorViewModel _graphingCalcViewModel;
35+
36+
[ObservableProperty]
37+
private UnitConverterViewModel _converterViewModel;
38+
2939
private ViewMode _mode = ViewMode.None;
40+
41+
[ObservableProperty]
3042
private ViewMode _previousMode = ViewMode.None;
43+
3144
private bool _isAlwaysOnTop = false;
3245
private bool _displayNormalAlwaysOnTopOption;
46+
47+
[ObservableProperty]
3348
private string _categoryName;
49+
50+
[ObservableProperty]
3451
private IList<NavCategoryGroup> _categories = NavCategoryStates.CreateMenuOptions();
3552

3653
public const string WidthLocalSettingsKey = "calculatorAlwaysOnTopLastWidth";
3754
public const string HeightLocalSettingsKey = "calculatorAlwaysOnTopLastHeight";
3855

39-
public event PropertyChangedEventHandler PropertyChanged;
40-
4156
public ViewMode Mode
4257
{
4358
get => _mode;
@@ -49,14 +64,14 @@ public ViewMode Mode
4964
_mode = value;
5065
SetDisplayNormalAlwaysOnTopOption();
5166
OnModeChanged();
52-
RaisePropertyChanged();
67+
OnPropertyChanged(nameof(Mode));
5368
}
5469
}
5570
}
5671

57-
public ICommand CopyCommand => new RelayCommand(OnCopyCommand);
72+
public ICommand CopyCommand => new RelayCommand<object>(OnCopyCommand);
5873

59-
public ICommand PasteCommand => new RelayCommand(OnPasteCommand);
74+
public ICommand PasteCommand => new RelayCommand<object>(OnPasteCommand);
6075

6176
public Visibility ClearMemoryVisibility
6277
{
@@ -70,130 +85,25 @@ public ApplicationSnapshot Snapshot
7085
{
7186
var snapshot = new ApplicationSnapshot();
7287
snapshot.Mode = (int)_mode;
73-
if (_calcVm != null)
88+
if (CalculatorViewModel != null)
7489
{
75-
snapshot.StandardCalculator = _calcVm.Snapshot;
90+
snapshot.StandardCalculator = CalculatorViewModel.Snapshot;
7691
}
7792
return snapshot;
7893
}
7994
}
8095

81-
public StandardCalculatorViewModel CalculatorViewModel
82-
{
83-
get => _calcVm;
84-
set
85-
{
86-
if (_calcVm != value)
87-
{
88-
_calcVm = value;
89-
RaisePropertyChanged();
90-
}
91-
}
92-
}
93-
94-
public DateCalculatorViewModel DateCalcViewModel
95-
{
96-
get => _dateVm;
97-
set
98-
{
99-
if (_dateVm != value)
100-
{
101-
_dateVm = value;
102-
RaisePropertyChanged();
103-
}
104-
}
105-
}
106-
107-
public GraphingCalculatorViewModel GraphingCalcViewModel
108-
{
109-
get => _graphVm;
110-
set
111-
{
112-
if (_graphVm != value)
113-
{
114-
_graphVm = value;
115-
RaisePropertyChanged();
116-
}
117-
}
118-
}
119-
120-
public UnitConverterViewModel ConverterViewModel
121-
{
122-
get => _unitVm;
123-
set
124-
{
125-
if (_unitVm != value)
126-
{
127-
_unitVm = value;
128-
RaisePropertyChanged();
129-
}
130-
}
131-
}
132-
133-
public ViewMode PreviousMode
134-
{
135-
get => _previousMode;
136-
set
137-
{
138-
if (_previousMode != value)
139-
{
140-
_previousMode = value;
141-
RaisePropertyChanged();
142-
}
143-
}
144-
}
145-
14696
public bool IsAlwaysOnTop
14797
{
14898
get => _isAlwaysOnTop;
149-
private set
150-
{
151-
if (_isAlwaysOnTop != value)
152-
{
153-
_isAlwaysOnTop = value;
154-
RaisePropertyChanged();
155-
}
156-
}
99+
private set => SetProperty(ref _isAlwaysOnTop, value);
157100
}
158101

159102
// Indicates whether calculator is currently in standard mode _and_ supports CompactOverlay _and_ is not in Always-on-Top mode
160103
public bool DisplayNormalAlwaysOnTopOption
161104
{
162105
get => _displayNormalAlwaysOnTopOption;
163-
private set
164-
{
165-
if (_displayNormalAlwaysOnTopOption != value)
166-
{
167-
_displayNormalAlwaysOnTopOption = value;
168-
RaisePropertyChanged();
169-
}
170-
}
171-
}
172-
173-
public string CategoryName
174-
{
175-
get => _categoryName;
176-
set
177-
{
178-
if (_categoryName != value)
179-
{
180-
_categoryName = value;
181-
RaisePropertyChanged();
182-
}
183-
}
184-
}
185-
186-
public IList<NavCategoryGroup> Categories
187-
{
188-
get => _categories;
189-
set
190-
{
191-
if (_categories != value)
192-
{
193-
_categories = value;
194-
RaisePropertyChanged();
195-
}
196-
}
106+
private set => SetProperty(ref _displayNormalAlwaysOnTopOption, value);
197107
}
198108

199109
public async Task ToggleAlwaysOnTop(double width, double height)
@@ -206,8 +116,8 @@ public async Task ToggleAlwaysOnTop(double width, double height)
206116
settings.Values[WidthLocalSettingsKey] = width;
207117
settings.Values[HeightLocalSettingsKey] = height;
208118
bool success = await ApplicationView.GetForCurrentView().TryEnterViewModeAsync(ApplicationViewMode.Default);
209-
_calcVm.HistoryVM.AreHistoryShortcutsEnabled = success;
210-
_calcVm.IsAlwaysOnTop = !success;
119+
CalculatorViewModel.HistoryVM.AreHistoryShortcutsEnabled = success;
120+
CalculatorViewModel.IsAlwaysOnTop = !success;
211121
IsAlwaysOnTop = !success;
212122
}
213123
else
@@ -238,8 +148,8 @@ public async Task ToggleAlwaysOnTop(double width, double height)
238148
}
239149
}
240150
bool success = await ApplicationView.GetForCurrentView().TryEnterViewModeAsync(ApplicationViewMode.CompactOverlay, compactOptions);
241-
_calcVm.HistoryVM.AreHistoryShortcutsEnabled = !success;
242-
_calcVm.IsAlwaysOnTop = success;
151+
CalculatorViewModel.HistoryVM.AreHistoryShortcutsEnabled = !success;
152+
CalculatorViewModel.IsAlwaysOnTop = success;
243153
IsAlwaysOnTop = success;
244154
}
245155
SetDisplayNormalAlwaysOnTopOption();
@@ -272,7 +182,7 @@ public void RestoreFromSnapshot(ApplicationSnapshot snapshot)
272182
Mode = (ViewMode)snapshot.Mode;
273183
if (snapshot.StandardCalculator != null)
274184
{
275-
_calcVm.Snapshot = snapshot.StandardCalculator;
185+
CalculatorViewModel.Snapshot = snapshot.StandardCalculator;
276186
}
277187
}
278188

@@ -281,33 +191,33 @@ private void OnModeChanged()
281191
Debug.Assert(NavCategoryStates.IsValidViewMode(_mode));
282192
if (NavCategory.IsCalculatorViewMode(_mode))
283193
{
284-
if (_calcVm == null)
194+
if (CalculatorViewModel == null)
285195
{
286-
_calcVm = new StandardCalculatorViewModel();
196+
CalculatorViewModel = new StandardCalculatorViewModel();
287197
}
288-
_calcVm.SetCalculatorType(_mode);
198+
CalculatorViewModel.SetCalculatorType(_mode);
289199
}
290200
else if (NavCategory.IsGraphingCalculatorViewMode(_mode))
291201
{
292-
if (_graphVm == null)
202+
if (GraphingCalcViewModel == null)
293203
{
294-
_graphVm = new GraphingCalculatorViewModel();
204+
GraphingCalcViewModel = new GraphingCalculatorViewModel();
295205
}
296206
}
297207
else if (NavCategory.IsDateCalculatorViewMode(_mode))
298208
{
299-
if (_dateVm == null)
209+
if (DateCalcViewModel == null)
300210
{
301-
_dateVm = new DateCalculatorViewModel();
211+
DateCalcViewModel = new DateCalculatorViewModel();
302212
}
303213
}
304214
else if (NavCategory.IsConverterViewMode(_mode))
305215
{
306-
if (_unitVm == null)
216+
if (ConverterViewModel == null)
307217
{
308-
_unitVm = new UnitConverterViewModel();
218+
ConverterViewModel = new UnitConverterViewModel();
309219
}
310-
_unitVm.Mode = _mode;
220+
ConverterViewModel.Mode = _mode;
311221
}
312222

313223
var resProvider = AppResourceProvider.GetInstance();
@@ -319,7 +229,7 @@ private void OnModeChanged()
319229
ApplicationData.Current.LocalSettings.Values[nameof(Mode)] = NavCategoryStates.Serialize(_mode);
320230

321231
// Log ModeChange event when not first launch, log WindowCreated on first launch
322-
if (NavCategoryStates.IsValidViewMode(_previousMode))
232+
if (NavCategoryStates.IsValidViewMode(PreviousMode))
323233
{
324234
TraceLogger.GetInstance().LogModeChange(_mode);
325235
}
@@ -329,34 +239,34 @@ private void OnModeChanged()
329239
_mode,
330240
ApplicationView.GetApplicationViewIdForWindow(CoreWindow.GetForCurrentThread()));
331241
}
332-
RaisePropertyChanged(nameof(ClearMemoryVisibility));
242+
OnPropertyChanged(nameof(ClearMemoryVisibility));
333243
}
334244

335245
private void OnCopyCommand(object param)
336246
{
337247
if (NavCategory.IsConverterViewMode(_mode))
338248
{
339-
_unitVm.OnCopyCommand(param);
249+
ConverterViewModel.OnCopyCommand(param);
340250
}
341251
else if (NavCategory.IsDateCalculatorViewMode(_mode))
342252
{
343-
_dateVm.OnCopyCommand(param);
253+
DateCalcViewModel.OnCopyCommand(param);
344254
}
345255
else if (NavCategory.IsCalculatorViewMode(_mode))
346256
{
347-
_calcVm.OnCopyCommand(param);
257+
CalculatorViewModel.OnCopyCommand(param);
348258
}
349259
}
350260

351261
private void OnPasteCommand(object param)
352262
{
353263
if (NavCategory.IsConverterViewMode(_mode))
354264
{
355-
_unitVm.OnPasteCommand(param);
265+
ConverterViewModel.OnPasteCommand(param);
356266
}
357267
else if (NavCategory.IsCalculatorViewMode(_mode))
358268
{
359-
_calcVm.OnPasteCommand(param);
269+
CalculatorViewModel.OnPasteCommand(param);
360270
}
361271
}
362272

@@ -382,10 +292,5 @@ private bool TryRecoverFromNavigationModeFailure()
382292
return false;
383293
}
384294
}
385-
386-
private void RaisePropertyChanged([CallerMemberName] string propertyName = null)
387-
{
388-
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
389-
}
390295
}
391296
}

src/Calculator.ViewModels/Calculator.ViewModels.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<NoWarn>NU1201</NoWarn>
102102
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
103103
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
104+
<LangVersion>13.0</LangVersion>
104105
</PropertyGroup>
105106
<ItemGroup>
106107
<Compile Include="**\*.cs" />
@@ -110,6 +111,9 @@
110111
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
111112
<Version>6.2.14</Version>
112113
</PackageReference>
114+
<PackageReference Include="CommunityToolkit.Mvvm">
115+
<Version>8.4.2</Version>
116+
</PackageReference>
113117
</ItemGroup>
114118
<ItemGroup>
115119
<ProjectReference Include="..\CalcManager.Interop\CalcManager.Interop.vcxproj">

src/Calculator.ViewModels/Common/DelegateCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public DelegateCommand(Action<object> handler)
1515
_handler = handler ?? throw new ArgumentNullException(nameof(handler));
1616
}
1717

18+
#pragma warning disable CS0067 // Event is never used
1819
public event EventHandler CanExecuteChanged;
20+
#pragma warning restore CS0067
1921

2022
public bool CanExecute(object parameter) => true;
2123

0 commit comments

Comments
 (0)