Skip to content

Commit a2a483f

Browse files
authored
Use CommunityToolkit.Mvvm with Buttons view in demo (#3557)
* OrClickMe Use CommunityToolkit.Mvvm with Demo Buttons OrClickMe * Floating action button with progress Use CommunityToolkit.Mvvm with Demo Button Save * Dismiss Use CommunityToolkit.Mvvm with Demo Button Dimiss
1 parent a7157ae commit a2a483f

File tree

1 file changed

+64
-94
lines changed

1 file changed

+64
-94
lines changed
Lines changed: 64 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,45 @@
11
using System.Diagnostics;
22
using System.Windows.Threading;
3+
using CommunityToolkit.Mvvm.ComponentModel;
4+
using CommunityToolkit.Mvvm.Input;
35

46
namespace MaterialDesignDemo.Domain;
57

6-
public class ButtonsViewModel : ViewModelBase
7-
{
8-
private bool _showDismissButton;
9-
private double _dismissButtonProgress;
10-
private string? _demoRestartCountdownText;
11-
private int _orClickMeCount;
8+
public sealed partial class ButtonsViewModel : ObservableObject
9+
{
10+
private bool _dismissRequested = false;
1211

1312
public ButtonsViewModel()
1413
{
1514
FloatingActionDemoCommand = new AnotherCommandImplementation(FloatingActionDemo);
1615

1716
var autoStartingActionCountdownStart = DateTime.Now;
1817
var demoRestartCountdownComplete = DateTime.Now;
19-
var dismissRequested = false;
20-
DismissCommand = new AnotherCommandImplementation(_ => dismissRequested = true);
18+
2119
ShowDismissButton = true;
2220

2321
#region DISMISS button demo control
2422
//just some demo code for the DISMISS button...it's up to you to set
2523
//up the progress on the button as it would be with a progress bar.
2624
//and then hide the button, do whatever action you want to do
27-
new DispatcherTimer(
25+
_ = new DispatcherTimer(
2826
TimeSpan.FromMilliseconds(100),
2927
DispatcherPriority.Normal,
3028
new EventHandler((o, e) =>
3129
{
32-
if (dismissRequested)
30+
if (_dismissRequested)
3331
{
3432
ShowDismissButton = false;
35-
dismissRequested = false;
33+
_dismissRequested = false;
3634
demoRestartCountdownComplete = DateTime.Now.AddSeconds(3);
3735
DismissButtonProgress = 0;
3836
}
3937

4038
if (ShowDismissButton)
4139
{
42-
var totalDuration = autoStartingActionCountdownStart.AddSeconds(5).Ticks - autoStartingActionCountdownStart.Ticks;
43-
var currentDuration = DateTime.Now.Ticks - autoStartingActionCountdownStart.Ticks;
44-
var autoCountdownPercentComplete = 100.0 / totalDuration * currentDuration;
40+
long totalDuration = autoStartingActionCountdownStart.AddSeconds(5).Ticks - autoStartingActionCountdownStart.Ticks;
41+
long currentDuration = DateTime.Now.Ticks - autoStartingActionCountdownStart.Ticks;
42+
double autoCountdownPercentComplete = 100.0 / totalDuration * currentDuration;
4543
DismissButtonProgress = autoCountdownPercentComplete;
4644

4745
if (DismissButtonProgress >= 100)
@@ -64,47 +62,7 @@ public ButtonsViewModel()
6462
}), Dispatcher.CurrentDispatcher);
6563
#endregion
6664

67-
IncrementOrClickMeCountCommand = new AnotherCommandImplementation(_ => OrClickMeCount += 1);
6865
OrClickMeCount = 0;
69-
70-
//just some demo code for the SAVE button
71-
SaveCommand = new AnotherCommandImplementation(_ =>
72-
{
73-
if (IsSaveComplete == true)
74-
{
75-
IsSaveComplete = false;
76-
return;
77-
}
78-
79-
if (SaveProgress != 0) return;
80-
81-
var started = DateTime.Now;
82-
IsSaving = true;
83-
84-
new DispatcherTimer(
85-
TimeSpan.FromMilliseconds(50),
86-
DispatcherPriority.Normal,
87-
new EventHandler((o, e) =>
88-
{
89-
var totalDuration = started.AddSeconds(3).Ticks - started.Ticks;
90-
var currentProgress = DateTime.Now.Ticks - started.Ticks;
91-
var currentProgressPercent = 100.0 / totalDuration * currentProgress;
92-
93-
SaveProgress = currentProgressPercent;
94-
95-
if (SaveProgress >= 100)
96-
{
97-
IsSaveComplete = true;
98-
IsSaving = false;
99-
SaveProgress = 0;
100-
if (o is DispatcherTimer timer)
101-
{
102-
timer.Stop();
103-
}
104-
}
105-
106-
}), Dispatcher.CurrentDispatcher);
107-
});
10866
}
10967

11068
public ICommand FloatingActionDemoCommand { get; }
@@ -113,26 +71,17 @@ private static void FloatingActionDemo(object? o)
11371
=> Debug.WriteLine($"Floating action button command. - {o ?? "NULL"}");
11472

11573
#region Dismiss button demo
74+
[RelayCommand]
75+
private void Dismiss() => _dismissRequested = true;
11676

117-
public ICommand DismissCommand { get; }
118-
119-
public bool ShowDismissButton
120-
{
121-
get => _showDismissButton;
122-
set => SetProperty(ref _showDismissButton, value);
123-
}
77+
[ObservableProperty]
78+
private bool _showDismissButton;
12479

125-
public double DismissButtonProgress
126-
{
127-
get => _dismissButtonProgress;
128-
set => SetProperty(ref _dismissButtonProgress, value);
129-
}
80+
[ObservableProperty]
81+
private double _dismissButtonProgress;
13082

131-
public string? DemoRestartCountdownText
132-
{
133-
get => _demoRestartCountdownText;
134-
private set => SetProperty(ref _demoRestartCountdownText, value);
135-
}
83+
[ObservableProperty]
84+
private string? _demoRestartCountdownText;
13685

13786
private void UpdateDemoRestartCountdownText(DateTime endTime, out bool isComplete)
13887
{
@@ -145,39 +94,60 @@ private void UpdateDemoRestartCountdownText(DateTime endTime, out bool isComplet
14594
#endregion
14695

14796
#region OrClickMe Demo
148-
public int OrClickMeCount
149-
{
150-
get => _orClickMeCount;
151-
private set => SetProperty(ref _orClickMeCount, value);
152-
}
153-
public ICommand IncrementOrClickMeCountCommand { get; }
97+
[RelayCommand]
98+
private void IncrementOrClickMeCount() => OrClickMeCount += 1;
15499

100+
[ObservableProperty]
101+
private int _orClickMeCount;
155102
#endregion
156103

157104
#region floating Save button demo
105+
[RelayCommand]
106+
private void Save()
107+
{
108+
if (IsSaveComplete == true)
109+
{
110+
IsSaveComplete = false;
111+
return;
112+
}
158113

159-
public ICommand SaveCommand { get; }
114+
if (SaveProgress != 0) return;
160115

161-
private bool _isSaving;
162-
public bool IsSaving
163-
{
164-
get => _isSaving;
165-
private set => SetProperty(ref _isSaving, value);
116+
var started = DateTime.Now;
117+
IsSaving = true;
118+
119+
_ = new DispatcherTimer(
120+
TimeSpan.FromMilliseconds(50),
121+
DispatcherPriority.Normal,
122+
new EventHandler((o, e) =>
123+
{
124+
long totalDuration = started.AddSeconds(3).Ticks - started.Ticks;
125+
long currentProgress = DateTime.Now.Ticks - started.Ticks;
126+
double currentProgressPercent = 100.0 / totalDuration * currentProgress;
127+
128+
SaveProgress = currentProgressPercent;
129+
130+
if (SaveProgress >= 100)
131+
{
132+
IsSaveComplete = true;
133+
IsSaving = false;
134+
SaveProgress = 0;
135+
if (o is DispatcherTimer timer)
136+
{
137+
timer.Stop();
138+
}
139+
}
140+
141+
}), Dispatcher.CurrentDispatcher);
166142
}
167143

144+
[ObservableProperty]
145+
private bool _isSaving;
146+
147+
[ObservableProperty]
168148
private bool _isSaveComplete;
169-
public bool IsSaveComplete
170-
{
171-
get => _isSaveComplete;
172-
private set => SetProperty(ref _isSaveComplete, value);
173-
}
174149

150+
[ObservableProperty]
175151
private double _saveProgress;
176-
public double SaveProgress
177-
{
178-
get => _saveProgress;
179-
private set => SetProperty(ref _saveProgress, value);
180-
}
181-
182152
#endregion
183153
}

0 commit comments

Comments
 (0)