This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathDatePickerRenderer.cs
240 lines (201 loc) · 6.31 KB
/
DatePickerRenderer.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
using System;
using System.ComponentModel;
using Android.App;
using Android.Content;
using Android.Util;
using Android.Views;
using Android.Widget;
using AColor = Android.Graphics.Color;
namespace Xamarin.Forms.Platform.Android
{
public abstract class DatePickerRendererBase<TControl> : ViewRenderer<DatePicker, TControl>, IPickerRenderer
where TControl : global::Android.Views.View
{
int _originalHintTextColor;
DatePickerDialog _dialog;
bool _disposed;
protected abstract EditText EditText { get; }
public DatePickerRendererBase(Context context) : base(context)
{
AutoPackage = false;
if (Forms.IsLollipopOrNewer)
Device.Info.PropertyChanged += DeviceInfoPropertyChanged;
}
[Obsolete("This constructor is obsolete as of version 2.5. Please use DatePickerRenderer(Context) instead.")]
[EditorBrowsable(EditorBrowsableState.Never)]
public DatePickerRendererBase()
{
AutoPackage = false;
if (Forms.IsLollipopOrNewer)
Device.Info.PropertyChanged += DeviceInfoPropertyChanged;
}
protected override void Dispose(bool disposing)
{
if (disposing && !_disposed)
{
if (Forms.IsLollipopOrNewer)
Device.Info.PropertyChanged -= DeviceInfoPropertyChanged;
_disposed = true;
if (_dialog != null)
{
if (Forms.IsLollipopOrNewer)
_dialog.CancelEvent -= OnCancelButtonClicked;
_dialog.Hide();
_dialog.Dispose();
_dialog = null;
}
}
base.Dispose(disposing);
}
protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{
var textField = CreateNativeControl();
SetNativeControl(textField);
_originalHintTextColor = EditText.CurrentHintTextColor;
}
SetDate(Element.Date);
UpdateFont();
UpdateMinimumDate();
UpdateMaximumDate();
UpdateTextColor();
UpdateCharacterSpacing();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == DatePicker.DateProperty.PropertyName || e.PropertyName == DatePicker.FormatProperty.PropertyName)
SetDate(Element.Date);
else if (e.PropertyName == DatePicker.MinimumDateProperty.PropertyName)
UpdateMinimumDate();
else if (e.PropertyName == DatePicker.MaximumDateProperty.PropertyName)
UpdateMaximumDate();
else if (e.PropertyName == DatePicker.TextColorProperty.PropertyName)
UpdateTextColor();
else if (e.PropertyName == DatePicker.CharacterSpacingProperty.PropertyName)
UpdateCharacterSpacing();
else if (e.PropertyName == DatePicker.FontAttributesProperty.PropertyName || e.PropertyName == DatePicker.FontFamilyProperty.PropertyName || e.PropertyName == DatePicker.FontSizeProperty.PropertyName)
UpdateFont();
}
protected override void OnFocusChangeRequested(object sender, VisualElement.FocusRequestArgs e)
{
base.OnFocusChangeRequested(sender, e);
if (e.Focus)
{
if (Clickable)
CallOnClick();
else
((IPickerRenderer)this)?.OnClick();
}
else if (_dialog != null)
{
_dialog.Hide();
((IElementController)Element).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
if (Forms.IsLollipopOrNewer)
_dialog.CancelEvent -= OnCancelButtonClicked;
_dialog = null;
}
}
protected virtual DatePickerDialog CreateDatePickerDialog(int year, int month, int day)
{
DatePicker view = Element;
var dialog = new DatePickerDialog(Context, (o, e) =>
{
view.Date = e.Date;
((IElementController)view).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
}, year, month, day);
return dialog;
}
void DeviceInfoPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "CurrentOrientation")
{
DatePickerDialog currentDialog = _dialog;
if (currentDialog != null && currentDialog.IsShowing)
{
currentDialog.Dismiss();
if (Forms.IsLollipopOrNewer)
currentDialog.CancelEvent -= OnCancelButtonClicked;
ShowPickerDialog(currentDialog.DatePicker.Year, currentDialog.DatePicker.Month, currentDialog.DatePicker.DayOfMonth);
}
}
}
void IPickerRenderer.OnClick()
{
if (_dialog != null && _dialog.IsShowing)
{
return;
}
DatePicker view = Element;
((IElementController)view).SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true);
ShowPickerDialog(view.Date.Year, view.Date.Month - 1, view.Date.Day);
}
void ShowPickerDialog(int year, int month, int day)
{
_dialog = CreateDatePickerDialog(year, month, day);
UpdateMinimumDate();
UpdateMaximumDate();
if (Forms.IsLollipopOrNewer)
_dialog.CancelEvent += OnCancelButtonClicked;
_dialog.Show();
}
void OnCancelButtonClicked(object sender, EventArgs e)
{
Element.Unfocus();
}
void SetDate(DateTime date)
{
EditText.Text = date.ToString(Element.Format);
}
void UpdateCharacterSpacing()
{
if (Forms.IsLollipopOrNewer)
{
EditText.LetterSpacing = Element.CharacterSpacing.ToEm();
}
}
void UpdateFont()
{
EditText.Typeface = Element.ToTypeface();
EditText.SetTextSize(ComplexUnitType.Sp, (float)Element.FontSize);
}
void UpdateMaximumDate()
{
if (_dialog != null)
{
_dialog.DatePicker.MaxDate = (long)Element.MaximumDate.ToUniversalTime().Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
}
}
void UpdateMinimumDate()
{
if (_dialog != null)
{
_dialog.DatePicker.MinDate = (long)Element.MinimumDate.ToUniversalTime().Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds;
}
}
abstract protected void UpdateTextColor();
}
public class DatePickerRenderer : DatePickerRendererBase<EditText>
{
TextColorSwitcher _textColorSwitcher;
[Obsolete("This constructor is obsolete as of version 2.5. Please use DatePickerRenderer(Context) instead.")]
public DatePickerRenderer()
{
}
public DatePickerRenderer(Context context) : base(context)
{
}
protected override EditText CreateNativeControl()
{
return new PickerEditText(Context);
}
protected override EditText EditText => Control;
protected override void UpdateTextColor()
{
_textColorSwitcher = _textColorSwitcher ?? new TextColorSwitcher(EditText.TextColors, Element.UseLegacyColorManagement());
_textColorSwitcher.UpdateTextColor(EditText, Element.TextColor);
}
}
}