Resolves #100 input_datetime
This commit is contained in:
parent
acd468ae75
commit
062392b38c
@ -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<String> _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<String>(
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ class _EntityViewPageState extends State<EntityViewPage> {
|
||||
padding: EdgeInsets.all(10.0),
|
||||
child: ListView(
|
||||
children: <Widget>[
|
||||
_entity.buildWidget(false),
|
||||
_entity.buildWidget(false, context),
|
||||
_entity.buildAdditionalWidget()
|
||||
],
|
||||
),
|
||||
|
@ -40,6 +40,10 @@ class EntityCollection {
|
||||
return ButtonEntity(rawEntityData);
|
||||
}
|
||||
|
||||
case "input_datetime": {
|
||||
return DateTimeEntity(rawEntityData);
|
||||
}
|
||||
|
||||
case "input_select": {
|
||||
return SelectEntity(rawEntityData);
|
||||
}
|
||||
|
@ -409,7 +409,7 @@ class _MainPageState extends State<MainPage> 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),
|
||||
));
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user