State const

This commit is contained in:
Yegor Vialov
2018-11-14 15:14:46 +02:00
parent 3190b45db3
commit 87f89b63e1
15 changed files with 116 additions and 42 deletions

View File

@ -3,7 +3,17 @@ part of '../../main.dart';
class MediaPlayerWidget extends StatelessWidget {
void _setPower(MediaPlayerEntity entity) {
TheLogger.debug('WAT?');
if (entity.state != EntityState.unavailable && entity.state != EntityState.unknown) {
if (entity.state == EntityState.off) {
TheLogger.debug("${entity.entityId} turn_on");
} else {
TheLogger.debug("${entity.entityId} turn_off");
}
}
}
void _callAction(MediaPlayerEntity entity, String action) {
TheLogger.debug("${entity.entityId} $action");
}
@override
@ -40,7 +50,7 @@ class MediaPlayerWidget extends StatelessWidget {
Widget _buildControls(MediaPlayerEntity entity) {
List<Widget> result = [];
if (entity.supportTurnOn) {
if (entity.supportTurnOn || entity.supportTurnOff) {
result.add(
IconButton(
icon: Icon(Icons.power_settings_new),
@ -49,6 +59,36 @@ class MediaPlayerWidget extends StatelessWidget {
)
);
}
if (entity.supportPreviousTrack) {
result.add(
IconButton(
icon: Icon(Icons.skip_previous),
onPressed: () => _callAction(entity, "media_previous_track"),
iconSize: Sizes.iconSize,
)
);
}
if (entity.supportPlay || entity.supportPause) {
if (entity.state == EntityState.playing) {
result.add(
IconButton(
icon: Icon(Icons.pause_circle_outline),
onPressed: () => _callAction(entity, "media_pause"),
iconSize: Sizes.iconSize*1.5,
)
);
} //else if (entity.state == '')
}
if (entity.supportNextTrack) {
result.add(
IconButton(
icon: Icon(Icons.skip_next),
onPressed: () => _callAction(entity, "media_next_track"),
iconSize: Sizes.iconSize,
)
);
}
return Row(
children: result,
mainAxisAlignment: MainAxisAlignment.center,
@ -65,7 +105,7 @@ class MediaPlayerWidget extends StatelessWidget {
List<Widget> states = [];
states.add(Text("${entity.displayName}", style: style));
String state = entity.state;
if (state == null || state == "off" || state == "unavailable" || state == "idle") {
if (state == null || state == EntityState.off || state == EntityState.unavailable || state == EntityState.idle) {
states.add(Text("${entity.state}", style: style.apply(fontSizeDelta: 4.0),));
}
if (entity.attributes['media_title'] != null) {
@ -87,7 +127,7 @@ class MediaPlayerWidget extends StatelessWidget {
Widget _buildImage(MediaPlayerEntity entity) {
String state = entity.state;
if (homeAssistantWebHost != null && entity.entityPicture != null && state != "off" && state != "unavailable" && state != "idle") {
if (homeAssistantWebHost != null && entity.entityPicture != null && state != EntityState.off && state != EntityState.unavailable && state != EntityState.idle) {
return Container(
color: Colors.black,
child: Row(
@ -109,7 +149,7 @@ class MediaPlayerWidget extends StatelessWidget {
Icon(
MaterialDesignIcons.createIconDataFromIconName("mdi:movie"),
size: 150.0,
color: EntityColors.stateColor("$state"),
color: EntityColor.stateColor("$state"),
)
],
);
@ -141,7 +181,7 @@ class _MediaPlayerProgressWidgetState extends State<MediaPlayerProgressWidget> {
Duration duration = Duration(seconds: entity._getIntAttributeValue("media_duration") ?? 1);
Duration position = Duration(seconds: entity._getIntAttributeValue("media_position") ?? 0);
int currentPosition = position.inSeconds;
if (entity.state == "playing") {
if (entity.state == EntityState.playing) {
_timer?.cancel();
_timer = Timer(Duration(seconds: 1), () {
setState(() {
@ -159,7 +199,7 @@ class _MediaPlayerProgressWidgetState extends State<MediaPlayerProgressWidget> {
return LinearProgressIndicator(
value: progress,
backgroundColor: Colors.black45,
valueColor: AlwaysStoppedAnimation<Color>(EntityColors.stateColor("on")),
valueColor: AlwaysStoppedAnimation<Color>(EntityColor.stateColor(EntityState.on)),
);
} catch (e) {
_timer?.cancel();