-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdatetimepicker.ios.ts
246 lines (221 loc) · 11.8 KB
/
datetimepicker.ios.ts
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
import { Color } from "tns-core-modules/color";
import { View, ios as iosView } from "tns-core-modules/ui/core/view";
import {
DateTimePickerBase, DateTimePickerStyleBase, getCurrentPage,
DatePickerOptions, TimePickerOptions, PickerOptions
} from "./datetimepicker.common";
import { LocalizationUtils } from "./utils/localization-utils";
import { getDateNow, getDateToday } from "./utils/date-utils";
export class DateTimePickerStyle extends DateTimePickerStyleBase {
}
export class DateTimePicker extends DateTimePickerBase {
public static PICKER_DEFAULT_MESSAGE_HEIGHT = 192;
public static PICKER_WIDTH_INSETS = 16;
public static PICKER_WIDTH_PAD = 304;
public static PICKER_DEFAULT_TITLE_OFFSET = 26.5;
public static PICKER_DEFAULT_TITLE_HEIGHT = 16;
public static PICKER_DEFAULT_MESSAGE = "\n\n\n\n\n\n\n\n\n";
static pickDate(options: DatePickerOptions, style?: DateTimePickerStyle): Promise<Date> {
const pickDate = new Promise<Date>((resolve) => {
const nativeDatePicker = DateTimePicker._createNativeDatePicker(options);
const nativeDialog = DateTimePicker._createNativeDialog(nativeDatePicker, options, style, (result) => {
resolve(result);
});
DateTimePicker._showNativeDialog(nativeDialog, nativeDatePicker, style);
});
return pickDate;
}
static pickTime(options: TimePickerOptions, style?: DateTimePickerStyle): Promise<Date> {
const pickTime = new Promise<Date>((resolve) => {
const nativeTimePicker = DateTimePicker._createNativeTimePicker(options);
const nativeDialog = DateTimePicker._createNativeDialog(nativeTimePicker, options, style, (result) => {
resolve(result);
});
DateTimePicker._showNativeDialog(nativeDialog, nativeTimePicker, style);
});
return pickTime;
}
static _createNativeDatePicker(options: DatePickerOptions): UIDatePicker {
const pickerView = UIDatePicker.alloc().initWithFrame(CGRectZero);
pickerView.datePickerMode = UIDatePickerMode.Date;
const date = options.date ? options.date : getDateToday();
pickerView.date = date;
if (options.maxDate) {
pickerView.maximumDate = options.maxDate;
}
if (options.minDate) {
pickerView.minimumDate = options.minDate;
}
if (options.locale) {
pickerView.locale = LocalizationUtils.createNativeLocale(options.locale);
}
return pickerView;
}
static _createNativeTimePicker(options: TimePickerOptions): UIDatePicker {
const pickerView = UIDatePicker.alloc().initWithFrame(CGRectZero);
pickerView.datePickerMode = UIDatePickerMode.Time;
const time = options.time ? options.time : getDateNow();
pickerView.date = time;
if (options.locale) {
pickerView.locale = LocalizationUtils.createNativeLocale(options.locale);
}
return pickerView;
}
static _createNativeDialog(nativePicker: UIDatePicker, options: PickerOptions, style: DateTimePickerStyle, callback: Function) {
const alertTitle = options.title ? options.title : "";
const alertController = UIAlertController.alertControllerWithTitleMessagePreferredStyle(
alertTitle, DateTimePicker.PICKER_DEFAULT_MESSAGE, UIAlertControllerStyle.ActionSheet);
const alertSize = Math.min(alertController.view.frame.size.width, alertController.view.frame.size.height);
const pickerViewWidth = UIDevice.currentDevice.userInterfaceIdiom === UIUserInterfaceIdiom.Pad ?
DateTimePicker.PICKER_WIDTH_PAD : alertSize - DateTimePicker.PICKER_WIDTH_INSETS;
let pickerContainerFrameTop = DateTimePicker.PICKER_DEFAULT_TITLE_OFFSET;
if (options.title) {
pickerContainerFrameTop += DateTimePicker.PICKER_DEFAULT_TITLE_HEIGHT;
}
const pickerViewHeight = DateTimePicker.PICKER_DEFAULT_MESSAGE_HEIGHT;
const pickerContainer = UIView.alloc().init();
let spinnersBackgroundColor = new Color("transparent");
let spinnersTextColor = null;
if (style) {
spinnersBackgroundColor = style.spinnersBackgroundColor ? style.spinnersBackgroundColor : spinnersBackgroundColor;
spinnersTextColor = style.spinnersTextColor;
}
DateTimePicker._applyDialogSpinnersColors(nativePicker, pickerContainer, spinnersTextColor, spinnersBackgroundColor);
const pickerView = nativePicker;
pickerView.frame = CGRectMake(0, 0, pickerViewWidth, pickerViewHeight);
pickerContainer.addSubview(pickerView);
DateTimePicker._clearVibrancyEffects(alertController.view);
const messageLabel = DateTimePicker._findLabelWithText(alertController.view, DateTimePicker.PICKER_DEFAULT_MESSAGE);
const messageLabelContainer = DateTimePicker._getLabelContainer(messageLabel);
messageLabelContainer.clipsToBounds = true;
messageLabelContainer.addSubview(pickerContainer);
pickerContainer.translatesAutoresizingMaskIntoConstraints = false;
pickerContainer.topAnchor.constraintEqualToAnchorConstant(alertController.view.topAnchor, pickerContainerFrameTop).active = true;
pickerContainer.leftAnchor.constraintEqualToAnchor(alertController.view.leftAnchor).active = true;
pickerContainer.rightAnchor.constraintEqualToAnchor(alertController.view.rightAnchor).active = true;
pickerContainer.bottomAnchor.constraintEqualToAnchor(alertController.view.bottomAnchor).active = true;
pickerView.leftAnchor.constraintLessThanOrEqualToAnchorConstant(pickerContainer.leftAnchor, DateTimePicker.PICKER_WIDTH_INSETS).active = true;
pickerView.rightAnchor.constraintLessThanOrEqualToAnchorConstant(pickerContainer.rightAnchor, DateTimePicker.PICKER_WIDTH_INSETS).active = true;
const cancelButtonText = options.cancelButtonText ? options.cancelButtonText : "Cancel";
const okButtonText = options.okButtonText ? options.okButtonText : "OK";
const cancelActionStyle = (style && style.buttonCancelBackgroundColor) ? UIAlertActionStyle.Default : UIAlertActionStyle.Cancel;
let cancelAction = UIAlertAction.actionWithTitleStyleHandler(cancelButtonText, cancelActionStyle, () => {
callback(null);
});
let okAction = UIAlertAction.actionWithTitleStyleHandler(okButtonText, UIAlertActionStyle.Default, () => {
callback(pickerView.date);
});
alertController.addAction(okAction);
if (cancelButtonText) {
alertController.addAction(cancelAction);
}
if (style) {
const buttonOkTextColor = style.buttonOkTextColor ? style.buttonOkTextColor : style.buttonsTextColor;
const buttonCancelTextColor = style.buttonCancelTextColor ? style.buttonCancelTextColor : style.buttonsTextColor;
DateTimePicker._applyDialogButtonTextColor(okAction, buttonOkTextColor);
DateTimePicker._applyDialogButtonTextColor(cancelAction, buttonCancelTextColor);
DateTimePicker._applyDialogTitleTextColor(alertController, style.titleTextColor);
DateTimePicker._applyBackgroundColors(messageLabelContainer, style);
}
return alertController;
}
static _showNativeDialog(nativeDialog: UIAlertController, nativePicker: UIDatePicker, style: DateTimePickerStyle) {
let currentPage = getCurrentPage();
if (currentPage) {
let view: View = currentPage;
let viewController: UIViewController = currentPage.ios;
if (currentPage.modal) {
view = currentPage.modal;
if (view.ios instanceof UIViewController) {
viewController = view.ios;
} else {
const parentWithController = iosView.getParentWithViewController(view);
viewController = parentWithController ? parentWithController.viewController : undefined;
}
}
if (viewController) {
if (nativeDialog.popoverPresentationController) {
nativeDialog.popoverPresentationController.sourceView = viewController.view;
nativeDialog.popoverPresentationController.sourceRect = CGRectMake(viewController.view.bounds.size.width / 2.0, viewController.view.bounds.size.height / 2.0, 1.0, 1.0);
nativeDialog.popoverPresentationController.permittedArrowDirections = 0;
}
viewController.presentViewControllerAnimatedCompletion(nativeDialog, true, () => {
});
}
}
}
private static _applyDialogTitleTextColor(nativeDialog: UIAlertController, color: Color) {
if (color) {
if (nativeDialog.title) {
let title = NSAttributedString.alloc().initWithStringAttributes(nativeDialog.title, <any>{ [NSForegroundColorAttributeName]: color.ios });
nativeDialog.setValueForKey(title, "attributedTitle");
}
}
}
private static _applyDialogSpinnersColors(nativePicker: UIDatePicker, nativeContainer: UIView, color: Color, backgroundColor: Color) {
if (backgroundColor) {
nativeContainer.backgroundColor = backgroundColor.ios;
}
if (color) {
nativePicker.setValueForKey(color.ios, "textColor");
nativePicker.setValueForKey(false, "highlightsToday");
}
}
private static _applyDialogButtonTextColor(action: UIAlertAction, textColor: Color) {
if (textColor) {
action.setValueForKey(textColor.ios, "titleTextColor");
}
}
private static _applyBackgroundColors(labelContainer: UIView, style: DateTimePickerStyle) {
if (!labelContainer || !style) {
return;
}
if (labelContainer.superview && labelContainer.superview.superview) {
const mainContainer = labelContainer.superview.superview;
if (style.dialogBackgroundColor) {
mainContainer.backgroundColor = style.dialogBackgroundColor.ios;
}
const buttonsContainer = mainContainer.subviews.lastObject;
let buttonsBackground = style.buttonCancelBackgroundColor;
if (!buttonsBackground) {
buttonsBackground = style.buttonOkBackgroundColor;
if (!buttonsBackground) {
buttonsBackground = style.buttonsBackgroundColor;
}
}
if (buttonsContainer && buttonsBackground) {
buttonsContainer.backgroundColor = buttonsBackground.ios;
}
}
}
private static _clearVibrancyEffects(uiView: UIView) {
if (uiView instanceof UIVisualEffectView && uiView.effect instanceof UIVibrancyEffect) {
// Since ios13 UIAlertController has some effects which cause
// semi-transparency and interfere with color customizations:
uiView.effect = null;
}
const subViewsCount = uiView.subviews.count;
for (let i = 0; i < subViewsCount; i++) {
DateTimePicker._clearVibrancyEffects(uiView.subviews[i]);
}
}
private static _getLabelContainer(uiView: UIView) {
if (uiView.superview.class() === (UIView.class())) {
return uiView.superview;
}
return DateTimePicker._getLabelContainer(uiView.superview);
}
private static _findLabelWithText(uiView: UIView, text: string) {
if ((uiView instanceof UILabel) && uiView.text === text) {
return uiView;
}
const subViewsCount = uiView.subviews.count;
for (let i = 0; i < subViewsCount; i++) {
let label = DateTimePicker._findLabelWithText(uiView.subviews[i], text);
if (label) {
return label;
}
}
return null;
}
}