2018-10-01 21:57:54 +03:00
|
|
|
part of '../main.dart';
|
|
|
|
|
2018-10-02 00:41:40 +03:00
|
|
|
class _TextEntityWidgetState extends _EntityWidgetState {
|
|
|
|
String _tmpValue;
|
|
|
|
FocusNode _focusNode = FocusNode();
|
2018-10-01 21:57:54 +03:00
|
|
|
bool validValue = false;
|
|
|
|
|
2018-10-02 00:41:40 +03:00
|
|
|
int get valueMinLength => widget.entity._attributes["min"] ?? -1;
|
|
|
|
int get valueMaxLength => widget.entity._attributes["max"] ?? -1;
|
|
|
|
String get valuePattern => widget.entity._attributes["pattern"] ?? null;
|
|
|
|
bool get isTextField => widget.entity._attributes["mode"] == "text";
|
|
|
|
bool get isPasswordField => widget.entity._attributes["mode"] == "password";
|
2018-10-01 21:57:54 +03:00
|
|
|
|
2018-10-02 00:41:40 +03:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2018-10-01 21:57:54 +03:00
|
|
|
_focusNode.addListener(_focusListener);
|
2018-10-02 00:41:40 +03:00
|
|
|
_tmpValue = widget.entity.state;
|
2018-10-01 21:57:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void sendNewState(newValue) {
|
|
|
|
if (validate(newValue)) {
|
2018-10-02 00:41:40 +03:00
|
|
|
eventBus.fire(new ServiceCallEvent(widget.entity.domain, "set_value", widget.entity.entityId,
|
2018-10-01 21:57:54 +03:00
|
|
|
{"value": "$newValue"}));
|
2018-10-02 00:41:40 +03:00
|
|
|
} else {
|
|
|
|
setState(() {
|
|
|
|
_tmpValue = widget.entity.state;
|
|
|
|
});
|
2018-10-01 21:57:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool validate(newValue) {
|
|
|
|
if (newValue is String) {
|
|
|
|
validValue = (newValue.length >= this.valueMinLength) &&
|
|
|
|
(this.valueMaxLength == -1 ||
|
|
|
|
(newValue.length <= this.valueMaxLength));
|
|
|
|
} else {
|
|
|
|
validValue = true;
|
|
|
|
}
|
|
|
|
return validValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _focusListener() {
|
2018-10-02 00:41:40 +03:00
|
|
|
if (!_focusNode.hasFocus && (_tmpValue != widget.entity.state)) {
|
|
|
|
sendNewState(_tmpValue);
|
2018-10-01 21:57:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget _buildActionWidget(bool inCard, BuildContext context) {
|
2018-10-02 00:41:40 +03:00
|
|
|
if (!_focusNode.hasFocus && (_tmpValue != widget.entity.state)) {
|
|
|
|
_tmpValue = widget.entity.state;
|
|
|
|
}
|
2018-10-01 21:57:54 +03:00
|
|
|
if (this.isTextField || this.isPasswordField) {
|
|
|
|
return Container(
|
|
|
|
width: Entity.INPUT_WIDTH,
|
|
|
|
child: TextField(
|
2018-10-02 00:41:40 +03:00
|
|
|
focusNode: _focusNode,
|
2018-10-01 21:57:54 +03:00
|
|
|
obscureText: this.isPasswordField,
|
|
|
|
controller: new TextEditingController.fromValue(
|
|
|
|
new TextEditingValue(
|
2018-10-02 00:41:40 +03:00
|
|
|
text: _tmpValue,
|
2018-10-01 21:57:54 +03:00
|
|
|
selection:
|
2018-10-02 00:41:40 +03:00
|
|
|
new TextSelection.collapsed(offset: _tmpValue.length))),
|
2018-10-01 21:57:54 +03:00
|
|
|
onChanged: (value) {
|
2018-10-02 00:41:40 +03:00
|
|
|
_tmpValue = value;
|
2018-10-01 21:57:54 +03:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else {
|
2018-10-02 00:41:40 +03:00
|
|
|
TheLogger.log("Warning", "Unsupported input mode for ${widget.entity.entityId}");
|
2018-10-01 21:57:54 +03:00
|
|
|
return super._buildActionWidget(inCard, context);
|
|
|
|
}
|
|
|
|
}
|
2018-10-02 00:41:40 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_focusNode.removeListener(_focusListener);
|
|
|
|
_focusNode.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2018-10-01 21:57:54 +03:00
|
|
|
}
|