scriptable-testlab
Version:
A lightweight, efficient tool designed to manage and update scripts for Scriptable.
121 lines • 3.93 kB
JavaScript
import { AbsDatePicker } from "scriptable-abstract";
const DEFAULT_STATE = {
minimumDate: null,
maximumDate: null,
countdownDuration: 0,
minuteInterval: 1,
initialDate: /* @__PURE__ */ new Date()
};
class MockDatePicker extends AbsDatePicker {
constructor() {
super(DEFAULT_STATE);
}
validateDateRange(date) {
if (this.state.minimumDate && date.getTime() < this.state.minimumDate.getTime()) {
throw new Error("Initial date cannot be earlier than minimum date");
}
if (this.state.maximumDate && date.getTime() > this.state.maximumDate.getTime()) {
throw new Error("Initial date cannot be later than maximum date");
}
}
validateMinMaxDates(min, max) {
if (min && max && min.getTime() > max.getTime()) {
throw new Error("Minimum date cannot be later than maximum date");
}
}
get minimumDate() {
return this.state.minimumDate ? new Date(this.state.minimumDate) : null;
}
set minimumDate(value) {
if (value) {
const newMin = new Date(value);
this.validateMinMaxDates(newMin, this.state.maximumDate);
}
this.setState({ minimumDate: value ? new Date(value) : null });
}
get maximumDate() {
return this.state.maximumDate ? new Date(this.state.maximumDate) : null;
}
set maximumDate(value) {
if (value) {
const newMax = new Date(value);
this.validateMinMaxDates(this.state.minimumDate, newMax);
}
this.setState({ maximumDate: value ? new Date(value) : null });
}
get countdownDuration() {
return this.state.countdownDuration;
}
set countdownDuration(value) {
const numValue = Number(value);
if (isNaN(numValue) || numValue < 0) {
throw new Error("Countdown duration must be a non-negative number");
}
this.setState({ countdownDuration: numValue });
}
get minuteInterval() {
return this.state.minuteInterval;
}
set minuteInterval(value) {
const numValue = Number(value);
if (isNaN(numValue) || numValue <= 0) {
throw new Error("Minute interval must be a positive number");
}
if (numValue > 30) {
throw new Error("Minute interval must not exceed 30");
}
if (60 % numValue !== 0) {
throw new Error("Minute interval must be a factor of 60");
}
this.setState({ minuteInterval: numValue });
}
get initialDate() {
return new Date(this.state.initialDate);
}
set initialDate(value) {
this.setState({ initialDate: new Date(value) });
}
/**
* Presents the date picker to the user.
* @returns A promise that resolves with the selected date.
*/
async pickDate() {
this.validateDateRange(this.state.initialDate);
return new Date(this.state.initialDate);
}
/**
* Presents the time picker to the user.
* @returns A promise that resolves with the selected date.
*/
async pickTime() {
this.validateDateRange(this.state.initialDate);
const date = new Date(this.state.initialDate);
const minutes = date.getMinutes();
const intervalMinutes = Math.round(minutes / this.state.minuteInterval) * this.state.minuteInterval;
date.setMinutes(intervalMinutes);
return new Date(date);
}
/**
* Presents the countdown duration picker to the user.
* @returns A promise that resolves with the selected duration in seconds.
*/
async pickCountdownDuration() {
return this.state.countdownDuration;
}
/**
* Presents the date picker displaying date and time.
* @returns A promise that resolves with the selected date.
*/
async pickDateAndTime() {
this.validateDateRange(this.state.initialDate);
const date = new Date(this.state.initialDate);
const minutes = date.getMinutes();
const intervalMinutes = Math.round(minutes / this.state.minuteInterval) * this.state.minuteInterval;
date.setMinutes(intervalMinutes);
return new Date(date);
}
}
export {
MockDatePicker
};
//# sourceMappingURL=date-picker.js.map