-
Notifications
You must be signed in to change notification settings - Fork 0
/
PinView.xaml.cs
398 lines (330 loc) · 13.7 KB
/
PinView.xaml.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
using System;
using System.Windows.Input;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace SkorXam.Pin
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PinView : ContentView
{
public PinView()
{
InitializeComponent();
}
public double Spacing
{
get => (double) GetValue(SpacingProperty);
set => SetValue(SpacingProperty, value);
}
public static readonly BindableProperty SpacingProperty =
BindableProperty.Create(nameof(Spacing), typeof(double), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: 16d);
public double DotSpacing
{
get => (double) GetValue(DotSpacingProperty);
set => SetValue(DotSpacingProperty, value);
}
public static readonly BindableProperty DotSpacingProperty =
BindableProperty.Create(nameof(DotSpacing), typeof(double), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: 4d);
public int PinLength
{
get => (int) GetValue(PinLengthProperty);
set => SetValue(PinLengthProperty, value);
}
public static readonly BindableProperty PinLengthProperty =
BindableProperty.Create(nameof(PinLength), typeof(int), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: 4);
public Color DotColor
{
get => (Color) GetValue(DotColorProperty);
set => SetValue(DotColorProperty, value);
}
public static readonly BindableProperty DotColorProperty =
BindableProperty.Create(nameof(DotColor), typeof(Color), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: Color.Red, propertyChanged: OnPinAppearanceChanged);
public double DotSize
{
get => (double) GetValue(DotSizeProperty);
set => SetValue(DotSizeProperty, value);
}
public static readonly BindableProperty DotSizeProperty =
BindableProperty.Create(nameof(DotSize), typeof(double), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: 24d);
public Color DotEmptyColor
{
get => (Color) GetValue(DotEmptyColorProperty);
set => SetValue(DotEmptyColorProperty, value);
}
public static readonly BindableProperty DotEmptyColorProperty =
BindableProperty.Create(nameof(DotEmptyColor), typeof(Color), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: Color.White, propertyChanged: OnPinAppearanceChanged);
public Color DotBorderColor
{
get => (Color) GetValue(DotBorderColorProperty);
set => SetValue(DotBorderColorProperty, value);
}
public static readonly BindableProperty DotBorderColorProperty =
BindableProperty.Create(nameof(DotBorderColor), typeof(Color), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: Color.Red, propertyChanged: OnPinAppearanceChanged);
public ICommand PinSubmitCommand
{
get => (ICommand) GetValue(PinSubmitCommandProperty);
set => SetValue(PinSubmitCommandProperty, value);
}
public static readonly BindableProperty PinSubmitCommandProperty =
BindableProperty.Create(nameof(PinSubmitCommand), typeof(ICommand), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: null);
public event EventHandler<PinChangedEventArg> PinChanged;
public event EventHandler<PinSubmitEventArg> PinSubmit;
public string Pin
{
get => (string) GetValue(PinProperty);
set => SetValue(PinProperty, value);
}
public static readonly BindableProperty PinProperty =
BindableProperty.Create(nameof(Pin), typeof(string), typeof(PinView),
defaultBindingMode: BindingMode.TwoWay,
defaultValue: string.Empty, propertyChanged: OnPinChanged);
public double ButtonSize
{
get => (double) GetValue(ButtonSizeProperty);
set => SetValue(ButtonSizeProperty, value);
}
public static readonly BindableProperty ButtonSizeProperty =
BindableProperty.Create(nameof(ButtonSize), typeof(double), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: 64d);
public double ButtonVerticalSpacing
{
get => (double) GetValue(ButtonVerticalSpacingProperty);
set => SetValue(ButtonVerticalSpacingProperty, value);
}
public static readonly BindableProperty ButtonVerticalSpacingProperty =
BindableProperty.Create(nameof(ButtonVerticalSpacing), typeof(double), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: 6d);
public double ButtonHorizontalSpacing
{
get => (double) GetValue(ButtonHorizontalSpacingProperty);
set => SetValue(ButtonHorizontalSpacingProperty, value);
}
public static readonly BindableProperty ButtonHorizontalSpacingProperty =
BindableProperty.Create(nameof(ButtonHorizontalSpacing), typeof(double), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: 6d);
public double ButtonFontsize
{
get => (double) GetValue(ButtonFontsizeProperty);
set => SetValue(ButtonFontsizeProperty, value);
}
public static readonly BindableProperty ButtonFontsizeProperty =
BindableProperty.Create(nameof(ButtonFontsize), typeof(double), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: 32d);
public int ButtonCornerRadius
{
get => (int) GetValue(ButtonCornerRadiusProperty);
set => SetValue(ButtonCornerRadiusProperty, value);
}
public static readonly BindableProperty ButtonCornerRadiusProperty =
BindableProperty.Create(nameof(ButtonCornerRadius), typeof(int), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: 32);
public Color ButtonColor
{
get => (Color) GetValue(ButtonColorProperty);
set => SetValue(ButtonColorProperty, value);
}
public static readonly BindableProperty ButtonColorProperty =
BindableProperty.Create(nameof(ButtonColor), typeof(Color), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: Color.Red);
public Color ButtonTextColor
{
get => (Color) GetValue(ButtonTextColorProperty);
set => SetValue(ButtonTextColorProperty, value);
}
public static readonly BindableProperty ButtonTextColorProperty =
BindableProperty.Create(nameof(ButtonTextColor), typeof(Color), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: Color.White);
public ImageSource ClearButtonImageSource
{
get => (ImageSource) GetValue(ClearButtonImageSourceProperty);
set => SetValue(ClearButtonImageSourceProperty, value);
}
public static readonly BindableProperty ClearButtonImageSourceProperty =
BindableProperty.Create(nameof(ClearButtonImageSource), typeof(ImageSource), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: ImageSource.FromResource("SkorXam.Pin.Extended.reset.png"));
public ImageSource DeleteButtonImageSource
{
get => (ImageSource) GetValue(DeleteButtonImageSourceProperty);
set => SetValue(DeleteButtonImageSourceProperty, value);
}
public static readonly BindableProperty DeleteButtonImageSourceProperty =
BindableProperty.Create(nameof(DeleteButtonImageSource), typeof(ImageSource), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: ImageSource.FromResource("SkorXam.Pin.Extended.backspace.png"));
public bool DotHasShadow
{
get => (bool) GetValue(DotHasShadowProperty);
set => SetValue(DotHasShadowProperty, value);
}
public static readonly BindableProperty DotHasShadowProperty =
BindableProperty.Create(nameof(DotHasShadow), typeof(bool), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: false);
public double DotCornerRadius
{
get => (double) GetValue(DotCornerRadiusProperty);
set => SetValue(DotCornerRadiusProperty, value);
}
public static readonly BindableProperty DotCornerRadiusProperty =
BindableProperty.Create(nameof(DotCornerRadius), typeof(double), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: 12d);
public double ButtonOpacity
{
get => (double) GetValue(ButtonOpacityProperty);
set => SetValue(ButtonOpacityProperty, value);
}
public static readonly BindableProperty ButtonOpacityProperty =
BindableProperty.Create(nameof(ButtonOpacity), typeof(double), typeof(PinView),
defaultBindingMode: BindingMode.OneWay,
defaultValue: 1d);
#region Methods
private static void OnPinChanged(BindableObject bindable, object oldValue, object newValue)
{
var pinView = (bindable as PinView);
pinView.RenderPin(newValue?.ToString() ?? string.Empty);
pinView?.PinChanged?.Invoke(pinView, new PinChangedEventArg(pinView, (string) newValue));
pinView?.TrySubmit();
}
private static void OnPinAppearanceChanged(BindableObject bindable, object oldValue, object newValue)
{
if (oldValue == newValue) return;
var pinView = (bindable as PinView);
pinView.RenderPin(pinView.Pin);
}
public void TrySubmit()
{
if (Pin.Length == PinLength)
{
if (this.PinSubmitCommand?.CanExecute(null) ?? false)
{
this.PinSubmitCommand?.Execute(Pin);
}
this.PinSubmit?.Invoke(this, new PinSubmitEventArg(this, Pin));
}
}
private void RenderPin(string newValue)
{
if (stackLayout == null)
return;
int newValueLength = newValue?.Length ?? 0;
for (int i = 0; i < PinLength; i++)
Fill(((stackLayout.Children[i]) as Frame), i < newValueLength);
}
private void Fill(Frame frame, bool isFill)
{
if (isFill)
frame.BackgroundColor = DotColor;
else
frame.BackgroundColor = DotEmptyColor;
}
private void Button1_Clicked(object sender, EventArgs e)
{
if (Pin.Length < PinLength)
Pin += "1";
}
private void Button2_Clicked(object sender, EventArgs e)
{
if (Pin.Length < PinLength)
Pin += "2";
}
private void Button3_Clicked(object sender, EventArgs e)
{
if (Pin.Length < PinLength)
Pin += "3";
}
private void Button4_Clicked(object sender, EventArgs e)
{
if (Pin.Length < PinLength)
Pin += "4";
}
private void Button5_Clicked(object sender, EventArgs e)
{
if (Pin.Length < PinLength)
Pin += "5";
}
private void Button6_Clicked(object sender, EventArgs e)
{
if (Pin.Length < PinLength)
Pin += "6";
}
private void Button7_Clicked(object sender, EventArgs e)
{
if (Pin.Length < PinLength)
Pin += "7";
}
private void Button8_Clicked(object sender, EventArgs e)
{
if (Pin.Length < PinLength)
Pin += "8";
}
private void Button9_Clicked(object sender, EventArgs e)
{
if (Pin.Length < PinLength)
Pin += "9";
}
private void Button0_Clicked(object sender, EventArgs e)
{
if (Pin.Length < PinLength)
Pin += "0";
}
private void Delete_Clicked(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(Pin))
return;
if (Pin.Length > 0)
{
Pin = Pin.Substring(0, Pin.Length - 1);
}
}
private void Clear_Clicked(object sender, EventArgs e)
{
Pin = string.Empty;
}
#endregion
}
public class PinChangedEventArg
{
public PinChangedEventArg(object sender, string pin)
{
Source = sender;
Pin = pin;
}
public object Source { get; private set; }
public string Pin { get; private set; }
}
public class PinSubmitEventArg
{
public PinSubmitEventArg(object sender, string pin)
{
Source = sender;
Pin = pin;
}
public object Source { get; private set; }
public string Pin { get; private set; }
}
}