Resolves #155
This commit is contained in:
parent
b8f6fda8d3
commit
26187e6233
@ -39,8 +39,6 @@ class SliderEntity extends Entity {
|
||||
|
||||
@override
|
||||
Widget _buildAdditionalControlsForPage(BuildContext context) {
|
||||
return SliderStateWidget(
|
||||
expanded: false,
|
||||
);
|
||||
return SliderControlsWidget();
|
||||
}
|
||||
}
|
70
lib/entity_widgets/controls/slider_controls.dart
Normal file
70
lib/entity_widgets/controls/slider_controls.dart
Normal file
@ -0,0 +1,70 @@
|
||||
part of '../../main.dart';
|
||||
|
||||
class SliderControlsWidget extends StatefulWidget {
|
||||
|
||||
SliderControlsWidget({Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_SliderControlsWidgetState createState() => _SliderControlsWidgetState();
|
||||
}
|
||||
|
||||
class _SliderControlsWidgetState extends State<SliderControlsWidget> {
|
||||
int _multiplier = 1;
|
||||
double _newValue;
|
||||
bool _changedHere = false;
|
||||
|
||||
void setNewState(newValue, domain, entityId) {
|
||||
setState(() {
|
||||
_newValue = newValue;
|
||||
_changedHere = true;
|
||||
});
|
||||
eventBus.fire(new ServiceCallEvent(domain, "set_value", entityId,
|
||||
{"value": "${newValue.toString()}"}));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final entityModel = EntityModel.of(context);
|
||||
final SliderEntity entity = entityModel.entity;
|
||||
if (entity.valueStep < 1) {
|
||||
_multiplier = 10;
|
||||
} else if (entity.valueStep < 0.1) {
|
||||
_multiplier = 100;
|
||||
}
|
||||
if (!_changedHere) {
|
||||
_newValue = entity.doubleState;
|
||||
} else {
|
||||
_changedHere = false;
|
||||
}
|
||||
Widget slider = Slider(
|
||||
min: entity.minValue * _multiplier,
|
||||
max: entity.maxValue * _multiplier,
|
||||
value: (_newValue <= entity.maxValue) &&
|
||||
(_newValue >= entity.minValue)
|
||||
? _newValue * _multiplier
|
||||
: entity.minValue * _multiplier,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_newValue = (value.roundToDouble() / _multiplier);
|
||||
_changedHere = true;
|
||||
});
|
||||
},
|
||||
onChangeEnd: (value) {
|
||||
setNewState(value.roundToDouble() / _multiplier, entity.domain, entity.entityId);
|
||||
},
|
||||
);
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: <Widget>[
|
||||
Text(
|
||||
"$_newValue",
|
||||
style: TextStyle(
|
||||
fontSize: Sizes.largeFontSize,
|
||||
color: Colors.blue
|
||||
),
|
||||
),
|
||||
slider
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
part of '../../main.dart';
|
||||
|
||||
class SliderStateWidget extends StatefulWidget {
|
||||
|
||||
final bool expanded;
|
||||
|
||||
SliderStateWidget({Key key, @required this.expanded}) : super(key: key);
|
||||
|
||||
@override
|
||||
_SliderStateWidgetState createState() => _SliderStateWidgetState();
|
||||
}
|
||||
|
||||
class _SliderStateWidgetState extends State<SliderStateWidget> {
|
||||
int _multiplier = 1;
|
||||
|
||||
void setNewState(newValue, domain, entityId) {
|
||||
eventBus.fire(new ServiceCallEvent(domain, "set_value", entityId,
|
||||
{"value": "${newValue.toString()}"}));
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final entityModel = EntityModel.of(context);
|
||||
final SliderEntity entity = entityModel.entity;
|
||||
if (entity.valueStep < 1) {
|
||||
_multiplier = 10;
|
||||
} else if (entity.valueStep < 0.1) {
|
||||
_multiplier = 100;
|
||||
}
|
||||
Widget slider = Slider(
|
||||
min: entity.minValue * _multiplier,
|
||||
max: entity.maxValue * _multiplier,
|
||||
value: (entity.doubleState <= entity.maxValue) &&
|
||||
(entity.doubleState >= entity.minValue)
|
||||
? entity.doubleState * _multiplier
|
||||
: entity.minValue * _multiplier,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
entity.state =
|
||||
(value.roundToDouble() / _multiplier).toString();
|
||||
});
|
||||
eventBus.fire(new StateChangedEvent(entity.entityId,
|
||||
(value.roundToDouble() / _multiplier).toString(), true));
|
||||
|
||||
},
|
||||
onChangeEnd: (value) {
|
||||
setNewState(value.roundToDouble() / _multiplier, entity.domain, entity.entityId);
|
||||
},
|
||||
);
|
||||
if (widget.expanded) {
|
||||
return Expanded(
|
||||
child: slider,
|
||||
);
|
||||
} else {
|
||||
return slider;
|
||||
}
|
||||
}
|
||||
}
|
@ -345,7 +345,7 @@ class HomeAssistant {
|
||||
//TheLogger.debug( "New state for ${eventData['entity_id']}");
|
||||
Map data = Map.from(eventData);
|
||||
entities.updateState(data);
|
||||
eventBus.fire(new StateChangedEvent(data["entity_id"], null, false));
|
||||
eventBus.fire(new StateChangedEvent(data["entity_id"], null));
|
||||
}
|
||||
|
||||
void _parseConfig(Map data) {
|
||||
|
@ -47,7 +47,7 @@ part 'entity_widgets/history_chart/combined_history_chart.dart';
|
||||
part 'entity_widgets/history_chart/history_control_widget.dart';
|
||||
part 'entity_widgets/history_chart/entity_history_moment.dart';
|
||||
part 'entity_widgets/state/switch_state.dart';
|
||||
part 'entity_widgets/state/slider_state.dart';
|
||||
part 'entity_widgets/controls/slider_controls.dart';
|
||||
part 'entity_widgets/state/text_input_state.dart';
|
||||
part 'entity_widgets/state/select_state.dart';
|
||||
part 'entity_widgets/state/simple_state.dart';
|
||||
@ -133,14 +133,12 @@ class MainPage extends StatefulWidget {
|
||||
|
||||
class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
||||
HomeAssistant _homeAssistant;
|
||||
EntityCollection _entities;
|
||||
//Map _instanceConfig;
|
||||
String _webSocketApiEndpoint;
|
||||
String _password;
|
||||
String _authType;
|
||||
//int _uiViewsCount = 0;
|
||||
String _instanceHost;
|
||||
StreamSubscription _stateSubscription;
|
||||
StreamSubscription _settingsSubscription;
|
||||
StreamSubscription _serviceCallSubscription;
|
||||
StreamSubscription _showEntityPageSubscription;
|
||||
@ -209,18 +207,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
||||
}
|
||||
|
||||
_subscribe() {
|
||||
if (_stateSubscription == null) {
|
||||
//TODO Move to homeAssistant or remove
|
||||
_stateSubscription = eventBus.on<StateChangedEvent>().listen((event) {
|
||||
setState(() {
|
||||
if (event.localChange) {
|
||||
_entities
|
||||
.get(event.entityId)
|
||||
.state = event.newState;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
if (_serviceCallSubscription == null) {
|
||||
_serviceCallSubscription =
|
||||
eventBus.on<ServiceCallEvent>().listen((event) {
|
||||
@ -257,10 +243,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
||||
});
|
||||
await _homeAssistant.fetch().then((result) {
|
||||
setState(() {
|
||||
//_instanceConfig = _homeAssistant.instanceConfig;
|
||||
_entities = _homeAssistant.entities;
|
||||
//_uiViewsCount = _homeAssistant.viewsCount;
|
||||
//TheLogger.debug("_uiViewsCount=$_uiViewsCount");
|
||||
_isLoading = 0;
|
||||
});
|
||||
}).catchError((e) {
|
||||
@ -566,7 +548,6 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
if (_stateSubscription != null) _stateSubscription.cancel();
|
||||
if (_settingsSubscription != null) _settingsSubscription.cancel();
|
||||
if (_serviceCallSubscription != null) _serviceCallSubscription.cancel();
|
||||
if (_showEntityPageSubscription != null) _showEntityPageSubscription.cancel();
|
||||
|
@ -58,9 +58,8 @@ class HAUtils {
|
||||
class StateChangedEvent {
|
||||
String entityId;
|
||||
String newState;
|
||||
bool localChange;
|
||||
|
||||
StateChangedEvent(this.entityId, this.newState, this.localChange);
|
||||
StateChangedEvent(this.entityId, this.newState);
|
||||
}
|
||||
|
||||
class SettingsChangedEvent {
|
||||
|
Reference in New Issue
Block a user