2018-10-01 21:57:54 +03:00
|
|
|
part of '../main.dart';
|
|
|
|
|
2018-10-02 00:41:40 +03:00
|
|
|
class _DateTimeEntityWidgetState extends _EntityWidgetState {
|
|
|
|
bool get hasDate => widget.entity._attributes["has_date"] ?? false;
|
|
|
|
bool get hasTime => widget.entity._attributes["has_time"] ?? false;
|
|
|
|
int get year => widget.entity._attributes["year"] ?? 1970;
|
|
|
|
int get month => widget.entity._attributes["month"] ?? 1;
|
|
|
|
int get day => widget.entity._attributes["day"] ?? 1;
|
|
|
|
int get hour => widget.entity._attributes["hour"] ?? 0;
|
|
|
|
int get minute => widget.entity._attributes["minute"] ?? 0;
|
|
|
|
int get second => widget.entity._attributes["second"] ?? 0;
|
2018-10-01 21:57:54 +03:00
|
|
|
String get formattedState => _getFormattedState();
|
|
|
|
DateTime get dateTimeState => _getDateTimeState();
|
|
|
|
|
|
|
|
DateTime _getDateTimeState() {
|
|
|
|
return DateTime(this.year, this.month, this.day, this.hour, this.minute, this.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
String _getFormattedState() {
|
|
|
|
String formattedState = "";
|
|
|
|
if (this.hasDate) {
|
|
|
|
formattedState += formatDate(dateTimeState, [M, ' ', d, ', ', yyyy]);
|
|
|
|
}
|
|
|
|
if (this.hasTime) {
|
|
|
|
formattedState += " "+formatDate(dateTimeState, [HH, ':', nn]);
|
|
|
|
}
|
|
|
|
return formattedState;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2018-10-02 23:10:40 +03:00
|
|
|
void setNewState(newValue) {
|
2018-10-02 00:41:40 +03:00
|
|
|
eventBus.fire(new ServiceCallEvent(widget.entity.domain, "set_datetime", widget.entity.entityId,
|
2018-10-01 21:57:54 +03:00
|
|
|
newValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2018-10-03 16:44:11 +03:00
|
|
|
Widget _buildActionWidget(BuildContext context) {
|
2018-10-01 21:57:54 +03:00
|
|
|
return Padding(
|
|
|
|
padding:
|
|
|
|
EdgeInsets.fromLTRB(0.0, 0.0, Entity.RIGHT_WIDGET_PADDING, 0.0),
|
|
|
|
child: GestureDetector(
|
|
|
|
child: Text(
|
|
|
|
"$formattedState",
|
|
|
|
textAlign: TextAlign.right,
|
|
|
|
style: new TextStyle(
|
|
|
|
fontSize: Entity.STATE_FONT_SIZE,
|
|
|
|
)),
|
|
|
|
onTap: () => _handleStateTap(context),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _handleStateTap(BuildContext context) {
|
|
|
|
if (hasDate) {
|
|
|
|
_showDatePicker(context).then((date) {
|
|
|
|
if (date != null) {
|
|
|
|
if (hasTime) {
|
|
|
|
_showTimePicker(context).then((time){
|
2018-10-02 23:10:40 +03:00
|
|
|
setNewState({"date": "${formatDate(date, [yyyy, '-', mm, '-', dd])}", "time": "${formatDate(DateTime(1970, 1, 1, time.hour, time.minute), [HH, ':', nn])}"});
|
2018-10-01 21:57:54 +03:00
|
|
|
});
|
|
|
|
} else {
|
2018-10-02 23:10:40 +03:00
|
|
|
setNewState({"date": "${formatDate(date, [yyyy, '-', mm, '-', dd])}"});
|
2018-10-01 21:57:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (hasTime) {
|
|
|
|
_showTimePicker(context).then((time){
|
|
|
|
if (time != null) {
|
2018-10-02 23:10:40 +03:00
|
|
|
setNewState({"time": "${formatDate(DateTime(1970, 1, 1, time.hour, time.minute), [HH, ':', nn])}"});
|
2018-10-01 21:57:54 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2018-10-02 00:41:40 +03:00
|
|
|
TheLogger.log("Warning", "${widget.entity.entityId} has no date and no time");
|
2018-10-01 21:57:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future _showDatePicker(BuildContext context) {
|
|
|
|
return showDatePicker(
|
|
|
|
context: context,
|
|
|
|
initialDate: dateTimeState,
|
|
|
|
firstDate: DateTime(1970),
|
|
|
|
lastDate: DateTime(2037) //Unix timestamp will finish at Jan 19, 2038
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future _showTimePicker(BuildContext context) {
|
|
|
|
return showTimePicker(
|
|
|
|
context: context,
|
|
|
|
initialTime: TimeOfDay.fromDateTime(dateTimeState)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|