1
1
using System . Diagnostics ;
2
2
using System . Windows . Threading ;
3
+ using CommunityToolkit . Mvvm . ComponentModel ;
4
+ using CommunityToolkit . Mvvm . Input ;
3
5
4
6
namespace MaterialDesignDemo . Domain ;
5
7
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 ;
12
11
13
12
public ButtonsViewModel ( )
14
13
{
15
14
FloatingActionDemoCommand = new AnotherCommandImplementation ( FloatingActionDemo ) ;
16
15
17
16
var autoStartingActionCountdownStart = DateTime . Now ;
18
17
var demoRestartCountdownComplete = DateTime . Now ;
19
- var dismissRequested = false ;
20
- DismissCommand = new AnotherCommandImplementation ( _ => dismissRequested = true ) ;
18
+
21
19
ShowDismissButton = true ;
22
20
23
21
#region DISMISS button demo control
24
22
//just some demo code for the DISMISS button...it's up to you to set
25
23
//up the progress on the button as it would be with a progress bar.
26
24
//and then hide the button, do whatever action you want to do
27
- new DispatcherTimer (
25
+ _ = new DispatcherTimer (
28
26
TimeSpan . FromMilliseconds ( 100 ) ,
29
27
DispatcherPriority . Normal ,
30
28
new EventHandler ( ( o , e ) =>
31
29
{
32
- if ( dismissRequested )
30
+ if ( _dismissRequested )
33
31
{
34
32
ShowDismissButton = false ;
35
- dismissRequested = false ;
33
+ _dismissRequested = false ;
36
34
demoRestartCountdownComplete = DateTime . Now . AddSeconds ( 3 ) ;
37
35
DismissButtonProgress = 0 ;
38
36
}
39
37
40
38
if ( ShowDismissButton )
41
39
{
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 ;
45
43
DismissButtonProgress = autoCountdownPercentComplete ;
46
44
47
45
if ( DismissButtonProgress >= 100 )
@@ -64,47 +62,7 @@ public ButtonsViewModel()
64
62
} ) , Dispatcher . CurrentDispatcher ) ;
65
63
#endregion
66
64
67
- IncrementOrClickMeCountCommand = new AnotherCommandImplementation ( _ => OrClickMeCount += 1 ) ;
68
65
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
- } ) ;
108
66
}
109
67
110
68
public ICommand FloatingActionDemoCommand { get ; }
@@ -113,26 +71,17 @@ private static void FloatingActionDemo(object? o)
113
71
=> Debug . WriteLine ( $ "Floating action button command. - { o ?? "NULL" } ") ;
114
72
115
73
#region Dismiss button demo
74
+ [ RelayCommand ]
75
+ private void Dismiss ( ) => _dismissRequested = true ;
116
76
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 ;
124
79
125
- public double DismissButtonProgress
126
- {
127
- get => _dismissButtonProgress ;
128
- set => SetProperty ( ref _dismissButtonProgress , value ) ;
129
- }
80
+ [ ObservableProperty ]
81
+ private double _dismissButtonProgress ;
130
82
131
- public string ? DemoRestartCountdownText
132
- {
133
- get => _demoRestartCountdownText ;
134
- private set => SetProperty ( ref _demoRestartCountdownText , value ) ;
135
- }
83
+ [ ObservableProperty ]
84
+ private string ? _demoRestartCountdownText ;
136
85
137
86
private void UpdateDemoRestartCountdownText ( DateTime endTime , out bool isComplete )
138
87
{
@@ -145,39 +94,60 @@ private void UpdateDemoRestartCountdownText(DateTime endTime, out bool isComplet
145
94
#endregion
146
95
147
96
#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 ;
154
99
100
+ [ ObservableProperty ]
101
+ private int _orClickMeCount ;
155
102
#endregion
156
103
157
104
#region floating Save button demo
105
+ [ RelayCommand ]
106
+ private void Save ( )
107
+ {
108
+ if ( IsSaveComplete == true )
109
+ {
110
+ IsSaveComplete = false ;
111
+ return ;
112
+ }
158
113
159
- public ICommand SaveCommand { get ; }
114
+ if ( SaveProgress != 0 ) return ;
160
115
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 ) ;
166
142
}
167
143
144
+ [ ObservableProperty ]
145
+ private bool _isSaving ;
146
+
147
+ [ ObservableProperty ]
168
148
private bool _isSaveComplete ;
169
- public bool IsSaveComplete
170
- {
171
- get => _isSaveComplete ;
172
- private set => SetProperty ( ref _isSaveComplete , value ) ;
173
- }
174
149
150
+ [ ObservableProperty ]
175
151
private double _saveProgress ;
176
- public double SaveProgress
177
- {
178
- get => _saveProgress ;
179
- private set => SetProperty ( ref _saveProgress , value ) ;
180
- }
181
-
182
152
#endregion
183
153
}
0 commit comments