From 062392b38c4b192f17fc75d296c82946837d7604 Mon Sep 17 00:00:00 2001 From: estevez Date: Sun, 30 Sep 2018 23:12:27 +0300 Subject: [PATCH] Resolves #100 input_datetime --- lib/entity.class.dart | 126 +++++++++++++++++++++++++++---- lib/entity.page.dart | 2 +- lib/entity_collection.class.dart | 4 + lib/main.dart | 2 +- 4 files changed, 117 insertions(+), 17 deletions(-) diff --git a/lib/entity.class.dart b/lib/entity.class.dart index 884e1af..01a3212 100644 --- a/lib/entity.class.dart +++ b/lib/entity.class.dart @@ -92,7 +92,7 @@ class Entity { return; } - Widget buildWidget(bool inCard) { + Widget buildWidget(bool inCard, BuildContext context) { return SizedBox( height: Entity.WIDGET_HEIGHT, child: Row( @@ -107,7 +107,7 @@ class Entity { onTap: inCard ? openEntityPage : null, ), ), - _buildActionWidget(inCard) + _buildActionWidget(inCard, context) ], ), ); @@ -152,7 +152,7 @@ class Entity { ); } - Widget _buildActionWidget(bool inCard) { + Widget _buildActionWidget(bool inCard, BuildContext context) { return Padding( padding: EdgeInsets.fromLTRB(0.0, 0.0, Entity.RIGHT_WIDGET_PADDING, 0.0), @@ -164,12 +164,9 @@ class Entity { fontSize: Entity.STATE_FONT_SIZE, )), onTap: openEntityPage, - )); + ) + ); } - - /*Widget _buildExtendedActionWidget(BuildContext context, String staticState) { - return _buildActionWidget(context); - }*/ } class SwitchEntity extends Entity { @@ -182,7 +179,7 @@ class SwitchEntity extends Entity { } @override - Widget _buildActionWidget(bool inCard) { + Widget _buildActionWidget(bool inCard, BuildContext context) { return Switch( value: this.isOn, onChanged: ((switchState) { @@ -201,7 +198,7 @@ class ButtonEntity extends Entity { } @override - Widget _buildActionWidget(bool inCard) { + Widget _buildActionWidget(bool inCard, BuildContext context) { return FlatButton( onPressed: (() { sendNewState(null); @@ -216,6 +213,9 @@ class ButtonEntity extends Entity { } } +// +// SLIDER +// class SliderEntity extends Entity { int _multiplier = 1; @@ -239,7 +239,7 @@ class SliderEntity extends Entity { } @override - Widget _buildActionWidget(bool inCard) { + Widget _buildActionWidget(bool inCard, BuildContext context) { return Container( width: 200.0, child: Row( @@ -253,7 +253,6 @@ class SliderEntity extends Entity { ? this.doubleState * _multiplier : this.minValue * _multiplier, onChanged: (value) { - //debugPrint("$value"); eventBus.fire(new StateChangedEvent(_entityId, (value.roundToDouble() / _multiplier).toString(), true)); }, @@ -276,6 +275,103 @@ class SliderEntity extends Entity { } } +// +// DATETIME +// + +class DateTimeEntity extends Entity { + bool get hasDate => _attributes["has_date"] ?? false; + bool get hasTime => _attributes["has_time"] ?? false; + int get year => _attributes["year"] ?? 1970; + int get month => _attributes["month"] ?? 1; + int get day => _attributes["day"] ?? 1; + int get hour => _attributes["hour"] ?? 0; + int get minute => _attributes["minute"] ?? 0; + int get second => _attributes["second"] ?? 0; + String get formattedState => _getFormattedState(); + DateTime get dateTimeState => _getDateTimeState(); + + DateTimeEntity(Map rawData) : super(rawData); + + 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 + void sendNewState(newValue) { + eventBus.fire(new ServiceCallEvent(_domain, "set_datetime", _entityId, + newValue)); + } + + @override + Widget _buildActionWidget(bool inCard, BuildContext context) { + 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){ + sendNewState({"date": "${formatDate(date, [yyyy, '-', mm, '-', dd])}", "time": "${formatDate(DateTime(1970, 1, 1, time.hour, time.minute), [HH, ':', nn])}"}); + }); + } else { + sendNewState({"date": "${formatDate(date, [yyyy, '-', mm, '-', dd])}"}); + } + } + }); + } else if (hasTime) { + _showTimePicker(context).then((time){ + if (time != null) { + sendNewState({"time": "${formatDate(DateTime(1970, 1, 1, time.hour, time.minute), [HH, ':', nn])}"}); + } + }); + } else { + TheLogger.log("Warning", "$entityId has no date and no time"); + } + } + + 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) + ); + } +} + class SelectEntity extends Entity { List _listOptions = []; String get initialValue => _attributes["initial"] ?? null; @@ -295,7 +391,7 @@ class SelectEntity extends Entity { } @override - Widget _buildActionWidget(bool inCard) { + Widget _buildActionWidget(bool inCard, BuildContext context) { return Container( width: Entity.INPUT_WIDTH, child: DropdownButton( @@ -366,7 +462,7 @@ class TextEntity extends Entity { } @override - Widget _buildActionWidget(bool inCard) { + Widget _buildActionWidget(bool inCard, BuildContext context) { if (this.isTextField || this.isPasswordField) { return Container( width: Entity.INPUT_WIDTH, @@ -384,7 +480,7 @@ class TextEntity extends Entity { ); } else { TheLogger.log("Warning", "Unsupported input mode for $entityId"); - return super._buildActionWidget(inCard); + return super._buildActionWidget(inCard, context); } } } diff --git a/lib/entity.page.dart b/lib/entity.page.dart index a81b44e..2bc2372 100644 --- a/lib/entity.page.dart +++ b/lib/entity.page.dart @@ -46,7 +46,7 @@ class _EntityViewPageState extends State { padding: EdgeInsets.all(10.0), child: ListView( children: [ - _entity.buildWidget(false), + _entity.buildWidget(false, context), _entity.buildAdditionalWidget() ], ), diff --git a/lib/entity_collection.class.dart b/lib/entity_collection.class.dart index 1b33480..4e06795 100644 --- a/lib/entity_collection.class.dart +++ b/lib/entity_collection.class.dart @@ -40,6 +40,10 @@ class EntityCollection { return ButtonEntity(rawEntityData); } + case "input_datetime": { + return DateTimeEntity(rawEntityData); + } + case "input_select": { return SelectEntity(rawEntityData); } diff --git a/lib/main.dart b/lib/main.dart index bb1be1b..259631d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -409,7 +409,7 @@ class _MainPageState extends State with WidgetsBindingObserver { entities.add( Padding( padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0), - child: entity.buildWidget(true), + child: entity.buildWidget(true, context), )); } });