Resolves #100 input_datetime

This commit is contained in:
estevez 2018-09-30 23:12:27 +03:00
parent acd468ae75
commit 062392b38c
4 changed files with 117 additions and 17 deletions

View File

@ -92,7 +92,7 @@ class Entity {
return; return;
} }
Widget buildWidget(bool inCard) { Widget buildWidget(bool inCard, BuildContext context) {
return SizedBox( return SizedBox(
height: Entity.WIDGET_HEIGHT, height: Entity.WIDGET_HEIGHT,
child: Row( child: Row(
@ -107,7 +107,7 @@ class Entity {
onTap: inCard ? openEntityPage : null, 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( return Padding(
padding: padding:
EdgeInsets.fromLTRB(0.0, 0.0, Entity.RIGHT_WIDGET_PADDING, 0.0), EdgeInsets.fromLTRB(0.0, 0.0, Entity.RIGHT_WIDGET_PADDING, 0.0),
@ -164,12 +164,9 @@ class Entity {
fontSize: Entity.STATE_FONT_SIZE, fontSize: Entity.STATE_FONT_SIZE,
)), )),
onTap: openEntityPage, onTap: openEntityPage,
)); )
);
} }
/*Widget _buildExtendedActionWidget(BuildContext context, String staticState) {
return _buildActionWidget(context);
}*/
} }
class SwitchEntity extends Entity { class SwitchEntity extends Entity {
@ -182,7 +179,7 @@ class SwitchEntity extends Entity {
} }
@override @override
Widget _buildActionWidget(bool inCard) { Widget _buildActionWidget(bool inCard, BuildContext context) {
return Switch( return Switch(
value: this.isOn, value: this.isOn,
onChanged: ((switchState) { onChanged: ((switchState) {
@ -201,7 +198,7 @@ class ButtonEntity extends Entity {
} }
@override @override
Widget _buildActionWidget(bool inCard) { Widget _buildActionWidget(bool inCard, BuildContext context) {
return FlatButton( return FlatButton(
onPressed: (() { onPressed: (() {
sendNewState(null); sendNewState(null);
@ -216,6 +213,9 @@ class ButtonEntity extends Entity {
} }
} }
//
// SLIDER
//
class SliderEntity extends Entity { class SliderEntity extends Entity {
int _multiplier = 1; int _multiplier = 1;
@ -239,7 +239,7 @@ class SliderEntity extends Entity {
} }
@override @override
Widget _buildActionWidget(bool inCard) { Widget _buildActionWidget(bool inCard, BuildContext context) {
return Container( return Container(
width: 200.0, width: 200.0,
child: Row( child: Row(
@ -253,7 +253,6 @@ class SliderEntity extends Entity {
? this.doubleState * _multiplier ? this.doubleState * _multiplier
: this.minValue * _multiplier, : this.minValue * _multiplier,
onChanged: (value) { onChanged: (value) {
//debugPrint("$value");
eventBus.fire(new StateChangedEvent(_entityId, eventBus.fire(new StateChangedEvent(_entityId,
(value.roundToDouble() / _multiplier).toString(), true)); (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 { class SelectEntity extends Entity {
List<String> _listOptions = []; List<String> _listOptions = [];
String get initialValue => _attributes["initial"] ?? null; String get initialValue => _attributes["initial"] ?? null;
@ -295,7 +391,7 @@ class SelectEntity extends Entity {
} }
@override @override
Widget _buildActionWidget(bool inCard) { Widget _buildActionWidget(bool inCard, BuildContext context) {
return Container( return Container(
width: Entity.INPUT_WIDTH, width: Entity.INPUT_WIDTH,
child: DropdownButton<String>( child: DropdownButton<String>(
@ -366,7 +462,7 @@ class TextEntity extends Entity {
} }
@override @override
Widget _buildActionWidget(bool inCard) { Widget _buildActionWidget(bool inCard, BuildContext context) {
if (this.isTextField || this.isPasswordField) { if (this.isTextField || this.isPasswordField) {
return Container( return Container(
width: Entity.INPUT_WIDTH, width: Entity.INPUT_WIDTH,
@ -384,7 +480,7 @@ class TextEntity extends Entity {
); );
} else { } else {
TheLogger.log("Warning", "Unsupported input mode for $entityId"); TheLogger.log("Warning", "Unsupported input mode for $entityId");
return super._buildActionWidget(inCard); return super._buildActionWidget(inCard, context);
} }
} }
} }

View File

@ -46,7 +46,7 @@ class _EntityViewPageState extends State<EntityViewPage> {
padding: EdgeInsets.all(10.0), padding: EdgeInsets.all(10.0),
child: ListView( child: ListView(
children: <Widget>[ children: <Widget>[
_entity.buildWidget(false), _entity.buildWidget(false, context),
_entity.buildAdditionalWidget() _entity.buildAdditionalWidget()
], ],
), ),

View File

@ -40,6 +40,10 @@ class EntityCollection {
return ButtonEntity(rawEntityData); return ButtonEntity(rawEntityData);
} }
case "input_datetime": {
return DateTimeEntity(rawEntityData);
}
case "input_select": { case "input_select": {
return SelectEntity(rawEntityData); return SelectEntity(rawEntityData);
} }

View File

@ -409,7 +409,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
entities.add( entities.add(
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0), padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
child: entity.buildWidget(true), child: entity.buildWidget(true, context),
)); ));
} }
}); });