Refactoring: Stateful entity widgets
This commit is contained in:
parent
4a0b447f00
commit
9e97bac85b
@ -3,7 +3,7 @@ part of 'main.dart';
|
|||||||
class EntityViewPage extends StatefulWidget {
|
class EntityViewPage extends StatefulWidget {
|
||||||
EntityViewPage({Key key, this.entity}) : super(key: key);
|
EntityViewPage({Key key, this.entity}) : super(key: key);
|
||||||
|
|
||||||
Entity entity;
|
final Entity entity;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
_EntityViewPageState createState() => new _EntityViewPageState();
|
_EntityViewPageState createState() => new _EntityViewPageState();
|
||||||
@ -44,22 +44,13 @@ class _EntityViewPageState extends State<EntityViewPage> {
|
|||||||
),
|
),
|
||||||
body: Padding(
|
body: Padding(
|
||||||
padding: EdgeInsets.all(10.0),
|
padding: EdgeInsets.all(10.0),
|
||||||
child: ListView(
|
child: _entity.buildWidget(context, false)
|
||||||
children: <Widget>[
|
|
||||||
_entity.buildWidget(false, context),
|
|
||||||
_entity.buildAdditionalWidget()
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose(){
|
void dispose(){
|
||||||
if (_entity is TextEntity && (_entity as TextEntity).tmpState != _entity.state) {
|
|
||||||
eventBus.fire(new ServiceCallEvent(_entity.domain, "set_value", _entity.entityId, {"value": "${(_entity as TextEntity).tmpState}"}));
|
|
||||||
TheLogger.log("Debug", "Saving changed input value for ${_entity.entityId}");
|
|
||||||
}
|
|
||||||
if (_stateSubscription != null) _stateSubscription.cancel();
|
if (_stateSubscription != null) _stateSubscription.cancel();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,10 @@
|
|||||||
part of '../main.dart';
|
part of '../main.dart';
|
||||||
|
|
||||||
class ButtonEntity extends Entity {
|
class _ButtonEntityWidgetState extends _EntityWidgetState {
|
||||||
ButtonEntity(Map rawData) : super(rawData);
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void sendNewState(newValue) {
|
void sendNewState(newValue) {
|
||||||
eventBus.fire(new ServiceCallEvent(_domain, "turn_on", _entityId, null));
|
eventBus.fire(new ServiceCallEvent(widget.entity.domain, "turn_on", widget.entity.entityId, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -1,19 +1,17 @@
|
|||||||
part of '../main.dart';
|
part of '../main.dart';
|
||||||
|
|
||||||
class DateTimeEntity extends Entity {
|
class _DateTimeEntityWidgetState extends _EntityWidgetState {
|
||||||
bool get hasDate => _attributes["has_date"] ?? false;
|
bool get hasDate => widget.entity._attributes["has_date"] ?? false;
|
||||||
bool get hasTime => _attributes["has_time"] ?? false;
|
bool get hasTime => widget.entity._attributes["has_time"] ?? false;
|
||||||
int get year => _attributes["year"] ?? 1970;
|
int get year => widget.entity._attributes["year"] ?? 1970;
|
||||||
int get month => _attributes["month"] ?? 1;
|
int get month => widget.entity._attributes["month"] ?? 1;
|
||||||
int get day => _attributes["day"] ?? 1;
|
int get day => widget.entity._attributes["day"] ?? 1;
|
||||||
int get hour => _attributes["hour"] ?? 0;
|
int get hour => widget.entity._attributes["hour"] ?? 0;
|
||||||
int get minute => _attributes["minute"] ?? 0;
|
int get minute => widget.entity._attributes["minute"] ?? 0;
|
||||||
int get second => _attributes["second"] ?? 0;
|
int get second => widget.entity._attributes["second"] ?? 0;
|
||||||
String get formattedState => _getFormattedState();
|
String get formattedState => _getFormattedState();
|
||||||
DateTime get dateTimeState => _getDateTimeState();
|
DateTime get dateTimeState => _getDateTimeState();
|
||||||
|
|
||||||
DateTimeEntity(Map rawData) : super(rawData);
|
|
||||||
|
|
||||||
DateTime _getDateTimeState() {
|
DateTime _getDateTimeState() {
|
||||||
return DateTime(this.year, this.month, this.day, this.hour, this.minute, this.second);
|
return DateTime(this.year, this.month, this.day, this.hour, this.minute, this.second);
|
||||||
}
|
}
|
||||||
@ -31,7 +29,7 @@ class DateTimeEntity extends Entity {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void sendNewState(newValue) {
|
void sendNewState(newValue) {
|
||||||
eventBus.fire(new ServiceCallEvent(_domain, "set_datetime", _entityId,
|
eventBus.fire(new ServiceCallEvent(widget.entity.domain, "set_datetime", widget.entity.entityId,
|
||||||
newValue));
|
newValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,7 +70,7 @@ class DateTimeEntity extends Entity {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
TheLogger.log("Warning", "$entityId has no date and no time");
|
TheLogger.log("Warning", "${widget.entity.entityId} has no date and no time");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,6 +55,13 @@ class Entity {
|
|||||||
_lastUpdated = DateTime.tryParse(rawData["last_updated"]);
|
_lastUpdated = DateTime.tryParse(rawData["last_updated"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EntityWidget buildWidget(BuildContext context, bool inCard) {
|
||||||
|
return EntityWidget(
|
||||||
|
entity: this,
|
||||||
|
inCard: inCard,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
String _getLastUpdatedFormatted() {
|
String _getLastUpdatedFormatted() {
|
||||||
if (_lastUpdated == null) {
|
if (_lastUpdated == null) {
|
||||||
return "-";
|
return "-";
|
||||||
@ -84,35 +91,98 @@ class Entity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void openEntityPage() {
|
|
||||||
eventBus.fire(new ShowEntityPageEvent(this));
|
}
|
||||||
|
|
||||||
|
class EntityWidget extends StatefulWidget {
|
||||||
|
EntityWidget({Key key, this.entity, this.inCard}) : super(key: key);
|
||||||
|
|
||||||
|
final Entity entity;
|
||||||
|
final bool inCard;
|
||||||
|
|
||||||
|
@override
|
||||||
|
_EntityWidgetState createState() {
|
||||||
|
switch (entity.domain) {
|
||||||
|
case "automation":
|
||||||
|
case "input_boolean ":
|
||||||
|
case "switch":
|
||||||
|
case "light": {
|
||||||
|
return _SwitchEntityWidgetState();
|
||||||
|
}
|
||||||
|
|
||||||
|
case "script":
|
||||||
|
case "scene": {
|
||||||
|
return _ButtonEntityWidgetState();
|
||||||
|
}
|
||||||
|
|
||||||
|
case "input_datetime": {
|
||||||
|
return _DateTimeEntityWidgetState();
|
||||||
|
}
|
||||||
|
|
||||||
|
case "input_select": {
|
||||||
|
return _SelectEntityWidgetState();
|
||||||
|
}
|
||||||
|
|
||||||
|
case "input_number": {
|
||||||
|
return _SliderEntityWidgetState();
|
||||||
|
}
|
||||||
|
|
||||||
|
case "input_text": {
|
||||||
|
return _TextEntityWidgetState();
|
||||||
|
}
|
||||||
|
|
||||||
|
default: {
|
||||||
|
return _EntityWidgetState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class _EntityWidgetState extends State<EntityWidget> {
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
if (widget.inCard) {
|
||||||
|
return _buildMainWidget(context);
|
||||||
|
} else {
|
||||||
|
return ListView(
|
||||||
|
children: <Widget>[
|
||||||
|
_buildMainWidget(context),
|
||||||
|
_buildLastUpdatedWidget()
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendNewState(newState) {
|
Widget _buildMainWidget(BuildContext context) {
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget buildWidget(bool inCard, BuildContext context) {
|
|
||||||
return SizedBox(
|
return SizedBox(
|
||||||
height: Entity.WIDGET_HEIGHT,
|
height: Entity.WIDGET_HEIGHT,
|
||||||
child: Row(
|
child: Row(
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
GestureDetector(
|
GestureDetector(
|
||||||
child: _buildIconWidget(),
|
child: _buildIconWidget(),
|
||||||
onTap: inCard ? openEntityPage : null,
|
onTap: widget.inCard ? openEntityPage : null,
|
||||||
),
|
),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: GestureDetector(
|
child: GestureDetector(
|
||||||
child: _buildNameWidget(),
|
child: _buildNameWidget(),
|
||||||
onTap: inCard ? openEntityPage : null,
|
onTap: widget.inCard ? openEntityPage : null,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
_buildActionWidget(inCard, context)
|
_buildActionWidget(widget.inCard, context)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void openEntityPage() {
|
||||||
|
eventBus.fire(new ShowEntityPageEvent(widget.entity));
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendNewState(newState) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Widget buildAdditionalWidget() {
|
Widget buildAdditionalWidget() {
|
||||||
return _buildLastUpdatedWidget();
|
return _buildLastUpdatedWidget();
|
||||||
}
|
}
|
||||||
@ -121,9 +191,9 @@ class Entity {
|
|||||||
return Padding(
|
return Padding(
|
||||||
padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, 12.0, 0.0),
|
padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, 12.0, 0.0),
|
||||||
child: MaterialDesignIcons.createIconWidgetFromEntityData(
|
child: MaterialDesignIcons.createIconWidgetFromEntityData(
|
||||||
this,
|
widget.entity,
|
||||||
Entity.ICON_SIZE,
|
Entity.ICON_SIZE,
|
||||||
Entity.STATE_ICONS_COLORS[_state] ?? Colors.blueGrey),
|
Entity.STATE_ICONS_COLORS[widget.entity.state] ?? Colors.blueGrey),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,10 +202,10 @@ class Entity {
|
|||||||
padding: EdgeInsets.fromLTRB(
|
padding: EdgeInsets.fromLTRB(
|
||||||
Entity.LEFT_WIDGET_PADDING, Entity.SMALL_FONT_SIZE, 0.0, 0.0),
|
Entity.LEFT_WIDGET_PADDING, Entity.SMALL_FONT_SIZE, 0.0, 0.0),
|
||||||
child: Text(
|
child: Text(
|
||||||
'${this.lastUpdated}',
|
'${widget.entity.lastUpdated}',
|
||||||
textAlign: TextAlign.left,
|
textAlign: TextAlign.left,
|
||||||
style:
|
style:
|
||||||
TextStyle(fontSize: Entity.SMALL_FONT_SIZE, color: Colors.black26),
|
TextStyle(fontSize: Entity.SMALL_FONT_SIZE, color: Colors.black26),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -144,7 +214,7 @@ class Entity {
|
|||||||
return Padding(
|
return Padding(
|
||||||
padding: EdgeInsets.only(right: 10.0),
|
padding: EdgeInsets.only(right: 10.0),
|
||||||
child: Text(
|
child: Text(
|
||||||
"${this.displayName}",
|
"${widget.entity.displayName}",
|
||||||
overflow: TextOverflow.ellipsis,
|
overflow: TextOverflow.ellipsis,
|
||||||
softWrap: false,
|
softWrap: false,
|
||||||
style: TextStyle(fontSize: Entity.NAME_FONT_SIZE),
|
style: TextStyle(fontSize: Entity.NAME_FONT_SIZE),
|
||||||
@ -155,10 +225,10 @@ class Entity {
|
|||||||
Widget _buildActionWidget(bool inCard, BuildContext context) {
|
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),
|
||||||
child: GestureDetector(
|
child: GestureDetector(
|
||||||
child: Text(
|
child: Text(
|
||||||
"$_state${this.unitOfMeasurement}",
|
"${widget.entity.state}${widget.entity.unitOfMeasurement}",
|
||||||
textAlign: TextAlign.right,
|
textAlign: TextAlign.right,
|
||||||
style: new TextStyle(
|
style: new TextStyle(
|
||||||
fontSize: Entity.STATE_FONT_SIZE,
|
fontSize: Entity.STATE_FONT_SIZE,
|
||||||
|
@ -1,29 +1,26 @@
|
|||||||
part of '../main.dart';
|
part of '../main.dart';
|
||||||
|
|
||||||
class SelectEntity extends Entity {
|
class _SelectEntityWidgetState extends _EntityWidgetState {
|
||||||
List<String> _listOptions = [];
|
List<String> _listOptions = [];
|
||||||
String get initialValue => _attributes["initial"] ?? null;
|
|
||||||
|
|
||||||
SelectEntity(Map rawData) : super(rawData) {
|
|
||||||
if (_attributes["options"] != null) {
|
|
||||||
_attributes["options"].forEach((value){
|
|
||||||
_listOptions.add(value.toString());
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void sendNewState(newValue) {
|
void sendNewState(newValue) {
|
||||||
eventBus.fire(new ServiceCallEvent(_domain, "select_option", _entityId,
|
eventBus.fire(new ServiceCallEvent(widget.entity.domain, "select_option", widget.entity.entityId,
|
||||||
{"option": "$newValue"}));
|
{"option": "$newValue"}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget _buildActionWidget(bool inCard, BuildContext context) {
|
Widget _buildActionWidget(bool inCard, BuildContext context) {
|
||||||
|
_listOptions.clear();
|
||||||
|
if (widget.entity._attributes["options"] != null) {
|
||||||
|
widget.entity._attributes["options"].forEach((value){
|
||||||
|
_listOptions.add(value.toString());
|
||||||
|
});
|
||||||
|
}
|
||||||
return Container(
|
return Container(
|
||||||
width: Entity.INPUT_WIDTH,
|
width: Entity.INPUT_WIDTH,
|
||||||
child: DropdownButton<String>(
|
child: DropdownButton<String>(
|
||||||
value: _state,
|
value: widget.entity.state,
|
||||||
items: this._listOptions.map((String value) {
|
items: this._listOptions.map((String value) {
|
||||||
return new DropdownMenuItem<String>(
|
return new DropdownMenuItem<String>(
|
||||||
value: value,
|
value: value,
|
||||||
|
@ -1,29 +1,31 @@
|
|||||||
part of '../main.dart';
|
part of '../main.dart';
|
||||||
|
|
||||||
class SliderEntity extends Entity {
|
class _SliderEntityWidgetState extends _EntityWidgetState {
|
||||||
int _multiplier = 1;
|
int _multiplier = 1;
|
||||||
|
|
||||||
double get minValue => _attributes["min"] ?? 0.0;
|
double get minValue => widget.entity._attributes["min"] ?? 0.0;
|
||||||
double get maxValue => _attributes["max"] ?? 100.0;
|
double get maxValue => widget.entity._attributes["max"] ?? 100.0;
|
||||||
double get valueStep => _attributes["step"] ?? 1.0;
|
double get valueStep => widget.entity._attributes["step"] ?? 1.0;
|
||||||
double get doubleState => double.tryParse(_state) ?? 0.0;
|
double get doubleState => double.tryParse(widget.entity.state) ?? 0.0;
|
||||||
|
|
||||||
SliderEntity(Map rawData) : super(rawData) {
|
@override
|
||||||
if (valueStep < 1) {
|
void initState() {
|
||||||
_multiplier = 10;
|
super.initState();
|
||||||
} else if (valueStep < 0.1) {
|
|
||||||
_multiplier = 100;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void sendNewState(newValue) {
|
void sendNewState(newValue) {
|
||||||
eventBus.fire(new ServiceCallEvent(_domain, "set_value", _entityId,
|
eventBus.fire(new ServiceCallEvent(widget.entity.domain, "set_value", widget.entity.entityId,
|
||||||
{"value": "${newValue.toString()}"}));
|
{"value": "${newValue.toString()}"}));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget _buildActionWidget(bool inCard, BuildContext context) {
|
Widget _buildActionWidget(bool inCard, BuildContext context) {
|
||||||
|
if (valueStep < 1) {
|
||||||
|
_multiplier = 10;
|
||||||
|
} else if (valueStep < 0.1) {
|
||||||
|
_multiplier = 100;
|
||||||
|
}
|
||||||
return Container(
|
return Container(
|
||||||
width: 200.0,
|
width: 200.0,
|
||||||
child: Row(
|
child: Row(
|
||||||
@ -32,13 +34,16 @@ class SliderEntity extends Entity {
|
|||||||
child: Slider(
|
child: Slider(
|
||||||
min: this.minValue * _multiplier,
|
min: this.minValue * _multiplier,
|
||||||
max: this.maxValue * _multiplier,
|
max: this.maxValue * _multiplier,
|
||||||
value: (this.doubleState <= this.maxValue) &&
|
value: (doubleState <= this.maxValue) &&
|
||||||
(this.doubleState >= this.minValue)
|
(doubleState >= this.minValue)
|
||||||
? this.doubleState * _multiplier
|
? doubleState * _multiplier
|
||||||
: this.minValue * _multiplier,
|
: this.minValue * _multiplier,
|
||||||
onChanged: (value) {
|
onChanged: (value) {
|
||||||
eventBus.fire(new StateChangedEvent(_entityId,
|
setState(() {
|
||||||
(value.roundToDouble() / _multiplier).toString(), true));
|
widget.entity.state = (value.roundToDouble() / _multiplier).toString();
|
||||||
|
});
|
||||||
|
/*eventBus.fire(new StateChangedEvent(widget.entity.entityId,
|
||||||
|
(value.roundToDouble() / _multiplier).toString(), true));*/
|
||||||
},
|
},
|
||||||
onChangeEnd: (value) {
|
onChangeEnd: (value) {
|
||||||
sendNewState(value.roundToDouble() / _multiplier);
|
sendNewState(value.roundToDouble() / _multiplier);
|
||||||
@ -47,7 +52,7 @@ class SliderEntity extends Entity {
|
|||||||
),
|
),
|
||||||
Padding(
|
Padding(
|
||||||
padding: EdgeInsets.only(right: Entity.RIGHT_WIDGET_PADDING),
|
padding: EdgeInsets.only(right: Entity.RIGHT_WIDGET_PADDING),
|
||||||
child: Text("$_state${this.unitOfMeasurement}",
|
child: Text("${widget.entity.state}${widget.entity.unitOfMeasurement}",
|
||||||
textAlign: TextAlign.right,
|
textAlign: TextAlign.right,
|
||||||
style: new TextStyle(
|
style: new TextStyle(
|
||||||
fontSize: Entity.STATE_FONT_SIZE,
|
fontSize: Entity.STATE_FONT_SIZE,
|
||||||
|
@ -1,20 +1,25 @@
|
|||||||
part of '../main.dart';
|
part of '../main.dart';
|
||||||
|
|
||||||
class SwitchEntity extends Entity {
|
class _SwitchEntityWidgetState extends _EntityWidgetState {
|
||||||
SwitchEntity(Map rawData) : super(rawData);
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void sendNewState(newValue) {
|
void sendNewState(newValue) {
|
||||||
eventBus.fire(new ServiceCallEvent(
|
eventBus.fire(new ServiceCallEvent(
|
||||||
_domain, (newValue as bool) ? "turn_on" : "turn_off", entityId, null));
|
widget.entity.domain, (newValue as bool) ? "turn_on" : "turn_off", widget.entity.entityId, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget _buildActionWidget(bool inCard, BuildContext context) {
|
Widget _buildActionWidget(bool inCard, BuildContext context) {
|
||||||
return Switch(
|
return Switch(
|
||||||
value: this.isOn,
|
value: widget.entity.isOn,
|
||||||
onChanged: ((switchState) {
|
onChanged: ((switchState) {
|
||||||
sendNewState(switchState);
|
sendNewState(switchState);
|
||||||
|
widget.entity.state = switchState ? 'on' : 'off';
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -1,40 +1,37 @@
|
|||||||
part of '../main.dart';
|
part of '../main.dart';
|
||||||
|
|
||||||
class TextEntity extends Entity {
|
class _TextEntityWidgetState extends _EntityWidgetState {
|
||||||
String tmpState;
|
String _tmpValue;
|
||||||
FocusNode _focusNode;
|
FocusNode _focusNode = FocusNode();
|
||||||
bool validValue = false;
|
bool validValue = false;
|
||||||
|
|
||||||
int get valueMinLength => _attributes["min"] ?? -1;
|
int get valueMinLength => widget.entity._attributes["min"] ?? -1;
|
||||||
int get valueMaxLength => _attributes["max"] ?? -1;
|
int get valueMaxLength => widget.entity._attributes["max"] ?? -1;
|
||||||
String get valuePattern => _attributes["pattern"] ?? null;
|
String get valuePattern => widget.entity._attributes["pattern"] ?? null;
|
||||||
bool get isTextField => _attributes["mode"] == "text";
|
bool get isTextField => widget.entity._attributes["mode"] == "text";
|
||||||
bool get isPasswordField => _attributes["mode"] == "password";
|
bool get isPasswordField => widget.entity._attributes["mode"] == "password";
|
||||||
|
|
||||||
TextEntity(Map rawData) : super(rawData) {
|
@override
|
||||||
_focusNode = FocusNode();
|
void initState() {
|
||||||
//TODO possible memory leak generator
|
super.initState();
|
||||||
_focusNode.addListener(_focusListener);
|
_focusNode.addListener(_focusListener);
|
||||||
//tmpState = state;
|
_tmpValue = widget.entity.state;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void sendNewState(newValue) {
|
void sendNewState(newValue) {
|
||||||
if (validate(newValue)) {
|
if (validate(newValue)) {
|
||||||
eventBus.fire(new ServiceCallEvent(_domain, "set_value", _entityId,
|
eventBus.fire(new ServiceCallEvent(widget.entity.domain, "set_value", widget.entity.entityId,
|
||||||
{"value": "$newValue"}));
|
{"value": "$newValue"}));
|
||||||
|
} else {
|
||||||
|
setState(() {
|
||||||
|
_tmpValue = widget.entity.state;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
void update(Map rawData) {
|
|
||||||
super.update(rawData);
|
|
||||||
tmpState = _state;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool validate(newValue) {
|
bool validate(newValue) {
|
||||||
if (newValue is String) {
|
if (newValue is String) {
|
||||||
//TODO add pattern support
|
|
||||||
validValue = (newValue.length >= this.valueMinLength) &&
|
validValue = (newValue.length >= this.valueMinLength) &&
|
||||||
(this.valueMaxLength == -1 ||
|
(this.valueMaxLength == -1 ||
|
||||||
(newValue.length <= this.valueMaxLength));
|
(newValue.length <= this.valueMaxLength));
|
||||||
@ -45,32 +42,42 @@ class TextEntity extends Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void _focusListener() {
|
void _focusListener() {
|
||||||
if (!_focusNode.hasFocus && (tmpState != state)) {
|
if (!_focusNode.hasFocus && (_tmpValue != widget.entity.state)) {
|
||||||
sendNewState(tmpState);
|
sendNewState(_tmpValue);
|
||||||
tmpState = state;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget _buildActionWidget(bool inCard, BuildContext context) {
|
Widget _buildActionWidget(bool inCard, BuildContext context) {
|
||||||
|
if (!_focusNode.hasFocus && (_tmpValue != widget.entity.state)) {
|
||||||
|
_tmpValue = widget.entity.state;
|
||||||
|
}
|
||||||
if (this.isTextField || this.isPasswordField) {
|
if (this.isTextField || this.isPasswordField) {
|
||||||
return Container(
|
return Container(
|
||||||
width: Entity.INPUT_WIDTH,
|
width: Entity.INPUT_WIDTH,
|
||||||
child: TextField(
|
child: TextField(
|
||||||
focusNode: inCard ? _focusNode : null,
|
focusNode: _focusNode,
|
||||||
obscureText: this.isPasswordField,
|
obscureText: this.isPasswordField,
|
||||||
controller: new TextEditingController.fromValue(
|
controller: new TextEditingController.fromValue(
|
||||||
new TextEditingValue(
|
new TextEditingValue(
|
||||||
text: tmpState,
|
text: _tmpValue,
|
||||||
selection:
|
selection:
|
||||||
new TextSelection.collapsed(offset: tmpState.length))),
|
new TextSelection.collapsed(offset: _tmpValue.length))),
|
||||||
onChanged: (value) {
|
onChanged: (value) {
|
||||||
tmpState = value;
|
_tmpValue = value;
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
TheLogger.log("Warning", "Unsupported input mode for $entityId");
|
TheLogger.log("Warning", "Unsupported input mode for ${widget.entity.entityId}");
|
||||||
return super._buildActionWidget(inCard, context);
|
return super._buildActionWidget(inCard, context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_focusNode.removeListener(_focusListener);
|
||||||
|
_focusNode.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -27,39 +27,7 @@ class EntityCollection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Entity _createEntityInstance(rawEntityData) {
|
Entity _createEntityInstance(rawEntityData) {
|
||||||
switch (rawEntityData["entity_id"].split(".")[0]) {
|
return Entity(rawEntityData);
|
||||||
case "automation":
|
|
||||||
case "input_boolean ":
|
|
||||||
case "switch":
|
|
||||||
case "light": {
|
|
||||||
return SwitchEntity(rawEntityData);
|
|
||||||
}
|
|
||||||
|
|
||||||
case "script":
|
|
||||||
case "scene": {
|
|
||||||
return ButtonEntity(rawEntityData);
|
|
||||||
}
|
|
||||||
|
|
||||||
case "input_datetime": {
|
|
||||||
return DateTimeEntity(rawEntityData);
|
|
||||||
}
|
|
||||||
|
|
||||||
case "input_select": {
|
|
||||||
return SelectEntity(rawEntityData);
|
|
||||||
}
|
|
||||||
|
|
||||||
case "input_number": {
|
|
||||||
return SliderEntity(rawEntityData);
|
|
||||||
}
|
|
||||||
|
|
||||||
case "input_text": {
|
|
||||||
return TextEntity(rawEntityData);
|
|
||||||
}
|
|
||||||
|
|
||||||
default: {
|
|
||||||
return Entity(rawEntityData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateState(Map rawStateData) {
|
void updateState(Map rawStateData) {
|
||||||
|
@ -44,7 +44,7 @@ class _LogViewPageState extends State<LogViewPage> {
|
|||||||
onPressed: () {
|
onPressed: () {
|
||||||
String body = "```\n$_logData```";
|
String body = "```\n$_logData```";
|
||||||
String encodedBody = "${Uri.encodeFull(body)}";
|
String encodedBody = "${Uri.encodeFull(body)}";
|
||||||
haUtils.launchURL("https://github.com/estevez-dev/ha_client_pub/issues/new?body=$encodedBody");
|
HAUtils.launchURL("https://github.com/estevez-dev/ha_client_pub/issues/new?body=$encodedBody");
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
@ -67,7 +67,7 @@ class HAClientApp extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
initialRoute: "/",
|
initialRoute: "/",
|
||||||
routes: {
|
routes: {
|
||||||
"/": (context) => MainPage(title: 'Hass Client'),
|
"/": (context) => MainPage(title: 'HA Client'),
|
||||||
"/connection-settings": (context) => ConnectionSettingsPage(title: "Connection Settings"),
|
"/connection-settings": (context) => ConnectionSettingsPage(title: "Connection Settings"),
|
||||||
"/log-view": (context) => LogViewPage(title: "Log")
|
"/log-view": (context) => LogViewPage(title: "Log")
|
||||||
},
|
},
|
||||||
@ -416,7 +416,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, context),
|
child: entity.buildWidget(context, true),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -487,7 +487,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
|||||||
title: Text("Report an issue"),
|
title: Text("Report an issue"),
|
||||||
onTap: () {
|
onTap: () {
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
haUtils.launchURL("https://github.com/estevez-dev/ha_client_pub/issues/new");
|
HAUtils.launchURL("https://github.com/estevez-dev/ha_client_pub/issues/new");
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
new AboutListTile(
|
new AboutListTile(
|
||||||
|
@ -32,7 +32,7 @@ class TheLogger {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class haUtils {
|
class HAUtils {
|
||||||
static void launchURL(String url) async {
|
static void launchURL(String url) async {
|
||||||
if (await canLaunch(url)) {
|
if (await canLaunch(url)) {
|
||||||
await launch(url);
|
await launch(url);
|
||||||
|
Reference in New Issue
Block a user