/
AddCommandPlatformEffect.cs
293 lines (241 loc) · 9.11 KB
/
AddCommandPlatformEffect.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
using System.Threading.Tasks;
using System.Windows.Input;
using AiForms.Effects;
using AiForms.Effects.iOS;
using CoreGraphics;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using AudioToolbox;
using System;
using System.Linq;
[assembly: ResolutionGroupName("AiForms")]
[assembly: ExportEffect(typeof(AddCommandPlatformEffect), nameof(AddCommand))]
namespace AiForms.Effects.iOS
{
public class AddCommandPlatformEffect : PlatformEffect
{
public static uint PlaySoundNo = 1306;
private static Type[] ExceptDisableEffectTargetType = {
typeof(Button),
typeof(Picker),
typeof(Slider),
typeof(Switch),
};
private ICommand _command;
private object _commandParameter;
private ICommand _longCommand;
private object _longCommandParameter;
private UITapGestureRecognizer _tapGesture;
private UILongPressGestureRecognizer _longTapGesture;
private bool _enableSound;
private UIView _view;
private UIView _layer;
private double _alpha;
private SystemSound _clickSound;
private bool _syncCanExecute;
private bool _isDisableEffectTarget;
private readonly float _disabledAlpha = 0.3f;
protected override void OnAttached()
{
_view = Control ?? Container;
_tapGesture = new UITapGestureRecognizer(async (obj) => {
if (_command == null)
return;
if (!_command.CanExecute(_commandParameter))
return;
await TapAnimation(0.3, _alpha, 0);
if (_enableSound)
PlayClickSound();
_command.Execute(_commandParameter ?? Element);
});
_view.UserInteractionEnabled = true;
_view.AddGestureRecognizer(_tapGesture);
_isDisableEffectTarget = !ExceptDisableEffectTargetType.Any(x => x == Element.GetType());
UpdateSyncCanExecute();
UpdateCommandParameter();
UpdateLongCommandParameter();
UpdateEffectColor();
UpdateEnableSound();
}
protected override void OnDetached()
{
_view.RemoveGestureRecognizer(_tapGesture);
_tapGesture.Dispose();
if (_longTapGesture != null) {
_view.RemoveGestureRecognizer(_longTapGesture);
_longTapGesture.Dispose();
}
if (_layer != null) {
_layer.Dispose();
_layer = null;
}
if (_command != null) {
_command.CanExecuteChanged -= CommandCanExecuteChanged;
}
if (_longCommand != null) {
_longCommand.CanExecuteChanged -= CommandCanExecuteChanged;
}
_clickSound?.Dispose();
}
protected override void OnElementPropertyChanged(System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(e);
if (e.PropertyName == AddCommand.CommandProperty.PropertyName) {
UpdateCommand();
}
else if (e.PropertyName == AddCommand.CommandParameterProperty.PropertyName) {
UpdateCommandParameter();
}
else if (e.PropertyName == AddCommand.EffectColorProperty.PropertyName) {
UpdateEffectColor();
}
else if (e.PropertyName == AddCommand.LongCommandProperty.PropertyName) {
UpdateLongCommand();
}
else if (e.PropertyName == AddCommand.LongCommandParameterProperty.PropertyName) {
UpdateLongCommandParameter();
}
else if (e.PropertyName == AddCommand.EnableSoundProperty.PropertyName) {
UpdateEnableSound();
}
else if (e.PropertyName == AddCommand.SyncCanExecuteProperty.PropertyName) {
UpdateSyncCanExecute();
}
}
void UpdateSyncCanExecute()
{
_syncCanExecute = AddCommand.GetSyncCanExecute(Element);
UpdateCommand();
UpdateLongCommand();
}
void UpdateCommand()
{
if (_command != null) {
_command.CanExecuteChanged -= CommandCanExecuteChanged;
}
_command = AddCommand.GetCommand(Element);
if (_command != null && _syncCanExecute) {
_command.CanExecuteChanged += CommandCanExecuteChanged;
CommandCanExecuteChanged(_command, System.EventArgs.Empty);
}
}
void CommandCanExecuteChanged(object sender, System.EventArgs e)
{
var forms = Element as View;
if (forms == null)
return;
if (JudgeDisabled()) {
//Entrust the process of disabled to Forms
forms.IsEnabled = false;
if (_isDisableEffectTarget) {
forms.FadeTo(_disabledAlpha);
}
}
else {
forms.IsEnabled = true;
if (_isDisableEffectTarget) {
forms.FadeTo(1f);
}
}
}
bool JudgeDisabled()
{
if (_command != null && _longCommand == null) {
return !_command.CanExecute(_commandParameter);
}
else if (_command == null && _longCommand != null) {
return !_longCommand.CanExecute(_longCommandParameter);
}
else if (_command == null && _longCommand == null) {
return false;
}
else {
// only when both Command and LongCommand cannot execute,do disabled.
return !_command.CanExecute(_commandParameter) && !_longCommand.CanExecute(_longCommandParameter);
}
}
void UpdateCommandParameter()
{
_commandParameter = AddCommand.GetCommandParameter(Element);
}
void UpdateLongCommand()
{
if (_longCommand != null) {
_longCommand.CanExecuteChanged -= CommandCanExecuteChanged;
}
_longCommand = AddCommand.GetLongCommand(Element);
if (_longTapGesture != null) {
_view.RemoveGestureRecognizer(_longTapGesture);
_longTapGesture.Dispose();
}
if (_longCommand == null) {
return;
}
if (_syncCanExecute) {
_longCommand.CanExecuteChanged += CommandCanExecuteChanged;
CommandCanExecuteChanged(_longCommand, System.EventArgs.Empty);
}
_longTapGesture = new UILongPressGestureRecognizer(async (obj) => {
if (!_longCommand.CanExecute(_longCommandParameter)) {
return;
}
if (obj.State == UIGestureRecognizerState.Began) {
if (_enableSound)
PlayClickSound();
_longCommand?.Execute(_longCommandParameter ?? Element);
await TapAnimation(0.5, 0, _alpha, false);
}
else if (obj.State == UIGestureRecognizerState.Ended ||
obj.State == UIGestureRecognizerState.Cancelled ||
obj.State == UIGestureRecognizerState.Failed) {
await TapAnimation(0.5, _alpha, 0);
}
});
_view.AddGestureRecognizer(_longTapGesture);
}
void UpdateLongCommandParameter()
{
_longCommandParameter = AddCommand.GetLongCommandParameter(Element);
}
void UpdateEffectColor()
{
if (_layer != null) {
_layer.Dispose();
_layer = null;
}
var color = AddCommand.GetEffectColor(Element);
if (color == Xamarin.Forms.Color.Default) {
return;
}
_alpha = color.A < 1.0 ? 1 : 0.3;
_layer = new UIView();
_layer.BackgroundColor = color.ToUIColor();
}
void UpdateEnableSound()
{
_enableSound = AddCommand.GetEnableSound(Element);
}
async Task TapAnimation(double duration, double start = 1, double end = 0, bool remove = true)
{
if (_layer != null) {
_layer.Frame = new CGRect(0, 0, Container.Bounds.Width, Container.Bounds.Height);
Container.AddSubview(_layer);
Container.BringSubviewToFront(_layer);
_layer.Alpha = (float)start;
await UIView.AnimateAsync(duration, () => {
_layer.Alpha = (float)end;
});
if (remove) {
_layer.RemoveFromSuperview();
}
}
}
void PlayClickSound()
{
if (_clickSound == null)
_clickSound = new SystemSound(PlaySoundNo);
_clickSound.PlaySystemSoundAsync();
}
}
}