Skip to content

Commit

Permalink
Merge pull request #69 from alonstar/master
Browse files Browse the repository at this point in the history
(^) fix ios 14
  • Loading branch information
shiv19 authored Sep 28, 2020
2 parents 245cd7a + f4f001f commit ccdcf3f
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 112 deletions.
44 changes: 27 additions & 17 deletions src/modal-datetimepicker.android.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* tslint:disable */
// 第三方的套件,在別人修正前先臨時用
import * as app from "tns-core-modules/application";

export class ModalDatetimepicker {
datePicker: android.app.DatePickerDialog;
timePicker;
constructor() {}

public pickDate(options: PickerOptions = {}) {
public pickDate(options: PickerOptions = {}): Promise<DateResponse> {
return new Promise((resolve, reject) => {
if (
options.startingDate &&
Expand All @@ -22,7 +24,9 @@ export class ModalDatetimepicker {

// let now = Calendar.getInstance();
let startDate = new Date();
if (options.startingDate) startDate = options.startingDate;
if (options.startingDate) {
startDate = options.startingDate;
}

try {
this.datePicker = new android.app.DatePickerDialog(
Expand All @@ -43,18 +47,22 @@ export class ModalDatetimepicker {
);

if (options.maxDate || options.minDate) {
let datePickerInstance = this.datePicker.getDatePicker();
if (options.maxDate)
const datePickerInstance = this.datePicker.getDatePicker();
if (options.maxDate) {
datePickerInstance.setMaxDate(options.maxDate.getTime());
if (options.minDate)
}
if (options.minDate) {
datePickerInstance.setMinDate(options.minDate.getTime());
}
}

this.datePicker.setOnCancelListener(new android.content.DialogInterface.OnCancelListener({
onCancel: () => {
resolve();
}
}));
this.datePicker.setOnCancelListener(
new android.content.DialogInterface.OnCancelListener({
onCancel: () => {
resolve();
}
})
);

this.datePicker.show();
} catch (err) {
Expand All @@ -74,10 +82,10 @@ export class ModalDatetimepicker {
}
}

public pickTime(options: PickerOptions = {}) {
public pickTime(options: PickerOptions = {}): Promise<TimeResponse> {
options.is24HourView = options.is24HourView || false;
return new Promise((resolve, reject) => {
let now = java.util.Calendar.getInstance();
const now = java.util.Calendar.getInstance();
const hour =
options.startingHour !== undefined && options.startingHour >= 0
? +options.startingHour
Expand All @@ -103,11 +111,13 @@ export class ModalDatetimepicker {
options.is24HourView
);

this.timePicker.setOnCancelListener(new android.content.DialogInterface.OnCancelListener({
onCancel: () => {
resolve();
}
}));
this.timePicker.setOnCancelListener(
new android.content.DialogInterface.OnCancelListener({
onCancel: () => {
resolve();
}
})
);

this.timePicker.show();

Expand Down
8 changes: 8 additions & 0 deletions src/modal-datetimepicker.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as frame from "tns-core-modules/ui/frame";
import { Label } from "tns-core-modules/ui/label/";
import { Page } from "tns-core-modules/ui/page";
import { Color } from "tns-core-modules/ui/frame";
import { device } from "tns-core-modules/platform";

class ButtonHandler extends NSObject {
public close(nativeButton: UIButton, nativeEvent: _UIEvent) {
Expand Down Expand Up @@ -44,6 +45,10 @@ let titleLabel: UILabel;
// let minMaxLabel: UILabel;
let datePickerView: UIDatePicker;

const SUPPORT_DATE_PICKER_STYLE = parseFloat(device.osVersion) >= 14.0;
const SUPPORT_TEXT_COLOR = parseFloat(device.osVersion) < 14.0;
const DEFAULT_DATE_PICKER_STYLE = 1;

export class ModalDatetimepicker {
constructor() {}

Expand Down Expand Up @@ -281,6 +286,9 @@ export class ModalDatetimepicker {
);
datePickerView.datePickerMode =
options.type === "date" ? UIDatePickerMode.Date : UIDatePickerMode.Time;
if (SUPPORT_DATE_PICKER_STYLE) {
(datePickerView as any).preferredDatePickerStyle = DEFAULT_DATE_PICKER_STYLE;
}
datePickerView.autoresizingMask = UIViewAutoresizing.FlexibleWidth;
datePickerView.date = startingDate;
if (options.minDate) datePickerView.minimumDate = options.minDate;
Expand Down
Loading

0 comments on commit ccdcf3f

Please sign in to comment.